1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_serviceworkercontainer_h__
8 #define mozilla_dom_serviceworkercontainer_h__
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/ErrorResult.h"
12 #include "mozilla/dom/ServiceWorkerUtils.h"
14 class nsIGlobalWindow
;
16 namespace mozilla::dom
{
18 class ClientPostMessageArgs
;
19 struct MessageEventInit
;
21 struct RegistrationOptions
;
23 class ServiceWorkerContainerChild
;
25 // Lightweight serviceWorker APIs collection.
26 class ServiceWorkerContainer final
: public DOMEventTargetHelper
{
28 NS_DECL_ISUPPORTS_INHERITED
29 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerContainer
,
32 IMPL_EVENT_HANDLER(controllerchange
)
33 IMPL_EVENT_HANDLER(messageerror
)
35 // Almost a manual expansion of IMPL_EVENT_HANDLER(message), but
36 // with the additional StartMessages() when setting the handler, as
37 // required by the spec.
38 inline mozilla::dom::EventHandlerNonNull
* GetOnmessage() {
39 return GetEventHandler(nsGkAtoms::onmessage
);
41 inline void SetOnmessage(mozilla::dom::EventHandlerNonNull
* aCallback
) {
42 SetEventHandler(nsGkAtoms::onmessage
, aCallback
);
46 static bool IsEnabled(JSContext
* aCx
, JSObject
* aGlobal
);
48 static already_AddRefed
<ServiceWorkerContainer
> Create(
49 nsIGlobalObject
* aGlobal
);
51 virtual JSObject
* WrapObject(JSContext
* aCx
,
52 JS::Handle
<JSObject
*> aGivenProto
) override
;
54 already_AddRefed
<Promise
> Register(const nsAString
& aScriptURL
,
55 const RegistrationOptions
& aOptions
,
56 const CallerType aCallerType
,
59 already_AddRefed
<ServiceWorker
> GetController();
61 already_AddRefed
<Promise
> GetRegistration(const nsAString
& aDocumentURL
,
64 already_AddRefed
<Promise
> GetRegistrations(ErrorResult
& aRv
);
68 Promise
* GetReady(ErrorResult
& aRv
);
71 void GetScopeForUrl(const nsAString
& aUrl
, nsString
& aScope
,
74 // DOMEventTargetHelper
75 void DisconnectFromOwner() override
;
77 // Invalidates |mControllerWorker| and dispatches a "controllerchange"
79 void ControllerChanged(ErrorResult
& aRv
);
81 void ReceiveMessage(const ClientPostMessageArgs
& aArgs
);
83 void RevokeActor(ServiceWorkerContainerChild
* aActor
);
86 explicit ServiceWorkerContainer(nsIGlobalObject
* aGlobal
);
88 ~ServiceWorkerContainer();
90 // Utility method to get the global if its present and if certain
91 // additional validaty checks pass. One of these additional checks
92 // verifies the global can access storage. Since storage access can
93 // vary based on user settings we want to often provide some error
94 // message if the storage check fails. This method takes an optional
95 // callback that can be used to report the storage failure to the
97 nsIGlobalObject
* GetGlobalIfValid(
99 const std::function
<void(Document
*)>&& aStorageFailureCB
= nullptr) const;
101 struct ReceivedMessage
;
103 // Dispatch a Runnable that dispatches the given message on this
104 // object. When the owner of this object is a Window, the Runnable
105 // is dispatched on the corresponding TabGroup.
106 void EnqueueReceivedMessageDispatch(RefPtr
<ReceivedMessage
> aMessage
);
108 template <typename F
>
109 void RunWithJSContext(F
&& aCallable
);
111 void DispatchMessage(RefPtr
<ReceivedMessage
> aMessage
);
113 // When it fails, returning boolean means whether it's because deserailization
115 static Result
<Ok
, bool> FillInMessageEventInit(JSContext
* aCx
,
116 nsIGlobalObject
* aGlobal
,
117 ReceivedMessage
& aMessage
,
118 MessageEventInit
& aInit
,
123 RefPtr
<ServiceWorkerContainerChild
> mActor
;
126 // This only changes when a worker hijacks everything in its scope by calling
128 RefPtr
<ServiceWorker
> mControllerWorker
;
130 RefPtr
<Promise
> mReadyPromise
;
131 MozPromiseRequestHolder
<ServiceWorkerRegistrationPromise
> mReadyPromiseHolder
;
133 // Set after StartMessages() has been called.
134 bool mMessagesStarted
= false;
136 // Queue holding messages posted from service worker as long as
137 // StartMessages() hasn't been called.
138 nsTArray
<RefPtr
<ReceivedMessage
>> mPendingMessages
;
141 } // namespace mozilla::dom
143 #endif /* mozilla_dom_serviceworkercontainer_h__ */