[webkit-changes] [WebKit/WebKit] cc9d7c: Support non-persistent background pages in Web Ext...

Timothy Hatcher noreply at github.com
Thu Mar 7 19:22:18 PST 2024


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: cc9d7c9f1548c51d31b6695b7c06a43c3ffa50f7
      https://github.com/WebKit/WebKit/commit/cc9d7c9f1548c51d31b6695b7c06a43c3ffa50f7
  Author: Timothy Hatcher <timothy at apple.com>
  Date:   2024-03-07 (Thu, 07 Mar 2024)

  Changed paths:
    M Source/WTF/wtf/HashCountedSet.h
    M Source/WebKit/Platform/IPC/ArgumentCoders.h
    M Source/WebKit/Scripts/webkit/messages.py
    M Source/WebKit/Shared/Extensions/WebExtensionContentWorldType.h
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIActionCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIAlarmsCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPICommandsCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPICookiesCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIEventCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIMenusCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIPermissionsCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIPortCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIRuntimeCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIStorageCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPITabsCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIWindowsCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionContextCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionControllerCocoa.mm
    M Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionMessagePortCocoa.mm
    M Source/WebKit/UIProcess/Extensions/WebExtensionContext.cpp
    M Source/WebKit/UIProcess/Extensions/WebExtensionContext.h
    M Source/WebKit/UIProcess/Extensions/WebExtensionContext.messages.in
    M Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIPortCocoa.mm
    M Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIRuntimeCocoa.mm
    M Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.h
    M Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.messages.in

  Log Message:
  -----------
  Support non-persistent background pages in Web Extensions.
https://webkit.org/b/246483
rdar://problem/114823336

Reviewed by Brian Weinstein.

Fixes a number of things related to non-persistent pages. The main one was that all current uses
of wakeUpBackgroundContentIfNecessaryToFireEvents() were not capturing the lambda variables correctly.
This was mostly fine since the lambda was almost always called immediately, but not it will be stored
and called later when the page needs woken back up. Changed all lambdas to protect the context, and
copy their simple parameters and use Ref / RefPtr for counted objects.

The next issue was that ports were not being disconnected when the extension pages went away, leaving
a dangling port that the other pages could still message to no avail. We now disconnect ports for any
page when it is removed from the WebExtensionController, which is when WebPageProxy is deallocated.
This required having the UI process track what ports were open for which page.

And third, the runtime.connect() and runtime.sendMessage() implementations were not waking the background.
This was fine before since it was almost always guaranteed to exist (but likely a race condition). Now
we wait for the page to load before trying to deliver the messages.

Currently almost all tests use non-persistent pages, and this change is well exercised with the tests.
When using the TestWebKitAPI runner, the timeouts for the background page is shortened to 3 seconds, vs
the typical 30 seconds. And open and active ports is 6 seconds, vs the typical 2 minutes. Most tests will
only wake the background page once, but some do run long enough to have it terminated and get relaunched.

