2 * Copyright (C) 2012 Intel Corporation. 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 "ewk_cookie_manager.h"
29 #include "SoupCookiePersistentStorageType.h"
30 #include "WKAPICast.h"
33 #include "WebCookieManagerProxy.h"
34 #include "ewk_cookie_manager_private.h"
35 #include "ewk_error_private.h"
36 #include "ewk_private.h"
37 #include <wtf/OwnPtr.h>
38 #include <wtf/text/CString.h>
39 #include <wtf/text/WTFString.h>
41 using namespace WebKit;
43 static void cookiesDidChange(WKCookieManagerRef, const void* clientInfo);
45 Ewk_Cookie_Manager::Ewk_Cookie_Manager(WKCookieManagerRef cookieManagerRef)
46 : wkCookieManager(cookieManagerRef)
48 WKCookieManagerClient wkCookieManagerClient = {
49 kWKCookieManagerClientCurrentVersion,
53 WKCookieManagerSetClient(wkCookieManager.get(), &wkCookieManagerClient);
56 Ewk_Cookie_Manager::~Ewk_Cookie_Manager()
58 if (changeHandler.callback)
59 WKCookieManagerStopObservingCookieChanges(wkCookieManager.get());
62 #define EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager_, ...) \
64 EINA_LOG_CRIT("manager is NULL."); \
67 if (!(manager)->wkCookieManager) { \
68 EINA_LOG_CRIT("manager->wkCookieManager is NULL."); \
71 WKCookieManagerRef wkManager_ = (manager)->wkCookieManager.get()
73 // Ewk_Cookie_Accept_Policy enum validation
74 COMPILE_ASSERT_MATCHING_ENUM(EWK_COOKIE_ACCEPT_POLICY_ALWAYS, kWKHTTPCookieAcceptPolicyAlways);
75 COMPILE_ASSERT_MATCHING_ENUM(EWK_COOKIE_ACCEPT_POLICY_NEVER, kWKHTTPCookieAcceptPolicyNever);
76 COMPILE_ASSERT_MATCHING_ENUM(EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY, kWKHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain);
78 // Ewk_Cookie_Persistent_Storage enum validation
79 COMPILE_ASSERT_MATCHING_ENUM(EWK_COOKIE_PERSISTENT_STORAGE_TEXT, SoupCookiePersistentStorageText);
80 COMPILE_ASSERT_MATCHING_ENUM(EWK_COOKIE_PERSISTENT_STORAGE_SQLITE, SoupCookiePersistentStorageSQLite);
82 static void cookiesDidChange(WKCookieManagerRef, const void* clientInfo)
84 Ewk_Cookie_Manager* manager = static_cast<Ewk_Cookie_Manager*>(const_cast<void*>(clientInfo));
86 if (!manager->changeHandler.callback)
89 manager->changeHandler.callback(manager->changeHandler.userData);
92 void ewk_cookie_manager_persistent_storage_set(Ewk_Cookie_Manager* manager, const char* filename, Ewk_Cookie_Persistent_Storage storage)
94 EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
95 EINA_SAFETY_ON_NULL_RETURN(filename);
97 if (manager->changeHandler.callback)
98 WKCookieManagerStopObservingCookieChanges(wkManager);
100 toImpl(wkManager)->setCookiePersistentStorage(String::fromUTF8(filename), storage);
102 if (manager->changeHandler.callback)
103 WKCookieManagerStartObservingCookieChanges(wkManager);
106 void ewk_cookie_manager_accept_policy_set(Ewk_Cookie_Manager* manager, Ewk_Cookie_Accept_Policy policy)
108 EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
110 WKCookieManagerSetHTTPCookieAcceptPolicy(wkManager, static_cast<WKHTTPCookieAcceptPolicy>(policy));
113 struct Get_Policy_Async_Data {
114 Ewk_Cookie_Manager_Async_Policy_Get_Cb callback;
118 static void getAcceptPolicyCallback(WKHTTPCookieAcceptPolicy policy, WKErrorRef wkError, void* data)
120 Get_Policy_Async_Data* callbackData = static_cast<Get_Policy_Async_Data*>(data);
121 OwnPtr<Ewk_Error> ewkError = Ewk_Error::create(wkError);
123 callbackData->callback(static_cast<Ewk_Cookie_Accept_Policy>(policy), ewkError.get(), callbackData->userData);
128 void ewk_cookie_manager_async_accept_policy_get(const Ewk_Cookie_Manager* manager, Ewk_Cookie_Manager_Async_Policy_Get_Cb callback, void* data)
130 EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
131 EINA_SAFETY_ON_NULL_RETURN(callback);
133 Get_Policy_Async_Data* callbackData = new Get_Policy_Async_Data;
134 callbackData->callback = callback;
135 callbackData->userData = data;
137 WKCookieManagerGetHTTPCookieAcceptPolicy(wkManager, callbackData, getAcceptPolicyCallback);
140 struct Get_Hostnames_Async_Data {
141 Ewk_Cookie_Manager_Async_Hostnames_Get_Cb callback;
145 static void getHostnamesWithCookiesCallback(WKArrayRef wkHostnames, WKErrorRef wkError, void* context)
147 Eina_List* hostnames = 0;
148 Get_Hostnames_Async_Data* callbackData = static_cast<Get_Hostnames_Async_Data*>(context);
149 OwnPtr<Ewk_Error> ewkError = Ewk_Error::create(wkError);
151 const size_t hostnameCount = WKArrayGetSize(wkHostnames);
152 for (size_t i = 0; i < hostnameCount; ++i) {
153 WKStringRef wkHostname = static_cast<WKStringRef>(WKArrayGetItemAtIndex(wkHostnames, i));
154 String hostname = toImpl(wkHostname)->string();
155 if (hostname.isEmpty())
157 hostnames = eina_list_append(hostnames, eina_stringshare_add(hostname.utf8().data()));
160 callbackData->callback(hostnames, ewkError.get(), callbackData->userData);
163 EINA_LIST_FREE(hostnames, item)
164 eina_stringshare_del(static_cast<Eina_Stringshare*>(item));
169 void ewk_cookie_manager_async_hostnames_with_cookies_get(const Ewk_Cookie_Manager* manager, Ewk_Cookie_Manager_Async_Hostnames_Get_Cb callback, void* data)
171 EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
172 EINA_SAFETY_ON_NULL_RETURN(callback);
174 Get_Hostnames_Async_Data* callbackData = new Get_Hostnames_Async_Data;
175 callbackData->callback = callback;
176 callbackData->userData = data;
178 WKCookieManagerGetHostnamesWithCookies(wkManager, callbackData, getHostnamesWithCookiesCallback);
181 void ewk_cookie_manager_hostname_cookies_clear(Ewk_Cookie_Manager* manager, const char* hostName)
183 EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
184 EINA_SAFETY_ON_NULL_RETURN(hostName);
186 WKRetainPtr<WKStringRef> wkHostName(AdoptWK, WKStringCreateWithUTF8CString(hostName));
187 WKCookieManagerDeleteCookiesForHostname(wkManager, wkHostName.get());
190 void ewk_cookie_manager_cookies_clear(Ewk_Cookie_Manager* manager)
192 EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
194 WKCookieManagerDeleteAllCookies(wkManager);
197 void ewk_cookie_manager_changes_watch(Ewk_Cookie_Manager* manager, Ewk_Cookie_Manager_Changes_Watch_Cb callback, void* data)
199 EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
201 manager->changeHandler = Cookie_Change_Handler(callback, data);
204 WKCookieManagerStartObservingCookieChanges(wkManager);
206 WKCookieManagerStopObservingCookieChanges(wkManager);