| 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 "WebsiteDataStore.h" |
| 35 | #include <wtf/RunLoop.h> |
| 36 | |
| 37 | using namespace WebKit; |
| 38 | |
| 39 | WKTypeID WKKeyValueStorageManagerGetTypeID() |
| 40 | { |
| 41 | return toAPI(API::WebsiteDataStore::APIType); |
| 42 | } |
| 43 | |
| 44 | WKStringRef WKKeyValueStorageManagerGetOriginKey() |
| 45 | { |
| 46 | static API::String& key = API::String::create("WebKeyValueStorageManagerStorageDetailsOriginKey" ).leakRef(); |
| 47 | return toAPI(&key); |
| 48 | } |
| 49 | |
| 50 | WKStringRef WKKeyValueStorageManagerGetCreationTimeKey() |
| 51 | { |
| 52 | static API::String& key = API::String::create("WebKeyValueStorageManagerStorageDetailsCreationTimeKey" ).leakRef(); |
| 53 | return toAPI(&key); |
| 54 | } |
| 55 | |
| 56 | WKStringRef WKKeyValueStorageManagerGetModificationTimeKey() |
| 57 | { |
| 58 | static API::String& key = API::String::create("WebKeyValueStorageManagerStorageDetailsModificationTimeKey" ).leakRef(); |
| 59 | return toAPI(&key); |
| 60 | } |
| 61 | |
| 62 | void WKKeyValueStorageManagerGetKeyValueStorageOrigins(WKKeyValueStorageManagerRef keyValueStorageManager, void* context, WKKeyValueStorageManagerGetKeyValueStorageOriginsFunction callback) |
| 63 | |
| 64 | { |
| 65 | StorageManager* storageManager = toImpl(reinterpret_cast<WKWebsiteDataStoreRef>(keyValueStorageManager))->websiteDataStore().storageManager(); |
| 66 | if (!storageManager) { |
| 67 | RunLoop::main().dispatch([context, callback] { |
| 68 | callback(toAPI(API::Array::create().ptr()), nullptr, context); |
| 69 | }); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | storageManager->getLocalStorageOrigins([context, callback](auto&& securityOrigins) { |
| 74 | Vector<RefPtr<API::Object>> webSecurityOrigins; |
| 75 | webSecurityOrigins.reserveInitialCapacity(securityOrigins.size()); |
| 76 | for (auto& origin : securityOrigins) |
| 77 | webSecurityOrigins.uncheckedAppend(API::SecurityOrigin::create(origin.securityOrigin())); |
| 78 | |
| 79 | callback(toAPI(API::Array::create(WTFMove(webSecurityOrigins)).ptr()), nullptr, context); |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | void WKKeyValueStorageManagerGetStorageDetailsByOrigin(WKKeyValueStorageManagerRef keyValueStorageManager, void* context, WKKeyValueStorageManagerGetStorageDetailsByOriginFunction callback) |
| 84 | { |
| 85 | StorageManager* storageManager = toImpl(reinterpret_cast<WKWebsiteDataStoreRef>(keyValueStorageManager))->websiteDataStore().storageManager(); |
| 86 | if (!storageManager) { |
| 87 | RunLoop::main().dispatch([context, callback] { |
| 88 | callback(toAPI(API::Array::create().ptr()), nullptr, context); |
| 89 | }); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | storageManager->getLocalStorageOriginDetails([context, callback](auto storageDetails) { |
| 94 | HashMap<String, RefPtr<API::Object>> detailsMap; |
| 95 | Vector<RefPtr<API::Object>> result; |
| 96 | result.reserveInitialCapacity(storageDetails.size()); |
| 97 | |
| 98 | for (const auto& originDetails : storageDetails) { |
| 99 | HashMap<String, RefPtr<API::Object>> detailsMap; |
| 100 | |
| 101 | RefPtr<API::Object> origin = API::SecurityOrigin::create(WebCore::SecurityOriginData::fromDatabaseIdentifier(originDetails.originIdentifier)->securityOrigin()); |
| 102 | |
| 103 | detailsMap.set(toImpl(WKKeyValueStorageManagerGetOriginKey())->string(), origin); |
| 104 | if (originDetails.creationTime) |
| 105 | detailsMap.set(toImpl(WKKeyValueStorageManagerGetCreationTimeKey())->string(), API::Double::create(originDetails.creationTime->secondsSinceEpoch().value())); |
| 106 | if (originDetails.modificationTime) |
| 107 | detailsMap.set(toImpl(WKKeyValueStorageManagerGetModificationTimeKey())->string(), API::Double::create(originDetails.modificationTime->secondsSinceEpoch().value())); |
| 108 | |
| 109 | result.uncheckedAppend(API::Dictionary::create(WTFMove(detailsMap))); |
| 110 | } |
| 111 | |
| 112 | callback(toAPI(API::Array::create(WTFMove(result)).ptr()), nullptr, context); |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | void WKKeyValueStorageManagerDeleteEntriesForOrigin(WKKeyValueStorageManagerRef keyValueStorageManager, WKSecurityOriginRef origin) |
| 117 | { |
| 118 | StorageManager* storageManager = toImpl(reinterpret_cast<WKWebsiteDataStoreRef>(keyValueStorageManager))->websiteDataStore().storageManager(); |
| 119 | if (!storageManager) |
| 120 | return; |
| 121 | |
| 122 | storageManager->deleteLocalStorageEntriesForOrigin(toImpl(origin)->securityOrigin().data()); |
| 123 | } |
| 124 | |
| 125 | void WKKeyValueStorageManagerDeleteAllEntries(WKKeyValueStorageManagerRef keyValueStorageManager) |
| 126 | { |
| 127 | StorageManager* storageManager = toImpl(reinterpret_cast<WKWebsiteDataStoreRef>(keyValueStorageManager))->websiteDataStore().storageManager(); |
| 128 | if (!storageManager) |
| 129 | return; |
| 130 | |
| 131 | storageManager->deleteLocalStorageOriginsModifiedSince(-WallTime::infinity(), [] { }); |
| 132 | } |
| 133 | |