1/*
2 * Copyright (C) 2011, 2013 Apple Inc. 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 APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * 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 APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebKeyValueStorageManager.h"
28
29#include "SecurityOriginData.h"
30#include "WebContext.h"
31#include "WebSecurityOrigin.h"
32
33using namespace WebCore;
34
35namespace WebKit {
36
37const char* WebKeyValueStorageManager::supplementName()
38{
39    return "WebKeyValueStorageManager";
40}
41
42PassRefPtr<WebKeyValueStorageManager> WebKeyValueStorageManager::create(WebContext* context)
43{
44    return adoptRef(new WebKeyValueStorageManager(context));
45}
46
47WebKeyValueStorageManager::WebKeyValueStorageManager(WebContext* context)
48    : WebContextSupplement(context)
49{
50}
51
52WebKeyValueStorageManager::~WebKeyValueStorageManager()
53{
54}
55
56// WebContextSupplement
57
58void WebKeyValueStorageManager::refWebContextSupplement()
59{
60    APIObject::ref();
61}
62
63void WebKeyValueStorageManager::derefWebContextSupplement()
64{
65    APIObject::deref();
66}
67
68static void didGetKeyValueStorageOrigins(const Vector<RefPtr<WebCore::SecurityOrigin>>& securityOrigins, void* context)
69{
70    RefPtr<ArrayCallback> callback = adoptRef(static_cast<ArrayCallback*>(context));
71
72    Vector<RefPtr<APIObject>> webSecurityOrigins;
73    webSecurityOrigins.reserveInitialCapacity(securityOrigins.size());
74
75    for (unsigned i = 0; i < securityOrigins.size(); ++i)
76        webSecurityOrigins.uncheckedAppend(WebSecurityOrigin::create(securityOrigins[i]));
77
78    callback->performCallbackWithReturnValue(ImmutableArray::adopt(webSecurityOrigins).get());
79}
80
81void WebKeyValueStorageManager::getKeyValueStorageOrigins(PassRefPtr<ArrayCallback> prpCallback)
82{
83    context()->storageManager().getOrigins(RunLoop::main(), prpCallback.leakRef(), didGetKeyValueStorageOrigins);
84}
85
86void WebKeyValueStorageManager::deleteEntriesForOrigin(WebSecurityOrigin* origin)
87{
88    context()->storageManager().deleteEntriesForOrigin(origin->securityOrigin());
89}
90
91void WebKeyValueStorageManager::deleteAllEntries()
92{
93    context()->storageManager().deleteAllEntries();
94}
95
96} // namespace WebKit
97