forked from mirrors/gecko-dev
Excerpting some documentation I wrote up for an OVERVIEW.md for Service Workers for this exact use situation below. The basic situation of this patch is that we were trying to create the FetchEventOpProxyParent immediately before the managing PRemoteWorker instance existed, which isn't a thing we can do. Because FetchEvent ops are more complicated they require somewhat more unique handling, but it should have been unified with the PendingOp infrastructure and was not. The one notable thing going on actor-wise is that the request body (if present) requires special RemoteLazyInputStream serialization and this is something that can only be done once we have the RemoteWorkerParent. See https://phabricator.services.mozilla.com/D73173 and its commit message and my "Restating" blocks for more context. ### Threads and Proxies #### Main Thread ServiceWorkerManager's authoritative ServiceWorker state lives on the main thread in the parent process. This is due to a combination of legacy factors and that currently the nsHttpChannel AsyncOpen logic where navigation interception occurs must be on the main thread. #### IPDL Background Thread The IPDL Background Thread is the thread where PBackground parent actors are created. Because IPDL actors are explicitly tied to the thread they are created on and PBackground is the only top-level protocol created for use by DOM Workers, this thread is the natural home for book-keeping and authoritative state for APIs that are accessed via PBackground-managed protocols. For example, IndexedDB and other QuotaManager-managed storage APIs. The Remote Worker APIs are all PBackground managed and so all messages sent via the Remote Worker API need to be sent from the IPDL Background thread. #### Main Thread to IPDL Background Proxies There are 2 ways to get data from the main thread to the IPDL Background thread: either via direct runnable dispatch or via IPDL IPC. We use IPDL IPC (which has optimizations for same-process communication). The following interfaces exist exclusively to proxy requests from the ServiceWorkerManager on the main thread to the IPDL Background thread. - `PRemoteWorkerController` is a proxy wrapper around the `RemoteWorkerController` API exposed on the IPDL Background thread. - `PFetchEventOp` is paired with `PFetchEventOpProxy` managed by the above `PRemoteWorkerController`. `PFetchEventOp` gets the data to the IPDL Background thread from the main thread, then `PFetchEventOpProxy` gets the data down to the content process. ## Non-Fetch ServiceWorker events AKA ExtendableEvents How non-fetch events are dispatched to the serviceworker (including the IPC). Because ServiceWorkers are intended to be shutdown and restarted on demand and most event processing is asynchronous, there needs to be a way to track outstanding requests and for content logic to indicate when it is done processing requests. `ExtendableEvent`s are the mechanism for this. A method `waitUntil(promise)` adds promises to be track as long as the event is still "active". This straightforward lifecycle means that these events map well to our IPC async return value mechanism. This is used by `PRemoteWorker::ExecServiceWorkerOp`. ## Fetch events and FetchEvent.respondWith() `FetchEvent`s have a different lifecycle and dataflow than regular `ExtendableEvents`. They expose a `respondWith()` method that will eventually resolve with a fetch `Response` object that potentially needs to be propagated before the ExtendableEvent is no longer active. (The response will never be propagated after `waitUntil()` settles because every call to `respondWith()` implicitly calls `waitUntil()`.) From an IPC perspective, this means that `FetchEvent` instances need their own IPC actor rather than being able to depend on the async return value mechanism of IPDL. That's why `PFetchEventOpProxy` exists and is managed by `PRemoteWorker`. Differential Revision: https://phabricator.services.mozilla.com/D92021
305 lines
10 KiB
C++
305 lines
10 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#ifndef mozilla_dom_RemoteWorkerController_h
|
|
#define mozilla_dom_RemoteWorkerController_h
|
|
|
|
#include "nsISupportsImpl.h"
|
|
#include "nsTArray.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
#include "mozilla/UniquePtr.h"
|
|
#include "mozilla/dom/DOMTypes.h"
|
|
#include "mozilla/dom/ServiceWorkerOpArgs.h"
|
|
#include "mozilla/dom/ServiceWorkerOpPromise.h"
|
|
|
|
namespace mozilla {
|
|
namespace dom {
|
|
|
|
/* Here's a graph about this remote workers are spawned.
|
|
*
|
|
* _________________________________ | ________________________________
|
|
* | | | | |
|
|
* | Parent process | IPC | Creation of Process X |
|
|
* | PBackground thread | | | |
|
|
* | | | | [RemoteWorkerService::Init()] |
|
|
* | | | | | |
|
|
* | | | | | (1) |
|
|
* | [RemoteWorkerManager:: (2) | | | V |
|
|
* | RegisterActor()]<-------- [new RemoteWorkerServiceChild] |
|
|
* | | | | |
|
|
* | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | |________________________________|
|
|
* | | |
|
|
* | new SharedWorker/ServiceWorker | |
|
|
* | | ^ | IPC
|
|
* | (3) | (4)| |
|
|
* | V | | |
|
|
* | [RemoteWorkerController:: | |
|
|
* | | Create(data)] | |
|
|
* | | (5) | |
|
|
* | V | |
|
|
* | [RemoteWorkerManager::Launch()] | |
|
|
* | | | IPC _____________________________
|
|
* | | (6) | | | |
|
|
* | | | | Selected content process |
|
|
* | V | (7) | |
|
|
* | [SendPRemoteWorkerConstructor()]--------->[new RemoteWorkerChild()] |
|
|
* | | | | | | |
|
|
* | | (8) | | | | |
|
|
* | V | | | V |
|
|
* | [RemoteWorkerController-> | | | RemoteWorkerChild->Exec() |
|
|
* | | SetControllerActor()] | | |_____________________________|
|
|
* | (9) | | IPC
|
|
* | V | |
|
|
* | [RemoteWorkerObserver-> | |
|
|
* | CreationCompleted()] | |
|
|
* |_________________________________| |
|
|
* |
|
|
*
|
|
* 1. When a new process starts, it creates a RemoteWorkerService singleton.
|
|
* This service creates a new thread (Worker Launcher) and from there, it
|
|
* starts a PBackground RemoteWorkerServiceChild actor.
|
|
* 2. On the parent process, PBackground thread, RemoteWorkerServiceParent
|
|
* actors are registered into the RemoteWorkerManager service.
|
|
*
|
|
* 3. At some point, a SharedWorker or a ServiceWorker must be executed.
|
|
* RemoteWorkerController::Create() is used to start the launching. This
|
|
* method must be called on the parent process, on the PBackground thread.
|
|
* 4. RemoteWorkerController object is immediately returned to the caller. Any
|
|
* operation done with this controller object will be stored in a queue,
|
|
* until the launching is correctly executed.
|
|
* 5. RemoteWorkerManager has the list of active RemoteWorkerServiceParent
|
|
* actors. From them, it picks one.
|
|
* In case we don't have any content process to select, a new one is
|
|
* spawned. If this happens, the operation is suspended until a new
|
|
* RemoteWorkerServiceParent is registered.
|
|
* 6. RemoteWorkerServiceParent is used to create a RemoteWorkerParent.
|
|
* 7. RemoteWorkerChild is created on a selected process and it executes the
|
|
* WorkerPrivate.
|
|
* 8. The RemoteWorkerParent actor is passed to the RemoteWorkerController.
|
|
* 9. RemoteWorkerController now is ready to continue and it called
|
|
* RemoteWorkerObserver to inform that the operation is completed.
|
|
* In case there were pending operations, they are now executed.
|
|
*/
|
|
|
|
class ErrorValue;
|
|
class FetchEventOpParent;
|
|
class RemoteWorkerControllerParent;
|
|
class RemoteWorkerData;
|
|
class RemoteWorkerManager;
|
|
class RemoteWorkerParent;
|
|
|
|
class RemoteWorkerObserver {
|
|
public:
|
|
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
|
|
|
|
virtual void CreationFailed() = 0;
|
|
|
|
virtual void CreationSucceeded() = 0;
|
|
|
|
virtual void ErrorReceived(const ErrorValue& aValue) = 0;
|
|
|
|
virtual void Terminated() = 0;
|
|
};
|
|
|
|
class RemoteWorkerController final {
|
|
friend class RemoteWorkerControllerParent;
|
|
friend class RemoteWorkerManager;
|
|
friend class RemoteWorkerParent;
|
|
|
|
public:
|
|
NS_INLINE_DECL_REFCOUNTING(RemoteWorkerController)
|
|
|
|
static already_AddRefed<RemoteWorkerController> Create(
|
|
const RemoteWorkerData& aData, RemoteWorkerObserver* aObserver,
|
|
base::ProcessId = 0);
|
|
|
|
void AddWindowID(uint64_t aWindowID);
|
|
|
|
void RemoveWindowID(uint64_t aWindowID);
|
|
|
|
void AddPortIdentifier(const MessagePortIdentifier& aPortIdentifier);
|
|
|
|
void Terminate();
|
|
|
|
void Suspend();
|
|
|
|
void Resume();
|
|
|
|
void Freeze();
|
|
|
|
void Thaw();
|
|
|
|
RefPtr<ServiceWorkerOpPromise> ExecServiceWorkerOp(
|
|
ServiceWorkerOpArgs&& aArgs);
|
|
|
|
RefPtr<ServiceWorkerFetchEventOpPromise> ExecServiceWorkerFetchEventOp(
|
|
const ServiceWorkerFetchEventOpArgs& aArgs,
|
|
RefPtr<FetchEventOpParent> aReal);
|
|
|
|
RefPtr<GenericPromise> SetServiceWorkerSkipWaitingFlag() const;
|
|
|
|
bool IsTerminated() const;
|
|
|
|
private:
|
|
RemoteWorkerController(const RemoteWorkerData& aData,
|
|
RemoteWorkerObserver* aObserver);
|
|
|
|
~RemoteWorkerController();
|
|
|
|
void SetWorkerActor(RemoteWorkerParent* aActor);
|
|
|
|
void NoteDeadWorkerActor();
|
|
|
|
void ErrorPropagation(const ErrorValue& aValue);
|
|
|
|
void WorkerTerminated();
|
|
|
|
void Shutdown();
|
|
|
|
void CreationFailed();
|
|
|
|
void CreationSucceeded();
|
|
|
|
void CancelAllPendingOps();
|
|
|
|
template <typename... Args>
|
|
void MaybeStartSharedWorkerOp(Args&&... aArgs);
|
|
|
|
void NoteDeadWorker();
|
|
|
|
RefPtr<RemoteWorkerObserver> mObserver;
|
|
RefPtr<RemoteWorkerParent> mActor;
|
|
|
|
enum {
|
|
ePending,
|
|
eReady,
|
|
eTerminated,
|
|
} mState;
|
|
|
|
const bool mIsServiceWorker;
|
|
|
|
/**
|
|
* `PendingOp` is responsible for encapsulating logic for starting and
|
|
* canceling pending remote worker operations, as this logic may vary
|
|
* depending on the type of the remote worker and the type of the operation.
|
|
*/
|
|
class PendingOp {
|
|
public:
|
|
PendingOp() = default;
|
|
|
|
PendingOp(const PendingOp&) = delete;
|
|
|
|
PendingOp& operator=(const PendingOp&) = delete;
|
|
|
|
virtual ~PendingOp() = default;
|
|
|
|
/**
|
|
* Returns `true` if execution has started or the operation is moot and
|
|
* doesn't need to be queued, `false` if execution hasn't started and the
|
|
* operation should be queued. In general, operations should only return
|
|
* false when a remote worker is first starting up. Operations may also
|
|
* somewhat non-intuitively return true without doing anything if the worker
|
|
* has already been told to shutdown.
|
|
*
|
|
* Starting execution may depend the state of `aOwner.`
|
|
*/
|
|
virtual bool MaybeStart(RemoteWorkerController* const aOwner) = 0;
|
|
|
|
/**
|
|
* Invoked if the operation will never have MaybeStart() called again
|
|
* because the RemoteWorkerController has terminated (or will never start).
|
|
* This should be used by PendingOps to clean up any resources they own and
|
|
* may also be called internally by their MaybeStart() methods if they
|
|
* determine the worker has been terminated. This should be idempotent.
|
|
*/
|
|
virtual void Cancel() = 0;
|
|
};
|
|
|
|
class PendingSharedWorkerOp final : public PendingOp {
|
|
public:
|
|
enum Type {
|
|
eTerminate,
|
|
eSuspend,
|
|
eResume,
|
|
eFreeze,
|
|
eThaw,
|
|
ePortIdentifier,
|
|
eAddWindowID,
|
|
eRemoveWindowID,
|
|
};
|
|
|
|
explicit PendingSharedWorkerOp(Type aType, uint64_t aWindowID = 0);
|
|
|
|
explicit PendingSharedWorkerOp(
|
|
const MessagePortIdentifier& aPortIdentifier);
|
|
|
|
~PendingSharedWorkerOp();
|
|
|
|
bool MaybeStart(RemoteWorkerController* const aOwner) override;
|
|
|
|
void Cancel() override;
|
|
|
|
private:
|
|
const Type mType;
|
|
const MessagePortIdentifier mPortIdentifier;
|
|
const uint64_t mWindowID = 0;
|
|
bool mCompleted = false;
|
|
};
|
|
|
|
class PendingServiceWorkerOp final : public PendingOp {
|
|
public:
|
|
PendingServiceWorkerOp(ServiceWorkerOpArgs&& aArgs,
|
|
RefPtr<ServiceWorkerOpPromise::Private> aPromise);
|
|
|
|
~PendingServiceWorkerOp();
|
|
|
|
bool MaybeStart(RemoteWorkerController* const aOwner) override;
|
|
|
|
void Cancel() override;
|
|
|
|
private:
|
|
ServiceWorkerOpArgs mArgs;
|
|
RefPtr<ServiceWorkerOpPromise::Private> mPromise;
|
|
};
|
|
|
|
/**
|
|
* Custom pending op type to deal with the complexities of FetchEvents having
|
|
* their own actor.
|
|
*
|
|
* FetchEvent Ops have their own actor type because their lifecycle is more
|
|
* complex than IPDL's async return value mechanism allows. Additionally,
|
|
* its IPC struct potentially has to serialize RemoteLazyStreams which
|
|
* requires us to hold an nsIInputStream when at rest and serialize it when
|
|
* eventually sending.
|
|
*/
|
|
class PendingSWFetchEventOp final : public PendingOp {
|
|
public:
|
|
PendingSWFetchEventOp(
|
|
const ServiceWorkerFetchEventOpArgs& aArgs,
|
|
RefPtr<ServiceWorkerFetchEventOpPromise::Private> aPromise,
|
|
RefPtr<FetchEventOpParent>&& aReal);
|
|
|
|
~PendingSWFetchEventOp();
|
|
|
|
bool MaybeStart(RemoteWorkerController* const aOwner) override;
|
|
|
|
void Cancel() override;
|
|
|
|
private:
|
|
ServiceWorkerFetchEventOpArgs mArgs;
|
|
RefPtr<ServiceWorkerFetchEventOpPromise::Private> mPromise;
|
|
RefPtr<FetchEventOpParent> mReal;
|
|
nsCOMPtr<nsIInputStream> mBodyStream;
|
|
};
|
|
|
|
nsTArray<UniquePtr<PendingOp>> mPendingOps;
|
|
};
|
|
|
|
} // namespace dom
|
|
} // namespace mozilla
|
|
|
|
#endif // mozilla_dom_RemoteWorkerController_h
|