Remove std::chrono completely
[WebKit.git] / Source / WebKit / 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 "OptionalCallbackID.h"
33 #include "WebCookieManagerMessages.h"
34 #include "WebCookieManagerProxyMessages.h"
35 #include "WebProcessPool.h"
36 #include <WebCore/Cookie.h>
37 #include <WebCore/SecurityOriginData.h>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 const char* WebCookieManagerProxy::supplementName()
44 {
45     return "WebCookieManagerProxy";
46 }
47
48 Ref<WebCookieManagerProxy> WebCookieManagerProxy::create(WebProcessPool* processPool)
49 {
50     return adoptRef(*new WebCookieManagerProxy(processPool));
51 }
52
53 WebCookieManagerProxy::WebCookieManagerProxy(WebProcessPool* processPool)
54     : WebContextSupplement(processPool)
55 #if USE(SOUP)
56     , m_cookiePersistentStorageType(SoupCookiePersistentStorageSQLite)
57 #endif
58 {
59     WebContextSupplement::processPool()->addMessageReceiver(Messages::WebCookieManagerProxy::messageReceiverName(), *this);
60 }
61
62 WebCookieManagerProxy::~WebCookieManagerProxy()
63 {
64     ASSERT(m_cookieObservers.isEmpty());
65 }
66
67 void WebCookieManagerProxy::initializeClient(const WKCookieManagerClientBase* client)
68 {
69     m_client.initialize(client);
70 }
71
72 // WebContextSupplement
73
74 void WebCookieManagerProxy::processPoolDestroyed()
75 {
76     m_callbacks.invalidate(CallbackBase::Error::OwnerWasInvalidated);
77
78     Vector<Observer*> observers;
79     for (auto& observerSet : m_cookieObservers.values()) {
80         for (auto* observer : observerSet)
81             observers.append(observer);
82     }
83
84     for (auto* observer : observers)
85         observer->managerDestroyed();
86
87     ASSERT(m_cookieObservers.isEmpty());
88 }
89
90 void WebCookieManagerProxy::processDidClose(WebProcessProxy*)
91 {
92     m_callbacks.invalidate(CallbackBase::Error::ProcessExited);
93 }
94
95 void WebCookieManagerProxy::processDidClose(NetworkProcessProxy*)
96 {
97     m_callbacks.invalidate(CallbackBase::Error::ProcessExited);
98 }
99
100 void WebCookieManagerProxy::refWebContextSupplement()
101 {
102     API::Object::ref();
103 }
104
105 void WebCookieManagerProxy::derefWebContextSupplement()
106 {
107     API::Object::deref();
108 }
109
110 void WebCookieManagerProxy::getHostnamesWithCookies(PAL::SessionID sessionID, Function<void (API::Array*, CallbackBase::Error)>&& callbackFunction)
111 {
112     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
113     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::GetHostnamesWithCookies(sessionID, callbackID));
114 }
115
116 void WebCookieManagerProxy::didGetHostnamesWithCookies(const Vector<String>& hostnames, WebKit::CallbackID callbackID)
117 {
118     auto callback = m_callbacks.take<ArrayCallback>(callbackID);
119     if (!callback) {
120         // FIXME: Log error or assert.
121         return;
122     }
123
124     callback->performCallbackWithReturnValue(API::Array::createStringArray(hostnames).ptr());
125 }
126
127 void WebCookieManagerProxy::deleteCookiesForHostname(PAL::SessionID sessionID, const String& hostname)
128 {
129     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::DeleteCookiesForHostname(sessionID, hostname));
130 }
131
132 void WebCookieManagerProxy::deleteAllCookies(PAL::SessionID sessionID)
133 {
134     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::DeleteAllCookies(sessionID));
135 }
136
137 void WebCookieManagerProxy::deleteCookie(PAL::SessionID sessionID, const Cookie& cookie, Function<void (CallbackBase::Error)>&& callbackFunction)
138 {
139     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
140     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::DeleteCookie(sessionID, cookie, callbackID));
141 }
142
143 void WebCookieManagerProxy::deleteAllCookiesModifiedSince(PAL::SessionID sessionID, WallTime time, Function<void (CallbackBase::Error)>&& callbackFunction)
144 {
145     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
146     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::DeleteAllCookiesModifiedSince(sessionID, time, callbackID));
147 }
148
149 void WebCookieManagerProxy::setCookie(PAL::SessionID sessionID, const Cookie& cookie, Function<void (CallbackBase::Error)>&& callbackFunction)
150 {
151     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
152     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::SetCookie(sessionID, cookie, callbackID));
153 }
154
155 void WebCookieManagerProxy::setCookies(PAL::SessionID sessionID, const Vector<Cookie>& cookies, const URL& url, const URL& mainDocumentURL, Function<void (CallbackBase::Error)>&& callbackFunction)
156 {
157     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
158     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::SetCookies(sessionID, cookies, url, mainDocumentURL, callbackID));
159 }
160
161 void WebCookieManagerProxy::getAllCookies(PAL::SessionID sessionID, Function<void (const Vector<Cookie>&, CallbackBase::Error)>&& callbackFunction)
162 {
163     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
164     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::GetAllCookies(sessionID, callbackID));
165 }
166
167 void WebCookieManagerProxy::getCookies(PAL::SessionID sessionID, const URL& url, Function<void (const Vector<Cookie>&, CallbackBase::Error)>&& callbackFunction)
168 {
169     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
170     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::GetCookies(sessionID, url, callbackID));
171 }
172
173 void WebCookieManagerProxy::didSetCookies(WebKit::CallbackID callbackID)
174 {
175     m_callbacks.take<VoidCallback>(callbackID)->performCallback();
176 }
177
178 void WebCookieManagerProxy::didGetCookies(const Vector<Cookie>& cookies, WebKit::CallbackID callbackID)
179 {
180     m_callbacks.take<GetCookiesCallback>(callbackID)->performCallbackWithReturnValue(cookies);
181 }
182
183 void WebCookieManagerProxy::didDeleteCookies(WebKit::CallbackID callbackID)
184 {
185     m_callbacks.take<VoidCallback>(callbackID)->performCallback();
186 }
187
188 void WebCookieManagerProxy::startObservingCookieChanges(PAL::SessionID sessionID)
189 {
190     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::StartObservingCookieChanges(sessionID));
191 }
192
193 void WebCookieManagerProxy::stopObservingCookieChanges(PAL::SessionID sessionID)
194 {
195     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::StopObservingCookieChanges(sessionID));
196 }
197
198
199 void WebCookieManagerProxy::setCookieObserverCallback(PAL::SessionID sessionID, WTF::Function<void ()>&& callback)
200 {
201     if (callback)
202         m_legacyCookieObservers.set(sessionID, WTFMove(callback));
203     else
204         m_legacyCookieObservers.remove(sessionID);
205 }
206
207 void WebCookieManagerProxy::registerObserver(PAL::SessionID sessionID, Observer& observer)
208 {
209     auto result = m_cookieObservers.set(sessionID, HashSet<Observer*>());
210     result.iterator->value.add(&observer);
211
212     if (result.isNewEntry)
213         startObservingCookieChanges(sessionID);
214 }
215
216 void WebCookieManagerProxy::unregisterObserver(PAL::SessionID sessionID, Observer& observer)
217 {
218     auto iterator = m_cookieObservers.find(sessionID);
219     if (iterator == m_cookieObservers.end())
220         return;
221
222     iterator->value.remove(&observer);
223     if (!iterator->value.isEmpty())
224         return;
225
226     m_cookieObservers.remove(iterator);
227     stopObservingCookieChanges(sessionID);
228 }
229
230 void WebCookieManagerProxy::cookiesDidChange(PAL::SessionID sessionID)
231 {
232     m_client.cookiesDidChange(this);
233     auto legacyIterator = m_legacyCookieObservers.find(sessionID);
234     if (legacyIterator != m_legacyCookieObservers.end())
235         ((*legacyIterator).value)();
236
237     auto iterator = m_cookieObservers.find(sessionID);
238     if (iterator == m_cookieObservers.end())
239         return;
240
241     for (auto* observer : iterator->value)
242         observer->cookiesDidChange();
243 }
244
245 void WebCookieManagerProxy::setHTTPCookieAcceptPolicy(PAL::SessionID, HTTPCookieAcceptPolicy policy, Function<void (CallbackBase::Error)>&& callbackFunction)
246 {
247 #if PLATFORM(COCOA)
248     if (!processPool()->isUsingTestingNetworkSession())
249         persistHTTPCookieAcceptPolicy(policy);
250 #endif
251 #if USE(SOUP)
252     processPool()->setInitialHTTPCookieAcceptPolicy(policy);
253 #endif
254
255     // The policy is not sent to newly created processes (only Soup does that via setInitialHTTPCookieAcceptPolicy()). This is not a serious problem, because:
256     // - When testing, we only have one WebProcess and one NetworkProcess, and WebKitTestRunner never restarts them;
257     // - When not testing, Cocoa has the policy persisted, and thus new processes use it (even for ephemeral sessions).
258     processPool()->sendToAllProcesses(Messages::WebCookieManager::SetHTTPCookieAcceptPolicy(policy, OptionalCallbackID()));
259
260     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
261     processPool()->sendToNetworkingProcess(Messages::WebCookieManager::SetHTTPCookieAcceptPolicy(policy, OptionalCallbackID(callbackID)));
262 }
263
264 void WebCookieManagerProxy::getHTTPCookieAcceptPolicy(PAL::SessionID, Function<void (HTTPCookieAcceptPolicy, CallbackBase::Error)>&& callbackFunction)
265 {
266     auto callbackID = m_callbacks.put(WTFMove(callbackFunction));
267     processPool()->sendToNetworkingProcessRelaunchingIfNecessary(Messages::WebCookieManager::GetHTTPCookieAcceptPolicy(callbackID));
268 }
269
270 void WebCookieManagerProxy::didGetHTTPCookieAcceptPolicy(uint32_t policy, WebKit::CallbackID callbackID)
271 {
272     m_callbacks.take<HTTPCookieAcceptPolicyCallback>(callbackID)->performCallbackWithReturnValue(policy);
273 }
274
275 void WebCookieManagerProxy::didSetHTTPCookieAcceptPolicy(WebKit::CallbackID callbackID)
276 {
277     m_callbacks.take<VoidCallback>(callbackID)->performCallback();
278 }
279
280 void WebCookieManagerProxy::setCookieStoragePartitioningEnabled(bool enabled)
281 {
282 #if PLATFORM(COCOA)
283     processPool()->sendToNetworkingProcess(Messages::NetworkProcess::SetCookieStoragePartitioningEnabled(enabled));
284 #else
285     UNUSED_PARAM(enabled);
286 #endif
287 }
288
289 void WebCookieManagerProxy::setStorageAccessAPIEnabled(bool enabled)
290 {
291 #if PLATFORM(COCOA)
292     processPool()->sendToNetworkingProcess(Messages::NetworkProcess::SetStorageAccessAPIEnabled(enabled));
293 #else
294     UNUSED_PARAM(enabled);
295 #endif
296 }
297
298 } // namespace WebKit