Skip to content

Commit

Permalink
Bug 1728326 - Add a new RequestInitWorkerRunnable to call mozIExtensi…
Browse files Browse the repository at this point in the history
…onAPIRequestHandler initExtensionWorker. r=baku

This patch introduce a new RequestInitWorkerRunnable that calls a new mozIExtensionAPIRequestHandler.initExtensionWorker
method, and a CreateAndDispatchInitWorkerRunnable helper function which will be used to send
this mozIExtensionAPIRequest from the worker thread to the main thread when the background
service worker is ready for being executed (but before its main scripts has been evaluated
in the newly created service worker global scope).

Differential Revision: https://phabricator.services.mozilla.com/D124699
  • Loading branch information
rpl committed Nov 5, 2021
1 parent 3ef8b55 commit d300da9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions toolkit/components/extensions/mozIExtensionAPIRequestHandling.idl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ interface mozIExtensionAPIRequestHandler : nsISupports
in mozIExtensionAPIRequest apiRequest,
[optional, retval] out mozIExtensionAPIRequestResult apiRequestResult);

/**
* A method called when an extension background service worker is initialized and
* ready to execute its main script.
*
* @param extension An instance of the WebExtensionPolicy webidl interface.
* @param serviceWorkerInfo
*/
void initExtensionWorker(in nsISupports extension,
in mozIExtensionServiceWorkerInfo serviceWorkerInfo);

/**
* 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 @@ -627,6 +627,38 @@ void RequestWorkerRunnable::ReadResult(JSContext* aCx,
aRv.Throw(NS_ERROR_UNEXPECTED);
}

// RequestInitWorkerContextRunnable

RequestInitWorkerRunnable::RequestInitWorkerRunnable(
dom::WorkerPrivate* aWorkerPrivate, Maybe<dom::ClientInfo>& aSWClientInfo)
: WorkerMainThreadRunnable(aWorkerPrivate,
"extensions::RequestInitWorkerRunnable"_ns) {
MOZ_ASSERT(dom::IsCurrentThreadRunningWorker());
MOZ_ASSERT(aSWClientInfo.isSome());
mClientInfo = aSWClientInfo;
}

bool RequestInitWorkerRunnable::MainThreadRun() {
MOZ_ASSERT(NS_IsMainThread());

auto* baseURI = mWorkerPrivate->GetBaseURI();
RefPtr<WebExtensionPolicy> policy =
ExtensionPolicyService::GetSingleton().GetByURL(baseURI);

RefPtr<ExtensionServiceWorkerInfo> swInfo = new ExtensionServiceWorkerInfo(
*mClientInfo, mWorkerPrivate->ServiceWorkerID());

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

if (NS_FAILED(handler->InitExtensionWorker(policy, swInfo))) {
NS_WARNING("nsIExtensionAPIRequestHandler.initExtensionWorker call failed");
}

return true;
}

// NotifyWorkerDestroyedRunnable

nsresult NotifyWorkerDestroyedRunnable::Run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ class RequestWorkerRunnable : public dom::WorkerMainThreadRunnable {
ExtensionAPIRequestForwarder* mOuterRequest;
};

class RequestInitWorkerRunnable : public dom::WorkerMainThreadRunnable {
Maybe<dom::ClientInfo> mClientInfo;

public:
RequestInitWorkerRunnable(dom::WorkerPrivate* aWorkerPrivate,
Maybe<dom::ClientInfo>& aSWClientInfo);
bool MainThreadRun() override;
};

class NotifyWorkerDestroyedRunnable : public Runnable {
uint64_t mSWDescriptorId;
nsCOMPtr<nsIURI> mSWBaseURI;
Expand Down
29 changes: 29 additions & 0 deletions toolkit/components/extensions/webidl-api/ExtensionBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ bool ExtensionAPIAllowed(JSContext* aCx, JSObject* aGlobal) {
#endif
}

void CreateAndDispatchInitWorkerContextRunnable() {
MOZ_ASSERT(dom::IsCurrentThreadRunningWorker());
// DO NOT pass this WorkerPrivate raw pointer to anything else but the
// RequestInitWorkerRunnable (which extends dom::WorkerMainThreadRunnable).
dom::WorkerPrivate* workerPrivate = dom::GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
MOZ_ASSERT(workerPrivate->ExtensionAPIAllowed());
MOZ_ASSERT(workerPrivate->IsServiceWorker());
workerPrivate->AssertIsOnWorkerThread();

auto* workerScope = workerPrivate->GlobalScope();
if (NS_WARN_IF(!workerScope)) {
return;
}

Maybe<dom::ClientInfo> clientInfo = workerScope->GetClientInfo();
if (NS_WARN_IF(clientInfo.isNothing())) {
return;
}

RefPtr<RequestInitWorkerRunnable> runnable =
new RequestInitWorkerRunnable(std::move(workerPrivate), clientInfo);
IgnoredErrorResult rv;
runnable->Dispatch(dom::WorkerStatus::Canceling, rv);
if (rv.Failed()) {
NS_WARNING("Failed to dispatch extensions::RequestInitWorkerRunnable");
}
}

already_AddRefed<Runnable> CreateWorkerDestroyedRunnable(
const uint64_t aServiceWorkerDescriptorId,
const nsCOMPtr<nsIURI>& aWorkerBaseURI) {
Expand Down
2 changes: 2 additions & 0 deletions toolkit/components/extensions/webidl-api/ExtensionBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ExtensionTest;

bool ExtensionAPIAllowed(JSContext* aCx, JSObject* aGlobal);

void CreateAndDispatchInitWorkerContextRunnable();

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

0 comments on commit d300da9

Please sign in to comment.