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_error_private.h"
33#include "ewk_security_origin_private.h"
34
35using namespace WebKit;
36
37EwkDatabaseManager::EwkDatabaseManager(WKDatabaseManagerRef databaseManager)
38    : m_databaseManager(databaseManager)
39{
40    ASSERT(databaseManager);
41}
42
43void EwkDatabaseManager::getDatabaseOrigins(WKDatabaseManagerGetDatabaseOriginsFunction callback, void* context) const
44{
45    WKDatabaseManagerGetDatabaseOrigins(m_databaseManager.get(), context, callback);
46}
47
48Eina_List* EwkDatabaseManager::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
66struct Ewk_Database_Origins_Async_Get_Context {
67    const Ewk_Database_Manager* manager;
68    Ewk_Database_Origins_Get_Cb callback;
69    void* userData;
70
71    Ewk_Database_Origins_Async_Get_Context(const Ewk_Database_Manager* manager, Ewk_Database_Origins_Get_Cb callback, void* userData)
72        : manager(manager)
73        , callback(callback)
74        , userData(userData)
75    { }
76};
77
78static void getDatabaseOriginsCallback(WKArrayRef origins, WKErrorRef wkError, void* context)
79{
80    OwnPtr<Ewk_Database_Origins_Async_Get_Context*> webDatabaseContext = adoptPtr(static_cast<Ewk_Database_Origins_Async_Get_Context*>(context));
81    Eina_List* originList = webDatabaseContext->manager->createOriginList(origins);
82    OwnPtr<EwkError> ewkError = EwkError::create(wkError);
83    webDatabaseContext->callback(originList, ewkError.get(), webDatabaseContext->userData);
84}
85
86Eina_Bool ewk_database_manager_origins_get(const Ewk_Database_Manager* ewkDatabaseManager, Ewk_Database_Origins_Get_Cb callback, void* userData)
87{
88    EINA_SAFETY_ON_NULL_RETURN_VAL(ewkDatabaseManager, false);
89    EINA_SAFETY_ON_NULL_RETURN_VAL(callback, false);
90
91    Ewk_Database_Origins_Async_Get_Context* context = new Ewk_Database_Origins_Async_Get_Context(ewkDatabaseManager, callback, userData);
92    ewkDatabaseManager->getDatabaseOrigins(getDatabaseOriginsCallback, context);
93
94    return true;
95}
96