Bug 1842509 - Remove media.webvtt.regions.enabled pref r=alwu,webidl,smaug,peterv
[gecko.git] / dom / serviceworkers / ServiceWorkerContainer.h
blob3a5dd5fa5d6a3832fcee657d6e1dd47b160f096c
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;
20 class Promise;
21 struct RegistrationOptions;
22 class ServiceWorker;
23 class ServiceWorkerContainerChild;
25 // Lightweight serviceWorker APIs collection.
26 class ServiceWorkerContainer final : public DOMEventTargetHelper {
27 public:
28 NS_DECL_ISUPPORTS_INHERITED
29 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerContainer,
30 DOMEventTargetHelper)
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);
43 StartMessages();
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,
57 ErrorResult& aRv);
59 already_AddRefed<ServiceWorker> GetController();
61 already_AddRefed<Promise> GetRegistration(const nsAString& aDocumentURL,
62 ErrorResult& aRv);
64 already_AddRefed<Promise> GetRegistrations(ErrorResult& aRv);
66 void StartMessages();
68 Promise* GetReady(ErrorResult& aRv);
70 // Testing only.
71 void GetScopeForUrl(const nsAString& aUrl, nsString& aScope,
72 ErrorResult& aRv);
74 // DOMEventTargetHelper
75 void DisconnectFromOwner() override;
77 // Invalidates |mControllerWorker| and dispatches a "controllerchange"
78 // event.
79 void ControllerChanged(ErrorResult& aRv);
81 void ReceiveMessage(const ClientPostMessageArgs& aArgs);
83 void RevokeActor(ServiceWorkerContainerChild* aActor);
85 private:
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
96 // devtools console.
97 nsIGlobalObject* GetGlobalIfValid(
98 ErrorResult& aRv,
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
114 // failed or not.
115 static Result<Ok, bool> FillInMessageEventInit(JSContext* aCx,
116 nsIGlobalObject* aGlobal,
117 ReceivedMessage& aMessage,
118 MessageEventInit& aInit,
119 ErrorResult& aRv);
121 void Shutdown();
123 RefPtr<ServiceWorkerContainerChild> mActor;
124 bool mShutdown;
126 // This only changes when a worker hijacks everything in its scope by calling
127 // claim.
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__ */