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