9dc577057dd44245811b2981154aff6ad193deac
[WebKit-https.git] / Source / WebKit / WebProcess / Cookies / WebCookieManager.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 "WebCookieManager.h"
28
29 #include "ChildProcess.h"
30 #include "WebCookieManagerMessages.h"
31 #include "WebCookieManagerProxyMessages.h"
32 #include "WebCoreArgumentCoders.h"
33 #include <WebCore/Cookie.h>
34 #include <WebCore/CookieStorage.h>
35 #include <WebCore/NetworkStorageSession.h>
36 #include <WebCore/PlatformCookieJar.h>
37 #include <WebCore/URL.h>
38 #include <wtf/MainThread.h>
39 #include <wtf/text/StringHash.h>
40 #include <wtf/text/WTFString.h>
41
42 using namespace WebCore;
43
44 namespace WebKit {
45
46 const char* WebCookieManager::supplementName()
47 {
48     return "WebCookieManager";
49 }
50
51 WebCookieManager::WebCookieManager(ChildProcess* process)
52     : m_process(process)
53 {
54     m_process->addMessageReceiver(Messages::WebCookieManager::messageReceiverName(), *this);
55 }
56
57 void WebCookieManager::getHostnamesWithCookies(PAL::SessionID sessionID, CallbackID callbackID)
58 {
59     HashSet<String> hostnames;
60     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
61         WebCore::getHostnamesWithCookies(*storageSession, hostnames);
62
63     Vector<String> hostnameList;
64     copyToVector(hostnames, hostnameList);
65
66     m_process->send(Messages::WebCookieManagerProxy::DidGetHostnamesWithCookies(hostnameList, callbackID), 0);
67 }
68
69 void WebCookieManager::deleteCookiesForHostname(PAL::SessionID sessionID, const String& hostname)
70 {
71     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
72         WebCore::deleteCookiesForHostnames(*storageSession, { hostname });
73 }
74
75
76 void WebCookieManager::deleteAllCookies(PAL::SessionID sessionID)
77 {
78     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
79         WebCore::deleteAllCookies(*storageSession);
80 }
81
82 void WebCookieManager::deleteCookie(PAL::SessionID sessionID, const Cookie& cookie, CallbackID callbackID)
83 {
84     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
85         storageSession->deleteCookie(cookie);
86
87     m_process->send(Messages::WebCookieManagerProxy::DidDeleteCookies(callbackID), 0);
88 }
89
90 void WebCookieManager::deleteAllCookiesModifiedSince(PAL::SessionID sessionID, std::chrono::system_clock::time_point time, CallbackID callbackID)
91 {
92     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
93         WebCore::deleteAllCookiesModifiedSince(*storageSession, time);
94
95     m_process->send(Messages::WebCookieManagerProxy::DidDeleteCookies(callbackID), 0);
96 }
97
98 void WebCookieManager::getAllCookies(PAL::SessionID sessionID, CallbackID callbackID)
99 {
100     Vector<Cookie> cookies;
101     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
102         cookies = storageSession->getAllCookies();
103
104     m_process->send(Messages::WebCookieManagerProxy::DidGetCookies(cookies, callbackID), 0);
105 }
106
107 void WebCookieManager::getCookies(PAL::SessionID sessionID, const URL& url, CallbackID callbackID)
108 {
109     Vector<Cookie> cookies;
110     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
111         cookies = storageSession->getCookies(url);
112
113     m_process->send(Messages::WebCookieManagerProxy::DidGetCookies(cookies, callbackID), 0);
114 }
115
116 void WebCookieManager::setCookie(PAL::SessionID sessionID, const Cookie& cookie, CallbackID callbackID)
117 {
118     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
119         storageSession->setCookie(cookie);
120
121     m_process->send(Messages::WebCookieManagerProxy::DidSetCookies(callbackID), 0);
122 }
123
124 void WebCookieManager::setCookies(PAL::SessionID sessionID, const Vector<Cookie>& cookies, const URL& url, const URL& mainDocumentURL, CallbackID callbackID)
125 {
126     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
127         storageSession->setCookies(cookies, url, mainDocumentURL);
128
129     m_process->send(Messages::WebCookieManagerProxy::DidSetCookies(callbackID), 0);
130 }
131
132 void WebCookieManager::notifyCookiesDidChange(PAL::SessionID sessionID)
133 {
134     ASSERT(RunLoop::isMain());
135     m_process->send(Messages::WebCookieManagerProxy::CookiesDidChange(sessionID), 0);
136 }
137
138 void WebCookieManager::startObservingCookieChanges(PAL::SessionID sessionID)
139 {
140     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID)) {
141         WebCore::startObservingCookieChanges(*storageSession, [this, sessionID] {
142             notifyCookiesDidChange(sessionID);
143         });
144     }
145 }
146
147 void WebCookieManager::stopObservingCookieChanges(PAL::SessionID sessionID)
148 {
149     if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
150         WebCore::stopObservingCookieChanges(*storageSession);
151 }
152
153 void WebCookieManager::setHTTPCookieAcceptPolicy(HTTPCookieAcceptPolicy policy, OptionalCallbackID callbackID)
154 {
155     platformSetHTTPCookieAcceptPolicy(policy);
156
157     if (callbackID)
158         m_process->send(Messages::WebCookieManagerProxy::DidSetHTTPCookieAcceptPolicy(callbackID.callbackID()), 0);
159 }
160
161 void WebCookieManager::getHTTPCookieAcceptPolicy(CallbackID callbackID)
162 {
163     m_process->send(Messages::WebCookieManagerProxy::DidGetHTTPCookieAcceptPolicy(platformGetHTTPCookieAcceptPolicy(), callbackID), 0);
164 }
165
166 } // namespace WebKit