add patch
[framework/osp/web.git] / src / controls / FWebCtrl_WebStorageManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FWebCtrl_WebStorageManagerImpl.cpp
20  * @brief               The file contains the definition of _WebStorageManagerImpl class.
21  */
22 #include <EWebKit2.h>
23 #include <FBaseColAllElementsDeleter.h>
24 #include <FBaseColArrayList.h>
25 #include <FBaseColIList.h>
26 #include <FBaseSysLog.h>
27 #include "FWebCtrl_Utility.h"
28 #include "FWebCtrl_WebPresenter.h"
29 #include "FWebCtrl_WebStorageManagerImpl.h"
30
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34
35
36 namespace Tizen { namespace Web { namespace Controls
37 {
38
39
40 void
41 OnOriginReceived(Eina_List* pOrigins, void* pUserData)
42 {
43         _WebPresenter* pPresenter = reinterpret_cast< _WebPresenter* >(pUserData);
44
45         pPresenter->EndAsyncProcess(pOrigins);
46 }
47
48
49 void
50 OnIntReceived(int64_t integer, void* pUserData)
51 {
52         _WebPresenter* pPresenter = reinterpret_cast< _WebPresenter* >(pUserData);
53
54         pPresenter->EndAsyncProcess((long)integer);
55 }
56
57
58 void
59 OnUIntReceived(uint64_t integer, void* pUserData)
60 {
61         _WebPresenter* pPresenter = reinterpret_cast< _WebPresenter* >(pUserData);
62
63         pPresenter->EndAsyncProcess((long)integer);
64 }
65
66
67 _WebStorageManagerImpl::_WebStorageManagerImpl(void)
68 {
69 }
70
71
72 _WebStorageManagerImpl::~_WebStorageManagerImpl(void)
73 {
74 }
75
76
77 IList*
78 _WebStorageManagerImpl::GetOriginListN(WebStorageType storageType)
79 {
80         result r = E_SUCCESS;
81
82         Ewk_Context* pDefaultContext = ewk_context_default_get();
83
84         std::unique_ptr<_WebPresenter> pPresenter(new (std::nothrow) _WebPresenter(this));
85         SysTryReturn(NID_WEB_CTRL, pPresenter.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
86
87         switch (storageType)
88         {
89         case WEB_STORAGE_TYPE_APPLICATION_CACHE:
90                 ewk_context_application_cache_origins_get(pDefaultContext, OnOriginReceived, pPresenter.get());
91                 break;
92
93         case WEB_STORAGE_TYPE_WEB_SQL_DATABASE:
94                 ewk_context_web_database_origins_get(pDefaultContext, OnOriginReceived, pPresenter.get());
95                 break;
96
97         case WEB_STORAGE_TYPE_WEB_STORAGE:
98                 ewk_context_web_storage_origins_get(pDefaultContext, OnOriginReceived, pPresenter.get());
99                 break;
100
101         case WEB_STORAGE_TYPE_FILE_SYSTEM:
102                 ewk_context_local_file_system_origins_get(pDefaultContext, OnOriginReceived, pPresenter.get());
103                 break;
104
105         default:
106                 break;
107         }
108
109         Eina_List* pOriginEinaList = pPresenter->WaitAsyncProcess();
110         if (!pOriginEinaList)
111         {
112                 return null;
113         }
114
115         std::unique_ptr<ArrayList, AllElementsDeleter> pOriginList(new (std::nothrow) ArrayList());
116         SysTryReturn(NID_WEB_CTRL, pOriginList.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
117
118         r = pOriginList->Construct();
119         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
120
121         for (; pOriginEinaList; pOriginEinaList = eina_list_next(pOriginEinaList))
122         {
123                 Ewk_Security_Origin* pOrigin = static_cast<Ewk_Security_Origin*>(eina_list_data_get(pOriginEinaList));
124                 std::unique_ptr<String> pStringOrigin(new (std::nothrow) String(_Utility::CreateOrigin(pOrigin)));
125                 SysTryReturn(NID_WEB_CTRL, pStringOrigin.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
126
127                 r = pOriginList->Add(pStringOrigin.get());
128                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
129                 pStringOrigin.release();
130         }
131
132         return pOriginList.release();
133 }
134
135
136 result
137 _WebStorageManagerImpl::SetQuotaForOrigin(WebStorageType storageType, const String& origin, long quota)
138 {
139         Ewk_Context* pDefaultContext = ewk_context_default_get();
140         Ewk_Security_Origin* pOrigin = null;
141
142         switch (storageType)
143         {
144         case WEB_STORAGE_TYPE_APPLICATION_CACHE:
145                 pOrigin = CompareOrigin(WEB_STORAGE_TYPE_APPLICATION_CACHE, origin);
146                 SysTryReturnResult(NID_WEB_CTRL, pOrigin, E_INVALID_ARG, "This origin does not exist.");
147
148                 ewk_context_application_cache_quota_for_origin_set(pDefaultContext, pOrigin, quota);
149                 break;
150
151         case WEB_STORAGE_TYPE_WEB_SQL_DATABASE:
152                 pOrigin = CompareOrigin(WEB_STORAGE_TYPE_WEB_SQL_DATABASE, origin);
153                 SysTryReturnResult(NID_WEB_CTRL, pOrigin, E_INVALID_ARG, "This origin does not exist.");
154
155                 ewk_context_web_database_quota_for_origin_set(pDefaultContext, pOrigin, quota);
156                 break;
157
158         default:
159                 SysLogException(NID_WEB_CTRL, E_UNSUPPORTED_TYPE, "This storage type %d is not supported.", storageType);
160                 return E_UNSUPPORTED_TYPE;
161         }
162         return E_SUCCESS;
163 }
164
165
166 long
167 _WebStorageManagerImpl::GetQuotaForOrigin(WebStorageType storageType, const String& origin)
168 {
169         Ewk_Context* pDefaultContext = ewk_context_default_get();
170         long quota = 0;
171
172         SysTryReturn(NID_WEB_CTRL, storageType == WEB_STORAGE_TYPE_WEB_SQL_DATABASE, -1, E_UNSUPPORTED_TYPE, "[%s] This storage type is not supported.", GetErrorMessage(E_UNSUPPORTED_TYPE));
173
174         Ewk_Security_Origin* pOrigin = CompareOrigin(storageType, origin);
175         SysTryReturn(NID_WEB_CTRL, pOrigin, -1.0, E_INVALID_ARG, "[%s] This origin does not exist.", GetErrorMessage(E_INVALID_ARG));
176
177         std::unique_ptr<_WebPresenter> pPresenter(new (std::nothrow) _WebPresenter(this));
178         SysTryReturn(NID_WEB_CTRL, pPresenter.get(), -1.0, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
179
180         ewk_context_web_database_quota_for_origin_get(pDefaultContext, OnUIntReceived, pPresenter.get(), pOrigin);
181
182         pPresenter->WaitAsyncProcess(quota);
183
184         return quota;
185 }
186
187
188 long
189 _WebStorageManagerImpl::GetUsageForOrigin(WebStorageType storageType, const String& origin)
190 {
191         Ewk_Context* pDefaultContext = ewk_context_default_get();
192         long usage = 0;
193
194         SysTryReturn(NID_WEB_CTRL, (storageType != WEB_STORAGE_TYPE_INDEXED_DATABASE) && (storageType != WEB_STORAGE_TYPE_FILE_SYSTEM), -1, E_UNSUPPORTED_TYPE, "[%s] This storage type is not supported.", GetErrorMessage(E_UNSUPPORTED_TYPE));
195
196         Ewk_Security_Origin* pOrigin = CompareOrigin(storageType, origin);
197         SysTryReturn(NID_WEB_CTRL, pOrigin, -1, E_INVALID_ARG, "[%s] This origin does not exist.", GetErrorMessage(E_INVALID_ARG));
198
199         std::unique_ptr<_WebPresenter> pPresenter(new (std::nothrow) _WebPresenter(this));
200         SysTryReturn(NID_WEB_CTRL, pPresenter.get(), -1.0, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
201
202         switch (storageType)
203         {
204         case WEB_STORAGE_TYPE_APPLICATION_CACHE:
205 //              ewk_context_application_cache_usage_for_origin_get(pDefaultContext, pOrigin, OnIntReceived, pPresenter.get());
206                 break;
207
208         case WEB_STORAGE_TYPE_WEB_SQL_DATABASE:
209                 ewk_context_web_database_usage_for_origin_get(pDefaultContext, OnUIntReceived, pPresenter.get(), pOrigin);
210                 break;
211
212         case WEB_STORAGE_TYPE_WEB_STORAGE :
213                 ewk_context_web_storage_usage_for_origin_get(pDefaultContext, pOrigin, OnUIntReceived, pPresenter.get());
214                 break;
215
216         default:
217                 break;
218         }
219
220         pPresenter->WaitAsyncProcess(usage);
221
222         return usage;
223 }
224
225
226 result
227 _WebStorageManagerImpl::Remove(WebStorageType storageType, const String& origin)
228 {
229         Ewk_Context* pDefaultContext = ewk_context_default_get();
230
231         SysTryReturnResult(NID_WEB_CTRL, storageType != WEB_STORAGE_TYPE_INDEXED_DATABASE, E_UNSUPPORTED_TYPE, "This storage type is not supported.");
232
233         Ewk_Security_Origin* pOrigin = CompareOrigin(storageType, origin);
234         SysTryReturnResult(NID_WEB_CTRL, pOrigin, E_INVALID_ARG, "This origin does not exist.");
235
236         switch (storageType)
237         {
238         case WEB_STORAGE_TYPE_APPLICATION_CACHE:
239                 ewk_context_application_cache_delete(pDefaultContext, pOrigin);
240                 break;
241
242         case WEB_STORAGE_TYPE_WEB_SQL_DATABASE:
243                 ewk_context_web_database_delete(pDefaultContext, pOrigin);
244                 break;
245
246         case WEB_STORAGE_TYPE_WEB_STORAGE :
247                 ewk_context_web_storage_origin_delete(pDefaultContext, pOrigin);
248                 break;
249
250         case WEB_STORAGE_TYPE_FILE_SYSTEM:
251                 ewk_context_local_file_system_delete(pDefaultContext, pOrigin);
252                 break;
253
254         default:
255                 break;
256         }
257
258         return E_SUCCESS;
259 }
260
261
262 result
263 _WebStorageManagerImpl::RemoveAll(WebStorageType storageType)
264 {
265         Ewk_Context* pDefaultContext = ewk_context_default_get();
266
267         switch (storageType)
268         {
269         case WEB_STORAGE_TYPE_APPLICATION_CACHE:
270                 ewk_context_application_cache_delete_all(pDefaultContext);
271                 break;
272
273         case WEB_STORAGE_TYPE_INDEXED_DATABASE :
274                 ewk_context_web_indexed_database_delete_all(pDefaultContext);
275                 break;
276
277         case WEB_STORAGE_TYPE_WEB_SQL_DATABASE:
278                 ewk_context_web_database_delete_all(pDefaultContext);
279                 break;
280
281         case WEB_STORAGE_TYPE_WEB_STORAGE :
282                 ewk_context_web_storage_delete_all(pDefaultContext);
283                 break;
284
285         case WEB_STORAGE_TYPE_FILE_SYSTEM:
286                 ewk_context_local_file_system_all_delete(pDefaultContext);
287                 break;
288
289         default:
290                 break;
291         }
292
293         return E_SUCCESS;
294 }
295
296
297 Ewk_Security_Origin*
298 _WebStorageManagerImpl::CompareOrigin(WebStorageType storageType, const String& origin)
299 {
300         Ewk_Context* pDefaultContext = ewk_context_default_get();
301
302         std::unique_ptr<_WebPresenter> pPresenter(new (std::nothrow) _WebPresenter(this));
303         SysTryReturn(NID_WEB_CTRL, pPresenter.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
304
305         switch (storageType)
306         {
307         case WEB_STORAGE_TYPE_APPLICATION_CACHE:
308                 ewk_context_application_cache_origins_get(pDefaultContext, OnOriginReceived, pPresenter.get());
309                 break;
310
311         case WEB_STORAGE_TYPE_WEB_SQL_DATABASE:
312                 ewk_context_web_database_origins_get(pDefaultContext, OnOriginReceived, pPresenter.get());
313                 break;
314
315         case WEB_STORAGE_TYPE_WEB_STORAGE:
316                 ewk_context_web_storage_origins_get(pDefaultContext, OnOriginReceived, pPresenter.get());
317                 break;
318
319         case WEB_STORAGE_TYPE_FILE_SYSTEM:
320                 ewk_context_local_file_system_origins_get(pDefaultContext, OnOriginReceived, pPresenter.get());
321                 break;
322
323         default:
324                 break;
325         }
326
327         Eina_List* pOriginList = pPresenter->WaitAsyncProcess();
328         if (!pOriginList)
329         {
330                 return null;
331         }
332
333         for (; pOriginList; pOriginList = eina_list_next(pOriginList))
334         {
335                 Ewk_Security_Origin* pOrigin = static_cast<Ewk_Security_Origin*>(eina_list_data_get(pOriginList));
336                 SysTryReturn(NID_WEB_CTRL, pOrigin, null, E_INVALID_ARG, "[%s] This origin does not exist.", GetErrorMessage(E_INVALID_ARG));
337
338                 int cmp = origin.CompareTo(_Utility::CreateOrigin(pOrigin));
339
340                 if (cmp ==0)
341                 {
342                         return pOrigin;
343                 }
344         }
345
346         return null;
347 }
348 }}} // Tizen::Web::Controls