Skip to content

Commit

Permalink
Add support for tabs.connect and runtime.connect in Web Extensions.
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=262022
rdar://115967048

Reviewed by Brian Weinstein.

* Added support for tabs.connect, tabs.onConnect, runtime.connect and runtime.onConnect.
* Added the WebExtensionAPIPort interface and implementation with onMessage and onDisconnect.
* Added support for unregistering all listeners, which is needed for ports since they can be
  garbaged collected and otherwise the UIProcess would think there are still port listeners.
* Improved the logging for function calls in the generator to log the world type (main vs content).
* Fixed generated code for ports since there is no optional callback last argument, but a optional
  last options argument instead.
* Fixed runtime.sendMessage, tabs.sendMessage and port.postMessage so the message can be any JSON
  value, not just object types. This matches Chrome and Firefox. Safari has always had this bug.

* Source/WebKit/DerivedSources-input.xcfilelist:
* Source/WebKit/DerivedSources-output.xcfilelist:
* Source/WebKit/DerivedSources.make:
* Source/WebKit/Platform/cocoa/CocoaHelpers.h:
(WebKit::parseJSON):
(WebKit::encodeJSONString):
(WebKit::encodeJSONData):
* Source/WebKit/Platform/cocoa/CocoaHelpers.mm:
(WebKit::toReadingImpl):
(WebKit::toWritingImpl):
(WebKit::parseJSON):
(WebKit::encodeJSONString):
(WebKit::encodeJSONData):
* Source/WebKit/Scripts/webkit/messages.py:
(serialized_identifiers):
* Source/WebKit/Scripts/webkit/tests/MessageArgumentDescriptions.cpp:
(IPC::serializedIdentifiers):
* Source/WebKit/Shared/Extensions/WebExtensionContentWorldType.h:
(WebKit::toDebugString):
* Source/WebKit/Shared/Extensions/WebExtensionContentWorldType.serialization.in:
* Source/WebKit/Shared/Extensions/WebExtensionPortChannelIdentifier.h: Added.
* Source/WebKit/Shared/Extensions/WebExtensionWindow.serialization.in:
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIEventCocoa.mm:
(WebKit::WebExtensionContext::addListener):
(WebKit::WebExtensionContext::removeListener):
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIPortCocoa.mm: Added.
(WebKit::WebExtensionContext::portPostMessage):
(WebKit::WebExtensionContext::portDisconnect):
(WebKit::WebExtensionContext::addPorts):
(WebKit::WebExtensionContext::isPortConnected):
(WebKit::WebExtensionContext::fireQueuedPortMessageEventIfNeeded):
(WebKit::WebExtensionContext::clearQueuedPortMessages):
(WebKit::WebExtensionContext::firePortDisconnectEventIfNeeded):
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIRuntimeCocoa.mm:
(WebKit::WebExtensionContext::runtimeSendMessage):
(WebKit::WebExtensionContext::runtimeConnect):
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPITabsCocoa.mm:
(WebKit::WebExtensionContext::tabsSendMessage):
(WebKit::WebExtensionContext::tabsConnect):
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionCocoa.mm:
(WebKit::WebExtension::parseManifest):
* Source/WebKit/UIProcess/Extensions/WebExtensionContext.h:
* Source/WebKit/UIProcess/Extensions/WebExtensionContext.messages.in:
* Source/WebKit/UIProcess/Extensions/WebExtensionWindow.h:
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIEventCocoa.mm:
(WebKit::WebExtensionAPIEvent::addListener):
(WebKit::WebExtensionAPIEvent::removeListener):
(WebKit::WebExtensionAPIEvent::removeAllListeners):
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIPortCocoa.mm: Added.
(WebKit::webExtensionPorts):
(WebKit::WebExtensionAPIPort::get):
(WebKit::WebExtensionAPIPort::add):
(WebKit::WebExtensionAPIPort::remove):
(WebKit::WebExtensionAPIPort::name):
(WebKit::WebExtensionAPIPort::sender):
(WebKit::WebExtensionAPIPort::error):
(WebKit::WebExtensionAPIPort::setError):
(WebKit::WebExtensionAPIPort::postMessage):
(WebKit::WebExtensionAPIPort::disconnect):
(WebKit::WebExtensionAPIPort::fireMessageEventIfNeeded):
(WebKit::WebExtensionAPIPort::fireDisconnectEventIfNeeded):
(WebKit::WebExtensionAPIPort::onMessage):
(WebKit::WebExtensionAPIPort::onDisconnect):
(WebKit::WebExtensionContextProxy::dispatchPortMessageEvent):
(WebKit::WebExtensionContextProxy::dispatchPortDisconnectEvent):
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIRuntimeCocoa.mm:
(WebKit::WebExtensionAPIRuntimeBase::reportError):
(WebKit::WebExtensionAPIRuntime::parseConnectOptions):
(WebKit::WebExtensionAPIRuntime::lastError):
(WebKit::WebExtensionAPIRuntime::sendMessage):
(WebKit::WebExtensionAPIRuntime::connect):
(WebKit::WebExtensionAPIRuntime::onConnect):
(WebKit::toWebAPI):
(WebKit::WebExtensionContextProxy::internalDispatchRuntimeMessageEvent):
(WebKit::WebExtensionContextProxy::dispatchRuntimeMessageEvent):
(WebKit::WebExtensionContextProxy::internalDispatchRuntimeConnectEvent):
(WebKit::WebExtensionContextProxy::dispatchRuntimeConnectEvent):
(WebKit::WebExtensionAPIRuntimeBase::reportErrorForCallbackHandler): Deleted.
(WebKit::WebExtensionContextProxy::dispatchRuntimeMainWorldMessageEvent): Deleted.
(WebKit::WebExtensionContextProxy::dispatchRuntimeContentScriptMessageEvent): Deleted.
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPITabsCocoa.mm:
(WebKit::WebExtensionAPITabs::parseConnectOptions):
(WebKit::WebExtensionAPITabs::sendMessage):
(WebKit::WebExtensionAPITabs::connect):
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIWebNavigationEventCocoa.mm:
(WebKit::WebExtensionAPIWebNavigationEvent::addListener):
(WebKit::WebExtensionAPIWebNavigationEvent::removeListener):
(WebKit::WebExtensionAPIWebNavigationEvent::removeAllListeners):
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIWindowsEventCocoa.mm:
(WebKit::WebExtensionAPIWindowsEvent::addListener):
(WebKit::WebExtensionAPIWindowsEvent::removeListener):
(WebKit::WebExtensionAPIWindowsEvent::removeAllListeners):
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIEvent.h:
(WebKit::WebExtensionAPIEvent::~WebExtensionAPIEvent):
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIObject.h:
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIPort.h: Added.
(WebKit::WebExtensionAPIPort::targetContentWorldType const):
(WebKit::WebExtensionAPIPort::channelIdentifier const):
(WebKit::WebExtensionAPIPort::disconnected const):
(WebKit::WebExtensionAPIPort::WebExtensionAPIPort):
(WebKit::WebExtensionAPIPort::~WebExtensionAPIPort):
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIRuntime.h:
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIScripting.h:
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPITabs.h:
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIWebNavigationEvent.h:
(WebKit::WebExtensionAPIWebNavigationEvent::~WebExtensionAPIWebNavigationEvent):
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPIWindowsEvent.h:
(WebKit::WebExtensionAPIWindowsEvent::~WebExtensionAPIWindowsEvent):
* Source/WebKit/WebProcess/Extensions/Bindings/Cocoa/JSWebExtensionWrapperCocoa.mm:
(WebKit::WebExtensionCallbackHandler::reportError):
* Source/WebKit/WebProcess/Extensions/Bindings/Scripts/CodeGeneratorExtensions.pm:
(_generateImplementationFile):
(_installAutomaticExceptions):
(_javaScriptTypeCondition):
* Source/WebKit/WebProcess/Extensions/Interfaces/WebExtensionAPIPort.idl: Copied from Source/WebKit/Shared/Extensions/WebExtensionContentWorldType.h.
* Source/WebKit/WebProcess/Extensions/Interfaces/WebExtensionAPIRuntime.idl:
* Source/WebKit/WebProcess/Extensions/Interfaces/WebExtensionAPITabs.idl:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.h:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.messages.in:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPIRuntime.mm:
(TestWebKitAPI::TEST):
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPITabs.mm:
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/268403@main
  • Loading branch information
