ServiceWorkerInfo.h (5915B)
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_serviceworkerinfo_h 8 #define mozilla_dom_serviceworkerinfo_h 9 10 #include "MainThreadUtils.h" 11 #include "mozilla/OriginAttributes.h" 12 #include "mozilla/TimeStamp.h" 13 #include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState 14 #include "mozilla/dom/ServiceWorkerDescriptor.h" 15 #include "mozilla/dom/ServiceWorkerLifetimeExtension.h" 16 #include "mozilla/dom/WorkerCommon.h" 17 #include "nsIServiceWorkerManager.h" 18 19 namespace mozilla::dom { 20 21 class ClientInfo; 22 class PostMessageSource; 23 class ServiceWorkerCloneData; 24 class ServiceWorkerPrivate; 25 26 /* 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. 31 */ 32 class ServiceWorkerInfo final : public nsIServiceWorkerInfo { 33 private: 34 nsCOMPtr<nsIPrincipal> mPrincipal; 35 ServiceWorkerDescriptor mDescriptor; 36 const nsString mCacheName; 37 OriginAttributes mOriginAttributes; 38 const nsString mWorkerPrivateId; 39 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; 49 50 // Timestamp to track SW's state 51 PRTime mCreationTime; 52 TimeStamp mCreationTimeStamp; 53 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; 59 60 RefPtr<ServiceWorkerPrivate> mServiceWorkerPrivate; 61 bool mSkipWaitingFlag; 62 63 enum { Unknown, Enabled, Disabled } mHandlesFetch; 64 65 uint32_t mNavigationFaultCount; 66 67 // Testing helper to trigger fetch event cancellation when not NS_OK. 68 // See `nsIServiceWorkerInfo::testingInjectCancellation`. 69 nsresult mTestingInjectCancellation; 70 71 ~ServiceWorkerInfo(); 72 73 // Generates a unique id for the service worker, with zero being treated as 74 // invalid. 75 uint64_t GetNextID() const; 76 77 public: 78 NS_DECL_ISUPPORTS 79 NS_DECL_NSISERVICEWORKERINFO 80 81 void PostMessage(RefPtr<ServiceWorkerCloneData>&& aData, 82 const PostMessageSource& aSource); 83 84 class ServiceWorkerPrivate* WorkerPrivate() const { 85 MOZ_ASSERT(mServiceWorkerPrivate); 86 return mServiceWorkerPrivate; 87 } 88 89 nsIPrincipal* Principal() const { return mPrincipal; } 90 91 const nsCString& ScriptSpec() const { return mDescriptor.ScriptURL(); } 92 93 const nsCString& Scope() const { return mDescriptor.Scope(); } 94 WorkerType Type() const { return mDescriptor.Type(); } 95 Maybe<ClientInfo> GetClientInfo(); 96 97 // Pass-through of ServiceWorkerPrivate::GetLifetimeDeadline(); note that 98 // we have an XPCOM variation that returns a double for testing purposes. 99 TimeStamp LifetimeDeadline(); 100 101 bool SkipWaitingFlag() const { 102 MOZ_ASSERT(NS_IsMainThread()); 103 return mSkipWaitingFlag; 104 } 105 106 void SetSkipWaitingFlag() { 107 MOZ_ASSERT(NS_IsMainThread()); 108 mSkipWaitingFlag = true; 109 } 110 111 void ReportNavigationFault() { 112 MOZ_ASSERT(NS_IsMainThread()); 113 mNavigationFaultCount++; 114 } 115 116 ServiceWorkerInfo(nsIPrincipal* aPrincipal, const nsACString& aScope, 117 const WorkerType& aType, uint64_t aRegistrationId, 118 uint64_t aRegistrationVersion, 119 const nsACString& aScriptSpec, const nsAString& aCacheName, 120 nsLoadFlags aImportsLoadFlags); 121 122 ServiceWorkerState State() const { return mDescriptor.State(); } 123 124 const OriginAttributes& GetOriginAttributes() const { 125 return mOriginAttributes; 126 } 127 128 const nsString& CacheName() const { return mCacheName; } 129 130 nsLoadFlags GetImportsLoadFlags() const { return mImportsLoadFlags; } 131 132 uint64_t ID() const { return mDescriptor.Id(); } 133 134 const ServiceWorkerDescriptor& Descriptor() const { return mDescriptor; } 135 136 nsresult TestingInjectCancellation() { return mTestingInjectCancellation; } 137 138 void UpdateState(ServiceWorkerState aState); 139 140 // Only used to set initial state when loading from disk! 141 void SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState) { 142 MOZ_ASSERT(NS_IsMainThread()); 143 mDescriptor.SetState(aState); 144 } 145 146 void SetHandlesFetch(bool aHandlesFetch) { 147 MOZ_ASSERT(NS_IsMainThread()); 148 MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch == Unknown); 149 mHandlesFetch = aHandlesFetch ? Enabled : Disabled; 150 mDescriptor.SetHandlesFetch(aHandlesFetch); 151 } 152 153 void SetRegistrationVersion(uint64_t aVersion); 154 155 bool HandlesFetch() const { 156 MOZ_ASSERT(NS_IsMainThread()); 157 MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch != Unknown); 158 return mHandlesFetch != Disabled; 159 } 160 161 void UpdateInstalledTime(); 162 163 void UpdateActivatedTime(); 164 165 void UpdateRedundantTime(); 166 167 int64_t GetInstalledTime() const { return mInstalledTime; } 168 169 void SetInstalledTime(const int64_t aTime) { 170 if (aTime == 0) { 171 return; 172 } 173 174 mInstalledTime = aTime; 175 } 176 177 int64_t GetActivatedTime() const { return mActivatedTime; } 178 179 void SetActivatedTime(const int64_t aTime) { 180 if (aTime == 0) { 181 return; 182 } 183 184 mActivatedTime = aTime; 185 } 186 }; 187 188 } // namespace mozilla::dom 189 190 #endif // mozilla_dom_serviceworkerinfo_h