/src/mozilla-central/dom/serviceworkers/ServiceWorkerInfo.h
Line | Count | Source (jump to first uncovered line) |
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/dom/ServiceWorkerBinding.h" // For ServiceWorkerState |
12 | | #include "mozilla/dom/WorkerCommon.h" |
13 | | #include "mozilla/OriginAttributes.h" |
14 | | #include "nsIServiceWorkerManager.h" |
15 | | |
16 | | namespace mozilla { |
17 | | namespace dom { |
18 | | |
19 | | class ClientInfoAndState; |
20 | | class ClientState; |
21 | | class ServiceWorkerCloneData; |
22 | | class ServiceWorkerPrivate; |
23 | | |
24 | | /* |
25 | | * Wherever the spec treats a worker instance and a description of said worker |
26 | | * as the same thing; i.e. "Resolve foo with |
27 | | * _GetNewestWorker(serviceWorkerRegistration)", we represent the description |
28 | | * by this class and spawn a ServiceWorker in the right global when required. |
29 | | */ |
30 | | class ServiceWorkerInfo final : public nsIServiceWorkerInfo |
31 | | { |
32 | | private: |
33 | | nsCOMPtr<nsIPrincipal> mPrincipal; |
34 | | ServiceWorkerDescriptor mDescriptor; |
35 | | const nsString mCacheName; |
36 | | OriginAttributes mOriginAttributes; |
37 | | |
38 | | // This LoadFlags is only applied to imported scripts, since the main script |
39 | | // has already been downloaded when performing the bytecheck. This LoadFlag is |
40 | | // composed of three parts: |
41 | | // 1. nsIChannel::LOAD_BYPASS_SERVICE_WORKER |
42 | | // 2. (Optional) nsIRequest::VALIDATE_ALWAYS |
43 | | // depends on ServiceWorkerUpdateViaCache of its registration. |
44 | | // 3. (optional) nsIRequest::LOAD_BYPASS_CACHE |
45 | | // depends on whether the update timer is expired. |
46 | | const nsLoadFlags mImportsLoadFlags; |
47 | | |
48 | | // Timestamp to track SW's state |
49 | | PRTime mCreationTime; |
50 | | TimeStamp mCreationTimeStamp; |
51 | | |
52 | | // The time of states are 0, if SW has not reached that state yet. Besides, we |
53 | | // update each of them after UpdateState() is called in SWRegistrationInfo. |
54 | | PRTime mInstalledTime; |
55 | | PRTime mActivatedTime; |
56 | | PRTime mRedundantTime; |
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 | | void |
79 | | PostMessage(RefPtr<ServiceWorkerCloneData>&& aData, |
80 | | const ClientInfo& aClientInfo, |
81 | | const ClientState& aClientState); |
82 | | |
83 | | class ServiceWorkerPrivate* |
84 | | WorkerPrivate() const |
85 | 0 | { |
86 | 0 | MOZ_ASSERT(mServiceWorkerPrivate); |
87 | 0 | return mServiceWorkerPrivate; |
88 | 0 | } |
89 | | |
90 | | nsIPrincipal* |
91 | | Principal() const |
92 | 0 | { |
93 | 0 | return mPrincipal; |
94 | 0 | } |
95 | | |
96 | | const nsCString& |
97 | | ScriptSpec() const |
98 | 0 | { |
99 | 0 | return mDescriptor.ScriptURL(); |
100 | 0 | } |
101 | | |
102 | | const nsCString& |
103 | | Scope() const |
104 | 0 | { |
105 | 0 | return mDescriptor.Scope(); |
106 | 0 | } |
107 | | |
108 | | bool SkipWaitingFlag() const |
109 | | { |
110 | | MOZ_ASSERT(NS_IsMainThread()); |
111 | | return mSkipWaitingFlag; |
112 | | } |
113 | | |
114 | | void SetSkipWaitingFlag() |
115 | 0 | { |
116 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
117 | 0 | mSkipWaitingFlag = true; |
118 | 0 | } |
119 | | |
120 | | ServiceWorkerInfo(nsIPrincipal* aPrincipal, |
121 | | const nsACString& aScope, |
122 | | uint64_t aRegistrationId, |
123 | | uint64_t aRegistrationVersion, |
124 | | const nsACString& aScriptSpec, |
125 | | const nsAString& aCacheName, |
126 | | nsLoadFlags aLoadFlags); |
127 | | |
128 | | ServiceWorkerState |
129 | | State() const |
130 | 0 | { |
131 | 0 | return mDescriptor.State(); |
132 | 0 | } |
133 | | |
134 | | const OriginAttributes& |
135 | | GetOriginAttributes() const |
136 | 0 | { |
137 | 0 | return mOriginAttributes; |
138 | 0 | } |
139 | | |
140 | | const nsString& |
141 | | CacheName() const |
142 | 0 | { |
143 | 0 | return mCacheName; |
144 | 0 | } |
145 | | |
146 | | nsLoadFlags |
147 | | GetImportsLoadFlags() const |
148 | 0 | { |
149 | 0 | return mImportsLoadFlags; |
150 | 0 | } |
151 | | |
152 | | uint64_t |
153 | | ID() const |
154 | | { |
155 | | return mDescriptor.Id(); |
156 | | } |
157 | | |
158 | | const ServiceWorkerDescriptor& |
159 | | Descriptor() const |
160 | 0 | { |
161 | 0 | return mDescriptor; |
162 | 0 | } |
163 | | |
164 | | void |
165 | | UpdateState(ServiceWorkerState aState); |
166 | | |
167 | | // Only used to set initial state when loading from disk! |
168 | | void |
169 | | SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState) |
170 | | { |
171 | | MOZ_ASSERT(NS_IsMainThread()); |
172 | | mDescriptor.SetState(aState); |
173 | | } |
174 | | |
175 | | void |
176 | | SetHandlesFetch(bool aHandlesFetch) |
177 | 0 | { |
178 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
179 | 0 | MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch == Unknown); |
180 | 0 | mHandlesFetch = aHandlesFetch ? Enabled : Disabled; |
181 | 0 | } |
182 | | |
183 | | void |
184 | | SetRegistrationVersion(uint64_t aVersion); |
185 | | |
186 | | bool |
187 | | HandlesFetch() const |
188 | 0 | { |
189 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
190 | 0 | MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch != Unknown); |
191 | 0 | return mHandlesFetch != Disabled; |
192 | 0 | } |
193 | | |
194 | | void |
195 | | UpdateInstalledTime(); |
196 | | |
197 | | void |
198 | | UpdateActivatedTime(); |
199 | | |
200 | | void |
201 | | UpdateRedundantTime(); |
202 | | |
203 | | int64_t |
204 | | GetInstalledTime() const |
205 | 0 | { |
206 | 0 | return mInstalledTime; |
207 | 0 | } |
208 | | |
209 | | void |
210 | | SetInstalledTime(const int64_t aTime) |
211 | 0 | { |
212 | 0 | if (aTime == 0) { |
213 | 0 | return; |
214 | 0 | } |
215 | 0 | |
216 | 0 | mInstalledTime = aTime; |
217 | 0 | } |
218 | | |
219 | | int64_t |
220 | | GetActivatedTime() const |
221 | 0 | { |
222 | 0 | return mActivatedTime; |
223 | 0 | } |
224 | | |
225 | | void |
226 | | SetActivatedTime(const int64_t aTime) |
227 | 0 | { |
228 | 0 | if (aTime == 0) { |
229 | 0 | return; |
230 | 0 | } |
231 | 0 | |
232 | 0 | mActivatedTime = aTime; |
233 | 0 | } |
234 | | }; |
235 | | |
236 | | } // namespace dom |
237 | | } // namespace mozilla |
238 | | |
239 | | #endif // mozilla_dom_serviceworkerinfo_h |