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