Skip to content

Commit

Permalink
Send data for browser.storage to the UI process
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=267782
rdar://121279041

Reviewed by Timothy Hatcher.

This patch sends the data to be added or removed from storage over to the UI process. Next and
final steps for the Storage API is to adopt the _WKWebExtensionSQLiteStore to manage the data added
with this API. This is being tracked in https://webkit.org/b/267649.

In addition, this patch adds support for browser.session.setAccessLevel. To do this, add a boolean
representing the access level to WebExtensionContextParameters so that the value can be read when
determining to expose the 'session' API.

* Source/WebKit/DerivedSources-input.xcfilelist:
* Source/WebKit/DerivedSources.make:
* Source/WebKit/Scripts/webkit/messages.py:
(types_that_cannot_be_forward_declared):
* Source/WebKit/Shared/Extensions/WebExtensionConstants.h:
* Source/WebKit/Shared/Extensions/WebExtensionContext.serialization.in:
* Source/WebKit/Shared/Extensions/WebExtensionContextParameters.h:
* Source/WebKit/Shared/Extensions/WebExtensionContextParameters.serialization.in:
* Source/WebKit/Shared/Extensions/WebExtensionStorageType.h:
Copied from Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIStorage.h.
(WebKit::toAPIPrefixString):
* Source/WebKit/Shared/Extensions/WebExtensionStorageType.serialization.in:
Copied from Source/WebKit/Shared/Extensions/WebExtensionContext.serialization.in.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIStorageCocoa.mm: Added.
(WebKit::WebExtensionContext::storageGet):
(WebKit::WebExtensionContext::storageGetBytesInUse):
(WebKit::WebExtensionContext::storageSet):
(WebKit::WebExtensionContext::storageRemove):
(WebKit::WebExtensionContext::storageClear):
(WebKit::WebExtensionContext::storageSetAccessLevel):
(WebKit::WebExtensionContext::isValidStorageRequest):
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionContextCocoa.mm:
(WebKit::WebExtensionContext::load):
(WebKit::WebExtensionContext::getTab):
(WebKit::WebExtensionContext::isBackgroundWebView):
(WebKit::WebExtensionContext::setSessionStorageAllowedInContentScripts):
Set the access level and send a message to update the value on the WebExtensionContextParameters.
* Source/WebKit/UIProcess/Extensions/WebExtensionContext.cpp:
(WebKit::WebExtensionContext::parameters const):
* Source/WebKit/UIProcess/Extensions/WebExtensionContext.h:
(WebKit::WebExtensionContext::isSessionStorageAllowedInContentScripts const):
* Source/WebKit/UIProcess/Extensions/WebExtensionContext.messages.in:
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIStorageAreaCocoa.mm:
(WebKit::WebExtensionAPIStorageArea::isPropertyAllowed):
(WebKit::WebExtensionAPIStorageArea::get):
(WebKit::WebExtensionAPIStorageArea::getBytesInUse):
(WebKit::WebExtensionAPIStorageArea::set):
(WebKit::WebExtensionAPIStorageArea::remove):
(WebKit::WebExtensionAPIStorageArea::clear):
(WebKit::WebExtensionAPIStorageArea::setAccessLevel):
(WebKit::WebExtensionAPIStorageArea::onChanged):
(WebKit::WebExtensionAPIStorageArea::quotaBytes):
(WebKit::WebExtensionAPIStorageArea::quotaBytesPerItem):
(WebKit::WebExtensionAPIStorageArea::maxItems):
(WebKit::WebExtensionAPIStorageArea::maxWriteOperationsPerHour):
(WebKit::WebExtensionAPIStorageArea::maxWriteOperationsPerMinute):
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIStorageCocoa.mm:
(WebKit::WebExtensionAPIStorage::isPropertyAllowed):
(WebKit::WebExtensionAPIStorage::local):
(WebKit::WebExtensionAPIStorage::session):
(WebKit::WebExtensionAPIStorage::sync):
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIStorage.h:
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIStorageArea.h:
(WebKit::WebExtensionAPIStorageArea::WebExtensionAPIStorageArea):
* Source/WebKit/WebProcess/Extensions/Cocoa/WebExtensionContextProxyCocoa.mm:
(WebKit::WebExtensionContextProxy::getOrCreate):
* Source/WebKit/WebProcess/Extensions/Interfaces/WebExtensionAPIStorage.idl:
* Source/WebKit/WebProcess/Extensions/Interfaces/WebExtensionAPIStorageArea.idl:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.cpp:
(WebKit::WebExtensionContextProxy::setStorageAccessLevel):
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.h:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.messages.in:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPIStorage.mm:
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/273286@main
  • Loading branch information