* Source/WTF/wtf/HashCountedSet.h:
(WTF::Traits>::isValidValue): Added. The only impl passing off to HashMap was dead code, and
was removed from HashMap. Add back an implemetation copied from HashSet.
* Source/WebKit/Platform/IPC/ArgumentCoders.h: Fix return to be std::nullopt. This was also dead
code that is now being hit since we are using it again.
* Source/WebKit/Scripts/webkit/messages.py:
(class_template_headers): Added HashCountedSet.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIActionCocoa.mm:
(WebKit::WebExtensionContext::fireActionClickedEventIfNeeded): Fix lambda to properly capture.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIAlarmsCocoa.mm:
(WebKit::WebExtensionContext::fireAlarmsEventIfNeeded): Ditto.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPICommandsCocoa.mm:
(WebKit::WebExtensionContext::fireCommandEventIfNeeded): Ditto.
(WebKit::WebExtensionContext::fireCommandChangedEventIfNeeded): Ditto.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPICookiesCocoa.mm:
(WebKit::WebExtensionContext::fireCookiesChangedEventIfNeeded): Ditto.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIEventCocoa.mm:
(WebKit::WebExtensionContext::addListener): Use isBackgroundPage() helper.
(WebKit::WebExtensionContext::removeListener): Ditto.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIMenusCocoa.mm:
(WebKit::WebExtensionContext::fireMenusClickedEventIfNeeded): Fix lambda to properly capture.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIPermissionsCocoa.mm:
(WebKit::WebExtensionContext::firePermissionsEventListenerIfNecessary): Ditto.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIPortCocoa.mm:
(WebKit::WebExtensionContext::portPostMessage): Track last time background used port. Don't use
wakeUpBackgroundContentIfNecessaryToFireEvents(), since the page needs loaded for ports to be
active and receive messages.
(WebKit::WebExtensionContext::portRemoved): Added WebPageProxyIdentifier param.
(WebKit::WebExtensionContext::pageHasOpenPorts): Added.
(WebKit::WebExtensionContext::disconnectPortsForPage): Added.
(WebKit::WebExtensionContext::addPorts): Added HashCountedSet param instead of simple count.
(WebKit::WebExtensionContext::removePort): Added WebPageProxyIdentifier param.
(WebKit::WebExtensionContext::addNativePort): Don't use addPorts() now, since that requires a page.
(WebKit::WebExtensionContext::removeNativePort): Ditto for removePort().
(WebKit::WebExtensionContext::firePortDisconnectEventIfNeeded): Don't wake up the background,
since the page needs loaded for ports to be active and receive messages.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIRuntimeCocoa.mm:
(WebKit::WebExtensionContext::runtimeSendMessage): This was missing the wake up the background call.
(WebKit::WebExtensionContext::runtimeConnect): Ditto.
(WebKit::WebExtensionContext::runtimeConnectNative): Track added ports by page.
(WebKit::WebExtensionContext::runtimeWebPageConnect): Ditto.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIStorageCocoa.mm:
(WebKit::WebExtensionContext::fireStorageChangedEventIfNeeded): Fix lambda to properly capture.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPITabsCocoa.mm:
(WebKit::WebExtensionContext::tabsConnect): Track added ports by page.
(WebKit::WebExtensionContext::fireTabsCreatedEventIfNeeded): Fix lambda to properly capture.
(WebKit::WebExtensionContext::fireTabsUpdatedEventIfNeeded): Ditto.
(WebKit::WebExtensionContext::fireTabsReplacedEventIfNeeded): Ditto.
(WebKit::WebExtensionContext::fireTabsDetachedEventIfNeeded): Ditto.
(WebKit::WebExtensionContext::fireTabsMovedEventIfNeeded): Ditto.
(WebKit::WebExtensionContext::fireTabsAttachedEventIfNeeded): Ditto.
(WebKit::WebExtensionContext::fireTabsActivatedEventIfNeeded): Ditto.
(WebKit::WebExtensionContext::fireTabsHighlightedEventIfNeeded): Ditto.
(WebKit::WebExtensionContext::fireTabsRemovedEventIfNeeded): Ditto.
* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIWindowsCocoa.mm:
(WebKit::WebExtensionContext::fireWindowsEventIfNeeded): Ditto.
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionContextCocoa.mm:
(WebKit::WebExtensionContext::removePage): Added. Disconnect ports.
(WebKit::WebExtensionContext::getCurrentTab const): Use isBackgroundPage() helper.
(WebKit::WebExtensionContext::didChangeTabProperties): Return early in lambda for !isLoaded().
(WebKit::WebExtensionContext::didStartProvisionalLoadForFrame): Fix lambda to properly capture.
(WebKit::WebExtensionContext::didCommitLoadForFrame): Ditto.
(WebKit::WebExtensionContext::didFinishLoadForFrame): Ditto.
(WebKit::WebExtensionContext::didFailLoadForFrame): Ditto.
(WebKit::WebExtensionContext::resourceLoadDidSendRequest): Fix lambda to properly capture. Also
fetch the window outside the lambda so it is known before time passes and the tab might move windows.
(WebKit::WebExtensionContext::resourceLoadDidPerformHTTPRedirection): Ditto.
(WebKit::WebExtensionContext::resourceLoadDidReceiveChallenge): Ditto.
(WebKit::WebExtensionContext::resourceLoadDidReceiveResponse): Ditto.
(WebKit::WebExtensionContext::resourceLoadDidCompleteWithError): Ditto.
(WebKit::WebExtensionContext::relatedWebView): Skip over Inspector pages, they have different process pools.
(WebKit::WebExtensionContext::isBackgroundPage const): Added.
(WebKit::WebExtensionContext::backgroundContentIsLoaded const): Added.
(WebKit::WebExtensionContext::loadBackgroundWebViewIfNeeded): Added.
(WebKit::WebExtensionContext::loadBackgroundWebView): Added logging. Remove unused m_lastBackgroundContentLoadDate.
(WebKit::WebExtensionContext::unloadBackgroundWebView):
(WebKit::isNotRunningInTestRunner): Added.
(WebKit::WebExtensionContext::scheduleBackgroundContentToUnload): Removed FIXME and schedule a timer.
(WebKit::WebExtensionContext::unloadBackgroundContentIfPossible): Added.
(WebKit::WebExtensionContext::performTasksAfterBackgroundContentLoads): Shorten delay and added !isLoaded() early return.
(WebKit::WebExtensionContext::wakeUpBackgroundContentIfNecessary): Drastically simplified.
(WebKit::WebExtensionContext::wakeUpBackgroundContentIfNecessaryToFireEvents): Simplied. Uses wakeUpBackgroundContentIfNecessary().
(WebKit::WebExtensionContext::webViewWebContentProcessDidTerminate): Only reload background page if persistent. Removed FIXME
since that is handled by removePage() now.
(WebKit::WebExtensionContext::processes const): Return early if !isLoaded().
(WebKit::WebExtensionContext::queueEventToFireAfterBackgroundContentLoads): Deleted.
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionControllerCocoa.mm:
(WebKit::WebExtensionController::removePage): Call removePage() on each context.
* Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionMessagePortCocoa.mm:
(WebKit::WebExtensionMessagePort::remove): Use firePortDisconnectEventIfNeeded() instead of page sentic portRemoved().
* Source/WebKit/UIProcess/Extensions/WebExtensionContext.cpp:
(WebKit::WebExtensionContext::pageListensForEvent const): Return early if !isLoaded().
(WebKit::WebExtensionContext::processes const): Ditto.
* Source/WebKit/UIProcess/Extensions/WebExtensionContext.h:
(WebKit::WebExtensionContext::sendToProcesses const): Return early if !isLoaded().
* Source/WebKit/UIProcess/Extensions/WebExtensionContext.messages.in:
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIPortCocoa.mm:
(WebKit::WebExtensionAPIPort::remove): Padd owningPageProxyIdentifier() in the message.
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPIRuntimeCocoa.mm:
(WebKit::WebExtensionAPIRuntime::connectNative): Pass webPageProxyIdentifier() in the message.
(WebKit::WebExtensionContextProxy::internalDispatchRuntimeConnectEvent): Use a HashCountedSet to track page port counts.
(WebKit::WebExtensionContextProxy::dispatchRuntimeConnectEvent): Use HashCountedSet.
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.h:
* Source/WebKit/WebProcess/Extensions/WebExtensionContextProxy.messages.in:

Canonical link: https://commits.webkit.org/275819@main



To unsubscribe from these emails, change your notification settings at https://github.com/WebKit/WebKit/settings/notifications


More information about the webkit-changes mailing list