Skip to content

Commit

Permalink
Use correct tab and frame identifiers in webNavigation events
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264283
rdar://102820594

Reviewed by Timothy Hatcher.

With this change, we now pass WebExtensionTabIdentifiers and WebExtensionFrameIdentifiers to the webNavigation event
handlers.

With this change, we can get rid of the test function to fire a webNavigation event, and actually use a local HTTP server to
load content and verify that webNavigation events are being fired.

* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionControllerCocoa.mm:
(WebKit::WebExtensionController::didStartProvisionalLoadForFrame): Continue early if we don't have a tab, and pass the tab's identifier.
(WebKit::WebExtensionController::didCommitLoadForFrame): Ditto.
(WebKit::WebExtensionController::didFinishLoadForFrame): Ditto.
(WebKit::WebExtensionController::didFailLoadForFrame): Ditto.
* Source/WebKit/UIProcess/Extensions/WebExtensionController.h:
* Source/WebKit/UIProcess/Extensions/WebExtensionController.messages.in:
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPITestCocoa.mm:
(WebKit::WebExtensionAPITest::testWebNavigationEvent): Deleted.
(WebKit::WebExtensionAPITest::fireTestWebNavigationEvent): Deleted.
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIWebNavigationCocoa.mm:
(WebKit::WebExtensionContextProxy::dispatchWebNavigationEvent): Update for the correct identifiers.
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPITest.h:
* Source/WebKit/WebProcess/Extensions/Cocoa/WebExtensionControllerProxyCocoa.mm:
(WebKit::WebExtensionControllerProxy::didStartProvisionalLoadForFrame): Pass a WebExtensionFrameIdentifier.
(WebKit::WebExtensionControllerProxy::didCommitLoadForFrame): Ditto.
(WebKit::WebExtensionControllerProxy::didFinishLoadForFrame): Ditto.
(WebKit::WebExtensionControllerProxy::didFailLoadForFrame): Ditto.
* Source/WebKit/WebProcess/Extensions/Interfaces/WebExtensionAPITest.idl:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.h:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.messages.in:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPIWebNavigation.mm:
(TestWebKitAPI::TEST): Update the tests to use actual HTTP servers and remove any use of the test Web Navigation event code.

Canonical link: https://commits.webkit.org/270297@main
  • Loading branch information
b-weinstein committed Nov 7, 2023
1 parent 3805718 commit 7bd9ef4
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,30 +261,32 @@

// MARK: Web Navigation

void WebExtensionController::didStartProvisionalLoadForFrame(WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL targetURL)
void WebExtensionController::didStartProvisionalLoadForFrame(WebPageProxyIdentifier pageID, WebExtensionFrameIdentifier frameID, const URL& targetURL)
{
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;

auto tab = context->getTab(pageID);
if (!tab)
continue;

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

void WebExtensionController::didCommitLoadForFrame(WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL frameURL)
void WebExtensionController::didCommitLoadForFrame(WebPageProxyIdentifier pageID, WebExtensionFrameIdentifier frameID, const URL& frameURL)
{
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;

Expand All @@ -296,41 +298,51 @@

context->dynamicallyInjectedUserStyleSheets().clear();

auto tab = context->getTab(pageID);
if (!tab)
continue;

context->wakeUpBackgroundContentIfNecessaryToFireEvents(listenerTypes, [&] {
context->sendToProcessesForEvent(completedEventType, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(completedEventType, pageID, frameID, frameURL));
context->sendToProcessesForEvent(contentLoadedtype, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(contentLoadedtype, pageID, frameID, frameURL));
context->sendToProcessesForEvent(completedEventType, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(completedEventType, tab->identifier(), frameID, frameURL));
context->sendToProcessesForEvent(contentLoadedtype, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(contentLoadedtype, tab->identifier(), frameID, frameURL));
});
}
}

void WebExtensionController::didFinishLoadForFrame(WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL frameURL)
void WebExtensionController::didFinishLoadForFrame(WebPageProxyIdentifier pageID, WebExtensionFrameIdentifier frameID, const URL& frameURL)
{
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;

auto tab = context->getTab(pageID);
if (!tab)
continue;

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

void WebExtensionController::didFailLoadForFrame(WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL frameURL)
void WebExtensionController::didFailLoadForFrame(WebPageProxyIdentifier pageID, WebExtensionFrameIdentifier frameID, const URL& frameURL)
{
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;

auto tab = context->getTab(pageID);
if (!tab)
continue;

context->wakeUpBackgroundContentIfNecessaryToFireEvents(listenerTypes, [&] {
context->sendToProcessesForEvent(eventType, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(eventType, pageID, frameID, frameURL));
context->sendToProcessesForEvent(eventType, Messages::WebExtensionContextProxy::DispatchWebNavigationEvent(eventType, tab->identifier(), frameID, frameURL));
});
}
}
Expand Down
9 changes: 5 additions & 4 deletions Source/WebKit/UIProcess/Extensions/WebExtensionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "WebExtensionContextIdentifier.h"
#include "WebExtensionControllerConfiguration.h"
#include "WebExtensionControllerIdentifier.h"
#include "WebExtensionFrameIdentifier.h"
#include "WebExtensionURLSchemeHandler.h"
#include "WebProcessProxy.h"
#include "WebUserContentControllerProxy.h"
Expand Down Expand Up @@ -132,10 +133,10 @@ class WebExtensionController : public API::ObjectImpl<API::Object::Type::WebExte
void removeUserContentController(WebUserContentControllerProxy&);

