/src/mozilla-central/dom/serviceworkers/ServiceWorkerRegistration.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef mozilla_dom_ServiceWorkerRegistration_h |
8 | | #define mozilla_dom_ServiceWorkerRegistration_h |
9 | | |
10 | | #include "mozilla/DOMEventTargetHelper.h" |
11 | | #include "mozilla/dom/DOMPrefs.h" |
12 | | #include "mozilla/dom/ServiceWorkerBinding.h" |
13 | | #include "mozilla/dom/ServiceWorkerRegistrationBinding.h" |
14 | | #include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h" |
15 | | #include "mozilla/dom/ServiceWorkerUtils.h" |
16 | | |
17 | | // Support for Notification API extension. |
18 | | #include "mozilla/dom/NotificationBinding.h" |
19 | | |
20 | | class nsIGlobalObject; |
21 | | |
22 | | namespace mozilla { |
23 | | namespace dom { |
24 | | |
25 | | class Promise; |
26 | | class PushManager; |
27 | | class WorkerPrivate; |
28 | | class ServiceWorker; |
29 | | |
30 | | #define NS_DOM_SERVICEWORKERREGISTRATION_IID \ |
31 | | {0x4578a90e, 0xa427, 0x4237, {0x98, 0x4a, 0xbd, 0x98, 0xe4, 0xcd, 0x5f, 0x3a}} |
32 | | |
33 | | class ServiceWorkerRegistration final : public DOMEventTargetHelper |
34 | | { |
35 | | public: |
36 | | class Inner |
37 | | { |
38 | | public: |
39 | | NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING |
40 | | |
41 | | virtual void |
42 | | SetServiceWorkerRegistration(ServiceWorkerRegistration* aReg) = 0; |
43 | | |
44 | | virtual void |
45 | | ClearServiceWorkerRegistration(ServiceWorkerRegistration* aReg) = 0; |
46 | | |
47 | | virtual void |
48 | | Update(ServiceWorkerRegistrationCallback&& aSuccessCB, |
49 | | ServiceWorkerFailureCallback&& aFailureCB) = 0; |
50 | | |
51 | | virtual void |
52 | | Unregister(ServiceWorkerBoolCallback&& aSuccessCB, |
53 | | ServiceWorkerFailureCallback&& aFailureCB) = 0; |
54 | | }; |
55 | | |
56 | | NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_SERVICEWORKERREGISTRATION_IID) |
57 | | NS_DECL_ISUPPORTS_INHERITED |
58 | | NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerRegistration, DOMEventTargetHelper) |
59 | | |
60 | | IMPL_EVENT_HANDLER(updatefound) |
61 | | |
62 | | static already_AddRefed<ServiceWorkerRegistration> |
63 | | CreateForMainThread(nsPIDOMWindowInner* aWindow, |
64 | | const ServiceWorkerRegistrationDescriptor& aDescriptor); |
65 | | |
66 | | static already_AddRefed<ServiceWorkerRegistration> |
67 | | CreateForWorker(WorkerPrivate* aWorkerPrivate, |
68 | | nsIGlobalObject* aGlobal, |
69 | | const ServiceWorkerRegistrationDescriptor& aDescriptor); |
70 | | |
71 | | JSObject* |
72 | | WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
73 | | |
74 | | void DisconnectFromOwner() override; |
75 | | |
76 | | void |
77 | | RegistrationRemoved(); |
78 | | |
79 | | already_AddRefed<ServiceWorker> |
80 | | GetInstalling() const; |
81 | | |
82 | | already_AddRefed<ServiceWorker> |
83 | | GetWaiting() const; |
84 | | |
85 | | already_AddRefed<ServiceWorker> |
86 | | GetActive() const; |
87 | | |
88 | | void |
89 | | UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor); |
90 | | |
91 | | bool |
92 | | MatchesDescriptor(const ServiceWorkerRegistrationDescriptor& aDescriptor) const; |
93 | | |
94 | | void |
95 | | GetScope(nsAString& aScope) const; |
96 | | |
97 | | ServiceWorkerUpdateViaCache |
98 | | GetUpdateViaCache(ErrorResult& aRv) const; |
99 | | |
100 | | already_AddRefed<Promise> |
101 | | Update(ErrorResult& aRv); |
102 | | |
103 | | already_AddRefed<Promise> |
104 | | Unregister(ErrorResult& aRv); |
105 | | |
106 | | already_AddRefed<PushManager> |
107 | | GetPushManager(JSContext* aCx, ErrorResult& aRv); |
108 | | |
109 | | already_AddRefed<Promise> |
110 | | ShowNotification(JSContext* aCx, |
111 | | const nsAString& aTitle, |
112 | | const NotificationOptions& aOptions, |
113 | | ErrorResult& aRv); |
114 | | |
115 | | already_AddRefed<Promise> |
116 | | GetNotifications(const GetNotificationOptions& aOptions, |
117 | | ErrorResult& aRv); |
118 | | |
119 | | const ServiceWorkerRegistrationDescriptor& |
120 | | Descriptor() const; |
121 | | |
122 | | void |
123 | | WhenVersionReached(uint64_t aVersion, ServiceWorkerBoolCallback&& aCallback); |
124 | | |
125 | | private: |
126 | | ServiceWorkerRegistration(nsIGlobalObject* aGlobal, |
127 | | const ServiceWorkerRegistrationDescriptor& aDescriptor, |
128 | | Inner* aInner); |
129 | | |
130 | | ~ServiceWorkerRegistration(); |
131 | | |
132 | | void |
133 | | UpdateStateInternal(const Maybe<ServiceWorkerDescriptor>& aInstalling, |
134 | | const Maybe<ServiceWorkerDescriptor>& aWaiting, |
135 | | const Maybe<ServiceWorkerDescriptor>& aActive); |
136 | | |
137 | | void |
138 | | MaybeScheduleUpdateFound(const Maybe<ServiceWorkerDescriptor>& aInstallingDescriptor); |
139 | | |
140 | | void |
141 | | MaybeDispatchUpdateFound(); |
142 | | |
143 | | void |
144 | | UpdatePromiseSettled(); |
145 | | |
146 | | ServiceWorkerRegistrationDescriptor mDescriptor; |
147 | | RefPtr<Inner> mInner; |
148 | | |
149 | | RefPtr<ServiceWorker> mInstallingWorker; |
150 | | RefPtr<ServiceWorker> mWaitingWorker; |
151 | | RefPtr<ServiceWorker> mActiveWorker; |
152 | | RefPtr<PushManager> mPushManager; |
153 | | |
154 | | struct VersionCallback |
155 | | { |
156 | | uint64_t mVersion; |
157 | | ServiceWorkerBoolCallback mFunc; |
158 | | |
159 | | VersionCallback(uint64_t aVersion, ServiceWorkerBoolCallback&& aFunc) |
160 | | : mVersion(aVersion) |
161 | | , mFunc(std::move(aFunc)) |
162 | 0 | { |
163 | 0 | MOZ_DIAGNOSTIC_ASSERT(mFunc); |
164 | 0 | } |
165 | | }; |
166 | | nsTArray<UniquePtr<VersionCallback>> mVersionCallbackList; |
167 | | |
168 | | uint64_t mScheduledUpdateFoundId; |
169 | | uint64_t mDispatchedUpdateFoundId; |
170 | | uint32_t mPendingUpdatePromises; |
171 | | }; |
172 | | |
173 | | NS_DEFINE_STATIC_IID_ACCESSOR(ServiceWorkerRegistration, NS_DOM_SERVICEWORKERREGISTRATION_IID) |
174 | | |
175 | | } // namespace dom |
176 | | } // namespace mozilla |
177 | | |
178 | | #endif /* mozilla_dom_ServiceWorkerRegistration_h */ |