Skip to content

Commit

Permalink
Port WebCompiledContentRuleListData to the new IPC serialization format
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264538

Reviewed by Alex Christensen.

* Source/WebKit/DerivedSources-input.xcfilelist:
* Source/WebKit/DerivedSources.make:
* Source/WebKit/NetworkProcess/NetworkContentRuleListManager.cpp:
(WebKit::NetworkContentRuleListManager::addContentRuleLists):
* Source/WebKit/Shared/WebCompiledContentRuleList.cpp:
(WebKit::WebCompiledContentRuleList::create):
* Source/WebKit/Shared/WebCompiledContentRuleList.h:
* Source/WebKit/Shared/WebCompiledContentRuleListData.cpp:
(WebKit::WebCompiledContentRuleListData::createDataHandle const):
(WebKit::WebCompiledContentRuleListData::WebCompiledContentRuleListData):
(WebKit::WebCompiledContentRuleListData::encode const): Deleted.
(WebKit::WebCompiledContentRuleListData::decode): Deleted.
* Source/WebKit/Shared/WebCompiledContentRuleListData.h:
* Source/WebKit/Shared/WebCompiledContentRuleListData.serialization.in: Added.
* Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp:
(API::createExtension):
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:
* Source/WebKit/WebProcess/UserContent/WebUserContentController.cpp:
(WebKit::WebUserContentController::addContentRuleLists):

Canonical link: https://commits.webkit.org/270505@main
  • Loading branch information
cdumez committed Nov 10, 2023
1 parent 3f93530 commit 5783584
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 99 deletions.
1 change: 1 addition & 0 deletions Source/WebKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ set(WebKit_SERIALIZATION_IN_FILES
Shared/WTFArgumentCoders.serialization.in
Shared/WebBackForwardListCounts.serialization.in
Shared/WebCoreArgumentCoders.serialization.in
Shared/WebCompiledContentRuleListData.serialization.in
Shared/WebEvent.serialization.in
Shared/WebFoundTextRange.serialization.in
Shared/WebHitTestResultData.serialization.in
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources-input.xcfilelist
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ $(PROJECT_DIR)/Shared/ViewWindowCoordinates.serialization.in
$(PROJECT_DIR)/Shared/VisibleContentRectUpdateInfo.serialization.in
$(PROJECT_DIR)/Shared/WTFArgumentCoders.serialization.in
$(PROJECT_DIR)/Shared/WebBackForwardListCounts.serialization.in
$(PROJECT_DIR)/Shared/WebCompiledContentRuleListData.serialization.in
$(PROJECT_DIR)/Shared/WebConnection.messages.in
$(PROJECT_DIR)/Shared/WebCoreArgumentCoders.serialization.in
$(PROJECT_DIR)/Shared/WebEvent.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 @@ -601,6 +601,7 @@ SERIALIZATION_DESCRIPTION_FILES = \
Shared/TextRecognitionResult.serialization.in \
Shared/UserContentControllerParameters.serialization.in \
Shared/UserInterfaceIdiom.serialization.in \
Shared/WebCompiledContentRuleListData.serialization.in \
Shared/ViewWindowCoordinates.serialization.in \
Shared/VisibleContentRectUpdateInfo.serialization.in \
Shared/WTFArgumentCoders.serialization.in \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ void NetworkContentRuleListManager::addContentRuleLists(UserContentControllerIde
for (auto&& pair : contentRuleLists) {
auto&& contentRuleList = WTFMove(pair.first);
String identifier = contentRuleList.identifier;
auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList));
backend.addContentExtension(identifier, WTFMove(compiledContentRuleList), WTFMove(pair.second), ContentExtensions::ContentExtension::ShouldCompileCSS::No);
if (RefPtr compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList)))
backend.addContentExtension(identifier, compiledContentRuleList.releaseNonNull(), WTFMove(pair.second), ContentExtensions::ContentExtension::ShouldCompileCSS::No);
}

auto pendingCallbacks = m_pendingCallbacks.take(identifier);
Expand Down
4 changes: 3 additions & 1 deletion Source/WebKit/Shared/WebCompiledContentRuleList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@

