Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.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 "SoupCookiePersistentStorageType.h"
30 #include "WKAPICast.h"
31 #include "WKArray.h"
32 #include "WKString.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>
40
41 using namespace WebKit;
42
43 static void cookiesDidChange(WKCookieManagerRef, const void* clientInfo);
44
45 Ewk_Cookie_Manager::Ewk_Cookie_Manager(WKCookieManagerRef cookieManagerRef)
46     : wkCookieManager(cookieManagerRef)
47 {
48     WKCookieManagerClient wkCookieManagerClient = {
49         kWKCookieManagerClientCurrentVersion,
50         this, // clientInfo
51         cookiesDidChange
52     };
53     WKCookieManagerSetClient(wkCookieManager.get(), &wkCookieManagerClient);
54 }
55
56 Ewk_Cookie_Manager::~Ewk_Cookie_Manager()
57 {
58     if (changeHandler.callback)
59         WKCookieManagerStopObservingCookieChanges(wkCookieManager.get());
60 }
61
62 #define EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager_, ...)    \
63     if (!(manager)) {                                                    \
64         EINA_LOG_CRIT("manager is NULL.");                               \
65         return __VA_ARGS__;                                              \
66     }                                                                    \
67     if (!(manager)->wkCookieManager) {                                   \
68         EINA_LOG_CRIT("manager->wkCookieManager is NULL.");              \
69         return __VA_ARGS__;                                              \
70     }                                                                    \
71     WKCookieManagerRef wkManager_ = (manager)->wkCookieManager.get()
72
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);
77
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);
81
82 static void cookiesDidChange(WKCookieManagerRef, const void* clientInfo)
83 {
84     Ewk_Cookie_Manager* manager = static_cast<Ewk_Cookie_Manager*>(const_cast<void*>(clientInfo));
85
86     if (!manager->changeHandler.callback)
87         return;
88
89     manager->changeHandler.callback(manager->changeHandler.userData);
90 }
91
92 void ewk_cookie_manager_persistent_storage_set(Ewk_Cookie_Manager* manager, const char* filename, Ewk_Cookie_Persistent_Storage storage)
93 {
94     EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
95     EINA_SAFETY_ON_NULL_RETURN(filename);
96
97     if (manager->changeHandler.callback)
98         WKCookieManagerStopObservingCookieChanges(wkManager);
99
100     toImpl(wkManager)->setCookiePersistentStorage(String::fromUTF8(filename), storage);
101
102     if (manager->changeHandler.callback)
103         WKCookieManagerStartObservingCookieChanges(wkManager);
104 }
105
106 void ewk_cookie_manager_accept_policy_set(Ewk_Cookie_Manager* manager, Ewk_Cookie_Accept_Policy policy)
107 {
108     EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
109
110     WKCookieManagerSetHTTPCookieAcceptPolicy(wkManager, static_cast<WKHTTPCookieAcceptPolicy>(policy));
111 }
112
113 struct Get_Policy_Async_Data {
114     Ewk_Cookie_Manager_Async_Policy_Get_Cb callback;
115     void* userData;
116 };
117
118 static void getAcceptPolicyCallback(WKHTTPCookieAcceptPolicy policy, WKErrorRef wkError, void* data)
119 {
120     Get_Policy_Async_Data* callbackData = static_cast<Get_Policy_Async_Data*>(data);
121     OwnPtr<Ewk_Error> ewkError = Ewk_Error::create(wkError);
122
123     callbackData->callback(static_cast<Ewk_Cookie_Accept_Policy>(policy), ewkError.get(), callbackData->userData);
124
125     delete callbackData;
126 }
127
128 void ewk_cookie_manager_async_accept_policy_get(const Ewk_Cookie_Manager* manager, Ewk_Cookie_Manager_Async_Policy_Get_Cb callback, void* data)
129 {
130     EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
131     EINA_SAFETY_ON_NULL_RETURN(callback);
132
133     Get_Policy_Async_Data* callbackData = new Get_Policy_Async_Data;
134     callbackData->callback = callback;
135     callbackData->userData = data;
136
137     WKCookieManagerGetHTTPCookieAcceptPolicy(wkManager, callbackData, getAcceptPolicyCallback);
138 }
139
140 struct Get_Hostnames_Async_Data {
141     Ewk_Cookie_Manager_Async_Hostnames_Get_Cb callback;
142     void* userData;
143 };
144
145 static void getHostnamesWithCookiesCallback(WKArrayRef wkHostnames, WKErrorRef wkError, void* context)
146 {
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);
150
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())
156             continue;
157         hostnames = eina_list_append(hostnames, eina_stringshare_add(hostname.utf8().data()));
158     }
159
160     callbackData->callback(hostnames, ewkError.get(), callbackData->userData);
161
162     void* item;
163     EINA_LIST_FREE(hostnames, item)
164       eina_stringshare_del(static_cast<Eina_Stringshare*>(item));
165
166     delete callbackData;
167 }
168
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)
170 {
171     EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
172     EINA_SAFETY_ON_NULL_RETURN(callback);
173
174     Get_Hostnames_Async_Data* callbackData = new Get_Hostnames_Async_Data;
175     callbackData->callback = callback;
176     callbackData->userData = data;
177
178     WKCookieManagerGetHostnamesWithCookies(wkManager, callbackData, getHostnamesWithCookiesCallback);
179 }
180
181 void ewk_cookie_manager_hostname_cookies_clear(Ewk_Cookie_Manager* manager, const char* hostName)
182 {
183     EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
184     EINA_SAFETY_ON_NULL_RETURN(hostName);
185
186     WKRetainPtr<WKStringRef> wkHostName(AdoptWK, WKStringCreateWithUTF8CString(hostName));
187     WKCookieManagerDeleteCookiesForHostname(wkManager, wkHostName.get());
188 }
189
190 void ewk_cookie_manager_cookies_clear(Ewk_Cookie_Manager* manager)
191 {
192     EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
193
194     WKCookieManagerDeleteAllCookies(wkManager);
195 }
196
197 void ewk_cookie_manager_changes_watch(Ewk_Cookie_Manager* manager, Ewk_Cookie_Manager_Changes_Watch_Cb callback, void* data)
198 {
199     EWK_COOKIE_MANAGER_WK_GET_OR_RETURN(manager, wkManager);
200
201     manager->changeHandler = Cookie_Change_Handler(callback, data);
202
203     if (callback)
204         WKCookieManagerStartObservingCookieChanges(wkManager);
205     else
206         WKCookieManagerStopObservingCookieChanges(wkManager);
207 }