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_database_manager.h"
28
29#include "WKAPICast.h"
30#include "WKArray.h"
31#include "ewk_database_manager_private.h"
32#include "ewk_security_origin_private.h"
33
34using namespace WebKit;
35
36EwkDatabaseManager::EwkDatabaseManager(WKDatabaseManagerRef databaseManager)
37    : m_databaseManager(databaseManager)
38{
39    ASSERT(databaseManager);
40}
41
42void EwkDatabaseManager::getDatabaseOrigins(WKDatabaseManagerGetDatabaseOriginsFunction callback, void* context) const
43{
44    WKDatabaseManagerGetDatabaseOrigins(m_databaseManager.get(), context, callback);
45}
46
47Eina_List* EwkDatabaseManager::createOriginList(WKArrayRef origins) const
48{
49    Eina_List* originList = 0;
50    const size_t length = WKArrayGetSize(origins);
51
52    for (size_t i = 0; i < length; ++i) {
53        WKSecurityOriginRef wkOriginRef = static_cast<WKSecurityOriginRef>(WKArrayGetItemAtIndex(origins, i));
54        RefPtr<EwkSecurityOrigin> origin = m_wrapperCache.get(wkOriginRef);
55        if (!origin) {
56            origin = EwkSecurityOrigin::create(wkOriginRef);
57            m_wrapperCache.set(wkOriginRef, origin);
58        }
59        originList = eina_list_append(originList, origin.release().leakRef());
60    }
61
62    return originList;
63}
64
65struct EwkDatabaseOriginsAsyncData {
66    const Ewk_Database_Manager* manager;
67    Ewk_Database_Origins_Async_Get_Cb callback;
68    void* userData;
69
70    EwkDatabaseOriginsAsyncData(const Ewk_Database_Manager* manager, Ewk_Database_Origins_Async_Get_Cb callback, void* userData)
71        : manager(manager)
72        , callback(callback)
73        , userData(userData)
74    { }
75};
76
77static void getDatabaseOriginsCallback(WKArrayRef origins, WKErrorRef, void* context)
78{
79    auto webDatabaseContext = std::unique_ptr<EwkDatabaseOriginsAsyncData>(static_cast<EwkDatabaseOriginsAsyncData*>(context));
80    Eina_List* originList = webDatabaseContext->manager->createOriginList(origins);
81    webDatabaseContext->callback(originList, webDatabaseContext->userData);
82}
83
84Eina_Bool ewk_database_manager_origins_async_get(const Ewk_Database_Manager* ewkDatabaseManager, Ewk_Database_Origins_Async_Get_Cb callback, void* userData)
85{
86    EINA_SAFETY_ON_NULL_RETURN_VAL(ewkDatabaseManager, false);
87    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, false);
88
89    EwkDatabaseOriginsAsyncData* context = new EwkDatabaseOriginsAsyncData(ewkDatabaseManager, callback, userData);
90    ewkDatabaseManager->getDatabaseOrigins(getDatabaseOriginsCallback, context);
91
92    return true;
93}
94