Skip to content

Commit

Permalink
Clean up existing Web Extension event dispatch code.
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=261017
rdar://problem/114807244

Reviewed by Brian Weinstein.

Have the alarms, permissions, and webNavigation event dispatch code match in style to
the windows event dispatch code. Use a switch on the event type and combine the event
messages into a single message for webNavigation.

* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIAlarmsCocoa.mm:
(WebKit::WebExtensionContext::fireAlarmsEventIfNeeded):
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIPermissionsCocoa.mm:
(WebKit::WebExtensionContext::firePermissionsEventListenerIfNecessary):
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionControllerCocoa.mm:
(WebKit::WebExtensionController::didStartProvisionalLoadForFrame):
(WebKit::WebExtensionController::didCommitLoadForFrame):
(WebKit::WebExtensionController::didFinishLoadForFrame):
(WebKit::WebExtensionController::didFailLoadForFrame):
* Source/WebKit/UIProcess/Extensions/WebExtensionController.h:
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIAlarmsCocoa.mm:
(WebKit::WebExtensionContextProxy::dispatchAlarmsEvent):
(WebKit::WebExtensionContextProxy::dispatchAlarmEvent): Deleted.
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIPermissionsCocoa.mm:
(WebKit::WebExtensionContextProxy::dispatchPermissionsEvent):
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIWebNavigationCocoa.mm:
(WebKit::WebExtensionAPIWebNavigation::getAllFrames):
(WebKit::WebExtensionAPIWebNavigation::getFrame):
(WebKit::WebExtensionAPIWebNavigation::onBeforeNavigate):
(WebKit::WebExtensionAPIWebNavigation::onCommitted):
(WebKit::WebExtensionAPIWebNavigation::onDOMContentLoaded):
(WebKit::WebExtensionAPIWebNavigation::onCompleted):
(WebKit::WebExtensionAPIWebNavigation::onErrorOccurred):
(WebKit::WebExtensionContextProxy::dispatchWebNavigationEvent):
* Source/WebKit/WebProcess/Extensions/Cocoa/WebExtensionContextProxyCocoa.mm:
(WebKit::WebExtensionContextProxy::dispatchPermissionsEvent): Deleted.
(WebKit::WebExtensionContextProxy::dispatchWebNavigationOnBeforeNavigateEvent): Deleted.
(WebKit::WebExtensionContextProxy::dispatchWebNavigationOnCommittedEvent): Deleted.
(WebKit::WebExtensionContextProxy::dispatchWebNavigationOnDOMContentLoadedEvent): Deleted.
(WebKit::WebExtensionContextProxy::dispatchWebNavigationOnCompletedEvent): Deleted.
(WebKit::WebExtensionContextProxy::dispatchWebNavigationOnErrorOccurredEvent): Deleted.
* Source/WebKit/WebProcess/Extensions/Cocoa/WebExtensionControllerProxyCocoa.mm:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.h:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.messages.in:
* Source/WebKit/WebProcess/Extensions/WebExtensionControllerProxy.h:

Canonical link: https://commits.webkit.org/267557@main
  • Loading branch information
