2015-10-01 02:11:03 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
#ifndef mozilla_dom_serviceworkerprivate_h
|
|
|
|
#define mozilla_dom_serviceworkerprivate_h
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2020-03-28 16:57:20 +03:00
|
|
|
#include <type_traits>
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2019-07-26 21:41:48 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2016-04-20 08:04:09 +03:00
|
|
|
#define NOTIFICATION_CLICK_EVENT_NAME u"notificationclick"
|
|
|
|
#define NOTIFICATION_CLOSE_EVENT_NAME u"notificationclose"
|
|
|
|
|
2016-03-16 21:51:11 +03:00
|
|
|
class nsIInterceptedChannel;
|
2018-07-10 02:02:39 +03:00
|
|
|
class nsIWorkerDebugger;
|
2016-03-16 21:51:11 +03:00
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
namespace mozilla {
|
2018-05-14 16:02:15 +03:00
|
|
|
|
|
|
|
class JSObjectHolder;
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
namespace dom {
|
2017-12-12 23:44:47 +03:00
|
|
|
|
|
|
|
class ClientInfoAndState;
|
2018-06-25 17:03:18 +03:00
|
|
|
class ServiceWorkerCloneData;
|
2015-10-01 02:11:03 +03:00
|
|
|
class ServiceWorkerInfo;
|
2019-08-15 20:27:07 +03:00
|
|
|
class ServiceWorkerPrivate;
|
2019-08-15 20:27:23 +03:00
|
|
|
class ServiceWorkerPrivateImpl;
|
2016-03-16 21:51:11 +03:00
|
|
|
class ServiceWorkerRegistrationInfo;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2018-05-14 16:02:15 +03:00
|
|
|
namespace ipc {
|
|
|
|
class StructuredCloneData;
|
|
|
|
} // namespace ipc
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class LifeCycleEventCallback : public Runnable {
|
2015-10-01 02:11:03 +03:00
|
|
|
public:
|
2018-01-27 00:08:59 +03:00
|
|
|
LifeCycleEventCallback() : Runnable("dom::LifeCycleEventCallback") {}
|
2017-06-12 22:34:10 +03:00
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
// Called on the worker thread.
|
|
|
|
virtual void SetResult(bool aResult) = 0;
|
|
|
|
};
|
|
|
|
|
2019-08-15 20:27:07 +03:00
|
|
|
// Used to keep track of pending waitUntil as well as in-flight extendable
|
|
|
|
// events. When the last token is released, we attempt to terminate the worker.
|
|
|
|
class KeepAliveToken final : public nsISupports {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
explicit KeepAliveToken(ServiceWorkerPrivate* aPrivate);
|
|
|
|
|
|
|
|
private:
|
|
|
|
~KeepAliveToken();
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerPrivate> mPrivate;
|
|
|
|
};
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
// ServiceWorkerPrivate is a wrapper for managing the on-demand aspect of
|
|
|
|
// service workers. It handles all event dispatching to the worker and ensures
|
|
|
|
// the worker thread is running when needed.
|
|
|
|
//
|
2015-10-01 02:11:03 +03:00
|
|
|
// Lifetime management: To spin up the worker thread we own a |WorkerPrivate|
|
|
|
|
// object which can be cancelled if no events are received for a certain
|
|
|
|
// amount of time. The worker is kept alive by holding a |KeepAliveToken|
|
|
|
|
// reference.
|
2015-10-01 02:11:03 +03:00
|
|
|
//
|
2015-10-01 02:11:03 +03:00
|
|
|
// Extendable events hold tokens for the duration of their handler execution
|
|
|
|
// and until their waitUntil promise is resolved, while ServiceWorkerPrivate
|
2015-10-01 02:11:03 +03:00
|
|
|
// will hold a token for |dom.serviceWorkers.idle_timeout| seconds after each
|
2015-10-01 02:11:03 +03:00
|
|
|
// new event.
|
|
|
|
//
|
|
|
|
// Note: All timer events must be handled on the main thread because the
|
|
|
|
// worker may block indefinitely the worker thread (e. g. infinite loop in the
|
|
|
|
// script).
|
|
|
|
//
|
|
|
|
// There are 3 cases where we may ignore keep alive tokens:
|
|
|
|
// 1. When ServiceWorkerPrivate's token expired, if there are still waitUntil
|
2015-10-01 02:11:03 +03:00
|
|
|
// handlers holding tokens, we wait another
|
|
|
|
// |dom.serviceWorkers.idle_extended_timeout| seconds before forcibly
|
2015-10-01 02:11:03 +03:00
|
|
|
// terminating the worker.
|
|
|
|
// 2. If the worker stopped controlling documents and it is not handling push
|
|
|
|
// events.
|
|
|
|
// 3. The content process is shutting down.
|
2015-10-01 02:11:03 +03:00
|
|
|
//
|
|
|
|
// Adding an API function for a new event requires calling |SpawnWorkerIfNeeded|
|
2015-10-01 02:11:03 +03:00
|
|
|
// with an appropriate reason before any runnable is dispatched to the worker.
|
|
|
|
// If the event is extendable then the runnable should inherit
|
|
|
|
// ExtendableEventWorkerRunnable.
|
2017-01-31 17:21:52 +03:00
|
|
|
class ServiceWorkerPrivate final {
|
2015-10-01 02:11:03 +03:00
|
|
|
friend class KeepAliveToken;
|
2019-08-15 20:27:23 +03:00
|
|
|
friend class ServiceWorkerPrivateImpl;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
public:
|
2017-01-31 17:21:52 +03:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef();
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release();
|
2018-06-25 17:03:18 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(ServiceWorkerPrivate)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2020-03-28 16:57:20 +03:00
|
|
|
using HasThreadSafeRefCnt = std::false_type;
|
2017-01-31 17:21:52 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCycleCollectingAutoRefCnt mRefCnt;
|
|
|
|
NS_DECL_OWNINGTHREAD
|
|
|
|
|
|
|
|
public:
|
2019-10-30 20:12:10 +03:00
|
|
|
// TODO: remove this class. There's one (and only should be one) concrete
|
|
|
|
// class that derives this abstract base class.
|
2019-08-15 20:27:23 +03:00
|
|
|
class Inner {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
|
|
|
|
|
|
|
|
virtual nsresult SendMessageEvent(
|
|
|
|
RefPtr<ServiceWorkerCloneData>&& aData,
|
|
|
|
const ClientInfoAndState& aClientInfoAndState) = 0;
|
|
|
|
|
|
|
|
virtual nsresult CheckScriptEvaluation(
|
|
|
|
RefPtr<LifeCycleEventCallback> aScriptEvaluationCallback) = 0;
|
|
|
|
|
|
|
|
virtual nsresult SendLifeCycleEvent(
|
|
|
|
const nsAString& aEventName,
|
|
|
|
RefPtr<LifeCycleEventCallback> aCallback) = 0;
|
|
|
|
|
|
|
|
virtual nsresult SendPushEvent(
|
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> aRegistration,
|
|
|
|
const nsAString& aMessageId, const Maybe<nsTArray<uint8_t>>& aData) = 0;
|
|
|
|
|
|
|
|
virtual nsresult SendPushSubscriptionChangeEvent() = 0;
|
|
|
|
|
|
|
|
virtual nsresult SendNotificationEvent(
|
|
|
|
const nsAString& aEventName, const nsAString& aID,
|
|
|
|
const nsAString& aTitle, const nsAString& aDir, const nsAString& aLang,
|
|
|
|
const nsAString& aBody, const nsAString& aTag, const nsAString& aIcon,
|
|
|
|
const nsAString& aData, const nsAString& aBehavior,
|
|
|
|
const nsAString& aScope, uint32_t aDisableOpenClickDelay) = 0;
|
|
|
|
|
|
|
|
virtual nsresult SendFetchEvent(
|
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> aRegistration,
|
|
|
|
nsCOMPtr<nsIInterceptedChannel> aChannel, const nsAString& aClientId,
|
2020-01-25 03:05:12 +03:00
|
|
|
const nsAString& aResultingClientId) = 0;
|
2019-08-15 20:27:23 +03:00
|
|
|
|
2019-08-20 18:21:11 +03:00
|
|
|
virtual nsresult SpawnWorkerIfNeeded() = 0;
|
|
|
|
|
2019-08-15 20:27:23 +03:00
|
|
|
virtual void TerminateWorker() = 0;
|
|
|
|
|
|
|
|
virtual void UpdateState(ServiceWorkerState aState) = 0;
|
|
|
|
|
|
|
|
virtual void NoteDeadOuter() = 0;
|
|
|
|
|
|
|
|
virtual bool WorkerIsDead() const = 0;
|
|
|
|
};
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
explicit ServiceWorkerPrivate(ServiceWorkerInfo* aInfo);
|
|
|
|
|
2018-06-25 17:03:18 +03:00
|
|
|
nsresult SendMessageEvent(RefPtr<ServiceWorkerCloneData>&& aData,
|
2017-12-12 23:44:47 +03:00
|
|
|
const ClientInfoAndState& aClientInfoAndState);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
// This is used to validate the worker script and continue the installation
|
2015-11-26 20:03:10 +03:00
|
|
|
// process.
|
|
|
|
nsresult CheckScriptEvaluation(LifeCycleEventCallback* aCallback);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
nsresult SendLifeCycleEvent(const nsAString& aEventType,
|
2018-04-02 17:50:23 +03:00
|
|
|
LifeCycleEventCallback* aCallback);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-03-28 21:50:39 +03:00
|
|
|
nsresult SendPushEvent(const nsAString& aMessageId,
|
|
|
|
const Maybe<nsTArray<uint8_t>>& aData,
|
2015-10-26 05:59:48 +03:00
|
|
|
ServiceWorkerRegistrationInfo* aRegistration);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
nsresult SendPushSubscriptionChangeEvent();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-04-20 08:04:09 +03:00
|
|
|
nsresult SendNotificationEvent(const nsAString& aEventName,
|
|
|
|
const nsAString& aID, const nsAString& aTitle,
|
|
|
|
const nsAString& aDir, const nsAString& aLang,
|
|
|
|
const nsAString& aBody, const nsAString& aTag,
|
|
|
|
const nsAString& aIcon, const nsAString& aData,
|
|
|
|
const nsAString& aBehavior,
|
|
|
|
const nsAString& aScope);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-15 11:28:23 +03:00
|
|
|
nsresult SendFetchEvent(nsIInterceptedChannel* aChannel,
|
|
|
|
nsILoadGroup* aLoadGroup, const nsAString& aClientId,
|
2020-01-25 03:05:12 +03:00
|
|
|
const nsAString& aResultingClientId);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-22 14:45:00 +03:00
|
|
|
bool MaybeStoreISupports(nsISupports* aSupports);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-24 15:16:23 +03:00
|
|
|
void RemoveISupports(nsISupports* aSupports);
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
// This will terminate the current running worker thread and drop the
|
|
|
|
// workerPrivate reference.
|
|
|
|
// Called by ServiceWorkerInfo when [[Clear Registration]] is invoked
|
|
|
|
// or whenever the spec mandates that we terminate the worker.
|
|
|
|
// This is a no-op if the worker has already been stopped.
|
|
|
|
void TerminateWorker();
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
void NoteDeadServiceWorkerInfo();
|
|
|
|
|
|
|
|
void NoteStoppedControllingDocuments();
|
|
|
|
|
2017-10-30 20:35:07 +03:00
|
|
|
void UpdateState(ServiceWorkerState aState);
|
2015-11-24 18:47:59 +03:00
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
nsresult GetDebugger(nsIWorkerDebugger** aResult);
|
|
|
|
|
|
|
|
nsresult AttachDebugger();
|
|
|
|
|
|
|
|
nsresult DetachDebugger();
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
bool IsIdle() const;
|
|
|
|
|
2019-10-30 20:12:10 +03:00
|
|
|
// This promise is used schedule clearing of the owning registrations and its
|
|
|
|
// associated Service Workers if that registration becomes "unreachable" by
|
|
|
|
// the ServiceWorkerManager. This occurs under two conditions, which are the
|
|
|
|
// preconditions to calling this method:
|
|
|
|
// - The owning registration must be unregistered.
|
|
|
|
// - The associated Service Worker must *not* be controlling clients.
|
|
|
|
//
|
|
|
|
// Additionally, perhaps stating the obvious, the associated Service Worker
|
|
|
|
// must *not* be idle (whatever must be done "when idle" can just be done
|
|
|
|
// immediately).
|
2019-07-26 21:41:48 +03:00
|
|
|
RefPtr<GenericPromise> GetIdlePromise();
|
|
|
|
|
2016-12-19 05:38:53 +03:00
|
|
|
void SetHandlesFetch(bool aValue);
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
private:
|
|
|
|
enum WakeUpReason {
|
|
|
|
FetchEvent = 0,
|
|
|
|
PushEvent,
|
|
|
|
PushSubscriptionChangeEvent,
|
|
|
|
MessageEvent,
|
|
|
|
NotificationClickEvent,
|
2016-04-20 08:04:09 +03:00
|
|
|
NotificationCloseEvent,
|
2015-11-26 14:18:56 +03:00
|
|
|
LifeCycleEvent,
|
2019-08-15 20:27:23 +03:00
|
|
|
AttachEvent,
|
|
|
|
Unknown
|
2015-10-01 02:11:03 +03:00
|
|
|
};
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
// Timer callbacks
|
2017-02-14 16:49:44 +03:00
|
|
|
void NoteIdleWorkerCallback(nsITimer* aTimer);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2017-02-14 16:49:44 +03:00
|
|
|
void TerminateWorkerCallback(nsITimer* aTimer);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
void RenewKeepAliveToken(WakeUpReason aWhy);
|
|
|
|
|
|
|
|
void ResetIdleTimeout();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
void AddToken();
|
|
|
|
|
|
|
|
void ReleaseToken();
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
nsresult SpawnWorkerIfNeeded(WakeUpReason aWhy,
|
2017-04-10 16:13:21 +03:00
|
|
|
bool* aNewWorkerCreated = nullptr,
|
2015-10-01 02:11:03 +03:00
|
|
|
nsILoadGroup* aLoadGroup = nullptr);
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
~ServiceWorkerPrivate();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
already_AddRefed<KeepAliveToken> CreateEventKeepAliveToken();
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
// The info object owns us. It is possible to outlive it for a brief period
|
|
|
|
// of time if there are pending waitUntil promises, in which case it
|
|
|
|
// will be null and |SpawnWorkerIfNeeded| will always fail.
|
2015-10-01 02:11:03 +03:00
|
|
|
ServiceWorkerInfo* MOZ_NON_OWNING_REF mInfo;
|
|
|
|
|
|
|
|
// The WorkerPrivate object can only be closed by this class or by the
|
|
|
|
// RuntimeService class if gecko is shutting down. Closing the worker
|
|
|
|
// multiple times is OK, since the second attempt will be a no-op.
|
2018-01-31 10:24:08 +03:00
|
|
|
RefPtr<WorkerPrivate> mWorkerPrivate;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mIdleWorkerTimer;
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
// We keep a token for |dom.serviceWorkers.idle_timeout| seconds to give the
|
2015-10-01 02:11:03 +03:00
|
|
|
// worker a grace period after each event.
|
2016-07-28 03:36:10 +03:00
|
|
|
RefPtr<KeepAliveToken> mIdleKeepAliveToken;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
uint64_t mDebuggerCount;
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
uint64_t mTokenCount;
|
2015-10-24 15:16:23 +03:00
|
|
|
|
|
|
|
// Meant for keeping objects alive while handling requests from the worker
|
|
|
|
// on the main thread. Access to this array is provided through
|
|
|
|
// |StoreISupports| and |RemoveISupports|. Note that the array is also
|
|
|
|
// cleared whenever the worker is terminated.
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>> mSupportsArray;
|
2015-11-24 18:47:59 +03:00
|
|
|
|
|
|
|
// Array of function event worker runnables that are pending due to
|
|
|
|
// the worker activating. Main thread only.
|
2018-01-31 10:22:56 +03:00
|
|
|
nsTArray<RefPtr<WorkerRunnable>> mPendingFunctionalEvents;
|
2019-07-26 21:41:48 +03:00
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
RefPtr<Inner> mInner;
|
|
|
|
|
2019-07-26 21:41:48 +03:00
|
|
|
// Used by the owning `ServiceWorkerRegistrationInfo` when it wants to call
|
|
|
|
// `Clear` after being unregistered and isn't controlling any clients but this
|
|
|
|
// worker (i.e. the registration's active worker) isn't idle yet. Note that
|
|
|
|
// such an event should happen at most once in a
|
|
|
|
// `ServiceWorkerRegistrationInfo`s lifetime, so this promise should also only
|
|
|
|
// be obtained at most once.
|
|
|
|
MozPromiseHolder<GenericPromise> mIdlePromiseHolder;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool mIdlePromiseObtained = false;
|
|
|
|
#endif
|
2015-10-01 02:11:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
#endif // mozilla_dom_serviceworkerprivate_h
|