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_workers_serviceworkerinfo_h 8 #define mozilla_dom_workers_serviceworkerinfo_h 9 10 #include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState 11 #include "nsIServiceWorkerManager.h" 12 13 namespace mozilla { 14 namespace dom { 15 namespace workers { 16 17 class ServiceWorker; 18 class ServiceWorkerPrivate; 19 20 /* 21 * Wherever the spec treats a worker instance and a description of said worker 22 * as the same thing; i.e. "Resolve foo with 23 * _GetNewestWorker(serviceWorkerRegistration)", we represent the description 24 * by this class and spawn a ServiceWorker in the right global when required. 25 */ 26 class ServiceWorkerInfo final : public nsIServiceWorkerInfo 27 { 28 private: 29 nsCOMPtr<nsIPrincipal> mPrincipal; 30 const nsCString mScope; 31 const nsCString mScriptSpec; 32 const nsString mCacheName; 33 ServiceWorkerState mState; 34 PrincipalOriginAttributes mOriginAttributes; 35 36 // This id is shared with WorkerPrivate to match requests issued by service 37 // workers to their corresponding serviceWorkerInfo. 38 uint64_t mServiceWorkerID; 39 40 // We hold rawptrs since the ServiceWorker constructor and destructor ensure 41 // addition and removal. 42 // There is a high chance of there being at least one ServiceWorker 43 // associated with this all the time. 44 AutoTArray<ServiceWorker*, 1> mInstances; 45 46 RefPtr<ServiceWorkerPrivate> mServiceWorkerPrivate; 47 bool mSkipWaitingFlag; 48 49 ~ServiceWorkerInfo(); 50 51 // Generates a unique id for the service worker, with zero being treated as 52 // invalid. 53 uint64_t 54 GetNextID() const; 55 56 public: 57 NS_DECL_ISUPPORTS 58 NS_DECL_NSISERVICEWORKERINFO 59 60 class ServiceWorkerPrivate* WorkerPrivate()61 WorkerPrivate() const 62 { 63 MOZ_ASSERT(mServiceWorkerPrivate); 64 return mServiceWorkerPrivate; 65 } 66 67 nsIPrincipal* GetPrincipal()68 GetPrincipal() const 69 { 70 return mPrincipal; 71 } 72 73 const nsCString& ScriptSpec()74 ScriptSpec() const 75 { 76 return mScriptSpec; 77 } 78 79 const nsCString& Scope()80 Scope() const 81 { 82 return mScope; 83 } 84 SkipWaitingFlag()85 bool SkipWaitingFlag() const 86 { 87 AssertIsOnMainThread(); 88 return mSkipWaitingFlag; 89 } 90 SetSkipWaitingFlag()91 void SetSkipWaitingFlag() 92 { 93 AssertIsOnMainThread(); 94 mSkipWaitingFlag = true; 95 } 96 97 ServiceWorkerInfo(nsIPrincipal* aPrincipal, 98 const nsACString& aScope, 99 const nsACString& aScriptSpec, 100 const nsAString& aCacheName); 101 102 ServiceWorkerState State()103 State() const 104 { 105 return mState; 106 } 107 108 const PrincipalOriginAttributes& GetOriginAttributes()109 GetOriginAttributes() const 110 { 111 return mOriginAttributes; 112 } 113 114 const nsString& CacheName()115 CacheName() const 116 { 117 return mCacheName; 118 } 119 120 uint64_t ID()121 ID() const 122 { 123 return mServiceWorkerID; 124 } 125 126 void 127 UpdateState(ServiceWorkerState aState); 128 129 // Only used to set initial state when loading from disk! 130 void SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState)131 SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState) 132 { 133 AssertIsOnMainThread(); 134 mState = aState; 135 } 136 137 void 138 AppendWorker(ServiceWorker* aWorker); 139 140 void 141 RemoveWorker(ServiceWorker* aWorker); 142 143 already_AddRefed<ServiceWorker> 144 GetOrCreateInstance(nsPIDOMWindowInner* aWindow); 145 }; 146 147 } // namespace workers 148 } // namespace dom 149 } // namespace mozilla 150 151 #endif // mozilla_dom_workers_serviceworkerinfo_h 152