1/*
2 * Copyright (C) 2019 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 "WebCookieJar.h"
28
29#include "NetworkConnectionToWebProcessMessages.h"
30#include "NetworkProcessConnection.h"
31#include "WebProcess.h"
32#include <WebCore/CookieRequestHeaderFieldProxy.h>
33#include <WebCore/Document.h>
34#include <WebCore/Frame.h>
35#include <WebCore/FrameLoader.h>
36#include <WebCore/FrameLoaderClient.h>
37#include <WebCore/StorageSessionProvider.h>
38
39namespace WebKit {
40
41using namespace WebCore;
42
43class WebStorageSessionProvider : public WebCore::StorageSessionProvider {
44 // NetworkStorageSessions are accessed only in the NetworkProcess.
45 WebCore::NetworkStorageSession* storageSession() const final { return nullptr; }
46};
47
48WebCookieJar::WebCookieJar()
49 : WebCore::CookieJar(adoptRef(*new WebStorageSessionProvider)) { }
50
51String WebCookieJar::cookies(WebCore::Document& document, const URL& url) const
52{
53 Optional<uint64_t> frameID;
54 Optional<PageIdentifier> pageID;
55 if (auto* frame = document.frame()) {
56 frameID = frame->loader().client().frameID();
57 pageID = frame->loader().client().pageID();
58 }
59
60 String cookieString;
61 bool secureCookiesAccessed = false;
62 if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().sendSync(Messages::NetworkConnectionToWebProcess::CookiesForDOM(document.sessionID(), document.firstPartyForCookies(), sameSiteInfo(document), url, frameID, pageID, shouldIncludeSecureCookies(document, url)), Messages::NetworkConnectionToWebProcess::CookiesForDOM::Reply(cookieString, secureCookiesAccessed), 0))
63 return { };
64
65 return cookieString;
66}
67
68void WebCookieJar::setCookies(WebCore::Document& document, const URL& url, const String& cookieString)
69{
70 Optional<uint64_t> frameID;
71 Optional<PageIdentifier> pageID;
72 if (auto* frame = document.frame()) {
73 frameID = frame->loader().client().frameID();
74 pageID = frame->loader().client().pageID();
75 }
76
77 WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::SetCookiesFromDOM(document.sessionID(), document.firstPartyForCookies(), sameSiteInfo(document), url, frameID, pageID, cookieString), 0);
78}
79
80bool WebCookieJar::cookiesEnabled(const WebCore::Document& document) const
81{
82 bool result = false;
83 if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().sendSync(Messages::NetworkConnectionToWebProcess::CookiesEnabled(document.sessionID()), Messages::NetworkConnectionToWebProcess::CookiesEnabled::Reply(result), 0))
84 return false;
85 return result;
86}
87
88std::pair<String, WebCore::SecureCookiesAccessed> WebCookieJar::cookieRequestHeaderFieldValue(const PAL::SessionID& sessionID, const URL& firstParty, const WebCore::SameSiteInfo& sameSiteInfo, const URL& url, Optional<uint64_t> frameID, Optional<PageIdentifier> pageID, WebCore::IncludeSecureCookies includeSecureCookies) const
89{
90 String cookieString;
91 bool secureCookiesAccessed = false;
92 if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().sendSync(Messages::NetworkConnectionToWebProcess::CookieRequestHeaderFieldValue(sessionID, firstParty, sameSiteInfo, url, frameID, pageID, includeSecureCookies), Messages::NetworkConnectionToWebProcess::CookieRequestHeaderFieldValue::Reply(cookieString, secureCookiesAccessed), 0))
93 return { };
94 return { cookieString, secureCookiesAccessed ? WebCore::SecureCookiesAccessed::Yes : WebCore::SecureCookiesAccessed::No };
95}
96
97bool WebCookieJar::getRawCookies(const WebCore::Document& document, const URL& url, Vector<WebCore::Cookie>& rawCookies) const
98{
99 Optional<uint64_t> frameID;
100 Optional<PageIdentifier> pageID;
101 if (auto* frame = document.frame()) {
102 frameID = frame->loader().client().frameID();
103 pageID = frame->loader().client().pageID();
104 }
105
106 if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().sendSync(Messages::NetworkConnectionToWebProcess::GetRawCookies(document.sessionID(), document.firstPartyForCookies(), sameSiteInfo(document), url, frameID, pageID), Messages::NetworkConnectionToWebProcess::GetRawCookies::Reply(rawCookies), 0))
107 return false;
108 return true;
109}
110
111void WebCookieJar::deleteCookie(const WebCore::Document& document, const URL& url, const String& cookieName)
112{
113 WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::DeleteCookie(document.sessionID(), url, cookieName), 0);
114}
115
116} // namespace WebKit
117