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_serviceworkerregistrationinfo_h
8 #define mozilla_dom_serviceworkerregistrationinfo_h
12 #include "mozilla/dom/IPCNavigationPreloadState.h"
13 #include "mozilla/dom/ServiceWorkerInfo.h"
14 #include "mozilla/dom/ServiceWorkerRegistrationBinding.h"
15 #include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h"
16 #include "nsProxyRelease.h"
17 #include "nsTObserverArray.h"
19 namespace mozilla::dom
{
21 class ServiceWorkerRegistrationListener
;
23 class ServiceWorkerRegistrationInfo final
24 : public nsIServiceWorkerRegistrationInfo
{
25 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
26 ServiceWorkerRegistrationDescriptor mDescriptor
;
27 nsTArray
<nsCOMPtr
<nsIServiceWorkerRegistrationInfoListener
>> mListeners
;
28 nsTObserverArray
<ServiceWorkerRegistrationListener
*> mInstanceList
;
31 const ServiceWorkerRegistrationDescriptor mDescriptor
;
34 explicit VersionEntry(
35 const ServiceWorkerRegistrationDescriptor
& aDescriptor
)
36 : mDescriptor(aDescriptor
), mTimeStamp(TimeStamp::Now()) {}
38 nsTArray
<UniquePtr
<VersionEntry
>> mVersionList
;
40 const nsID mAgentClusterId
= nsID::GenerateUUID();
42 uint32_t mControlledClientsCounter
;
43 uint32_t mDelayMultiplier
;
45 enum { NoUpdate
, NeedTimeCheckAndUpdate
, NeedUpdate
} mUpdateState
;
47 // Timestamp to track SWR's last update time
49 TimeStamp mCreationTimeStamp
;
50 // The time of update is 0, if SWR've never been updated yet.
51 PRTime mLastUpdateTime
;
53 RefPtr
<ServiceWorkerInfo
> mEvaluatingWorker
;
54 RefPtr
<ServiceWorkerInfo
> mActiveWorker
;
55 RefPtr
<ServiceWorkerInfo
> mWaitingWorker
;
56 RefPtr
<ServiceWorkerInfo
> mInstallingWorker
;
58 virtual ~ServiceWorkerRegistrationInfo();
60 // When unregister() is called on a registration, it is removed from the
61 // "scope to registration map" but not immediately "cleared" (i.e. its workers
62 // terminated, updated to the redundant state, etc.) because it may still be
63 // controlling clients. It is marked as unregistered and when all controlled
64 // clients go away, cleared. This way we can tell if a registration
65 // is unregistered by querying the object itself rather than incurring a table
66 // lookup (in the case when the registrations are passed around as pointers).
71 IPCNavigationPreloadState mNavigationPreloadState
;
75 NS_DECL_NSISERVICEWORKERREGISTRATIONINFO
77 using TryToActivateCallback
= std::function
<void()>;
79 ServiceWorkerRegistrationInfo(
80 const nsACString
& aScope
, nsIPrincipal
* aPrincipal
,
81 ServiceWorkerUpdateViaCache aUpdateViaCache
,
82 IPCNavigationPreloadState
&& aNavigationPreloadState
);
84 void AddInstance(ServiceWorkerRegistrationListener
* aInstance
,
85 const ServiceWorkerRegistrationDescriptor
& aDescriptor
);
87 void RemoveInstance(ServiceWorkerRegistrationListener
* aInstance
);
89 const nsCString
& Scope() const;
91 nsIPrincipal
* Principal() const;
93 bool IsUnregistered() const;
95 void SetUnregistered();
97 already_AddRefed
<ServiceWorkerInfo
> Newest() const {
98 RefPtr
<ServiceWorkerInfo
> newest
;
99 if (mInstallingWorker
) {
100 newest
= mInstallingWorker
;
101 } else if (mWaitingWorker
) {
102 newest
= mWaitingWorker
;
104 newest
= mActiveWorker
;
107 return newest
.forget();
110 already_AddRefed
<ServiceWorkerInfo
> NewestIncludingEvaluating() const {
111 if (mEvaluatingWorker
) {
112 RefPtr
<ServiceWorkerInfo
> newest
= mEvaluatingWorker
;
113 return newest
.forget();
118 already_AddRefed
<ServiceWorkerInfo
> GetServiceWorkerInfoById(uint64_t aId
);
120 void StartControllingClient() {
121 ++mControlledClientsCounter
;
122 mDelayMultiplier
= 0;
125 void StopControllingClient() {
126 MOZ_ASSERT(mControlledClientsCounter
);
127 --mControlledClientsCounter
;
130 bool IsControllingClients() const {
131 return mActiveWorker
&& mControlledClientsCounter
;
134 // As a side effect, this nullifies
135 // `m{Evaluating,Installing,Waiting,Active}Worker`s.
136 void ShutdownWorkers();
140 void ClearAsCorrupt();
142 bool IsCorrupt() const;
144 void TryToActivateAsync(TryToActivateCallback
&& aCallback
= nullptr);
146 void TryToActivate(TryToActivateCallback
&& aCallback
);
150 void FinishActivate(bool aSuccess
);
152 void RefreshLastUpdateCheckTime();
154 bool IsLastUpdateCheckTimeOverOneDay() const;
156 void MaybeScheduleTimeCheckAndUpdate();
158 void MaybeScheduleUpdate();
160 bool CheckAndClearIfUpdateNeeded();
162 ServiceWorkerInfo
* GetEvaluating() const;
164 ServiceWorkerInfo
* GetInstalling() const;
166 ServiceWorkerInfo
* GetWaiting() const;
168 ServiceWorkerInfo
* GetActive() const;
170 ServiceWorkerInfo
* GetByDescriptor(
171 const ServiceWorkerDescriptor
& aDescriptor
) const;
173 // Set the given worker as the evaluating service worker. The worker
174 // state is not changed.
175 void SetEvaluating(ServiceWorkerInfo
* aServiceWorker
);
177 // Remove an existing evaluating worker, if present. The worker will
178 // be transitioned to the Redundant state.
179 void ClearEvaluating();
181 // Remove an existing installing worker, if present. The worker will
182 // be transitioned to the Redundant state.
183 void ClearInstalling();
185 // Transition the current evaluating worker to be the installing worker. The
186 // worker's state is update to Installing.
187 void TransitionEvaluatingToInstalling();
189 // Transition the current installing worker to be the waiting worker. The
190 // worker's state is updated to Installed.
191 void TransitionInstallingToWaiting();
193 // Override the current active worker. This is used during browser
194 // initialization to load persisted workers. Its also used to propagate
195 // active workers across child processes in e10s. This second use will
196 // go away once the ServiceWorkerManager moves to the parent process.
197 // The worker is transitioned to the Activated state.
198 void SetActive(ServiceWorkerInfo
* aServiceWorker
);
200 // Transition the current waiting worker to be the new active worker. The
201 // worker is updated to the Activating state.
202 void TransitionWaitingToActive();
204 // Determine if the registration is actively performing work.
207 ServiceWorkerUpdateViaCache
GetUpdateViaCache() const;
209 void SetUpdateViaCache(ServiceWorkerUpdateViaCache aUpdateViaCache
);
211 int64_t GetLastUpdateTime() const;
213 void SetLastUpdateTime(const int64_t aTime
);
215 const ServiceWorkerRegistrationDescriptor
& Descriptor() const;
219 uint64_t Version() const;
221 uint32_t GetUpdateDelay(const bool aWithMultiplier
= true);
223 void FireUpdateFound();
225 void NotifyCleared();
227 void ClearWhenIdle();
229 const nsID
& AgentClusterId() const;
231 void SetNavigationPreloadEnabled(const bool& aEnabled
);
233 void SetNavigationPreloadHeader(const nsCString
& aHeader
);
235 IPCNavigationPreloadState
GetNavigationPreloadState() const;
238 // Roughly equivalent to [[Update Registration State algorithm]]. Make sure
239 // this is called *before* updating SW instances' state, otherwise they
241 void UpdateRegistrationState();
243 void UpdateRegistrationState(ServiceWorkerUpdateViaCache aUpdateViaCache
);
245 // Used by devtools to track changes to the properties of
246 // *nsIServiceWorkerRegistrationInfo*. Note, this doesn't necessarily need to
247 // be in sync with the DOM registration objects, but it does need to be called
248 // in the same task that changed |mInstallingWorker|, |mWaitingWorker| or
250 void NotifyChromeRegistrationListeners();
252 static uint64_t GetNextId();
254 static uint64_t GetNextVersion();
256 // `aFunc`'s argument will be a reference to
257 // `m{Evaluating,Installing,Waiting,Active}Worker` (not to copy of them).
258 // Additionally, a null check will be performed for each worker before each
259 // call to `aFunc`, so `aFunc` will always get a reference to a non-null
261 void ForEachWorker(void (*aFunc
)(RefPtr
<ServiceWorkerInfo
>&));
263 void CheckQuotaUsage();
266 } // namespace mozilla::dom
268 #endif // mozilla_dom_serviceworkerregistrationinfo_h