kiaraarose committed Jan 22, 2024
1 parent 5d07fef commit 0e52b5d
Show file tree
Hide file tree
Showing 26 changed files with 628 additions and 81 deletions.
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources-input.xcfilelist
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ $(PROJECT_DIR)/Shared/Extensions/WebExtensionFrameParameters.serialization.in
$(PROJECT_DIR)/Shared/Extensions/WebExtensionMatchedRuleParameters.serialization.in
$(PROJECT_DIR)/Shared/Extensions/WebExtensionMenuItem.serialization.in
$(PROJECT_DIR)/Shared/Extensions/WebExtensionMessageSenderParameters.serialization.in
$(PROJECT_DIR)/Shared/Extensions/WebExtensionStorage.serialization.in
$(PROJECT_DIR)/Shared/Extensions/WebExtensionTab.serialization.in
$(PROJECT_DIR)/Shared/Extensions/WebExtensionTabParameters.serialization.in
$(PROJECT_DIR)/Shared/Extensions/WebExtensionWindow.serialization.in
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources.make
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ SERIALIZATION_DESCRIPTION_FILES = \
Shared/Extensions/WebExtensionMatchedRuleParameters.serialization.in \
Shared/Extensions/WebExtensionMenuItem.serialization.in \
Shared/Extensions/WebExtensionMessageSenderParameters.serialization.in \
Shared/Extensions/WebExtensionStorage.serialization.in \
Shared/Extensions/WebExtensionTab.serialization.in \
Shared/Extensions/WebExtensionWindow.serialization.in \
Shared/FileSystemSyncAccessHandleInfo.serialization.in \
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/Scripts/webkit/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ def types_that_cannot_be_forward_declared():
'WebKit::WebExtensionRegisteredScriptParameters',
'WebKit::WebExtensionScriptInjectionParameters',
'WebKit::WebExtensionScriptInjectionResultParameters',
'WebKit::WebExtensionStorageAccessLevel',
'WebKit::WebExtensionStorageType',
'WebKit::WebExtensionTab::ImageFormat',
'WebKit::WebExtensionTabParameters',
'WebKit::WebExtensionTabQueryParameters',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023 Apple Inc. All rights reserved.
# Copyright (C) 2022-2024 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ struct WebExtensionContextParameters {
Ref<API::Data> localizationJSON;
Ref<API::Data> manifestJSON;

double manifestVersion;
bool testingMode;
double manifestVersion { 0 };
bool testingMode { false };
bool isSessionStorageAllowedInContentScripts { false };

