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_serviceworkerinfo_h
8 #define mozilla_dom_serviceworkerinfo_h
10 #include "MainThreadUtils.h"
11 #include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState
12 #include "mozilla/dom/ServiceWorkerDescriptor.h"
13 #include "mozilla/dom/WorkerCommon.h"
14 #include "mozilla/OriginAttributes.h"
15 #include "mozilla/TimeStamp.h"
16 #include "nsIServiceWorkerManager.h"
18 namespace mozilla::dom
{
20 class ClientInfoAndState
;
22 class ServiceWorkerCloneData
;
23 class ServiceWorkerPrivate
;
26 * Wherever the spec treats a worker instance and a description of said worker
27 * as the same thing; i.e. "Resolve foo with
28 * _GetNewestWorker(serviceWorkerRegistration)", we represent the description
29 * by this class and spawn a ServiceWorker in the right global when required.
31 class ServiceWorkerInfo final
: public nsIServiceWorkerInfo
{
33 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
34 ServiceWorkerDescriptor mDescriptor
;
35 const nsString mCacheName
;
36 OriginAttributes mOriginAttributes
;
37 const nsString mWorkerPrivateId
;
39 // This LoadFlags is only applied to imported scripts, since the main script
40 // has already been downloaded when performing the bytecheck. This LoadFlag is
41 // composed of three parts:
42 // 1. nsIChannel::LOAD_BYPASS_SERVICE_WORKER
43 // 2. (Optional) nsIRequest::VALIDATE_ALWAYS
44 // depends on ServiceWorkerUpdateViaCache of its registration.
45 // 3. (optional) nsIRequest::LOAD_BYPASS_CACHE
46 // depends on whether the update timer is expired.
47 const nsLoadFlags mImportsLoadFlags
;
49 // Timestamp to track SW's state
51 TimeStamp mCreationTimeStamp
;
53 // The time of states are 0, if SW has not reached that state yet. Besides, we
54 // update each of them after UpdateState() is called in SWRegistrationInfo.
55 PRTime mInstalledTime
;
56 PRTime mActivatedTime
;
57 PRTime mRedundantTime
;
59 RefPtr
<ServiceWorkerPrivate
> mServiceWorkerPrivate
;
60 bool mSkipWaitingFlag
;
62 enum { Unknown
, Enabled
, Disabled
} mHandlesFetch
;
64 uint32_t mNavigationFaultCount
;
66 // Testing helper to trigger fetch event cancellation when not NS_OK.
67 // See `nsIServiceWorkerInfo::testingInjectCancellation`.
68 nsresult mTestingInjectCancellation
;
72 // Generates a unique id for the service worker, with zero being treated as
74 uint64_t GetNextID() const;
78 NS_DECL_NSISERVICEWORKERINFO
80 void PostMessage(RefPtr
<ServiceWorkerCloneData
>&& aData
,
81 const ClientInfo
& aClientInfo
,
82 const ClientState
& aClientState
);
84 class ServiceWorkerPrivate
* WorkerPrivate() const {
85 MOZ_ASSERT(mServiceWorkerPrivate
);
86 return mServiceWorkerPrivate
;
89 nsIPrincipal
* Principal() const { return mPrincipal
; }
91 const nsCString
& ScriptSpec() const { return mDescriptor
.ScriptURL(); }
93 const nsCString
& Scope() const { return mDescriptor
.Scope(); }
95 bool SkipWaitingFlag() const {
96 MOZ_ASSERT(NS_IsMainThread());
97 return mSkipWaitingFlag
;
100 void SetSkipWaitingFlag() {
101 MOZ_ASSERT(NS_IsMainThread());
102 mSkipWaitingFlag
= true;
105 void ReportNavigationFault() {
106 MOZ_ASSERT(NS_IsMainThread());
107 mNavigationFaultCount
++;
110 ServiceWorkerInfo(nsIPrincipal
* aPrincipal
, const nsACString
& aScope
,
111 uint64_t aRegistrationId
, uint64_t aRegistrationVersion
,
112 const nsACString
& aScriptSpec
, const nsAString
& aCacheName
,
113 nsLoadFlags aLoadFlags
);
115 ServiceWorkerState
State() const { return mDescriptor
.State(); }
117 const OriginAttributes
& GetOriginAttributes() const {
118 return mOriginAttributes
;
121 const nsString
& CacheName() const { return mCacheName
; }
123 nsLoadFlags
GetImportsLoadFlags() const { return mImportsLoadFlags
; }
125 uint64_t ID() const { return mDescriptor
.Id(); }
127 const ServiceWorkerDescriptor
& Descriptor() const { return mDescriptor
; }
129 nsresult
TestingInjectCancellation() { return mTestingInjectCancellation
; }
131 void UpdateState(ServiceWorkerState aState
);
133 // Only used to set initial state when loading from disk!
134 void SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState
) {
135 MOZ_ASSERT(NS_IsMainThread());
136 mDescriptor
.SetState(aState
);
139 void SetHandlesFetch(bool aHandlesFetch
) {
140 MOZ_ASSERT(NS_IsMainThread());
141 MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch
== Unknown
);
142 mHandlesFetch
= aHandlesFetch
? Enabled
: Disabled
;
143 mDescriptor
.SetHandlesFetch(aHandlesFetch
);
146 void SetRegistrationVersion(uint64_t aVersion
);
148 bool HandlesFetch() const {
149 MOZ_ASSERT(NS_IsMainThread());
150 MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch
!= Unknown
);
151 return mHandlesFetch
!= Disabled
;
154 void UpdateInstalledTime();
156 void UpdateActivatedTime();
158 void UpdateRedundantTime();
160 int64_t GetInstalledTime() const { return mInstalledTime
; }
162 void SetInstalledTime(const int64_t aTime
) {
167 mInstalledTime
= aTime
;
170 int64_t GetActivatedTime() const { return mActivatedTime
; }
172 void SetActivatedTime(const int64_t aTime
) {
177 mActivatedTime
= aTime
;
181 } // namespace mozilla::dom
183 #endif // mozilla_dom_serviceworkerinfo_h