1/*
2 * Copyright (C) 2013, 2014 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 "WebViewPrivate.h"
27#include "WebFrameInternal.h"
28#include <WebFrameNetworkingContext.h>
29#include <WebCore/FrameLoader.h>
30#include <WebCore/FrameLoaderClient.h>
31#include <WebCore/NetworkStorageSession.h>
32#include <WebCore/Page.h>
33#include <WebCore/ResourceError.h>
34#include <WebCore/Settings.h>
35#include <wtf/NeverDestroyed.h>
36
37#if PLATFORM(IOS)
38#import <CFNetwork/CFHTTPCookiesPriv.h>
39#import <WebCore/WebCoreThread.h>
40#import <WebKitLegacy/WebFrameLoadDelegate.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
51void WebFrameNetworkingContext::ensurePrivateBrowsingSession()
52{
53    ASSERT(isMainThread());
54
55    if (privateSession())
56        return;
57
58    privateSession() = NetworkStorageSession::createPrivateBrowsingSession([[NSBundle mainBundle] bundleIdentifier]);
59}
60
61void WebFrameNetworkingContext::destroyPrivateBrowsingSession()
62{
63    ASSERT(isMainThread());
64
65    privateSession() = nullptr;
66}
67
68#if PLATFORM(IOS)
69void WebFrameNetworkingContext::clearPrivateBrowsingSessionCookieStorage()
70{
71    ASSERT(isMainThread());
72    ASSERT(privateSession());
73
74    CFHTTPCookieStorageDeleteAllCookies(privateSession()->cookieStorage().get());
75}
76#endif
77
78bool WebFrameNetworkingContext::needsSiteSpecificQuirks() const
79{
80    return frame() && frame()->settings().needsSiteSpecificQuirks();
81}
82
83bool WebFrameNetworkingContext::localFileContentSniffingEnabled() const
84{
85    return frame() && frame()->settings().localFileContentSniffingEnabled();
86}
87
88SchedulePairHashSet* WebFrameNetworkingContext::scheduledRunLoopPairs() const
89{
90    if (!frame() || !frame()->page())
91        return 0;
92    return frame()->page()->scheduledRunLoopPairs();
93}
94
95RetainPtr<CFDataRef> WebFrameNetworkingContext::sourceApplicationAuditData() const
96{
97    if (!frame() || !frame()->page())
98        return 0;
99
100    WebView *webview = kit(frame()->page());
101
102    if (!webview)
103        return 0;
104    return reinterpret_cast<CFDataRef>(webview._sourceApplicationAuditData);
105}
106
107String WebFrameNetworkingContext::sourceApplicationIdentifier() const
108{
109    return emptyString();
110}
111
112ResourceError WebFrameNetworkingContext::blockedError(const ResourceRequest& request) const
113{
114    return frame()->loader().client().blockedError(request);
115}
116
117NetworkStorageSession& WebFrameNetworkingContext::storageSession() const
118{
119    ASSERT(isMainThread());
120    if (frame() && frame()->page()->sessionID().isEphemeral()) {
121        if (NetworkStorageSession* session = privateSession().get())
122            return *session;
123        // Some requests may still be coming shortly before WebCore updates the session ID and after WebKit destroys the private browsing session.
124        LOG_ERROR("Invalid session ID. Please file a bug unless you just disabled private browsing, in which case it's an expected race.");
125    }
126    return NetworkStorageSession::defaultStorageSession();
127}
128