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 "WKKeyValueStorageManager.h"
32#include "ewk_error_private.h"
33#include "ewk_security_origin.h"
34#include "ewk_security_origin_private.h"
35#include "ewk_storage_manager_private.h"
36#include <wtf/RefCounted.h>
37
38using namespace WebKit;
39
40Ewk_Storage_Manager::Ewk_Storage_Manager(WKKeyValueStorageManagerRef wkStorageManagerRef)
41 : m_wkStorageManager(wkStorageManagerRef)
42{ }
43
44void Ewk_Storage_Manager::getStorageOrigins(void* context, WKKeyValueStorageManagerGetKeyValueStorageOriginsFunction callback) const
45{
46 WKKeyValueStorageManagerGetKeyValueStorageOrigins(m_wkStorageManager.get(), context, callback);
47}
48
49Eina_List* Ewk_Storage_Manager::createOriginList(WKArrayRef origins) const
50{
51 Eina_List* originList = 0;
52 const size_t length = WKArrayGetSize(origins);
53
54 for (size_t i = 0; i < length; ++i) {
55 WKSecurityOriginRef wkOriginRef = static_cast<WKSecurityOriginRef>(WKArrayGetItemAtIndex(origins, i));
56 RefPtr<Ewk_Security_Origin> origin = m_wrapperCache.get(wkOriginRef);
57 if (!origin) {
58 origin = Ewk_Security_Origin::create(wkOriginRef);
59 m_wrapperCache.set(wkOriginRef, origin);
60 }
61 originList = eina_list_append(originList, origin.release().leakRef());
62 }
63
64 return originList;
65}
66
67struct Ewk_Storage_Origins_Async_Get_Context {
68 const Ewk_Storage_Manager* manager;
69 Ewk_Storage_Origins_Get_Cb callback;
70 void* userData;
71
72 Ewk_Storage_Origins_Async_Get_Context(const Ewk_Storage_Manager* manager, Ewk_Storage_Origins_Get_Cb callback, void* userData)
73 : manager(manager)
74 , callback(callback)
75 , userData(userData)
76 { }
77};
78
79static void getStorageOriginsCallback(WKArrayRef origins, WKErrorRef wkError, void* context)
80{
81 Eina_List* originList = 0;
82 OwnPtr<Ewk_Storage_Origins_Async_Get_Context*> webStorageContext = adoptPtr(static_cast<Ewk_Storage_Origins_Async_Get_Context*>(context));
83
84 originList = webStorageContext->manager->createOriginList(origins);
85
86 OwnPtr<Ewk_Error> ewkError = Ewk_Error::create(wkError);
87 webStorageContext->callback(originList, ewkError.get(), webStorageContext->userData);
88}
89
90Eina_Bool ewk_storage_manager_origins_get(const Ewk_Storage_Manager* ewkStorageManager, Ewk_Storage_Origins_Get_Cb callback, void* userData)
91{
92 EINA_SAFETY_ON_NULL_RETURN_VAL(ewkStorageManager, false);
93 EINA_SAFETY_ON_NULL_RETURN_VAL(callback, false);
94
95 Ewk_Storage_Origins_Async_Get_Context* context = new Ewk_Storage_Origins_Async_Get_Context(ewkStorageManager, callback, userData);
96 ewkStorageManager->getStorageOrigins(context, getStorageOriginsCallback);
97
98 return true;
99}