// Web Navigation
void didStartProvisionalLoadForFrame(WebPageProxyIdentifier, WebCore::FrameIdentifier, URL targetURL);
void didCommitLoadForFrame(WebPageProxyIdentifier, WebCore::FrameIdentifier, URL);
void didFinishLoadForFrame(WebPageProxyIdentifier, WebCore::FrameIdentifier, URL);
void didFailLoadForFrame(WebPageProxyIdentifier, WebCore::FrameIdentifier, URL);
void didStartProvisionalLoadForFrame(WebPageProxyIdentifier, WebExtensionFrameIdentifier, const URL&);
void didCommitLoadForFrame(WebPageProxyIdentifier, WebExtensionFrameIdentifier, const URL&);
void didFinishLoadForFrame(WebPageProxyIdentifier, WebExtensionFrameIdentifier, const URL&);
void didFailLoadForFrame(WebPageProxyIdentifier, WebExtensionFrameIdentifier, const URL&);

Ref<WebExtensionControllerConfiguration> m_configuration;
WebExtensionControllerIdentifier m_identifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
messages -> WebExtensionController {

// webNavigation support.
DidStartProvisionalLoadForFrame(WebKit::WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL targetURL)
DidCommitLoadForFrame(WebKit::WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL targetURL)
DidFinishLoadForFrame(WebKit::WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL targetURL)
DidFailLoadForFrame(WebKit::WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL targetURL)
DidStartProvisionalLoadForFrame(WebKit::WebPageProxyIdentifier pageID, WebKit::WebExtensionFrameIdentifier frameID, URL targetURL)
DidCommitLoadForFrame(WebKit::WebPageProxyIdentifier pageID, WebKit::WebExtensionFrameIdentifier frameID, URL targetURL)
DidFinishLoadForFrame(WebKit::WebPageProxyIdentifier pageID, WebKit::WebExtensionFrameIdentifier frameID, URL targetURL)
DidFailLoadForFrame(WebKit::WebPageProxyIdentifier pageID, WebKit::WebExtensionFrameIdentifier frameID, URL targetURL)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,6 @@ static void assertEquals(WebExtensionContextProxy& extensionContext, JSContextRe
return assertResolves(context, result, message);
}

WebExtensionAPIWebNavigationEvent& WebExtensionAPITest::testWebNavigationEvent()
{
if (!m_webNavigationEvent)
m_webNavigationEvent = WebExtensionAPIWebNavigationEvent::create(forMainWorld(), runtime(), extensionContext(), WebExtensionEventListenerType::WebNavigationOnCompleted);

return *m_webNavigationEvent;
}

void WebExtensionAPITest::fireTestWebNavigationEvent(NSString *urlString)
{
NSURL *targetURL = [NSURL URLWithString:urlString];

WebExtensionAPIWebNavigationEvent& testEvent = testWebNavigationEvent();
testEvent.invokeListenersWithArgument(@{ @"url": urlString }, targetURL);
}

WebExtensionAPIEvent& WebExtensionAPITest::testEvent()
{
if (!m_event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@
return *m_onErrorOccurredEvent;
}

void WebExtensionContextProxy::dispatchWebNavigationEvent(WebExtensionEventListenerType type, WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL frameURL)
void WebExtensionContextProxy::dispatchWebNavigationEvent(WebExtensionEventListenerType type, WebExtensionTabIdentifier tabID, WebExtensionFrameIdentifier frameID, const URL& frameURL)
{
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())
// FIXME: <https://webkit.org/b/260160> We should be passing more arguments here.
@"tabId": @(toWebAPI(tabID)),
@"frameId": @(toWebAPI(frameID))
};

enumerateNamespaceObjects([&](auto& namespaceObject) {
Expand Down
3 changes: 0 additions & 3 deletions Source/WebKit/WebProcess/Extensions/API/WebExtensionAPITest.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ class WebExtensionAPITest : public WebExtensionAPIObject, public JSWebExtensionW

JSValue *assertSafeResolve(JSContextRef, JSValue *function, NSString *message);

WebExtensionAPIWebNavigationEvent& testWebNavigationEvent();
void fireTestWebNavigationEvent(NSString *urlString);

WebExtensionAPIEvent& testEvent();
void fireTestEvent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "WebExtensionAPINamespace.h"
#include "WebExtensionContextProxy.h"
#include "WebExtensionControllerMessages.h"
#include "WebExtensionFrameIdentifier.h"
#include "WebFrame.h"
#include "WebPage.h"
#include "WebProcess.h"
Expand Down Expand Up @@ -107,22 +108,22 @@

void WebExtensionControllerProxy::didStartProvisionalLoadForFrame(WebPage& page, WebFrame& frame, const URL& url)
{
WebProcess::singleton().send(Messages::WebExtensionController::DidStartProvisionalLoadForFrame(page.webPageProxyIdentifier(), frame.frameID(), url), identifier());
WebProcess::singleton().send(Messages::WebExtensionController::DidStartProvisionalLoadForFrame(page.webPageProxyIdentifier(), toWebExtensionFrameIdentifier(frame), url), identifier());
}

void WebExtensionControllerProxy::didCommitLoadForFrame(WebPage& page, WebFrame& frame, const URL& url)
{
WebProcess::singleton().send(Messages::WebExtensionController::DidCommitLoadForFrame(page.webPageProxyIdentifier(), frame.frameID(), url), identifier());
WebProcess::singleton().send(Messages::WebExtensionController::DidCommitLoadForFrame(page.webPageProxyIdentifier(), toWebExtensionFrameIdentifier(frame), url), identifier());
}

void WebExtensionControllerProxy::didFinishLoadForFrame(WebPage& page, WebFrame& frame, const URL& url)
{
WebProcess::singleton().send(Messages::WebExtensionController::DidFinishLoadForFrame(page.webPageProxyIdentifier(), frame.frameID(), url), identifier());
WebProcess::singleton().send(Messages::WebExtensionController::DidFinishLoadForFrame(page.webPageProxyIdentifier(), toWebExtensionFrameIdentifier(frame), url), identifier());
}

void WebExtensionControllerProxy::didFailLoadForFrame(WebPage& page, WebFrame& frame, const URL& url)
{
WebProcess::singleton().send(Messages::WebExtensionController::DidFailLoadForFrame(page.webPageProxyIdentifier(), frame.frameID(), url), identifier());
WebProcess::singleton().send(Messages::WebExtensionController::DidFailLoadForFrame(page.webPageProxyIdentifier(), toWebExtensionFrameIdentifier(frame), url), identifier());
}

} // namespace WebKit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@
// Asserts the function does not thow an exception and the result promise is resolved.
[NeedsScriptContext] any assertSafeResolve(any function, [Optional] DOMString message);

// Temporary webNavigation event for bring-up.
// FIXME: Remove this.
readonly attribute WebExtensionAPIWebNavigationEvent testWebNavigationEvent;
void fireTestWebNavigationEvent(DOMString targetUrl);

// FIXME: Temporary Event for bring-up. Remove this.
readonly attribute WebExtensionAPIEvent testEvent;
void fireTestEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class WebExtensionContextProxy final : public RefCounted<WebExtensionContextProx
void dispatchTabsRemovedEvent(WebExtensionTabIdentifier, WebExtensionWindowIdentifier, WebExtensionContext::WindowIsClosing);

// Web Navigation
void dispatchWebNavigationEvent(WebExtensionEventListenerType, WebPageProxyIdentifier, WebCore::FrameIdentifier, URL);
void dispatchWebNavigationEvent(WebExtensionEventListenerType, WebExtensionTabIdentifier, WebExtensionFrameIdentifier, const URL&);

// Windows
void dispatchWindowsEvent(WebExtensionEventListenerType, const std::optional<WebExtensionWindowParameters>&);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ messages -> WebExtensionContextProxy {
DispatchTabsRemovedEvent(WebKit::WebExtensionTabIdentifier tabIdentifier, WebKit::WebExtensionWindowIdentifier windowIdentifier, WebKit::WebExtensionContext::WindowIsClosing windowIsClosing)

// Web Navigation
DispatchWebNavigationEvent(WebKit::WebExtensionEventListenerType type, WebKit::WebPageProxyIdentifier pageID, WebCore::FrameIdentifier frameID, URL targetURL)
DispatchWebNavigationEvent(WebKit::WebExtensionEventListenerType type, WebKit::WebExtensionTabIdentifier tabID, WebKit::WebExtensionFrameIdentifier frameID, URL targetURL)

// Windows
DispatchWindowsEvent(WebKit::WebExtensionEventListenerType type, std::optional<WebKit::WebExtensionWindowParameters> windowParameters)
Expand Down
Loading

0 comments on commit 7bd9ef4

Please sign in to comment.