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_serviceworkerregistrationinfo_h
8 : #define mozilla_dom_workers_serviceworkerregistrationinfo_h
9 :
10 : #include "mozilla/dom/workers/ServiceWorkerInfo.h"
11 :
12 : namespace mozilla {
13 : namespace dom {
14 : namespace workers {
15 :
16 : class ServiceWorkerRegistrationInfo final
17 : : public nsIServiceWorkerRegistrationInfo
18 : {
19 : uint32_t mControlledDocumentsCounter;
20 :
21 : enum
22 : {
23 : NoUpdate,
24 : NeedTimeCheckAndUpdate,
25 : NeedUpdate
26 : } mUpdateState;
27 :
28 : // Timestamp to track SWR's last update time
29 : PRTime mCreationTime;
30 : TimeStamp mCreationTimeStamp;
31 : // The time of update is 0, if SWR've never been updated yet.
32 : PRTime mLastUpdateTime;
33 :
34 : nsLoadFlags mLoadFlags;
35 :
36 : RefPtr<ServiceWorkerInfo> mEvaluatingWorker;
37 : RefPtr<ServiceWorkerInfo> mActiveWorker;
38 : RefPtr<ServiceWorkerInfo> mWaitingWorker;
39 : RefPtr<ServiceWorkerInfo> mInstallingWorker;
40 :
41 : virtual ~ServiceWorkerRegistrationInfo();
42 :
43 : public:
44 : NS_DECL_ISUPPORTS
45 : NS_DECL_NSISERVICEWORKERREGISTRATIONINFO
46 :
47 : const nsCString mScope;
48 :
49 : nsCOMPtr<nsIPrincipal> mPrincipal;
50 :
51 : nsTArray<nsCOMPtr<nsIServiceWorkerRegistrationInfoListener>> mListeners;
52 :
53 : // When unregister() is called on a registration, it is not immediately
54 : // removed since documents may be controlled. It is marked as
55 : // pendingUninstall and when all controlling documents go away, removed.
56 : bool mPendingUninstall;
57 :
58 : ServiceWorkerRegistrationInfo(const nsACString& aScope,
59 : nsIPrincipal* aPrincipal,
60 : nsLoadFlags aLoadFlags);
61 :
62 : already_AddRefed<ServiceWorkerInfo>
63 0 : Newest() const
64 : {
65 0 : RefPtr<ServiceWorkerInfo> newest;
66 0 : if (mInstallingWorker) {
67 0 : newest = mInstallingWorker;
68 0 : } else if (mWaitingWorker) {
69 0 : newest = mWaitingWorker;
70 : } else {
71 0 : newest = mActiveWorker;
72 : }
73 :
74 0 : return newest.forget();
75 : }
76 :
77 : already_AddRefed<ServiceWorkerInfo>
78 : GetServiceWorkerInfoById(uint64_t aId);
79 :
80 : void
81 0 : StartControllingADocument()
82 : {
83 0 : ++mControlledDocumentsCounter;
84 0 : }
85 :
86 : void
87 0 : StopControllingADocument()
88 : {
89 0 : MOZ_ASSERT(mControlledDocumentsCounter);
90 0 : --mControlledDocumentsCounter;
91 0 : }
92 :
93 : bool
94 0 : IsControllingDocuments() const
95 : {
96 0 : return mActiveWorker && mControlledDocumentsCounter;
97 : }
98 :
99 : void
100 : Clear();
101 :
102 : void
103 : TryToActivateAsync();
104 :
105 : void
106 : TryToActivate();
107 :
108 : void
109 : Activate();
110 :
111 : void
112 : FinishActivate(bool aSuccess);
113 :
114 : void
115 : RefreshLastUpdateCheckTime();
116 :
117 : bool
118 : IsLastUpdateCheckTimeOverOneDay() const;
119 :
120 : void
121 : MaybeScheduleTimeCheckAndUpdate();
122 :
123 : void
124 : MaybeScheduleUpdate();
125 :
126 : bool
127 : CheckAndClearIfUpdateNeeded();
128 :
129 : ServiceWorkerInfo*
130 : GetEvaluating() const;
131 :
132 : ServiceWorkerInfo*
133 : GetInstalling() const;
134 :
135 : ServiceWorkerInfo*
136 : GetWaiting() const;
137 :
138 : ServiceWorkerInfo*
139 : GetActive() const;
140 :
141 : ServiceWorkerInfo*
142 : GetByID(uint64_t aID) const;
143 :
144 : // Set the given worker as the evaluating service worker. The worker
145 : // state is not changed.
146 : void
147 : SetEvaluating(ServiceWorkerInfo* aServiceWorker);
148 :
149 : // Remove an existing evaluating worker, if present. The worker will
150 : // be transitioned to the Redundant state.
151 : void
152 : ClearEvaluating();
153 :
154 : // Remove an existing installing worker, if present. The worker will
155 : // be transitioned to the Redundant state.
156 : void
157 : ClearInstalling();
158 :
159 : // Transition the current evaluating worker to be the installing worker. The
160 : // worker's state is update to Installing.
161 : void
162 : TransitionEvaluatingToInstalling();
163 :
164 : // Transition the current installing worker to be the waiting worker. The
165 : // worker's state is updated to Installed.
166 : void
167 : TransitionInstallingToWaiting();
168 :
169 : // Override the current active worker. This is used during browser
170 : // initialization to load persisted workers. Its also used to propagate
171 : // active workers across child processes in e10s. This second use will
172 : // go away once the ServiceWorkerManager moves to the parent process.
173 : // The worker is transitioned to the Activated state.
174 : void
175 : SetActive(ServiceWorkerInfo* aServiceWorker);
176 :
177 : // Transition the current waiting worker to be the new active worker. The
178 : // worker is updated to the Activating state.
179 : void
180 : TransitionWaitingToActive();
181 :
182 : // Determine if the registration is actively performing work.
183 : bool
184 : IsIdle() const;
185 :
186 : nsLoadFlags
187 : GetLoadFlags() const;
188 :
189 : void
190 : SetLoadFlags(nsLoadFlags aLoadFlags);
191 :
192 : int64_t
193 : GetLastUpdateTime() const;
194 :
195 : void
196 : SetLastUpdateTime(const int64_t aTime);
197 :
198 : private:
199 : enum TransitionType {
200 : TransitionToNextState = 0,
201 : Invalidate
202 : };
203 :
204 : // Queued as a runnable from UpdateRegistrationStateProperties.
205 : void
206 : AsyncUpdateRegistrationStateProperties(WhichServiceWorker aWorker, TransitionType aType);
207 :
208 : // Roughly equivalent to [[Update Registration State algorithm]]. Make sure
209 : // this is called *before* updating SW instances' state, otherwise they
210 : // may get CC-ed.
211 : void
212 : UpdateRegistrationStateProperties(WhichServiceWorker aWorker, TransitionType aType);
213 :
214 : // Used by devtools to track changes to the properties of *nsIServiceWorkerRegistrationInfo*.
215 : // Note, this doesn't necessarily need to be in sync with the DOM registration objects, but
216 : // it does need to be called in the same task that changed |mInstallingWorker|,
217 : // |mWaitingWorker| or |mActiveWorker|.
218 : void
219 : NotifyChromeRegistrationListeners();
220 : };
221 :
222 : } // namespace workers
223 : } // namespace dom
224 : } // namespace mozilla
225 :
226 : #endif // mozilla_dom_workers_serviceworkerregistrationinfo_h
|