namespace WebKit {

Ref<WebCompiledContentRuleList> WebCompiledContentRuleList::create(WebCompiledContentRuleListData&& data)
RefPtr<WebCompiledContentRuleList> WebCompiledContentRuleList::create(WebCompiledContentRuleListData&& data)
{
if (!data.data)
return nullptr;
return adoptRef(*new WebCompiledContentRuleList(WTFMove(data)));
}

Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/Shared/WebCompiledContentRuleList.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace WebKit {

class WebCompiledContentRuleList final : public WebCore::ContentExtensions::CompiledContentExtension {
public:
static Ref<WebCompiledContentRuleList> create(WebCompiledContentRuleListData&&);
static RefPtr<WebCompiledContentRuleList> create(WebCompiledContentRuleListData&&);
virtual ~WebCompiledContentRuleList();

const WebCompiledContentRuleListData& data() const { return m_data; }
Expand Down
105 changes: 23 additions & 82 deletions Source/WebKit/Shared/WebCompiledContentRuleListData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,97 +32,38 @@
#include "DataReference.h"

namespace WebKit {

static size_t ruleListDataSize(size_t topURLFiltersBytecodeOffset, size_t topURLFiltersBytecodeSize)
{
return topURLFiltersBytecodeOffset + topURLFiltersBytecodeSize;
}

void WebCompiledContentRuleListData::encode(IPC::Encoder& encoder) const
std::optional<SharedMemoryHandle> WebCompiledContentRuleListData::createDataHandle(SharedMemory::Protection protection) const
{
ASSERT(data->size() >= ruleListDataSize(topURLFiltersBytecodeOffset, topURLFiltersBytecodeSize));
encoder << identifier;
encoder << data->createHandle(SharedMemory::Protection::ReadOnly);
encoder << actionsOffset;
encoder << actionsSize;
encoder << urlFiltersBytecodeOffset;
encoder << urlFiltersBytecodeSize;
encoder << topURLFiltersBytecodeOffset;
encoder << topURLFiltersBytecodeSize;
encoder << frameURLFiltersBytecodeOffset;
encoder << frameURLFiltersBytecodeSize;
return data->createHandle(protection);
}

std::optional<WebCompiledContentRuleListData> WebCompiledContentRuleListData::decode(IPC::Decoder& decoder)
WebCompiledContentRuleListData::WebCompiledContentRuleListData(String&& identifier, std::optional<WebKit::SharedMemoryHandle>&& dataHandle, size_t actionsOffset, size_t actionsSize, size_t urlFiltersBytecodeOffset, size_t urlFiltersBytecodeSize, size_t topURLFiltersBytecodeOffset, size_t topURLFiltersBytecodeSize, size_t frameURLFiltersBytecodeOffset, size_t frameURLFiltersBytecodeSize)
: identifier(WTFMove(identifier))
, data(dataHandle ? SharedMemory::map(WTFMove(*dataHandle), SharedMemory::Protection::ReadOnly) : nullptr)
, actionsOffset(actionsOffset)
, actionsSize(actionsSize)
, urlFiltersBytecodeOffset(urlFiltersBytecodeOffset)
, urlFiltersBytecodeSize(urlFiltersBytecodeSize)
, topURLFiltersBytecodeOffset(topURLFiltersBytecodeOffset)
, topURLFiltersBytecodeSize(topURLFiltersBytecodeSize)
, frameURLFiltersBytecodeOffset(frameURLFiltersBytecodeOffset)
, frameURLFiltersBytecodeSize(frameURLFiltersBytecodeSize)
{
std::optional<String> identifier;
decoder >> identifier;
if (!identifier)
return std::nullopt;

std::optional<std::optional<SharedMemory::Handle>> handle;
decoder >> handle;
if (!handle)
return std::nullopt;

std::optional<size_t> actionsOffset;
decoder >> actionsOffset;
if (!actionsOffset)
return std::nullopt;

std::optional<size_t> actionsSize;
decoder >> actionsSize;
if (!actionsSize)
return std::nullopt;

std::optional<size_t> urlFiltersBytecodeOffset;
decoder >> urlFiltersBytecodeOffset;
if (!urlFiltersBytecodeOffset)
return std::nullopt;

std::optional<size_t> urlFiltersBytecodeSize;
decoder >> urlFiltersBytecodeSize;
if (!urlFiltersBytecodeSize)
return std::nullopt;

std::optional<size_t> topURLFiltersBytecodeOffset;
decoder >> topURLFiltersBytecodeOffset;
if (!topURLFiltersBytecodeOffset)
return std::nullopt;

std::optional<size_t> topURLFiltersBytecodeSize;
decoder >> topURLFiltersBytecodeSize;
if (!topURLFiltersBytecodeSize)
return std::nullopt;

std::optional<size_t> frameURLFiltersBytecodeOffset;
decoder >> frameURLFiltersBytecodeOffset;
if (!frameURLFiltersBytecodeOffset)
return std::nullopt;

std::optional<size_t> frameURLFiltersBytecodeSize;
decoder >> frameURLFiltersBytecodeSize;
if (!frameURLFiltersBytecodeSize)
return std::nullopt;

if (!*handle)
return std::nullopt;
auto data = SharedMemory::map(WTFMove(**handle), SharedMemory::Protection::ReadOnly);
if (!data)
return std::nullopt;
if (data->size() < ruleListDataSize(*topURLFiltersBytecodeOffset, *topURLFiltersBytecodeSize))
return std::nullopt;
return {{
WTFMove(*identifier),
data.releaseNonNull(),
WTFMove(*actionsOffset),
WTFMove(*actionsSize),
WTFMove(*urlFiltersBytecodeOffset),
WTFMove(*urlFiltersBytecodeSize),
WTFMove(*topURLFiltersBytecodeOffset),
WTFMove(*topURLFiltersBytecodeSize),
WTFMove(*frameURLFiltersBytecodeOffset),
WTFMove(*frameURLFiltersBytecodeSize)
}};
if (data) {
if (data->size() < ruleListDataSize(actionsOffset, actionsSize)
|| data->size() < ruleListDataSize(urlFiltersBytecodeOffset, urlFiltersBytecodeSize)
|| data->size() < ruleListDataSize(topURLFiltersBytecodeOffset, topURLFiltersBytecodeSize)
|| data->size() < ruleListDataSize(frameURLFiltersBytecodeOffset, frameURLFiltersBytecodeSize)) {
ASSERT_NOT_REACHED();
data = nullptr;
}
}
}

} // namespace WebKit
Expand Down
12 changes: 4 additions & 8 deletions Source/WebKit/Shared/WebCompiledContentRuleListData.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
#include <variant>
#include <wtf/RefPtr.h>

namespace IPC {
class Decoder;
class Encoder;
}

namespace WebKit {

class WebCompiledContentRuleListData {
Expand All @@ -55,11 +50,12 @@ class WebCompiledContentRuleListData {
{
}

void encode(IPC::Encoder&) const;
static std::optional<WebCompiledContentRuleListData> decode(IPC::Decoder&);
WebCompiledContentRuleListData(String&& identifier, std::optional<WebKit::SharedMemoryHandle>&& data, size_t actionsOffset, size_t actionsSize, size_t urlFiltersBytecodeOffset, size_t urlFiltersBytecodeSize, size_t topURLFiltersBytecodeOffset, size_t topURLFiltersBytecodeSize, size_t frameURLFiltersBytecodeOffset, size_t frameURLFiltersBytecodeSize);

std::optional<SharedMemoryHandle> createDataHandle(SharedMemory::Protection = SharedMemory::Protection::ReadOnly) const;

String identifier;
Ref<SharedMemory> data;
RefPtr<SharedMemory> data;
size_t actionsOffset { 0 };
size_t actionsSize { 0 };
size_t urlFiltersBytecodeOffset { 0 };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (C) 2023 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(CONTENT_EXTENSIONS)

class WebKit::WebCompiledContentRuleListData {
String identifier;
std::optional<WebKit::SharedMemoryHandle> createDataHandle();
size_t actionsOffset;
size_t actionsSize;
size_t urlFiltersBytecodeOffset;
size_t urlFiltersBytecodeSize;
size_t topURLFiltersBytecodeOffset;
size_t topURLFiltersBytecodeSize;
size_t frameURLFiltersBytecodeOffset;
size_t frameURLFiltersBytecodeSize;
};

#endif // ENABLE(CONTENT_EXTENSIONS)
5 changes: 3 additions & 2 deletions Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,9 @@ static Ref<API::ContentRuleList> createExtension(WTF::String&& identifier, Mappe
frameURLFiltersOffset,
data.metaData.frameURLFiltersBytecodeSize
);
auto compiledContentRuleList = WebKit::WebCompiledContentRuleList::create(WTFMove(compiledContentRuleListData));
return API::ContentRuleList::create(WTFMove(compiledContentRuleList), WTFMove(data.data));
RefPtr compiledContentRuleList = WebKit::WebCompiledContentRuleList::create(WTFMove(compiledContentRuleListData));
ASSERT(compiledContentRuleList);
return API::ContentRuleList::create(compiledContentRuleList.releaseNonNull(), WTFMove(data.data));
}

static WTF::String getContentRuleListSourceFromMappedFile(const MappedData& mappedData)
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/WebKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4892,6 +4892,7 @@
463A074A2AFD8C6600CA8DBE /* BufferAndBackendInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BufferAndBackendInfo.h; sourceTree = "<group>"; };
463FD47F1EB9458400A2982C /* WKProcessTerminationReason.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKProcessTerminationReason.h; sourceTree = "<group>"; };
463FD4811EB94EAD00A2982C /* ProcessTerminationReason.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ProcessTerminationReason.h; sourceTree = "<group>"; };
46411E3D2AFDA94400CC00E4 /* WebCompiledContentRuleListData.serialization.in */ = {isa = PBXFileReference; lastKnownFileType = text; path = WebCompiledContentRuleListData.serialization.in; sourceTree = "<group>"; };
4651ECE622178A850067EB95 /* WebProcessCacheCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebProcessCacheCocoa.mm; sourceTree = "<group>"; };
465250E51ECF52CD002025CB /* WebKit2InitializeCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebKit2InitializeCocoa.mm; sourceTree = "<group>"; };
4656F7BA27ACAA8000CB3D7C /* WebSharedWorkerServerToContextConnection.messages.in */ = {isa = PBXFileReference; lastKnownFileType = text; path = WebSharedWorkerServerToContextConnection.messages.in; sourceTree = "<group>"; };
Expand Down Expand Up @@ -8444,6 +8445,7 @@
7C4ABECF1AA8E9F00088AA37 /* WebCompiledContentRuleList.h */,
7C4ABED21AA8FCB80088AA37 /* WebCompiledContentRuleListData.cpp */,
7C4ABED31AA8FCB80088AA37 /* WebCompiledContentRuleListData.h */,
46411E3D2AFDA94400CC00E4 /* WebCompiledContentRuleListData.serialization.in */,
BC4A628B147312BE006C681A /* WebConnection.cpp */,
BC4A628C147312BE006C681A /* WebConnection.h */,
1A1FEC191627B3EF00700F6D /* WebConnection.messages.in */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,8 @@ void WebUserContentController::addContentRuleLists(Vector<std::pair<WebCompiledC
for (auto&& pair : contentRuleLists) {
auto&& contentRuleList = WTFMove(pair.first);
String identifier = contentRuleList.identifier;
auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList));

m_contentExtensionBackend.addContentExtension(identifier, WTFMove(compiledContentRuleList), WTFMove(pair.second));
if (RefPtr compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList)))
m_contentExtensionBackend.addContentExtension(identifier, compiledContentRuleList.releaseNonNull(), WTFMove(pair.second));
}
}

Expand Down

0 comments on commit 5783584

Please sign in to comment.