no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / dom / serviceworkers / ServiceWorkerRegistration.h
blobfa638d37b6568d88af510ded7378c276880a7f30
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_ServiceWorkerRegistration_h
8 #define mozilla_dom_ServiceWorkerRegistration_h
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/ServiceWorkerBinding.h"
12 #include "mozilla/dom/ServiceWorkerRegistrationBinding.h"
13 #include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h"
14 #include "mozilla/dom/ServiceWorkerUtils.h"
16 // Support for Notification API extension.
17 #include "mozilla/dom/NotificationBinding.h"
19 class nsIGlobalObject;
21 namespace mozilla::dom {
23 class NavigationPreloadManager;
24 class Promise;
25 class PushManager;
26 class WorkerPrivate;
27 class ServiceWorker;
28 class ServiceWorkerRegistrationChild;
30 #define NS_DOM_SERVICEWORKERREGISTRATION_IID \
31 { \
32 0x4578a90e, 0xa427, 0x4237, { \
33 0x98, 0x4a, 0xbd, 0x98, 0xe4, 0xcd, 0x5f, 0x3a \
34 } \
37 class ServiceWorkerRegistration final : public DOMEventTargetHelper {
38 public:
39 NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_SERVICEWORKERREGISTRATION_IID)
40 NS_DECL_ISUPPORTS_INHERITED
41 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerRegistration,
42 DOMEventTargetHelper)
44 IMPL_EVENT_HANDLER(updatefound)
46 static already_AddRefed<ServiceWorkerRegistration> CreateForMainThread(
47 nsPIDOMWindowInner* aWindow,
48 const ServiceWorkerRegistrationDescriptor& aDescriptor);
50 static already_AddRefed<ServiceWorkerRegistration> CreateForWorker(
51 WorkerPrivate* aWorkerPrivate, nsIGlobalObject* aGlobal,
52 const ServiceWorkerRegistrationDescriptor& aDescriptor);
54 JSObject* WrapObject(JSContext* aCx,
55 JS::Handle<JSObject*> aGivenProto) override;
57 void DisconnectFromOwner() override;
59 void RegistrationCleared();
61 already_AddRefed<ServiceWorker> GetInstalling() const;
63 already_AddRefed<ServiceWorker> GetWaiting() const;
65 already_AddRefed<ServiceWorker> GetActive() const;
67 already_AddRefed<NavigationPreloadManager> NavigationPreload();
69 void UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor);
71 bool MatchesDescriptor(
72 const ServiceWorkerRegistrationDescriptor& aDescriptor) const;
74 void GetScope(nsAString& aScope) const;
76 ServiceWorkerUpdateViaCache GetUpdateViaCache(ErrorResult& aRv) const;
78 already_AddRefed<Promise> Update(ErrorResult& aRv);
80 already_AddRefed<Promise> Unregister(ErrorResult& aRv);
82 already_AddRefed<PushManager> GetPushManager(JSContext* aCx,
83 ErrorResult& aRv);
85 already_AddRefed<Promise> ShowNotification(
86 JSContext* aCx, const nsAString& aTitle,
87 const NotificationOptions& aOptions, ErrorResult& aRv);
89 already_AddRefed<Promise> GetNotifications(
90 const GetNotificationOptions& aOptions, ErrorResult& aRv);
92 void SetNavigationPreloadEnabled(bool aEnabled,
93 ServiceWorkerBoolCallback&& aSuccessCB,
94 ServiceWorkerFailureCallback&& aFailureCB);
96 void SetNavigationPreloadHeader(const nsCString& aHeader,
97 ServiceWorkerBoolCallback&& aSuccessCB,
98 ServiceWorkerFailureCallback&& aFailureCB);
100 void GetNavigationPreloadState(NavigationPreloadGetStateCallback&& aSuccessCB,
101 ServiceWorkerFailureCallback&& aFailureCB);
103 const ServiceWorkerRegistrationDescriptor& Descriptor() const;
105 void WhenVersionReached(uint64_t aVersion,
106 ServiceWorkerBoolCallback&& aCallback);
108 void MaybeDispatchUpdateFoundRunnable();
110 void RevokeActor(ServiceWorkerRegistrationChild* aActor);
112 void FireUpdateFound();
114 private:
115 ServiceWorkerRegistration(
116 nsIGlobalObject* aGlobal,
117 const ServiceWorkerRegistrationDescriptor& aDescriptor);
119 ~ServiceWorkerRegistration();
121 void UpdateStateInternal(const Maybe<ServiceWorkerDescriptor>& aInstalling,
122 const Maybe<ServiceWorkerDescriptor>& aWaiting,
123 const Maybe<ServiceWorkerDescriptor>& aActive);
125 void MaybeScheduleUpdateFound(
126 const Maybe<ServiceWorkerDescriptor>& aInstallingDescriptor);
128 void MaybeDispatchUpdateFound();
130 void Shutdown();
132 ServiceWorkerRegistrationDescriptor mDescriptor;
133 RefPtr<ServiceWorkerRegistrationChild> mActor;
134 bool mShutdown;
136 RefPtr<ServiceWorker> mInstallingWorker;
137 RefPtr<ServiceWorker> mWaitingWorker;
138 RefPtr<ServiceWorker> mActiveWorker;
139 RefPtr<NavigationPreloadManager> mNavigationPreloadManager;
140 RefPtr<PushManager> mPushManager;
142 struct VersionCallback {
143 uint64_t mVersion;
144 ServiceWorkerBoolCallback mFunc;
146 VersionCallback(uint64_t aVersion, ServiceWorkerBoolCallback&& aFunc)
147 : mVersion(aVersion), mFunc(std::move(aFunc)) {
148 MOZ_DIAGNOSTIC_ASSERT(mFunc);
151 nsTArray<UniquePtr<VersionCallback>> mVersionCallbackList;
153 uint64_t mScheduledUpdateFoundId;
154 uint64_t mDispatchedUpdateFoundId;
157 NS_DEFINE_STATIC_IID_ACCESSOR(ServiceWorkerRegistration,
158 NS_DOM_SERVICEWORKERREGISTRATION_IID)
160 } // namespace mozilla::dom
162 #endif /* mozilla_dom_ServiceWorkerRegistration_h */