Skip to content

Commit

Permalink
Bug 1728326 - Notify WebExtensions internals when a background servic…
Browse files Browse the repository at this point in the history
…e worker scripts has been executed. r=asuth

This patch includes changes needed to notify the WebExtensions internals
when a background service worker script has been fully loaded, through a
NotifyWorkerLoadedRunnable (dispatched as low priority) that calls a new
mozIExtensionAPIRequestHandler.onExtensionWorkerLoaded method.

Differential Revision: https://phabricator.services.mozilla.com/D124701
  • Loading branch information
rpl committed Nov 5, 2021
1 parent a053a7d commit 60bb3c8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
16 changes: 15 additions & 1 deletion dom/workers/WorkerPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include "mozilla/dom/WorkerBinding.h"
#include "mozilla/dom/JSExecutionManager.h"
#include "mozilla/dom/WindowContext.h"
#include "mozilla/extensions/ExtensionBrowser.h" // extensions::Create{AndDispatchInitWorkerContext,WorkerDestroyed}Runnable
#include "mozilla/extensions/ExtensionBrowser.h" // extensions::Create{AndDispatchInitWorkerContext,WorkerLoaded,WorkerDestroyed}Runnable
#include "mozilla/extensions/WebExtensionPolicy.h"
#include "mozilla/StorageAccess.h"
#include "mozilla/StoragePrincipalHelper.h"
Expand Down Expand Up @@ -369,6 +369,20 @@ class CompileScriptRunnable final : public WorkerDebuggeeRunnable {
ErrorResult rv;
workerinternals::LoadMainScript(aWorkerPrivate, std::move(mOriginStack),
mScriptURL, WorkerScript, rv);

if (aWorkerPrivate->ExtensionAPIAllowed()) {
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
RefPtr<Runnable> extWorkerRunnable =
extensions::CreateWorkerLoadedRunnable(
aWorkerPrivate->ServiceWorkerID(), aWorkerPrivate->GetBaseURI());
// Dispatch as a low priority runnable.
if (NS_FAILED(aWorkerPrivate->DispatchToMainThreadForMessaging(
extWorkerRunnable.forget()))) {
NS_WARNING(
"Failed to dispatch runnable to notify extensions worker loaded");
}
}

rv.WouldReportJSException();
// Explicitly ignore NS_BINDING_ABORTED on rv. Or more precisely, still
// return false and don't SetWorkerScriptExecutedSuccessfully() in that
Expand Down
10 changes: 10 additions & 0 deletions toolkit/components/extensions/mozIExtensionAPIRequestHandling.idl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ interface mozIExtensionAPIRequestHandler : nsISupports
void initExtensionWorker(in nsISupports extension,
in mozIExtensionServiceWorkerInfo serviceWorkerInfo);

/**
* A method called when an extension background service worker has loaded its
* main script.
*
* @param extension An instance of the WebExtensionPolicy webidl interface.
* @param serviceWorkerDescriptorId
*/
void onExtensionWorkerLoaded(in nsISupports extension,
in unsigned long long serviceWorkerDescriptorId);

/**
* A method called when an extension background service worker is destroyed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,24 @@ bool RequestInitWorkerRunnable::MainThreadRun() {
return true;
}

// NotifyWorkerLoadedRunnable

nsresult NotifyWorkerLoadedRunnable::Run() {
MOZ_ASSERT(NS_IsMainThread());

RefPtr<WebExtensionPolicy> policy =
ExtensionPolicyService::GetSingleton().GetByURL(mSWBaseURI.get());

nsCOMPtr<mozIExtensionAPIRequestHandler> handler =
&ExtensionAPIRequestForwarder::APIRequestHandler();
MOZ_ASSERT(handler);

NS_WARN_IF(
NS_FAILED(handler->OnExtensionWorkerLoaded(policy, mSWDescriptorId)));

return NS_OK;
}

// NotifyWorkerDestroyedRunnable

nsresult NotifyWorkerDestroyedRunnable::Run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ class RequestInitWorkerRunnable : public dom::WorkerMainThreadRunnable {
bool MainThreadRun() override;
};

class NotifyWorkerLoadedRunnable : public Runnable {
uint64_t mSWDescriptorId;
nsCOMPtr<nsIURI> mSWBaseURI;

public:
explicit NotifyWorkerLoadedRunnable(const uint64_t aServiceWorkerDescriptorId,
const nsCOMPtr<nsIURI>& aWorkerBaseURI)
: Runnable("extensions::NotifyWorkerLoadedRunnable"),
mSWDescriptorId(aServiceWorkerDescriptorId),
mSWBaseURI(aWorkerBaseURI) {
MOZ_ASSERT(mSWDescriptorId > 0);
MOZ_ASSERT(mSWBaseURI);
}

NS_IMETHOD Run() override;

NS_INLINE_DECL_REFCOUNTING_INHERITED(NotifyWorkerLoadedRunnable, Runnable)

private:
~NotifyWorkerLoadedRunnable() = default;
};

class NotifyWorkerDestroyedRunnable : public Runnable {
uint64_t mSWDescriptorId;
nsCOMPtr<nsIURI> mSWBaseURI;
Expand Down
8 changes: 8 additions & 0 deletions toolkit/components/extensions/webidl-api/ExtensionBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ void CreateAndDispatchInitWorkerContextRunnable() {
}
}

already_AddRefed<Runnable> CreateWorkerLoadedRunnable(
const uint64_t aServiceWorkerDescriptorId,
const nsCOMPtr<nsIURI>& aWorkerBaseURI) {
RefPtr<NotifyWorkerLoadedRunnable> runnable = new NotifyWorkerLoadedRunnable(
aServiceWorkerDescriptorId, aWorkerBaseURI);
return runnable.forget();
}

already_AddRefed<Runnable> CreateWorkerDestroyedRunnable(
const uint64_t aServiceWorkerDescriptorId,
const nsCOMPtr<nsIURI>& aWorkerBaseURI) {
Expand Down
4 changes: 4 additions & 0 deletions toolkit/components/extensions/webidl-api/ExtensionBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ bool ExtensionAPIAllowed(JSContext* aCx, JSObject* aGlobal);

void CreateAndDispatchInitWorkerContextRunnable();

already_AddRefed<Runnable> CreateWorkerLoadedRunnable(
const uint64_t aServiceWorkerDescriptorId,
const nsCOMPtr<nsIURI>& aWorkerBaseURI);

already_AddRefed<Runnable> CreateWorkerDestroyedRunnable(
const uint64_t aServiceWorkerDescriptorId,
const nsCOMPtr<nsIURI>& aWorkerBaseURI);
Expand Down

0 comments on commit 60bb3c8

Please sign in to comment.