1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebFrameNetworkingContext.h"
28
29#include "SessionTracker.h"
30#include "WebCookieManager.h"
31#include "WebPage.h"
32#include "WebProcess.h"
33#include <WebCore/Frame.h>
34#include <WebCore/FrameLoader.h>
35#include <WebCore/FrameLoaderClient.h>
36#include <WebCore/NetworkStorageSession.h>
37#include <WebCore/Page.h>
38#include <WebCore/ResourceError.h>
39#include <WebCore/Settings.h>
40#include <WebKitSystemInterface.h>
41
42using namespace WebCore;
43
44namespace WebKit {
45
46void WebFrameNetworkingContext::ensurePrivateBrowsingSession(SessionID sessionID)
47{
48    ASSERT(sessionID.isEphemeral());
49
50    if (SessionTracker::session(sessionID))
51        return;
52
53    String base;
54    if (SessionTracker::getIdentifierBase().isNull())
55        base = [[NSBundle mainBundle] bundleIdentifier];
56    else
57        base = SessionTracker::getIdentifierBase();
58
59    SessionTracker::setSession(sessionID, NetworkStorageSession::createPrivateBrowsingSession(base + '.' + String::number(sessionID.sessionID())));
60}
61
62void WebFrameNetworkingContext::setCookieAcceptPolicyForAllContexts(HTTPCookieAcceptPolicy policy)
63{
64    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:static_cast<NSHTTPCookieAcceptPolicy>(policy)];
65
66    if (RetainPtr<CFHTTPCookieStorageRef> cookieStorage = NetworkStorageSession::defaultStorageSession().cookieStorage())
67        WKSetHTTPCookieAcceptPolicy(cookieStorage.get(), policy);
68
69    for (const auto& session : SessionTracker::sessionMap().values()) {
70        if (session)
71            WKSetHTTPCookieAcceptPolicy(session->cookieStorage().get(), policy);
72    }
73}
74
75bool WebFrameNetworkingContext::needsSiteSpecificQuirks() const
76{
77    return frame() && frame()->settings().needsSiteSpecificQuirks();
78}
79
80bool WebFrameNetworkingContext::localFileContentSniffingEnabled() const
81{
82    return frame() && frame()->settings().localFileContentSniffingEnabled();
83}
84
85SchedulePairHashSet* WebFrameNetworkingContext::scheduledRunLoopPairs() const
86{
87    if (!frame() || !frame()->page())
88        return 0;
89    return frame()->page()->scheduledRunLoopPairs();
90}
91
92RetainPtr<CFDataRef> WebFrameNetworkingContext::sourceApplicationAuditData() const
93{
94#if PLATFORM(IOS)
95    audit_token_t auditToken;
96    if (!WebProcess::shared().parentProcessConnection()->getAuditToken(auditToken))
97        return nullptr;
98    return adoptCF(CFDataCreate(0, (const UInt8*)&auditToken, sizeof(auditToken)));
99#else
100    return nullptr;
101#endif
102}
103
104String WebFrameNetworkingContext::sourceApplicationIdentifier() const
105{
106    return SessionTracker::getIdentifierBase();
107}
108
109ResourceError WebFrameNetworkingContext::blockedError(const ResourceRequest& request) const
110{
111    return frame()->loader().client().blockedError(request);
112}
113
114NetworkStorageSession& WebFrameNetworkingContext::storageSession() const
115{
116    ASSERT(RunLoop::isMain());
117    if (frame()) {
118        if (NetworkStorageSession* session = SessionTracker::session(frame()->page()->sessionID()))
119            return *session;
120        // Some requests may still be coming shortly after WebProcess was told to destroy its session.
121        LOG_ERROR("Invalid session ID. Please file a bug unless you just disabled private browsing, in which case it's an expected race.");
122    }
123    return NetworkStorageSession::defaultStorageSession();
124}
125
126WebFrameLoaderClient* WebFrameNetworkingContext::webFrameLoaderClient() const
127{
128    if (!frame())
129        return 0;
130
131    return toWebFrameLoaderClient(frame()->loader().client());
132}
133
134}
135