ec4f287162b22eaa67f9da5e0b24b0865e5338c8
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / ewk_cookie_manager.cpp
1 /*
2  * Copyright (C) 2012 Intel Corporation. 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 "ewk_cookie_manager.h"
28
29 #include "WKAPICast.h"
30 #include "WKArray.h"
31 #include "WKCookieManagerSoup.h"
32 #include "WKString.h"
33 #include "ewk_cookie_manager_private.h"
34 #include "ewk_error_private.h"
35 #include "ewk_private.h"
36 #include <wtf/Assertions.h>
37 #include <wtf/OwnPtr.h>
38
39 using namespace WebKit;
40
41 EwkCookieManager::EwkCookieManager(WKCookieManagerRef cookieManager)
42     : m_cookieManager(cookieManager)
43 {
44     ASSERT(m_cookieManager);
45
46     WKCookieManagerClientV0 wkCookieManagerClient;
47     memset(&wkCookieManagerClient, 0, sizeof(wkCookieManagerClient));
48     wkCookieManagerClient.base.version = 0;
49     wkCookieManagerClient.base.clientInfo = this;
50     wkCookieManagerClient.cookiesDidChange = cookiesDidChange;
51     WKCookieManagerSetClient(m_cookieManager.get(), &wkCookieManagerClient.base);
52 }
53
54 EwkCookieManager::~EwkCookieManager()
55 {
56     if (isWatchingForChanges())
57         WKCookieManagerStopObservingCookieChanges(m_cookieManager.get());
58 }
59
60 void EwkCookieManager::setPersistentStorage(const char* filename, WKCookieStorageType storageType)
61 {
62     bool isWatchingChanges = isWatchingForChanges();
63     if (isWatchingChanges)
64         WKCookieManagerStopObservingCookieChanges(m_cookieManager.get());
65
66     WKRetainPtr<WKStringRef> wkFilename(AdoptWK, WKStringCreateWithUTF8CString(filename));
67     WKCookieManagerSetCookiePersistentStorage(m_cookieManager.get(), wkFilename.get(), storageType);
68
69     if (isWatchingChanges)
70         WKCookieManagerStartObservingCookieChanges(m_cookieManager.get());
71 }
72
73 void EwkCookieManager::setHTTPAcceptPolicy(WKHTTPCookieAcceptPolicy policy)
74 {
75     WKCookieManagerSetHTTPCookieAcceptPolicy(m_cookieManager.get(), policy);
76 }
77
78 void EwkCookieManager::clearHostnameCookies(const char* hostname)
79 {
80     WKRetainPtr<WKStringRef> wkHostname(AdoptWK, WKStringCreateWithUTF8CString(hostname));
81     WKCookieManagerDeleteCookiesForHostname(m_cookieManager.get(), wkHostname.get());
82 }
83
84 void EwkCookieManager::clearAllCookies()
85 {
86     WKCookieManagerDeleteAllCookies(m_cookieManager.get());
87 }
88
89 void EwkCookieManager::watchChanges(const Cookie_Change_Handler& changeHandler)
90 {
91     m_changeHandler = changeHandler;
92
93     if (changeHandler.callback)
94         WKCookieManagerStartObservingCookieChanges(m_cookieManager.get());
95     else
96         WKCookieManagerStopObservingCookieChanges(m_cookieManager.get());
97 }
98
99 bool EwkCookieManager::isWatchingForChanges() const
100 {
101     return static_cast<bool>(m_changeHandler.callback);
102 }
103
104 void EwkCookieManager::getHostNamesWithCookies(WKCookieManagerGetCookieHostnamesFunction callback, void* userData) const
105 {
106     WKCookieManagerGetHostnamesWithCookies(m_cookieManager.get(), userData, callback);
107 }
108
109 void EwkCookieManager::getHTTPAcceptPolicy(WKCookieManagerGetHTTPCookieAcceptPolicyFunction callback, void* userData) const
110 {
111     WKCookieManagerGetHTTPCookieAcceptPolicy(m_cookieManager.get(), userData, callback);
112 }
113
114 void EwkCookieManager::cookiesDidChange(WKCookieManagerRef, const void* clientInfo)
115 {
116     EwkCookieManager* manager = static_cast<EwkCookieManager*>(const_cast<void*>(clientInfo));
117
118     if (!manager->isWatchingForChanges())
119         return;
120
121     manager->m_changeHandler.callback(manager->m_changeHandler.userData);
122 }
123
124 void ewk_cookie_manager_persistent_storage_set(Ewk_Cookie_Manager* manager, const char* filename, Ewk_Cookie_Persistent_Storage storage)
125 {
126     EINA_SAFETY_ON_NULL_RETURN(manager);
127     EINA_SAFETY_ON_NULL_RETURN(filename);
128
129     WKCookieStorageType wkCookieStorageType = kWKCookieStorageTypeText;
130
131     switch (storage) {
132     case EWK_COOKIE_PERSISTENT_STORAGE_TEXT:
133         wkCookieStorageType = kWKCookieStorageTypeText;
134         break;
135     case EWK_COOKIE_PERSISTENT_STORAGE_SQLITE:
136         wkCookieStorageType = kWKCookieStorageTypeSQLite;
137         break;
138     default:
139         ASSERT_NOT_REACHED();
140     }
141
142     manager->setPersistentStorage(filename, wkCookieStorageType);
143 }
144
145 void ewk_cookie_manager_accept_policy_set(Ewk_Cookie_Manager* manager, Ewk_Cookie_Accept_Policy policy)
146 {
147     EINA_SAFETY_ON_NULL_RETURN(manager);
148
149     WKHTTPCookieAcceptPolicy wkPolicy = kWKHTTPCookieAcceptPolicyAlways;
150
151     switch (policy) {
152     case EWK_COOKIE_ACCEPT_POLICY_ALWAYS:
153         wkPolicy = kWKHTTPCookieAcceptPolicyAlways;
154         break;
155     case EWK_COOKIE_ACCEPT_POLICY_NEVER:
156         wkPolicy = kWKHTTPCookieAcceptPolicyNever;
157         break;
158     case EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY:
159         wkPolicy = kWKHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain;
160         break;
161     default:
162         ASSERT_NOT_REACHED();
163     }
164
165     manager->setHTTPAcceptPolicy(wkPolicy);
166 }
167
168 struct Get_Policy_Async_Data {
169     Ewk_Cookie_Manager_Async_Policy_Get_Cb callback;
170     void* userData;
171
172     Get_Policy_Async_Data(Ewk_Cookie_Manager_Async_Policy_Get_Cb callback, void* userData)
173         : callback(callback)
174         , userData(userData)
175     { }
176 };
177
178 static void getAcceptPolicyCallback(WKHTTPCookieAcceptPolicy policy, WKErrorRef wkError, void* data)
179 {
180     auto callbackData = std::unique_ptr<Get_Policy_Async_Data>(static_cast<Get_Policy_Async_Data*>(data));
181     OwnPtr<EwkError> ewkError = EwkError::create(wkError);
182
183     callbackData->callback(static_cast<Ewk_Cookie_Accept_Policy>(policy), ewkError.get(), callbackData->userData);
184 }
185
186 void ewk_cookie_manager_async_accept_policy_get(const Ewk_Cookie_Manager* manager, Ewk_Cookie_Manager_Async_Policy_Get_Cb callback, void* data)
187 {
188     EINA_SAFETY_ON_NULL_RETURN(manager);
189     EINA_SAFETY_ON_NULL_RETURN(callback);
190
191     Get_Policy_Async_Data* callbackData = new Get_Policy_Async_Data(callback, data);
192     manager->getHTTPAcceptPolicy(getAcceptPolicyCallback, callbackData);
193 }
194
195 struct Get_Hostnames_Async_Data {
196     Ewk_Cookie_Manager_Async_Hostnames_Get_Cb callback;
197     void* userData;
198
199     Get_Hostnames_Async_Data(Ewk_Cookie_Manager_Async_Hostnames_Get_Cb callback, void* userData)
200         : callback(callback)
201         , userData(userData)
202     { }
203 };
204
205 static void getHostnamesWithCookiesCallback(WKArrayRef wkHostnames, WKErrorRef wkError, void* context)
206 {
207     Eina_List* hostnames = 0;
208     auto callbackData = std::unique_ptr<Get_Hostnames_Async_Data>(static_cast<Get_Hostnames_Async_Data*>(context));
209     OwnPtr<EwkError> ewkError = EwkError::create(wkError);
210
211     const size_t hostnameCount = WKArrayGetSize(wkHostnames);
212     for (size_t i = 0; i < hostnameCount; ++i) {
213         WKStringRef wkHostname = static_cast<WKStringRef>(WKArrayGetItemAtIndex(wkHostnames, i));
214         if (WKStringIsEmpty(wkHostname))
215             continue;
216         hostnames = eina_list_append(hostnames, WKEinaSharedString(wkHostname).leakString());
217     }
218
219     callbackData->callback(hostnames, ewkError.get(), callbackData->userData);
220
221     void* item;
222     EINA_LIST_FREE(hostnames, item)
223       eina_stringshare_del(static_cast<Eina_Stringshare*>(item));
224 }
225
226 void ewk_cookie_manager_async_hostnames_with_cookies_get(const Ewk_Cookie_Manager* manager, Ewk_Cookie_Manager_Async_Hostnames_Get_Cb callback, void* data)
227 {
228     EINA_SAFETY_ON_NULL_RETURN(manager);
229     EINA_SAFETY_ON_NULL_RETURN(callback);
230
231     Get_Hostnames_Async_Data* callbackData = new Get_Hostnames_Async_Data(callback, data);
232     manager->getHostNamesWithCookies(getHostnamesWithCookiesCallback, callbackData);
233 }
234
235 void ewk_cookie_manager_hostname_cookies_clear(Ewk_Cookie_Manager* manager, const char* hostname)
236 {
237     EINA_SAFETY_ON_NULL_RETURN(manager);
238     EINA_SAFETY_ON_NULL_RETURN(hostname);
239
240     manager->clearHostnameCookies(hostname);
241 }
242
243 void ewk_cookie_manager_cookies_clear(Ewk_Cookie_Manager* manager)
244 {
245     EINA_SAFETY_ON_NULL_RETURN(manager);
246
247     manager->clearAllCookies();
248 }
249
250 void ewk_cookie_manager_changes_watch(Ewk_Cookie_Manager* manager, Ewk_Cookie_Manager_Changes_Watch_Cb callback, void* data)
251 {
252     EINA_SAFETY_ON_NULL_RETURN(manager);
253
254     manager->watchChanges(Cookie_Change_Handler(callback, data));
255 }