Line data Source code
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 "mozilla/dom/workers/Workers.h"
12 : #include "nsIServiceWorkerManager.h"
13 :
14 : namespace mozilla {
15 : namespace dom {
16 : namespace workers {
17 :
18 : class ServiceWorker;
19 : class ServiceWorkerPrivate;
20 :
21 : /*
22 : * Wherever the spec treats a worker instance and a description of said worker
23 : * as the same thing; i.e. "Resolve foo with
24 : * _GetNewestWorker(serviceWorkerRegistration)", we represent the description
25 : * by this class and spawn a ServiceWorker in the right global when required.
26 : */
27 : class ServiceWorkerInfo final : public nsIServiceWorkerInfo
28 : {
29 : private:
30 : nsCOMPtr<nsIPrincipal> mPrincipal;
31 : const nsCString mScope;
32 : const nsCString mScriptSpec;
33 : const nsString mCacheName;
34 : const nsLoadFlags mLoadFlags;
35 : ServiceWorkerState mState;
36 : OriginAttributes mOriginAttributes;
37 :
38 : // This id is shared with WorkerPrivate to match requests issued by service
39 : // workers to their corresponding serviceWorkerInfo.
40 : uint64_t mServiceWorkerID;
41 :
42 : // Timestamp to track SW's state
43 : PRTime mCreationTime;
44 : TimeStamp mCreationTimeStamp;
45 :
46 : // The time of states are 0, if SW has not reached that state yet. Besides, we
47 : // update each of them after UpdateState() is called in SWRegistrationInfo.
48 : PRTime mInstalledTime;
49 : PRTime mActivatedTime;
50 : PRTime mRedundantTime;
51 :
52 : // We hold rawptrs since the ServiceWorker constructor and destructor ensure
53 : // addition and removal.
54 : // There is a high chance of there being at least one ServiceWorker
55 : // associated with this all the time.
56 : AutoTArray<ServiceWorker*, 1> mInstances;
57 :
58 : RefPtr<ServiceWorkerPrivate> mServiceWorkerPrivate;
59 : bool mSkipWaitingFlag;
60 :
61 : enum {
62 : Unknown,
63 : Enabled,
64 : Disabled
65 : } mHandlesFetch;
66 :
67 : ~ServiceWorkerInfo();
68 :
69 : // Generates a unique id for the service worker, with zero being treated as
70 : // invalid.
71 : uint64_t
72 : GetNextID() const;
73 :
74 : public:
75 : NS_DECL_ISUPPORTS
76 : NS_DECL_NSISERVICEWORKERINFO
77 :
78 : class ServiceWorkerPrivate*
79 0 : WorkerPrivate() const
80 : {
81 0 : MOZ_ASSERT(mServiceWorkerPrivate);
82 0 : return mServiceWorkerPrivate;
83 : }
84 :
85 : nsIPrincipal*
86 0 : Principal() const
87 : {
88 0 : return mPrincipal;
89 : }
90 :
91 : const nsCString&
92 0 : ScriptSpec() const
93 : {
94 0 : return mScriptSpec;
95 : }
96 :
97 : const nsCString&
98 0 : Scope() const
99 : {
100 0 : return mScope;
101 : }
102 :
103 0 : bool SkipWaitingFlag() const
104 : {
105 0 : AssertIsOnMainThread();
106 0 : return mSkipWaitingFlag;
107 : }
108 :
109 0 : void SetSkipWaitingFlag()
110 : {
111 0 : AssertIsOnMainThread();
112 0 : mSkipWaitingFlag = true;
113 0 : }
114 :
115 : ServiceWorkerInfo(nsIPrincipal* aPrincipal,
116 : const nsACString& aScope,
117 : const nsACString& aScriptSpec,
118 : const nsAString& aCacheName,
119 : nsLoadFlags aLoadFlags);
120 :
121 : ServiceWorkerState
122 0 : State() const
123 : {
124 0 : return mState;
125 : }
126 :
127 : const OriginAttributes&
128 0 : GetOriginAttributes() const
129 : {
130 0 : return mOriginAttributes;
131 : }
132 :
133 : const nsString&
134 0 : CacheName() const
135 : {
136 0 : return mCacheName;
137 : }
138 :
139 : nsLoadFlags
140 0 : GetLoadFlags() const
141 : {
142 0 : return mLoadFlags;
143 : }
144 :
145 : uint64_t
146 0 : ID() const
147 : {
148 0 : return mServiceWorkerID;
149 : }
150 :
151 : void
152 : UpdateState(ServiceWorkerState aState);
153 :
154 : // Only used to set initial state when loading from disk!
155 : void
156 0 : SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState)
157 : {
158 0 : AssertIsOnMainThread();
159 0 : mState = aState;
160 0 : }
161 :
162 : void
163 0 : SetHandlesFetch(bool aHandlesFetch)
164 : {
165 0 : AssertIsOnMainThread();
166 0 : MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch == Unknown);
167 0 : mHandlesFetch = aHandlesFetch ? Enabled : Disabled;
168 0 : }
169 :
170 : bool
171 0 : HandlesFetch() const
172 : {
173 0 : AssertIsOnMainThread();
174 0 : MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch != Unknown);
175 0 : return mHandlesFetch != Disabled;
176 : }
177 :
178 : void
179 : AppendWorker(ServiceWorker* aWorker);
180 :
181 : void
182 : RemoveWorker(ServiceWorker* aWorker);
183 :
184 : already_AddRefed<ServiceWorker>
185 : GetOrCreateInstance(nsPIDOMWindowInner* aWindow);
186 :
187 : void
188 : UpdateInstalledTime();
189 :
190 : void
191 : UpdateActivatedTime();
192 :
193 : void
194 : UpdateRedundantTime();
195 :
196 : int64_t
197 0 : GetInstalledTime() const
198 : {
199 0 : return mInstalledTime;
200 : }
201 :
202 : void
203 0 : SetInstalledTime(const int64_t aTime)
204 : {
205 0 : if (aTime == 0) {
206 0 : return;
207 : }
208 :
209 0 : mInstalledTime = aTime;
210 : }
211 :
212 : int64_t
213 0 : GetActivatedTime() const
214 : {
215 0 : return mActivatedTime;
216 : }
217 :
218 : void
219 0 : SetActivatedTime(const int64_t aTime)
220 : {
221 0 : if (aTime == 0) {
222 0 : return;
223 : }
224 :
225 0 : mActivatedTime = aTime;
226 : }
227 : };
228 :
229 : } // namespace workers
230 : } // namespace dom
231 : } // namespace mozilla
232 :
233 : #endif // mozilla_dom_workers_serviceworkerinfo_h
|