xeenon committed Sep 1, 2023
1 parent 5c26aee commit 27a5832
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
{
auto type = WebExtensionEventListenerType::AlarmsOnAlarm;
wakeUpBackgroundContentIfNecessaryToFireEvents({ type }, [&] {
sendToProcessesForEvent(type, Messages::WebExtensionContextProxy::DispatchAlarmEvent(alarm.parameters()));
sendToProcessesForEvent(type, Messages::WebExtensionContextProxy::DispatchAlarmsEvent(alarm.parameters()));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@
ASSERT(type == WebExtensionEventListenerType::PermissionsOnAdded || type == WebExtensionEventListenerType::PermissionsOnRemoved);

HashSet<String> origins = toStrings(matchPatterns);
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { type };
wakeUpBackgroundContentIfNecessaryToFireEvents(listenerTypes, [&] {

wakeUpBackgroundContentIfNecessaryToFireEvents({ type }, [&] {
sendToProcessesForEvent(type, Messages::WebExtensionContextProxy::DispatchPermissionsEvent(type, permissions, origins));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,65 +245,70 @@
return extensions;
}

// MARK: WebNavigation support
// MARK: Web Navigation

void WebExtensionController::didStartProvisionalLoadForFrame(WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL targetURL)
{
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { WebExtensionEventListenerType::WebNavigationOnBeforeNavigate };
auto eventType = WebExtensionEventListenerType::WebNavigationOnBeforeNavigate;
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { eventType };

for (auto& context : m_extensionContexts) {
// FIXME: We need to turn pageID into a _WKWebExtensionTab and pass that here.
if (!context->hasPermission(targetURL))
continue;

context->wakeUpBackgroundContentIfNecessaryToFireEvents(listenerTypes, [&] {
context->sendToProcessesForEvent(WebExtensionEventListenerType::WebNavigationOnBeforeNavigate, Messages::WebExtensionContextProxy::DispatchWebNavigationOnBeforeNavigateEvent(pageID, frameID, targetURL));
context->sendToProcessesForEvent(eventType, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(eventType, pageID, frameID, targetURL));
});
}
}

void WebExtensionController::didCommitLoadForFrame(WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL frameURL)
{
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { WebExtensionEventListenerType::WebNavigationOnCommitted, WebExtensionEventListenerType::WebNavigationOnDOMContentLoaded };
auto completedEventType = WebExtensionEventListenerType::WebNavigationOnCompleted;
auto contentLoadedtype = WebExtensionEventListenerType::WebNavigationOnDOMContentLoaded;
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { completedEventType, contentLoadedtype };

for (auto& context : m_extensionContexts) {
// FIXME: We need to turn pageID into a _WKWebExtensionTab and pass that here.
if (!context->hasPermission(frameURL))
continue;

context->wakeUpBackgroundContentIfNecessaryToFireEvents(listenerTypes, [&] {
context->sendToProcessesForEvent(WebExtensionEventListenerType::WebNavigationOnCommitted, Messages::WebExtensionContextProxy::DispatchWebNavigationOnCommittedEvent(pageID, frameID, frameURL));
context->sendToProcessesForEvent(WebExtensionEventListenerType::WebNavigationOnDOMContentLoaded, Messages::WebExtensionContextProxy::DispatchWebNavigationOnDOMContentLoadedEvent(pageID, frameID, frameURL));
context->sendToProcessesForEvent(completedEventType, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(completedEventType, pageID, frameID, frameURL));
context->sendToProcessesForEvent(contentLoadedtype, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(contentLoadedtype, pageID, frameID, frameURL));
});
}
}

void WebExtensionController::didFinishLoadForFrame(WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL frameURL)
{
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { WebExtensionEventListenerType::WebNavigationOnCompleted };
auto eventType = WebExtensionEventListenerType::WebNavigationOnCompleted;
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { eventType };

for (auto& context : m_extensionContexts) {
// FIXME: We need to turn pageID into a _WKWebExtensionTab and pass that here.
if (!context->hasPermission(frameURL))
continue;

context->wakeUpBackgroundContentIfNecessaryToFireEvents(listenerTypes, [&] {
context->sendToProcessesForEvent(WebExtensionEventListenerType::WebNavigationOnCompleted, Messages::WebExtensionContextProxy::DispatchWebNavigationOnCompletedEvent(pageID, frameID, frameURL));
context->sendToProcessesForEvent(eventType, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(eventType, pageID, frameID, frameURL));
});
}
}

void WebExtensionController::didFailLoadForFrame(WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL frameURL)
{
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { WebExtensionEventListenerType::WebNavigationOnErrorOccurred };
auto eventType = WebExtensionEventListenerType::WebNavigationOnErrorOccurred;
auto listenerTypes = WebExtensionContext::EventListenerTypeSet { eventType };

for (auto& context : m_extensionContexts) {
// FIXME: We need to turn pageID into a _WKWebExtensionTab and pass that here.
if (!context->hasPermission(frameURL))
continue;

context->wakeUpBackgroundContentIfNecessaryToFireEvents(listenerTypes, [&] {
context->sendToProcessesForEvent(WebExtensionEventListenerType::WebNavigationOnErrorOccurred, Messages::WebExtensionContextProxy::DispatchWebNavigationOnErrorOccurredEvent(pageID, frameID, frameURL));
context->sendToProcessesForEvent(eventType, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(eventType, pageID, frameID, frameURL));
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/WebKit/UIProcess/Extensions/WebExtensionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class WebExtensionController : public API::ObjectImpl<API::Object::Type::WebExte
#endif

private:
// IPC::MessageReceiver.
// IPC::MessageReceiver
void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;

void addProcessPool(WebProcessPool&);
Expand All @@ -124,7 +124,7 @@ class WebExtensionController : public API::ObjectImpl<API::Object::Type::WebExte
void addUserContentController(WebUserContentControllerProxy&);
void removeUserContentController(WebUserContentControllerProxy&);

// MARK: webNavigation support.
// Web Navigation
void didStartProvisionalLoadForFrame(WebPageProxyIdentifier, WebCore::FrameIdentifier, URL targetURL);
void didCommitLoadForFrame(WebPageProxyIdentifier, WebCore::FrameIdentifier, URL);
void didFinishLoadForFrame(WebPageProxyIdentifier, WebCore::FrameIdentifier, URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,13 @@
return *m_onAlarm;
}

void WebExtensionContextProxy::dispatchAlarmEvent(const WebExtensionAlarmParameters& alarm)
void WebExtensionContextProxy::dispatchAlarmsEvent(const WebExtensionAlarmParameters& alarm)
{
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/alarms/onAlarm

NSDictionary *alarmDictionary = toAPI(alarm);
auto *details = toAPI(alarm);

enumerateNamespaceObjects([&](auto& namespaceObject) {
namespaceObject.alarms().onAlarm().invokeListenersWithArgument(alarmDictionary);
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/alarms/onAlarm
namespaceObject.alarms().onAlarm().invokeListenersWithArgument(details);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
#import "config.h"
#import "WebExtensionAPIPermissions.h"

#import "CocoaHelpers.h"
#import "Logging.h"
#import "MessageSenderInlines.h"
#import "WebExtension.h"
#import "WebExtensionAPINamespace.h"
#import "WebExtensionContextMessages.h"
#import "WebProcess.h"
#import <wtf/cocoa/VectorCocoa.h>
Expand Down Expand Up @@ -241,6 +243,33 @@
return *m_onRemoved;
}

void WebExtensionContextProxy::dispatchPermissionsEvent(WebExtensionEventListenerType type, HashSet<String> permissions, HashSet<String> origins)
{
auto *permissionDetails = toAPIArray(permissions);
auto *originDetails = toAPIArray(origins);
auto *details = @{ @"permissions": permissionDetails, @"origins": originDetails };

enumerateNamespaceObjects([&](auto& namespaceObject) {
auto& permissionsObject = namespaceObject.permissions();

switch (type) {
case WebExtensionEventListenerType::PermissionsOnAdded:
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/permissions/onAdded
permissionsObject.onAdded().invokeListenersWithArgument(details);
break;

case WebExtensionEventListenerType::PermissionsOnRemoved:
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/permissions/onRemoved
permissionsObject.onRemoved().invokeListenersWithArgument(details);
break;

default:
ASSERT_NOT_REACHED();
break;
}
});
}

} // namespace WebKit

#endif // ENABLE(WK_WEB_EXTENSIONS)
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,34 @@
#endif

#import "config.h"
#import "WebExtensionAPINamespace.h"
#import "WebExtensionAPIWebNavigation.h"

#if ENABLE(WK_WEB_EXTENSIONS)

namespace WebKit {

void WebExtensionAPIWebNavigation::getAllFrames(WebPage* webPage, NSDictionary *details, Ref<WebExtensionCallbackHandler>&& callback, NSString **errorString)
{
// Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/getAllFrames

// FIXME: <https://webkit.org/b/260160> Implement this.
}

void WebExtensionAPIWebNavigation::getFrame(WebPage* webPage, NSDictionary *details, Ref<WebExtensionCallbackHandler>&& callback, NSString **errorString)
{
// Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/getFrame

// FIXME: <https://webkit.org/b/260160> Implement this.
}

WebExtensionAPIWebNavigationEvent& WebExtensionAPIWebNavigation::onBeforeNavigate()
{
// Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/onBeforeNavigate

if (!m_onBeforeNavigateEvent)
m_onBeforeNavigateEvent = WebExtensionAPIWebNavigationEvent::create(forMainWorld(), runtime(), extensionContext(), WebExtensionEventListenerType::WebNavigationOnBeforeNavigate);

return *m_onBeforeNavigateEvent;
}

Expand All @@ -49,6 +65,7 @@

if (!m_onCommittedEvent)
m_onCommittedEvent = WebExtensionAPIWebNavigationEvent::create(forMainWorld(), runtime(), extensionContext(), WebExtensionEventListenerType::WebNavigationOnCommitted);

return *m_onCommittedEvent;
}

Expand All @@ -58,6 +75,7 @@

if (!m_onDOMContentLoadedEvent)
m_onDOMContentLoadedEvent = WebExtensionAPIWebNavigationEvent::create(forMainWorld(), runtime(), extensionContext(), WebExtensionEventListenerType::WebNavigationOnDOMContentLoaded);

return *m_onDOMContentLoadedEvent;
}

Expand All @@ -67,6 +85,7 @@

if (!m_onCompletedEvent)
m_onCompletedEvent = WebExtensionAPIWebNavigationEvent::create(forMainWorld(), runtime(), extensionContext(), WebExtensionEventListenerType::WebNavigationOnCompleted);

return *m_onCompletedEvent;
}

Expand All @@ -76,21 +95,54 @@

if (!m_onErrorOccurredEvent)
m_onErrorOccurredEvent = WebExtensionAPIWebNavigationEvent::create(forMainWorld(), runtime(), extensionContext(), WebExtensionEventListenerType::WebNavigationOnErrorOccurred);
return *m_onErrorOccurredEvent;
}

void WebExtensionAPIWebNavigation::getAllFrames(WebPage* webPage, NSDictionary *details, Ref<WebExtensionCallbackHandler>&& callback, NSString **errorString)
{
// Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/getAllFrames

// FIXME: <https://webkit.org/b/260160> Implement this.
return *m_onErrorOccurredEvent;
}

void WebExtensionAPIWebNavigation::getFrame(WebPage* webPage, NSDictionary *details, Ref<WebExtensionCallbackHandler>&& callback, NSString **errorString)
void WebExtensionContextProxy::dispatchWebNavigationEvent(WebExtensionEventListenerType type, WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL frameURL)
{
// Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/getFrame

// FIXME: <https://webkit.org/b/260160> Implement this.
auto *navigationDetails = @{
@"url": (NSString *)frameURL.string(),

// FIXME: <https://webkit.org/b/260160> We should be passing more arguments here and these arguments should have the correct values.
@"tabId": @(pageID.toUInt64()),
@"frameId": @(frameID.object().toUInt64())
};

enumerateNamespaceObjects([&](auto& namespaceObject) {
auto& webNavigationObject = namespaceObject.webNavigation();

switch (type) {
case WebExtensionEventListenerType::WebNavigationOnBeforeNavigate:
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/onBeforeNavigate
webNavigationObject.onBeforeNavigate().invokeListenersWithArgument(navigationDetails, frameURL);
break;

case WebExtensionEventListenerType::WebNavigationOnCommitted:
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/onCommitted
webNavigationObject.onCommitted().invokeListenersWithArgument(navigationDetails, frameURL);
break;

case WebExtensionEventListenerType::WebNavigationOnDOMContentLoaded:
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/onDOMContentLoaded
webNavigationObject.onDOMContentLoaded().invokeListenersWithArgument(navigationDetails, frameURL);
break;

case WebExtensionEventListenerType::WebNavigationOnCompleted:
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/onCompleted
webNavigationObject.onCompleted().invokeListenersWithArgument(navigationDetails, frameURL);
break;

case WebExtensionEventListenerType::WebNavigationOnErrorOccurred:
// Documentation: https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation/onErrorOccurred
webNavigationObject.onErrorOccurred().invokeListenersWithArgument(navigationDetails, frameURL);
break;

default:
ASSERT_NOT_REACHED();
break;
}
});
}

} // namespace WebKit
Expand Down
Loading

0 comments on commit 27a5832

Please sign in to comment.