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/ServiceWorkerLifetimeExtension.h"
14 #include "mozilla/dom/WorkerCommon.h"
15 #include "mozilla/OriginAttributes.h"
16 #include "mozilla/TimeStamp.h"
17 #include "nsIServiceWorkerManager.h"
19 namespace mozilla::dom
{
22 class PostMessageSource
;
23 class ServiceWorkerCloneData
;
24 class ServiceWorkerPrivate
;
27 * Wherever the spec treats a worker instance and a description of said worker
28 * as the same thing; i.e. "Resolve foo with
29 * _GetNewestWorker(serviceWorkerRegistration)", we represent the description
30 * by this class and spawn a ServiceWorker in the right global when required.
32 class ServiceWorkerInfo final
: public nsIServiceWorkerInfo
{
34 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
35 ServiceWorkerDescriptor mDescriptor
;
36 const nsString mCacheName
;
37 OriginAttributes mOriginAttributes
;
38 const nsString mWorkerPrivateId
;
40 // This LoadFlags is only applied to imported scripts, since the main script
41 // has already been downloaded when performing the bytecheck. This LoadFlag is
42 // composed of three parts:
43 // 1. nsIChannel::LOAD_BYPASS_SERVICE_WORKER
44 // 2. (Optional) nsIRequest::VALIDATE_ALWAYS
45 // depends on ServiceWorkerUpdateViaCache of its registration.
46 // 3. (optional) nsIRequest::LOAD_BYPASS_CACHE
47 // depends on whether the update timer is expired.
48 const nsLoadFlags mImportsLoadFlags
;
50 // Timestamp to track SW's state
52 TimeStamp mCreationTimeStamp
;
54 // The time of states are 0, if SW has not reached that state yet. Besides, we
55 // update each of them after UpdateState() is called in SWRegistrationInfo.
56 PRTime mInstalledTime
;
57 PRTime mActivatedTime
;
58 PRTime mRedundantTime
;
60 RefPtr
<ServiceWorkerPrivate
> mServiceWorkerPrivate
;
61 bool mSkipWaitingFlag
;
63 enum { Unknown
, Enabled
, Disabled
} mHandlesFetch
;
65 uint32_t mNavigationFaultCount
;
67 // Testing helper to trigger fetch event cancellation when not NS_OK.
68 // See `nsIServiceWorkerInfo::testingInjectCancellation`.
69 nsresult mTestingInjectCancellation
;
73 // Generates a unique id for the service worker, with zero being treated as
75 uint64_t GetNextID() const;
79 NS_DECL_NSISERVICEWORKERINFO
81 void PostMessage(RefPtr
<ServiceWorkerCloneData
>&& aData
,
82 const PostMessageSource
& aSource
);
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 Maybe
<ClientInfo
> GetClientInfo();
97 // Pass-through of ServiceWorkerPrivate::GetLifetimeDeadline(); note that
98 // we have an XPCOM variation that returns a double for testing purposes.
99 TimeStamp
LifetimeDeadline();
101 bool SkipWaitingFlag() const {
102 MOZ_ASSERT(NS_IsMainThread());
103 return mSkipWaitingFlag
;
106 void SetSkipWaitingFlag() {
107 MOZ_ASSERT(NS_IsMainThread());
108 mSkipWaitingFlag
= true;
111 void ReportNavigationFault() {
112 MOZ_ASSERT(NS_IsMainThread());
113 mNavigationFaultCount
++;
116 ServiceWorkerInfo(nsIPrincipal
* aPrincipal
, const nsACString
& aScope
,
117 uint64_t aRegistrationId
, uint64_t aRegistrationVersion
,
118 const nsACString
& aScriptSpec
, const nsAString
& aCacheName
,
119 nsLoadFlags aImportsLoadFlags
);
121 ServiceWorkerState
State() const { return mDescriptor
.State(); }
123 const OriginAttributes
& GetOriginAttributes() const {
124 return mOriginAttributes
;
127 const nsString
& CacheName() const { return mCacheName
; }
129 nsLoadFlags
GetImportsLoadFlags() const { return mImportsLoadFlags
; }
131 uint64_t ID() const { return mDescriptor
.Id(); }
133 const ServiceWorkerDescriptor
& Descriptor() const { return mDescriptor
; }
135 nsresult
TestingInjectCancellation() { return mTestingInjectCancellation
; }
137 void UpdateState(ServiceWorkerState aState
);
139 // Only used to set initial state when loading from disk!
140 void SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState
) {
141 MOZ_ASSERT(NS_IsMainThread());
142 mDescriptor
.SetState(aState
);
145 void SetHandlesFetch(bool aHandlesFetch
) {
146 MOZ_ASSERT(NS_IsMainThread());
147 MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch
== Unknown
);
148 mHandlesFetch
= aHandlesFetch
? Enabled
: Disabled
;
149 mDescriptor
.SetHandlesFetch(aHandlesFetch
);
152 void SetRegistrationVersion(uint64_t aVersion
);
154 bool HandlesFetch() const {
155 MOZ_ASSERT(NS_IsMainThread());
156 MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch
!= Unknown
);
157 return mHandlesFetch
!= Disabled
;
160 void UpdateInstalledTime();
162 void UpdateActivatedTime();
164 void UpdateRedundantTime();
166 int64_t GetInstalledTime() const { return mInstalledTime
; }
168 void SetInstalledTime(const int64_t aTime
) {
173 mInstalledTime
= aTime
;
176 int64_t GetActivatedTime() const { return mActivatedTime
; }
178 void SetActivatedTime(const int64_t aTime
) {
183 mActivatedTime
= aTime
;
187 } // namespace mozilla::dom
189 #endif // mozilla_dom_serviceworkerinfo_h