Resource Load Statistics: Communicate to the network process which domains to partition
[WebKit.git] / Source / WebKit2 / UIProcess / WebCookieManagerProxy.cpp
1 /*
2  * Copyright (C) 2011, 2013 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 "WebCookieManagerProxy.h"
28
29 #include "APIArray.h"
30 #include "APISecurityOrigin.h"
31 #include "NetworkProcessMessages.h"
32 #include "WebCookieManagerMessages.h"
33 #include "WebCookieManagerProxyMessages.h"
34 #include "WebProcessPool.h"
35 #include <WebCore/SecurityOriginData.h>
36
37 namespace WebKit {
38
39 const char* WebCookieManagerProxy::supplementName()
40 {
41     return "WebCookieManagerProxy";
42 }
43
44 PassRefPtr<WebCookieManagerProxy> WebCookieManagerProxy::create(WebProcessPool* processPool)
45 {
46     return adoptRef(new WebCookieManagerProxy(processPool));
47 }
48
49 WebCookieManagerProxy::WebCookieManagerProxy(WebProcessPool* processPool)
50     : WebContextSupplement(processPool)
51 #if USE(SOUP)
52     , m_cookiePersistentStorageType(SoupCookiePersistentStorageSQLite)
53 #endif
54 {
55     WebContextSupplement::processPool()->addMessageReceiver(Messages::WebCookieManagerProxy::messageReceiverName(), *this);
56 }
57
58 WebCookieManagerProxy::~WebCookieManagerProxy()
59 {
60 }
61
62 void WebCookieManagerProxy::initializeClient(const WKCookieManagerClientBase* client)
63 {
64     m_client.initialize(client);
65 }
66
67 // WebContextSupplement
68
69 void WebCookieManagerProxy::processPoolDestroyed()
70 {
71     invalidateCallbackMap(m_arrayCallbacks, CallbackBase::Error::OwnerWasInvalidated);
72     invalidateCallbackMap(m_httpCookieAcceptPolicyCallbacks, CallbackBase::Error::OwnerWasInvalidated);
73 }
74
75 void WebCookieManagerProxy::processDidClose(WebProcessProxy*)
76 {
77     invalidateCallbackMap(m_arrayCallbacks, CallbackBase::Error::ProcessExited);
78     invalidateCallbackMap(m_httpCookieAcceptPolicyCallbacks, CallbackBase::Error::ProcessExited);
79 }
80
81 void WebCookieManagerProxy::processDidClose(NetworkProcessProxy*)
82 {
83     invalidateCallbackMap(m_arrayCallbacks, CallbackBase::Error::ProcessExited);
84     invalidateCallbackMap(m_httpCookieAcceptPolicyCallbacks, CallbackBase::Error::ProcessExited);
85 }
86
87 void WebCookieManagerProxy::refWebContextSupplement()
88 {
89     API::Object::ref();
90 }
91
92 void WebCookieManagerProxy::derefWebContextSupplement()
93 {
94     API::Object::deref();
95 }
96
97 void WebCookieManagerProxy::getHostnamesWithCookies(WebCore::SessionID sessionID, std::function<void (API::Array*, CallbackBase::Error)> callbackFunction)
98 {
99     auto callback = ArrayCallback::create(WTFMove(callbackFunction));
100     uint64_t callbackID = callback->callbackID();
101     m_arrayCallbacks.set(callbackID, WTFMove(callback));
102
103     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::GetHostnamesWithCookies(sessionID, callbackID));
104 }
105
106 void WebCookieManagerProxy::didGetHostnamesWithCookies(const Vector<String>& hostnames, uint64_t callbackID)
107 {
108     RefPtr<ArrayCallback> callback = m_arrayCallbacks.take(callbackID);
109     if (!callback) {
110         // FIXME: Log error or assert.
111         return;
112     }
113
114     callback->performCallbackWithReturnValue(API::Array::createStringArray(hostnames).ptr());
115 }
116
117 void WebCookieManagerProxy::deleteCookiesForHostname(WebCore::SessionID sessionID, const String& hostname)
118 {
119     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::DeleteCookiesForHostname(sessionID, hostname));
120 }
121
122 void WebCookieManagerProxy::deleteAllCookies(WebCore::SessionID sessionID)
123 {
124     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::DeleteAllCookies(sessionID));
125 }
126
127 void WebCookieManagerProxy::deleteAllCookiesModifiedSince(WebCore::SessionID sessionID, std::chrono::system_clock::time_point time)
128 {
129     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::DeleteAllCookiesModifiedSince(sessionID, time));
130 }
131
132 void WebCookieManagerProxy::addCookie(WebCore::SessionID sessionID, const WebCore::Cookie& cookie, const String& hostname)
133 {
134     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::AddCookie(sessionID, cookie, hostname));
135 }
136
137 void WebCookieManagerProxy::startObservingCookieChanges(WebCore::SessionID sessionID)
138 {
139     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::StartObservingCookieChanges(sessionID));
140 }
141
142 void WebCookieManagerProxy::stopObservingCookieChanges(WebCore::SessionID sessionID)
143 {
144     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::StopObservingCookieChanges(sessionID));
145 }
146
147 void WebCookieManagerProxy::setCookieObserverCallback(WebCore::SessionID sessionID, std::function<void ()>&& callback)
148 {
149     if (callback)
150         m_cookieObservers.set(sessionID, WTFMove(callback));
151     else
152         m_cookieObservers.remove(sessionID);
153 }
154
155 void WebCookieManagerProxy::cookiesDidChange(WebCore::SessionID sessionID)
156 {
157     m_client.cookiesDidChange(this);
158     if (auto callback = m_cookieObservers.get(sessionID))
159         callback();
160 }
161
162 void WebCookieManagerProxy::setHTTPCookieAcceptPolicy(HTTPCookieAcceptPolicy policy)
163 {
164 #if PLATFORM(COCOA)
165     if (!processPool()->isUsingTestingNetworkSession())
166         persistHTTPCookieAcceptPolicy(policy);
167 #endif
168 #if USE(SOUP)
169     processPool()->setInitialHTTPCookieAcceptPolicy(policy);
170 #endif
171
172     // The policy is not sent to newly created processes (only Soup does that via setInitialHTTPCookieAcceptPolicy()). This is not a serious problem, because:
173     // - When testing, we only have one WebProcess and one NetworkProcess, and WebKitTestRunner never restarts them;
174     // - When not testing, Cocoa has the policy persisted, and thus new processes use it (even for ephemeral sessions).
175     processPool()->sendToAllProcesses(Messages::WebCookieManager::SetHTTPCookieAcceptPolicy(policy));
176     processPool()->sendToNetworkingProcess(Messages::WebCookieManager::SetHTTPCookieAcceptPolicy(policy));
177 }
178
179 void WebCookieManagerProxy::getHTTPCookieAcceptPolicy(std::function<void (HTTPCookieAcceptPolicy, CallbackBase::Error)> callbackFunction)
180 {
181     auto callback = HTTPCookieAcceptPolicyCallback::create(WTFMove(callbackFunction));
182
183     uint64_t callbackID = callback->callbackID();
184     m_httpCookieAcceptPolicyCallbacks.set(callbackID, WTFMove(callback));
185
186     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::GetHTTPCookieAcceptPolicy(callbackID));
187 }
188
189 void WebCookieManagerProxy::didGetHTTPCookieAcceptPolicy(uint32_t policy, uint64_t callbackID)
190 {
191     RefPtr<HTTPCookieAcceptPolicyCallback> callback = m_httpCookieAcceptPolicyCallbacks.take(callbackID);
192     if (!callback) {
193         // FIXME: Log error or assert.
194         return;
195     }
196
197     callback->performCallbackWithReturnValue(policy);
198 }
199
200 void WebCookieManagerProxy::setCookieStoragePartitioningEnabled(bool enabled)
201 {
202 #if PLATFORM(COCOA)
203     processPool()->sendToNetworkingProcess(Messages::NetworkProcess::SetCookieStoragePartitioningEnabled(enabled));
204 #else
205     UNUSED_PARAM(enabled);
206 #endif
207 }
208
209 } // namespace WebKit