33f4fd2ab8c4b4635ce47fdbf068ccaf8c59c647
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / ewk_storage_manager.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics. 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" 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 THE COPYRIGHT HOLDER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ewk_storage_manager.h"
28
29 #include "WKAPICast.h"
30 #include "WKArray.h"
31 #include "ewk_error_private.h"
32 #include "ewk_security_origin_private.h"
33 #include "ewk_storage_manager_private.h"
34
35 using namespace WebKit;
36
37 EwkStorageManager::EwkStorageManager(WKKeyValueStorageManagerRef storageManager)
38     : m_storageManager(storageManager)
39 {
40     ASSERT(storageManager);
41 }
42
43 void EwkStorageManager::getStorageOrigins(void* context, WKKeyValueStorageManagerGetKeyValueStorageOriginsFunction callback) const
44 {
45     WKKeyValueStorageManagerGetKeyValueStorageOrigins(m_storageManager.get(), context, callback);
46 }
47
48 Eina_List* EwkStorageManager::createOriginList(WKArrayRef origins) const
49 {
50     Eina_List* originList = 0;
51     const size_t length = WKArrayGetSize(origins);
52
53     for (size_t i = 0; i < length; ++i) {
54         WKSecurityOriginRef wkOriginRef = static_cast<WKSecurityOriginRef>(WKArrayGetItemAtIndex(origins, i));
55         RefPtr<EwkSecurityOrigin> origin = m_wrapperCache.get(wkOriginRef);
56         if (!origin) {
57             origin = EwkSecurityOrigin::create(wkOriginRef);
58             m_wrapperCache.set(wkOriginRef, origin);
59         }
60         originList = eina_list_append(originList, origin.release().leakRef());
61     }
62
63     return originList;
64 }
65
66 struct Ewk_Storage_Origins_Async_Get_Context {
67     const Ewk_Storage_Manager* manager;
68     Ewk_Storage_Origins_Get_Cb callback;
69     void* userData;
70
71     Ewk_Storage_Origins_Async_Get_Context(const Ewk_Storage_Manager* manager, Ewk_Storage_Origins_Get_Cb callback, void* userData)
72         : manager(manager)
73         , callback(callback)
74         , userData(userData)
75     { }
76 };
77
78 static void getStorageOriginsCallback(WKArrayRef origins, WKErrorRef wkError, void* context)
79 {
80     Eina_List* originList = 0;
81     auto webStorageContext = std::unique_ptr<Ewk_Storage_Origins_Async_Get_Context>(static_cast<Ewk_Storage_Origins_Async_Get_Context*>(context));
82
83     originList = webStorageContext->manager->createOriginList(origins);
84
85     OwnPtr<EwkError> ewkError = EwkError::create(wkError);
86     webStorageContext->callback(originList, ewkError.get(), webStorageContext->userData);
87 }
88
89 Eina_Bool ewk_storage_manager_origins_get(const Ewk_Storage_Manager* ewkStorageManager, Ewk_Storage_Origins_Get_Cb callback, void* userData)
90 {
91     EINA_SAFETY_ON_NULL_RETURN_VAL(ewkStorageManager, false);
92     EINA_SAFETY_ON_NULL_RETURN_VAL(callback, false);
93
94     Ewk_Storage_Origins_Async_Get_Context* context = new Ewk_Storage_Origins_Async_Get_Context(ewkStorageManager, callback, userData);
95     ewkStorageManager->getStorageOrigins(context, getStorageOriginsCallback);
96
97     return true;
98 }