1/*
2 * Copyright (C) 2011 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 "WKKeyValueStorageManager.h"
28
29#include "APIArray.h"
30#include "APIDictionary.h"
31#include "APIWebsiteDataStore.h"
32#include "StorageManager.h"
33#include "WKAPICast.h"
34#include "WebsiteDataRecord.h"
35#include "WebsiteDataStore.h"
36#include <wtf/RunLoop.h>
37
38using namespace WebKit;
39
40WKTypeID WKKeyValueStorageManagerGetTypeID()
41{
42 return toAPI(API::WebsiteDataStore::APIType);
43}
44
45WKStringRef WKKeyValueStorageManagerGetOriginKey()
46{
47 static API::String& key = API::String::create("WebKeyValueStorageManagerStorageDetailsOriginKey").leakRef();
48 return toAPI(&key);
49}
50
51WKStringRef WKKeyValueStorageManagerGetCreationTimeKey()
52{
53 static API::String& key = API::String::create("WebKeyValueStorageManagerStorageDetailsCreationTimeKey").leakRef();
54 return toAPI(&key);
55}
56
57WKStringRef WKKeyValueStorageManagerGetModificationTimeKey()
58{
59 static API::String& key = API::String::create("WebKeyValueStorageManagerStorageDetailsModificationTimeKey").leakRef();
60 return toAPI(&key);
61}
62
63void WKKeyValueStorageManagerGetKeyValueStorageOrigins(WKKeyValueStorageManagerRef keyValueStorageManager, void* context, WKKeyValueStorageManagerGetKeyValueStorageOriginsFunction callback)
64
65{
66 auto& websiteDataStore = toImpl(reinterpret_cast<WKWebsiteDataStoreRef>(keyValueStorageManager))->websiteDataStore();
67 websiteDataStore.fetchData({ WebsiteDataType::LocalStorage, WebsiteDataType::SessionStorage }, { }, [context, callback](auto dataRecords) {
68 Vector<RefPtr<API::Object>> securityOrigins;
69 for (const auto& dataRecord : dataRecords) {
70 for (const auto& origin : dataRecord.origins)
71 securityOrigins.append(API::SecurityOrigin::create(origin.securityOrigin()));
72 }
73
74 callback(toAPI(API::Array::create(WTFMove(securityOrigins)).ptr()), nullptr, context);
75 });
76}
77
78void WKKeyValueStorageManagerGetStorageDetailsByOrigin(WKKeyValueStorageManagerRef keyValueStorageManager, void* context, WKKeyValueStorageManagerGetStorageDetailsByOriginFunction callback)
79{
80 auto& websiteDataStore = toImpl(reinterpret_cast<WKWebsiteDataStoreRef>(keyValueStorageManager))->websiteDataStore();
81 websiteDataStore.getLocalStorageDetails([context, callback](auto&& details) {
82 Vector<RefPtr<API::Object>> result;
83 result.reserveInitialCapacity(details.size());
84
85 for (const auto& detail : details) {
86 HashMap<String, RefPtr<API::Object>> detailMap;
87 RefPtr<API::Object> origin = API::SecurityOrigin::create(WebCore::SecurityOriginData::fromDatabaseIdentifier(detail.originIdentifier)->securityOrigin());
88
89 detailMap.set(toImpl(WKKeyValueStorageManagerGetOriginKey())->string(), origin);
90 if (detail.creationTime)
91 detailMap.set(toImpl(WKKeyValueStorageManagerGetCreationTimeKey())->string(), API::Double::create(detail.creationTime->secondsSinceEpoch().value()));
92 if (detail.modificationTime)
93 detailMap.set(toImpl(WKKeyValueStorageManagerGetModificationTimeKey())->string(), API::Double::create(detail.modificationTime->secondsSinceEpoch().value()));
94
95 result.uncheckedAppend(API::Dictionary::create(WTFMove(detailMap)));
96 }
97
98 callback(toAPI(API::Array::create(WTFMove(result)).ptr()), nullptr, context);
99 });
100}
101
102void WKKeyValueStorageManagerDeleteEntriesForOrigin(WKKeyValueStorageManagerRef keyValueStorageManager, WKSecurityOriginRef origin)
103{
104 auto& websiteDataStore = toImpl(reinterpret_cast<WKWebsiteDataStoreRef>(keyValueStorageManager))->websiteDataStore();
105 WebsiteDataRecord dataRecord;
106 dataRecord.add(WebsiteDataType::LocalStorage, toImpl(origin)->securityOrigin().data());
107 dataRecord.add(WebsiteDataType::SessionStorage, toImpl(origin)->securityOrigin().data());
108 websiteDataStore.removeData({ WebsiteDataType::LocalStorage, WebsiteDataType::SessionStorage }, { dataRecord }, [] { });
109}
110
111void WKKeyValueStorageManagerDeleteAllEntries(WKKeyValueStorageManagerRef keyValueStorageManager)
112{
113 auto& websiteDataStore = toImpl(reinterpret_cast<WKWebsiteDataStoreRef>(keyValueStorageManager))->websiteDataStore();
114 websiteDataStore.removeData({ WebsiteDataType::LocalStorage, WebsiteDataType::SessionStorage }, -WallTime::infinity(), [] { });
115}
116