2 * Copyright (C) 2011, 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebCookieManager.h"
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>
42 using namespace WebCore;
46 const char* WebCookieManager::supplementName()
48 return "WebCookieManager";
51 WebCookieManager::WebCookieManager(ChildProcess* process)
54 m_process->addMessageReceiver(Messages::WebCookieManager::messageReceiverName(), *this);
57 void WebCookieManager::getHostnamesWithCookies(PAL::SessionID sessionID, CallbackID callbackID)
59 HashSet<String> hostnames;
60 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
61 WebCore::getHostnamesWithCookies(*storageSession, hostnames);
63 Vector<String> hostnameList;
64 copyToVector(hostnames, hostnameList);
66 m_process->send(Messages::WebCookieManagerProxy::DidGetHostnamesWithCookies(hostnameList, callbackID), 0);
69 void WebCookieManager::deleteCookiesForHostname(PAL::SessionID sessionID, const String& hostname)
71 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
72 WebCore::deleteCookiesForHostnames(*storageSession, { hostname });
76 void WebCookieManager::deleteAllCookies(PAL::SessionID sessionID)
78 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
79 WebCore::deleteAllCookies(*storageSession);
82 void WebCookieManager::deleteCookie(PAL::SessionID sessionID, const Cookie& cookie, CallbackID callbackID)
84 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
85 storageSession->deleteCookie(cookie);
87 m_process->send(Messages::WebCookieManagerProxy::DidDeleteCookies(callbackID), 0);
90 void WebCookieManager::deleteAllCookiesModifiedSince(PAL::SessionID sessionID, std::chrono::system_clock::time_point time, CallbackID callbackID)
92 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
93 WebCore::deleteAllCookiesModifiedSince(*storageSession, time);
95 m_process->send(Messages::WebCookieManagerProxy::DidDeleteCookies(callbackID), 0);
98 void WebCookieManager::getAllCookies(PAL::SessionID sessionID, CallbackID callbackID)
100 Vector<Cookie> cookies;
101 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
102 cookies = storageSession->getAllCookies();
104 m_process->send(Messages::WebCookieManagerProxy::DidGetCookies(cookies, callbackID), 0);
107 void WebCookieManager::getCookies(PAL::SessionID sessionID, const URL& url, CallbackID callbackID)
109 Vector<Cookie> cookies;
110 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
111 cookies = storageSession->getCookies(url);
113 m_process->send(Messages::WebCookieManagerProxy::DidGetCookies(cookies, callbackID), 0);
116 void WebCookieManager::setCookie(PAL::SessionID sessionID, const Cookie& cookie, CallbackID callbackID)
118 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
119 storageSession->setCookie(cookie);
121 m_process->send(Messages::WebCookieManagerProxy::DidSetCookies(callbackID), 0);
124 void WebCookieManager::setCookies(PAL::SessionID sessionID, const Vector<Cookie>& cookies, const URL& url, const URL& mainDocumentURL, CallbackID callbackID)
126 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
127 storageSession->setCookies(cookies, url, mainDocumentURL);
129 m_process->send(Messages::WebCookieManagerProxy::DidSetCookies(callbackID), 0);
132 void WebCookieManager::notifyCookiesDidChange(PAL::SessionID sessionID)
134 ASSERT(RunLoop::isMain());
135 m_process->send(Messages::WebCookieManagerProxy::CookiesDidChange(sessionID), 0);
138 void WebCookieManager::startObservingCookieChanges(PAL::SessionID sessionID)
140 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID)) {
141 WebCore::startObservingCookieChanges(*storageSession, [this, sessionID] {
142 notifyCookiesDidChange(sessionID);
147 void WebCookieManager::stopObservingCookieChanges(PAL::SessionID sessionID)
149 if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
150 WebCore::stopObservingCookieChanges(*storageSession);
153 void WebCookieManager::setHTTPCookieAcceptPolicy(HTTPCookieAcceptPolicy policy, OptionalCallbackID callbackID)
155 platformSetHTTPCookieAcceptPolicy(policy);
158 m_process->send(Messages::WebCookieManagerProxy::DidSetHTTPCookieAcceptPolicy(callbackID.callbackID()), 0);
161 void WebCookieManager::getHTTPCookieAcceptPolicy(CallbackID callbackID)
163 m_process->send(Messages::WebCookieManagerProxy::DidGetHTTPCookieAcceptPolicy(platformGetHTTPCookieAcceptPolicy(), callbackID), 0);
166 } // namespace WebKit