1/*
2 * Copyright (C) 2012 Igalia S.L.
3 * Copyright (C) 2013 Company 100 Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "WebFrameNetworkingContext.h"
29
30#include "SessionTracker.h"
31#include "WebFrame.h"
32#include "WebPage.h"
33#include <WebCore/CookieJarSoup.h>
34#include <WebCore/NetworkStorageSession.h>
35#include <WebCore/SessionID.h>
36#include <WebCore/Settings.h>
37#include <WebCore/SoupNetworkSession.h>
38#include <wtf/NeverDestroyed.h>
39
40using namespace WebCore;
41
42namespace WebKit {
43
44void WebFrameNetworkingContext::ensurePrivateBrowsingSession(SessionID sessionID)
45{
46    ASSERT(isMainThread());
47
48    if (SessionTracker::session(sessionID))
49        return;
50
51    SessionTracker::setSession(sessionID, NetworkStorageSession::createPrivateBrowsingSession(String::number(sessionID.sessionID())));
52}
53
54void WebFrameNetworkingContext::setCookieAcceptPolicyForAllContexts(HTTPCookieAcceptPolicy policy)
55{
56    SoupCookieJarAcceptPolicy soupPolicy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
57    switch (policy) {
58    case HTTPCookieAcceptPolicyAlways:
59        soupPolicy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
60        break;
61    case HTTPCookieAcceptPolicyNever:
62        soupPolicy = SOUP_COOKIE_JAR_ACCEPT_NEVER;
63        break;
64    case HTTPCookieAcceptPolicyOnlyFromMainDocumentDomain:
65        soupPolicy = SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
66        break;
67    }
68
69    SoupCookieJar* cookieJar = WebCore::soupCookieJar();
70    soup_cookie_jar_set_accept_policy(cookieJar, soupPolicy);
71
72    SoupNetworkSession& soupSession = NetworkStorageSession::defaultStorageSession().soupNetworkSession();
73    soup_cookie_jar_set_accept_policy(soupSession.cookieJar(), soupPolicy);
74
75    for (const auto& session : SessionTracker::sessionMap().values()) {
76        if (session)
77            soup_cookie_jar_set_accept_policy(session->soupNetworkSession().cookieJar(), soupPolicy);
78    }
79}
80
81WebFrameNetworkingContext::WebFrameNetworkingContext(WebFrame* frame)
82    : FrameNetworkingContext(frame->coreFrame())
83{
84}
85
86NetworkStorageSession& WebFrameNetworkingContext::storageSession() const
87{
88    if (frame() && frame()->page()->usesEphemeralSession())
89        return *SessionTracker::session(SessionID::legacyPrivateSessionID());
90
91    return NetworkStorageSession::defaultStorageSession();
92}
93
94WebFrameLoaderClient* WebFrameNetworkingContext::webFrameLoaderClient() const
95{
96    if (!frame())
97        return nullptr;
98
99    return toWebFrameLoaderClient(frame()->loader().client());
100}
101
102}
103