std::optional<WebCore::PageIdentifier> backgroundPageIdentifier;
Vector<WebExtensionContext::PageIdentifierTuple> popupPageIdentifiers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct WebKit::WebExtensionContextParameters {

double manifestVersion;
bool testingMode;
bool isSessionStorageAllowedInContentScripts;

std::optional<WebCore::PageIdentifier> backgroundPageIdentifier;
Vector<WebKit::WebExtensionContext::PageIdentifierTuple> popupPageIdentifiers;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (C) 2024 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#if ENABLE(WK_WEB_EXTENSIONS)

headers: "WebExtensionStorageAccessLevel.h" "WebExtensionStorageType.h"

enum class WebKit::WebExtensionStorageAccessLevel : uint8_t {
TrustedContexts,
TrustedAndUntrustedContexts,
}

enum class WebKit::WebExtensionStorageType : uint8_t {
Local,
Session,
Sync,
}

#endif
56 changes: 56 additions & 0 deletions Source/WebKit/Shared/Extensions/WebExtensionStorageAccessLevel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#if ENABLE(WK_WEB_EXTENSIONS)

#include <wtf/text/WTFString.h>

namespace WebKit {

enum class WebExtensionStorageAccessLevel : uint8_t {
TrustedContexts,
TrustedAndUntrustedContexts,
};

} // namespace WebKit

namespace WTF {

template<> struct EnumTraits<WebKit::WebExtensionStorageAccessLevel> {
using values = EnumValues<
WebKit::WebExtensionStorageAccessLevel,
WebKit::WebExtensionStorageAccessLevel::TrustedContexts,
WebKit::WebExtensionStorageAccessLevel::TrustedAndUntrustedContexts
>;
};

template<> struct DefaultHash<WebKit::WebExtensionStorageAccessLevel> : IntHash<WebKit::WebExtensionStorageAccessLevel> { };
template<> struct HashTraits<WebKit::WebExtensionStorageAccessLevel> : StrongEnumHashTraits<WebKit::WebExtensionStorageAccessLevel> { };

} // namespace WTF

#endif // ENABLE(WK_WEB_EXTENSIONS)
70 changes: 70 additions & 0 deletions Source/WebKit/Shared/Extensions/WebExtensionStorageType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#if ENABLE(WK_WEB_EXTENSIONS)

#include <wtf/text/WTFString.h>

namespace WebKit {

enum class WebExtensionStorageType : uint8_t {
Local,
Session,
Sync,
};

inline String toAPIPrefixString(WebExtensionStorageType storageType)
{
switch (storageType) {
case WebExtensionStorageType::Local:
return "browser.local"_s;
case WebExtensionStorageType::Session:
return "browser.session"_s;
case WebExtensionStorageType::Sync:
return "browser.sync"_s;
}
}

} // namespace WebKit

namespace WTF {

template<> struct EnumTraits<WebKit::WebExtensionStorageType> {
using values = EnumValues<
WebKit::WebExtensionStorageType,
WebKit::WebExtensionStorageType::Local,
WebKit::WebExtensionStorageType::Session,
WebKit::WebExtensionStorageType::Sync
>;
};

template<> struct DefaultHash<WebKit::WebExtensionStorageType> : IntHash<WebKit::WebExtensionStorageType> { };
template<> struct HashTraits<WebKit::WebExtensionStorageType> : StrongEnumHashTraits<WebKit::WebExtensionStorageType> { };

} // namespace WTF

#endif // ENABLE(WK_WEB_EXTENSIONS)
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (C) 2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#if !__has_feature(objc_arc)
#error This file requires ARC. Add the "-fobjc-arc" compiler flag for this file.
#endif

#import "config.h"
#import "WebExtensionContext.h"

#if ENABLE(WK_WEB_EXTENSIONS)

#import "WebExtensionConstants.h"
#import "WebExtensionStorageAccessLevel.h"
#import "WebExtensionStorageType.h"
#import "WebExtensionUtilities.h"

namespace WebKit {

void WebExtensionContext::storageGet(WebPageProxyIdentifier webPageProxyIdentifier, WebExtensionStorageType storageType, const IPC::DataReference& dataReference, CompletionHandler<void(std::optional<IPC::DataReference>, ErrorString)>&& completionHandler)
{
static NSString * const callingAPIName = [NSString stringWithFormat:@"%@.get()", (NSString *)toAPIPrefixString(storageType)];

String errorString;
if (!extensionCanAccessWebPage(webPageProxyIdentifier, errorString)) {
completionHandler(std::nullopt, toErrorString(callingAPIName, nil, errorString));
return;
}

// FIXME: <https://webkit.org/b/267649> Get values from StorageAPISQLiteStore.
completionHandler(std::nullopt, std::nullopt);
}

void WebExtensionContext::storageGetBytesInUse(WebPageProxyIdentifier webPageProxyIdentifier, WebExtensionStorageType storageType, const Vector<String>& keys, CompletionHandler<void(std::optional<size_t> size, ErrorString)>&& completionHandler)
{
static NSString * const callingAPIName = [NSString stringWithFormat:@"%@.getBytesInUse()", (NSString *)toAPIPrefixString(storageType)];

String errorString;
if (!extensionCanAccessWebPage(webPageProxyIdentifier, errorString)) {
completionHandler(std::nullopt, toErrorString(callingAPIName, nil, errorString));
return;
}

// FIXME: <https://webkit.org/b/267649> Get storage size from StorageAPISQLiteStore.
completionHandler(std::nullopt, std::nullopt);
}

void WebExtensionContext::storageSet(WebPageProxyIdentifier webPageProxyIdentifier, WebExtensionStorageType storageType, const IPC::DataReference& dataReference, CompletionHandler<void(ErrorString)>&& completionHandler)
{
static NSString * const callingAPIName = [NSString stringWithFormat:@"%@.set()", (NSString *)toAPIPrefixString(storageType)];

String errorString;
if (!extensionCanAccessWebPage(webPageProxyIdentifier, errorString)) {
completionHandler(toErrorString(callingAPIName, nil, errorString));
return;
}

// FIXME: <https://webkit.org/b/267649> Set values for StorageAPISQLiteStore.
completionHandler(std::nullopt);
}

void WebExtensionContext::storageRemove(WebPageProxyIdentifier webPageProxyIdentifier, WebExtensionStorageType storageType, const Vector<String>& keys, CompletionHandler<void(ErrorString)>&& completionHandler)
{
static NSString * const callingAPIName = [NSString stringWithFormat:@"%@.remove()", (NSString *)toAPIPrefixString(storageType)];

String errorString;
if (!extensionCanAccessWebPage(webPageProxyIdentifier, errorString)) {
completionHandler(toErrorString(callingAPIName, nil, errorString));
return;
}

// FIXME: <https://webkit.org/b/267649> Remove values from StorageAPISQLiteStore.
completionHandler(std::nullopt);
}

void WebExtensionContext::storageClear(WebPageProxyIdentifier webPageProxyIdentifier, WebExtensionStorageType storageType, CompletionHandler<void(ErrorString)>&& completionHandler)
{
static NSString * const callingAPIName = [NSString stringWithFormat:@"%@.clear()", (NSString *)toAPIPrefixString(storageType)];

String errorString;
if (!extensionCanAccessWebPage(webPageProxyIdentifier, errorString)) {
completionHandler(toErrorString(callingAPIName, nil, errorString));
return;
}

// FIXME: <https://webkit.org/b/267649> Clear values in StorageAPISQLiteStore.
completionHandler(std::nullopt);
}

void WebExtensionContext::storageSetAccessLevel(WebPageProxyIdentifier webPageProxyIdentifier, WebExtensionStorageType storageType, const WebExtensionStorageAccessLevel accessLevel, CompletionHandler<void(ErrorString)>&& completionHandler)
{
static NSString * const callingAPIName = @"browser.session.setAccessLevel()";

String errorString;
if (!extensionCanAccessWebPage(webPageProxyIdentifier, errorString)) {
completionHandler(toErrorString(callingAPIName, nil, errorString));
return;
}

setSessionStorageAllowedInContentScripts(accessLevel == WebExtensionStorageAccessLevel::TrustedAndUntrustedContexts);

completionHandler(std::nullopt);
}

} // namespace WebKit

#endif // ENABLE(WK_WEB_EXTENSIONS)

Loading

0 comments on commit 0e52b5d

Please sign in to comment.