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 "WebView.h"
30#include <WebCore/FrameLoader.h>
31#include <WebCore/FrameLoaderClient.h>
32#include <WebCore/NetworkStorageSession.h>
33#include <WebCore/Page.h>
34#include <WebCore/ResourceError.h>
35#include <WebCore/Settings.h>
36#include <wtf/NeverDestroyed.h>
37
38#if USE(CFNETWORK)
39#include <CFNetwork/CFHTTPCookiesPriv.h>
40#include <WebKitSystemInterface/WebKitSystemInterface.h>
41#endif
42
43using namespace WebCore;
44
45static std::unique_ptr<NetworkStorageSession>& privateSession()
46{
47    static NeverDestroyed<std::unique_ptr<NetworkStorageSession>> session;
48    return session;
49}
50
51static String& identifierBase()
52{
53    static NeverDestroyed<String> base;
54    return base;
55}
56
57#if USE(CFNETWORK)
58void WebFrameNetworkingContext::setCookieAcceptPolicyForAllContexts(WebKitCookieStorageAcceptPolicy policy)
59{
60    if (RetainPtr<CFHTTPCookieStorageRef> cookieStorage = NetworkStorageSession::defaultStorageSession().cookieStorage())
61        CFHTTPCookieStorageSetCookieAcceptPolicy(cookieStorage.get(), policy);
62
63    if (privateSession())
64        CFHTTPCookieStorageSetCookieAcceptPolicy(privateSession()->cookieStorage().get(), policy);
65}
66#endif
67
68void WebFrameNetworkingContext::setPrivateBrowsingStorageSessionIdentifierBase(const String& base)
69{
70    ASSERT(isMainThread());
71
72    identifierBase() = base;
73}
74
75void WebFrameNetworkingContext::ensurePrivateBrowsingSession()
76{
77#if USE(CFNETWORK)
78    ASSERT(isMainThread());
79
80    if (privateSession())
81        return;
82
83    String base;
84    if (identifierBase().isNull()) {
85        if (CFTypeRef bundleValue = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleIdentifierKey))
86            if (CFGetTypeID(bundleValue) == CFStringGetTypeID())
87                base = reinterpret_cast<CFStringRef>(bundleValue);
88    } else
89        base = identifierBase();
90
91    privateSession() = NetworkStorageSession::createPrivateBrowsingSession(base);
92#endif
93}
94
95void WebFrameNetworkingContext::destroyPrivateBrowsingSession()
96{
97    ASSERT(isMainThread());
98
99    privateSession() = nullptr;
100}
101
102ResourceError WebFrameNetworkingContext::blockedError(const ResourceRequest& request) const
103{
104    return frame()->loader().client().blockedError(request);
105}
106
107String WebFrameNetworkingContext::referrer() const
108{
109    return frame()->loader().referrer();
110}
111
112#if USE(CFNETWORK)
113NetworkStorageSession& WebFrameNetworkingContext::storageSession() const
114{
115    ASSERT(isMainThread());
116
117    if (frame() && frame()->page()->usesEphemeralSession())
118        return *privateSession();
119
120    return NetworkStorageSession::defaultStorageSession();
121}
122#endif
123