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