2 * Copyright (C) 2019 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "NetworkStorageSessionMap.h"
28 #include <WebCore/NetworkStorageSession.h>
29 #include <pal/SessionID.h>
30 #include <wtf/MainThread.h>
31 #include <wtf/ProcessID.h>
32 #include <wtf/ProcessPrivilege.h>
33 #include <wtf/text/StringConcatenateNumbers.h>
35 static std::unique_ptr<WebCore::NetworkStorageSession>& defaultNetworkStorageSession()
37 ASSERT(isMainThread());
38 static NeverDestroyed<std::unique_ptr<WebCore::NetworkStorageSession>> session;
42 static HashMap<PAL::SessionID, std::unique_ptr<WebCore::NetworkStorageSession>>& globalSessionMap()
44 static NeverDestroyed<HashMap<PAL::SessionID, std::unique_ptr<WebCore::NetworkStorageSession>>> map;
48 WebCore::NetworkStorageSession* NetworkStorageSessionMap::storageSession(const PAL::SessionID& sessionID)
50 if (sessionID == PAL::SessionID::defaultSessionID())
51 return &defaultStorageSession();
52 return globalSessionMap().get(sessionID);
55 WebCore::NetworkStorageSession& NetworkStorageSessionMap::defaultStorageSession()
57 if (!defaultNetworkStorageSession())
58 defaultNetworkStorageSession() = std::make_unique<WebCore::NetworkStorageSession>(PAL::SessionID::defaultSessionID());
59 return *defaultNetworkStorageSession();
62 void NetworkStorageSessionMap::switchToNewTestingSession()
64 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
65 // Session name should be short enough for shared memory region name to be under the limit, otehrwise sandbox rules won't work (see <rdar://problem/13642852>).
66 String sessionName = makeString("WebKit Test-", getCurrentProcessID());
68 auto session = adoptCF(WebCore::createPrivateStorageSession(sessionName.createCFString().get()));
70 RetainPtr<CFHTTPCookieStorageRef> cookieStorage;
71 if (WebCore::NetworkStorageSession::processMayUseCookieAPI()) {
72 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessRawCookies));
74 cookieStorage = adoptCF(_CFURLStorageSessionCopyCookieStorage(kCFAllocatorDefault, session.get()));
77 defaultNetworkStorageSession() = std::make_unique<WebCore::NetworkStorageSession>(PAL::SessionID::defaultSessionID(), WTFMove(session), WTFMove(cookieStorage));
81 void NetworkStorageSessionMap::ensureSession(const PAL::SessionID& sessionID, const String& identifierBase)
83 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
84 auto addResult = globalSessionMap().add(sessionID, nullptr);
85 if (!addResult.isNewEntry)
88 RetainPtr<CFStringRef> cfIdentifier = String(identifierBase + ".PrivateBrowsing").createCFString();
90 RetainPtr<CFURLStorageSessionRef> storageSession;
91 if (sessionID.isEphemeral())
92 storageSession = adoptCF(WebCore::createPrivateStorageSession(cfIdentifier.get()));
94 storageSession = WebCore::NetworkStorageSession::createCFStorageSessionForIdentifier(cfIdentifier.get());
96 RetainPtr<CFHTTPCookieStorageRef> cookieStorage;
97 if (WebCore::NetworkStorageSession::processMayUseCookieAPI()) {
98 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessRawCookies));
100 cookieStorage = adoptCF(_CFURLStorageSessionCopyCookieStorage(kCFAllocatorDefault, storageSession.get()));
103 addResult.iterator->value = std::make_unique<WebCore::NetworkStorageSession>(sessionID, WTFMove(storageSession), WTFMove(cookieStorage));
106 globalSessionMap().ensure(sessionID, [sessionID] {
107 return std::make_unique<WebCore::NetworkStorageSession>(sessionID);
112 void NetworkStorageSessionMap::destroySession(const PAL::SessionID& sessionID)
114 globalSessionMap().remove(sessionID);