xeenon committed Sep 25, 2023
1 parent 72258db commit c796edb
Show file tree
Hide file tree
Showing 43 changed files with 1,594 additions and 120 deletions.
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources-input.xcfilelist
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ $(PROJECT_DIR)/WebProcess/Extensions/Interfaces/WebExtensionAPIExtension.idl
$(PROJECT_DIR)/WebProcess/Extensions/Interfaces/WebExtensionAPILocalization.idl
$(PROJECT_DIR)/WebProcess/Extensions/Interfaces/WebExtensionAPINamespace.idl
$(PROJECT_DIR)/WebProcess/Extensions/Interfaces/WebExtensionAPIPermissions.idl
$(PROJECT_DIR)/WebProcess/Extensions/Interfaces/WebExtensionAPIPort.idl
$(PROJECT_DIR)/WebProcess/Extensions/Interfaces/WebExtensionAPIRuntime.idl
$(PROJECT_DIR)/WebProcess/Extensions/Interfaces/WebExtensionAPIScripting.idl
$(PROJECT_DIR)/WebProcess/Extensions/Interfaces/WebExtensionAPITabs.idl
Expand Down
9 changes: 4 additions & 5 deletions Source/WebKit/DerivedSources-output.xcfilelist
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ $(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPINamespace.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPINamespace.mm
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPIPermissions.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPIPermissions.mm
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPIPort.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPIPort.mm
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPIRuntime.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPIRuntime.mm
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/JSWebExtensionAPIScripting.h
Expand Down Expand Up @@ -141,6 +143,8 @@ $(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/PluginProcessProxyMessageReceiver.cp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/PluginProcessProxyMessages.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/PluginProxyMessageReceiver.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/PluginProxyMessages.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/PushClientConnectionMessageReceiver.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/PushClientConnectionMessages.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/RTCDataChannelRemoteManagerMessageReceiver.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/RTCDataChannelRemoteManagerMessages.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/RTCDataChannelRemoteManagerProxyMessageReceiver.cpp
Expand Down Expand Up @@ -468,8 +472,6 @@ $(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/WebProcessPoolMessageReceiver.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/WebProcessPoolMessages.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/WebProcessProxyMessageReceiver.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/WebProcessProxyMessages.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/PushClientConnectionMessageReceiver.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/PushClientConnectionMessages.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/WebRTCMonitorMessageReceiver.cpp
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/WebRTCMonitorMessages.h
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/WebRTCResolverMessageReceiver.cpp
Expand Down Expand Up @@ -517,7 +519,4 @@ $(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/com.apple.WebKit.webpushd.mac.sb
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/com.apple.WebKit.webpushd.relocatable.mac.sb
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/com.apple.WebKit.webpushd.sb
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/com.apple.WebProcess.sb
<<<<<<< ours
$(BUILT_PRODUCTS_DIR)/DerivedSources/WebKit/module.private.modulemap
=======
>>>>>>> theirs
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources.make
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ EXTENSION_INTERFACES = \
WebExtensionAPILocalization \
WebExtensionAPINamespace \
WebExtensionAPIPermissions \
WebExtensionAPIPort \
WebExtensionAPIRuntime \
WebExtensionAPIScripting \
WebExtensionAPITabs \
Expand Down
17 changes: 12 additions & 5 deletions Source/WebKit/Platform/cocoa/CocoaHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#import <wtf/HashSet.h>
#import <wtf/OptionSet.h>
#import <wtf/RetainPtr.h>
#import <wtf/UUID.h>
#import <wtf/WallTime.h>
Expand Down Expand Up @@ -85,12 +86,18 @@ T *objectForKey(const RetainPtr<NSDictionary>& dictionary, id key, bool returnin
return objectForKey<T>(dictionary.get(), key, returningNilIfEmpty, containingObjectsOfClass);
}

NSDictionary *parseJSON(NSString *, NSError ** = nullptr);
NSDictionary *parseJSON(NSData *, NSError ** = nullptr);
NSDictionary *parseJSON(API::Data&, NSError ** = nullptr);
enum class JSONOptions {
FragmentsAllowed = 1 << 0, /// Allows for top-level scalar types, in addition to arrays and dictionaries.
};

NSString *encodeJSONString(NSDictionary *, NSError ** = nullptr);
NSData *encodeJSONData(NSDictionary *, NSError ** = nullptr);
using JSONOptionSet = OptionSet<JSONOptions>;

id parseJSON(NSString *, JSONOptionSet = { }, NSError ** = nullptr);
id parseJSON(NSData *, JSONOptionSet = { }, NSError ** = nullptr);
id parseJSON(API::Data&, JSONOptionSet = { }, NSError ** = nullptr);

NSString *encodeJSONString(id, JSONOptionSet = { }, NSError ** = nullptr);
NSData *encodeJSONData(id, JSONOptionSet = { }, NSError ** = nullptr);

NSDictionary *dictionaryWithLowercaseKeys(NSDictionary *);
NSDictionary *mergeDictionaries(NSDictionary *, NSDictionary *);
Expand Down
49 changes: 37 additions & 12 deletions Source/WebKit/Platform/cocoa/CocoaHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -192,37 +192,62 @@

// MARK: JSON Helpers

NSDictionary *parseJSON(NSData *json, NSError **error)
static inline NSJSONReadingOptions toReadingImpl(JSONOptionSet options)
{
NSJSONReadingOptions result = 0;
if (options.contains(JSONOptions::FragmentsAllowed))
result |= NSJSONReadingFragmentsAllowed;
return result;
}

static inline NSJSONWritingOptions toWritingImpl(JSONOptionSet options)
{
NSJSONWritingOptions result = 0;
if (options.contains(JSONOptions::FragmentsAllowed))
result |= NSJSONWritingFragmentsAllowed;
return result;
}

id parseJSON(NSData *json, JSONOptionSet options, NSError **error)
{
if (!json)
return nil;
return dynamic_objc_cast<NSDictionary>([NSJSONSerialization JSONObjectWithData:json options:0 error:error]);

id result = [NSJSONSerialization JSONObjectWithData:json options:toReadingImpl(options) error:error];
if (options.contains(JSONOptions::FragmentsAllowed))
return result;

return dynamic_objc_cast<NSDictionary>(result);
}

NSDictionary *parseJSON(NSString *json, NSError **error)
id parseJSON(NSString *json, JSONOptionSet options, NSError **error)
{
return parseJSON([json dataUsingEncoding:NSUTF8StringEncoding], error);
return parseJSON([json dataUsingEncoding:NSUTF8StringEncoding], options, error);
}

NSDictionary *parseJSON(API::Data& json, NSError **error)
id parseJSON(API::Data& json, JSONOptionSet options, NSError **error)
{
return parseJSON(wrapper(json), error);
return parseJSON(wrapper(json), options, error);
}

NSString *encodeJSONString(NSDictionary *dictionary, NSError **error)
NSString *encodeJSONString(id object, JSONOptionSet options, NSError **error)
{
if (auto *data = encodeJSONData(dictionary, error))
if (auto *data = encodeJSONData(object, options, error))
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return nil;
}

NSData *encodeJSONData(NSDictionary *dictionary, NSError **error)
NSData *encodeJSONData(id object, JSONOptionSet options, NSError **error)
{
if (!dictionary)
if (!object)
return nil;

ASSERT([NSJSONSerialization isValidJSONObject:object]);

if (!options.contains(JSONOptions::FragmentsAllowed) && ![object isKindOfClass:NSDictionary.class])
return nil;

ASSERT([NSJSONSerialization isValidJSONObject:dictionary]);
return [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:error];
return [NSJSONSerialization dataWithJSONObject:object options:toWritingImpl(options) error:error];
}

// MARK: NSDictionary Helpers
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/Scripts/webkit/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def serialized_identifiers():
'WebKit::WebExtensionContextIdentifier',
'WebKit::WebExtensionControllerIdentifier',
'WebKit::WebExtensionFrameIdentifier',
'WebKit::WebExtensionPortChannelIdentifier',
'WebKit::WebExtensionTabIdentifier',
'WebKit::WebExtensionWindowIdentifier',
'WebKit::WebGPUIdentifier',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#include "WebExtensionContextIdentifier.h"
#include "WebExtensionControllerIdentifier.h"
#include "WebExtensionFrameIdentifier.h"
#include "WebExtensionPortChannelIdentifier.h"
#include "WebExtensionTabIdentifier.h"
#include "WebExtensionWindowIdentifier.h"
#include "WebGPUIdentifier.h"
Expand Down Expand Up @@ -539,6 +540,7 @@ Vector<ASCIILiteral> serializedIdentifiers()
static_assert(sizeof(uint64_t) == sizeof(WebKit::WebExtensionContextIdentifier));
static_assert(sizeof(uint64_t) == sizeof(WebKit::WebExtensionControllerIdentifier));
static_assert(sizeof(uint64_t) == sizeof(WebKit::WebExtensionFrameIdentifier));
static_assert(sizeof(uint64_t) == sizeof(WebKit::WebExtensionPortChannelIdentifier));
static_assert(sizeof(uint64_t) == sizeof(WebKit::WebExtensionTabIdentifier));
static_assert(sizeof(uint64_t) == sizeof(WebKit::WebExtensionWindowIdentifier));
static_assert(sizeof(uint64_t) == sizeof(WebKit::WebGPUIdentifier));
Expand Down Expand Up @@ -644,6 +646,7 @@ Vector<ASCIILiteral> serializedIdentifiers()
"WebKit::WebExtensionContextIdentifier"_s,
"WebKit::WebExtensionControllerIdentifier"_s,
"WebKit::WebExtensionFrameIdentifier"_s,
"WebKit::WebExtensionPortChannelIdentifier"_s,
"WebKit::WebExtensionTabIdentifier"_s,
"WebKit::WebExtensionWindowIdentifier"_s,
"WebKit::WebGPUIdentifier"_s,
Expand Down
14 changes: 13 additions & 1 deletion Source/WebKit/Shared/Extensions/WebExtensionContentWorldType.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@

#if ENABLE(WK_WEB_EXTENSIONS)

#include <wtf/text/WTFString.h>

namespace WebKit {

enum class WebExtensionContentWorldType : bool {
enum class WebExtensionContentWorldType : uint8_t {
Main,
ContentScript,
};

inline String toDebugString(WebExtensionContentWorldType contentWorldType)
{
switch (contentWorldType) {
case WebExtensionContentWorldType::Main:
return "main"_s;
case WebExtensionContentWorldType::ContentScript:
return "content script"_s;
}
}

} // namespace WebKit

namespace WTF {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

headers: "WebExtensionContentWorldType.h"

enum class WebKit::WebExtensionContentWorldType : bool;
enum class WebKit::WebExtensionContentWorldType : uint8_t {
Main,
ContentScript,
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.
*/

#pragma once

#include <wtf/ObjectIdentifier.h>

namespace WebKit {

struct WebExtensionPortChannelIdentifierType;
using WebExtensionPortChannelIdentifier = ObjectIdentifier<WebExtensionPortChannelIdentifierType>;

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ struct WebKit::WebExtensionWindowParameters {

[Nested] enum class WebKit::WebExtensionWindow::PopulateTabs : bool;

[Nested] enum class WebKit::WebExtensionWindow::Type : bool;
[Nested] enum class WebKit::WebExtensionWindow::Type : uint8_t {
Normal,
Popup,
};

[Nested, OptionSet] enum class WebKit::WebExtensionWindow::TypeFilter : uint8_t {
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,36 @@
if (!page)
return;

RELEASE_LOG_DEBUG(Extensions, "Registered event listener for type %{public}hhu in %{public}@ world", type, (NSString *)toDebugString(contentWorldType));

if (!extension().backgroundContentIsPersistent() && m_backgroundWebView.get()._page->identifier() == identifier)
m_backgroundContentEventListeners.add(type);

auto result = m_eventListenerPages.add({ type, contentWorldType }, WeakPageCountedSet { });
result.iterator->value.add(*page);
}

void WebExtensionContext::removeListener(WebPageProxyIdentifier identifier, WebExtensionEventListenerType type, WebExtensionContentWorldType contentWorldType)
void WebExtensionContext::removeListener(WebPageProxyIdentifier identifier, WebExtensionEventListenerType type, WebExtensionContentWorldType contentWorldType, size_t removedCount)
{
ASSERT(removedCount);

auto page = WebProcessProxy::webPage(identifier);
if (!page)
return;

if (!extension().backgroundContentIsPersistent() && m_backgroundWebView.get()._page->identifier() == identifier)
m_backgroundContentEventListeners.remove(type);
RELEASE_LOG_DEBUG(Extensions, "Unregistered %{public}zu event listener(s) for type %{public}hhu in %{public}@ world", removedCount, type, (NSString *)toDebugString(contentWorldType));

if (!extension().backgroundContentIsPersistent() && m_backgroundWebView.get()._page->identifier() == identifier) {
for (size_t i = 0; i < removedCount; ++i)
m_backgroundContentEventListeners.remove(type);
}

auto iterator = m_eventListenerPages.find({ type, contentWorldType });
if (iterator == m_eventListenerPages.end())
return;

iterator->value.remove(*page);
for (size_t i = 0; i < removedCount; ++i)
iterator->value.remove(*page);

if (!iterator->value.isEmptyIgnoringNullReferences())
return;
Expand Down
Loading

0 comments on commit c796edb

Please sign in to comment.