aa33ce62fd364d4dd942b2b99c851dcece9e58fc
[WebKit-https.git] / Source / WebKit2 / UIProcess / WebCookieManagerProxy.cpp
1 /*
2  * Copyright (C) 2011 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 "SecurityOriginData.h"
30 #include "WebCookieManagerMessages.h"
31 #include "WebContext.h"
32 #include "WebSecurityOrigin.h"
33
34 namespace WebKit {
35
36 PassRefPtr<WebCookieManagerProxy> WebCookieManagerProxy::create(WebContext* context)
37 {
38     return adoptRef(new WebCookieManagerProxy(context));
39 }
40
41 WebCookieManagerProxy::WebCookieManagerProxy(WebContext* context)
42     : m_webContext(context)
43 {
44     m_webContext->addMessageReceiver(CoreIPC::MessageClassWebCookieManagerProxy, this);
45 }
46
47 WebCookieManagerProxy::~WebCookieManagerProxy()
48 {
49 }
50
51 void WebCookieManagerProxy::invalidate()
52 {
53     invalidateCallbackMap(m_arrayCallbacks);
54     invalidateCallbackMap(m_httpCookieAcceptPolicyCallbacks);
55 }
56
57 bool WebCookieManagerProxy::shouldTerminate(WebProcessProxy*) const
58 {
59     return m_arrayCallbacks.isEmpty() && m_httpCookieAcceptPolicyCallbacks.isEmpty();
60 }
61
62 void WebCookieManagerProxy::initializeClient(const WKCookieManagerClient* client)
63 {
64     m_client.initialize(client);
65 }
66
67 void WebCookieManagerProxy::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
68 {
69     didReceiveWebCookieManagerProxyMessage(connection, messageID, arguments);
70 }
71
72 void WebCookieManagerProxy::getHostnamesWithCookies(PassRefPtr<ArrayCallback> prpCallback)
73 {
74     ASSERT(m_webContext);
75
76     RefPtr<ArrayCallback> callback = prpCallback;
77     uint64_t callbackID = callback->callbackID();
78     m_arrayCallbacks.set(callbackID, callback.release());
79
80     // FIXME (Multi-WebProcess): <rdar://problem/12239765> Cookies shouldn't be stored in the web process.
81     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebCookieManager::GetHostnamesWithCookies(callbackID));
82 }
83     
84 void WebCookieManagerProxy::didGetHostnamesWithCookies(const Vector<String>& hostnameList, uint64_t callbackID)
85 {
86     RefPtr<ArrayCallback> callback = m_arrayCallbacks.take(callbackID);
87     if (!callback) {
88         // FIXME: Log error or assert.
89         return;
90     }
91
92     size_t hostnameCount = hostnameList.size();
93     Vector<RefPtr<APIObject> > hostnames(hostnameCount);
94
95     for (size_t i = 0; i < hostnameCount; ++i)
96         hostnames[i] = WebString::create(hostnameList[i]);
97
98     callback->performCallbackWithReturnValue(ImmutableArray::adopt(hostnames).get());
99 }
100
101 void WebCookieManagerProxy::deleteCookiesForHostname(const String& hostname)
102 {
103     ASSERT(m_webContext);
104     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebCookieManager::DeleteCookiesForHostname(hostname));
105 }
106
107 void WebCookieManagerProxy::deleteAllCookies()
108 {
109     ASSERT(m_webContext);
110     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebCookieManager::DeleteAllCookies());
111 }
112
113 void WebCookieManagerProxy::startObservingCookieChanges()
114 {
115     ASSERT(m_webContext);
116     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebCookieManager::StartObservingCookieChanges());
117 }
118
119 void WebCookieManagerProxy::stopObservingCookieChanges()
120 {
121     ASSERT(m_webContext);
122     m_webContext->sendToAllProcesses(Messages::WebCookieManager::StopObservingCookieChanges());
123 }
124
125 void WebCookieManagerProxy::cookiesDidChange()
126 {
127     m_client.cookiesDidChange(this);
128 }
129
130 void WebCookieManagerProxy::setHTTPCookieAcceptPolicy(HTTPCookieAcceptPolicy policy)
131 {
132     ASSERT(m_webContext);
133 #if PLATFORM(MAC)
134     persistHTTPCookieAcceptPolicy(policy);
135 #endif
136
137     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebCookieManager::SetHTTPCookieAcceptPolicy(policy));
138 }
139
140 void WebCookieManagerProxy::getHTTPCookieAcceptPolicy(PassRefPtr<HTTPCookieAcceptPolicyCallback> prpCallback)
141 {
142     ASSERT(m_webContext);
143
144     RefPtr<HTTPCookieAcceptPolicyCallback> callback = prpCallback;
145
146     uint64_t callbackID = callback->callbackID();
147     m_httpCookieAcceptPolicyCallbacks.set(callbackID, callback.release());
148
149     // FIXME (Multi-WebProcess): <rdar://problem/12240189> Make cookie policies work with per-tab WebProcess.
150     // We can't ber asking all web processes for this information.
151     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebCookieManager::GetHTTPCookieAcceptPolicy(callbackID));
152 }
153
154 void WebCookieManagerProxy::didGetHTTPCookieAcceptPolicy(uint32_t policy, uint64_t callbackID)
155 {
156     RefPtr<HTTPCookieAcceptPolicyCallback> callback = m_httpCookieAcceptPolicyCallbacks.take(callbackID);
157     if (!callback) {
158         // FIXME: Log error or assert.
159         return;
160     }
161
162     callback->performCallbackWithReturnValue(policy);
163 }
164
165 } // namespace WebKit