1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/id_map.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/timer/timer.h"
20 #include "content/browser/service_worker/embedded_worker_instance.h"
21 #include "content/browser/service_worker/service_worker_cache_listener.h"
22 #include "content/browser/service_worker/service_worker_script_cache_map.h"
23 #include "content/common/content_export.h"
24 #include "content/common/service_worker/service_worker_status_code.h"
25 #include "content/common/service_worker/service_worker_types.h"
26 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h"
27 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
32 struct WebCircularGeofencingRegion
;
37 class EmbeddedWorkerRegistry
;
38 class ServiceWorkerContextCore
;
39 class ServiceWorkerProviderHost
;
40 class ServiceWorkerRegistration
;
41 class ServiceWorkerVersionInfo
;
43 // This class corresponds to a specific version of a ServiceWorker
44 // script for a given pattern. When a script is upgraded, there may be
45 // more than one ServiceWorkerVersion "running" at a time, but only
46 // one of them is activated. This class connects the actual script with a
48 class CONTENT_EXPORT ServiceWorkerVersion
49 : NON_EXPORTED_BASE(public base::RefCounted
<ServiceWorkerVersion
>),
50 public EmbeddedWorkerInstance::Listener
{
52 typedef base::Callback
<void(ServiceWorkerStatusCode
)> StatusCallback
;
53 typedef base::Callback
<void(ServiceWorkerStatusCode
,
54 const IPC::Message
& message
)> MessageCallback
;
55 typedef base::Callback
<void(ServiceWorkerStatusCode
,
56 ServiceWorkerFetchEventResult
,
57 const ServiceWorkerResponse
&)> FetchCallback
;
60 STOPPED
= EmbeddedWorkerInstance::STOPPED
,
61 STARTING
= EmbeddedWorkerInstance::STARTING
,
62 RUNNING
= EmbeddedWorkerInstance::RUNNING
,
63 STOPPING
= EmbeddedWorkerInstance::STOPPING
,
66 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED)
67 // should be persisted unlike running status.
69 NEW
, // The version is just created.
70 INSTALLING
, // Install event is dispatched and being handled.
71 INSTALLED
, // Install event is finished and is ready to be activated.
72 ACTIVATING
, // Activate event is dispatched and being handled.
73 ACTIVATED
, // Activation is finished and can run as activated.
74 REDUNDANT
, // The version is no longer running as activated, due to
75 // unregistration or replace.
80 virtual void OnWorkerStarted(ServiceWorkerVersion
* version
) {}
81 virtual void OnWorkerStopped(ServiceWorkerVersion
* version
) {}
82 virtual void OnVersionStateChanged(ServiceWorkerVersion
* version
) {}
83 virtual void OnErrorReported(ServiceWorkerVersion
* version
,
84 const base::string16
& error_message
,
87 const GURL
& source_url
) {}
88 virtual void OnReportConsoleMessage(ServiceWorkerVersion
* version
,
89 int source_identifier
,
91 const base::string16
& message
,
93 const GURL
& source_url
) {}
94 // Fires when a version transitions from having a controllee to not.
95 virtual void OnNoControllees(ServiceWorkerVersion
* version
) {}
98 virtual ~Listener() {}
101 ServiceWorkerVersion(
102 ServiceWorkerRegistration
* registration
,
103 const GURL
& script_url
,
105 base::WeakPtr
<ServiceWorkerContextCore
> context
);
107 int64
version_id() const { return version_id_
; }
108 int64
registration_id() const { return registration_id_
; }
109 const GURL
& script_url() const { return script_url_
; }
110 const GURL
& scope() const { return scope_
; }
111 RunningStatus
running_status() const {
112 return static_cast<RunningStatus
>(embedded_worker_
->status());
114 ServiceWorkerVersionInfo
GetInfo();
115 Status
status() const { return status_
; }
117 // This sets the new status and also run status change callbacks
118 // if there're any (see RegisterStatusChangeCallback).
119 void SetStatus(Status status
);
121 // Registers status change callback. (This is for one-off observation,
122 // the consumer needs to re-register if it wants to continue observing
124 void RegisterStatusChangeCallback(const base::Closure
& callback
);
126 // Starts an embedded worker for this version.
127 // This returns OK (success) if the worker is already running.
128 void StartWorker(const StatusCallback
& callback
);
130 // Starts an embedded worker for this version.
131 // |pause_after_download| notifies worker to pause after download finished
132 // which could be resumed by EmbeddedWorkerInstance::ResumeAfterDownload.
133 // This returns OK (success) if the worker is already running.
134 void StartWorker(bool pause_after_download
,
135 const StatusCallback
& callback
);
137 // Stops an embedded worker for this version.
138 // This returns OK (success) if the worker is already stopped.
139 void StopWorker(const StatusCallback
& callback
);
141 // Schedules an update to be run 'soon'.
142 void ScheduleUpdate();
144 // If an update is scheduled but not yet started, this resets the timer
145 // delaying the start time by a 'small' amount.
146 void DeferScheduledUpdate();
148 // Starts an update now.
151 // Sends an IPC message to the worker.
152 // If the worker is not running this first tries to start it by
153 // calling StartWorker internally.
154 // |callback| can be null if the sender does not need to know if the
155 // message is successfully sent or not.
156 void SendMessage(const IPC::Message
& message
, const StatusCallback
& callback
);
158 // Sends a message event to the associated embedded worker.
159 void DispatchMessageEvent(const base::string16
& message
,
160 const std::vector
<int>& sent_message_port_ids
,
161 const StatusCallback
& callback
);
163 // Sends install event to the associated embedded worker and asynchronously
164 // calls |callback| when it errors out or it gets a response from the worker
165 // to notify install completion.
166 // |active_version_id| must be a valid positive ID
167 // if there's an activated (previous) version running.
169 // This must be called when the status() is NEW. Calling this changes
170 // the version's status to INSTALLING.
171 // Upon completion, the version's status will be changed to INSTALLED
172 // on success, or back to NEW on failure.
173 void DispatchInstallEvent(int active_version_id
,
174 const StatusCallback
& callback
);
176 // Sends activate event to the associated embedded worker and asynchronously
177 // calls |callback| when it errors out or it gets a response from the worker
178 // to notify activation completion.
180 // This must be called when the status() is INSTALLED. Calling this changes
181 // the version's status to ACTIVATING.
182 // Upon completion, the version's status will be changed to ACTIVATED
183 // on success, or back to INSTALLED on failure.
184 void DispatchActivateEvent(const StatusCallback
& callback
);
186 // Sends fetch event to the associated embedded worker and calls
187 // |callback| with the response from the worker.
189 // This must be called when the status() is ACTIVATED. Calling this in other
190 // statuses will result in an error SERVICE_WORKER_ERROR_FAILED.
191 void DispatchFetchEvent(const ServiceWorkerFetchRequest
& request
,
192 const base::Closure
& prepare_callback
,
193 const FetchCallback
& fetch_callback
);
195 // Sends sync event to the associated embedded worker and asynchronously calls
196 // |callback| when it errors out or it gets a response from the worker to
197 // notify completion.
199 // This must be called when the status() is ACTIVATED.
200 void DispatchSyncEvent(const StatusCallback
& callback
);
202 // Sends push event to the associated embedded worker and asynchronously calls
203 // |callback| when it errors out or it gets a response from the worker to
204 // notify completion.
206 // This must be called when the status() is ACTIVATED.
207 void DispatchPushEvent(const StatusCallback
& callback
,
208 const std::string
& data
);
210 // Sends geofencing event to the associated embedded worker and asynchronously
211 // calls |callback| when it errors out or it gets a response from the worker
212 // to notify completion.
214 // This must be called when the status() is ACTIVATED.
215 void DispatchGeofencingEvent(
216 const StatusCallback
& callback
,
217 blink::WebGeofencingEventType event_type
,
218 const std::string
& region_id
,
219 const blink::WebCircularGeofencingRegion
& region
);
221 // Adds and removes |provider_host| as a controllee of this ServiceWorker.
222 // A potential controllee is a host having the version as its .installing
223 // or .waiting version.
224 void AddControllee(ServiceWorkerProviderHost
* provider_host
);
225 void RemoveControllee(ServiceWorkerProviderHost
* provider_host
);
227 // Returns if it has controllee.
228 bool HasControllee() const { return !controllee_map_
.empty(); }
230 // Adds and removes Listeners.
231 void AddListener(Listener
* listener
);
232 void RemoveListener(Listener
* listener
);
234 ServiceWorkerScriptCacheMap
* script_cache_map() { return &script_cache_map_
; }
235 EmbeddedWorkerInstance
* embedded_worker() { return embedded_worker_
.get(); }
237 // Dooms this version to have REDUNDANT status and its resources deleted. If
238 // the version is controlling a page, these changes will happen when the
239 // version no longer controls any pages.
241 bool is_doomed() const { return is_doomed_
; }
244 friend class base::RefCounted
<ServiceWorkerVersion
>;
245 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest
,
246 ActivateWaitingVersion
);
247 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest
, ScheduleStopWorker
);
248 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest
, ListenerAvailability
);
249 typedef ServiceWorkerVersion self
;
250 typedef std::map
<ServiceWorkerProviderHost
*, int> ControlleeMap
;
251 typedef IDMap
<ServiceWorkerProviderHost
> ControlleeByIDMap
;
253 ~ServiceWorkerVersion() override
;
255 // EmbeddedWorkerInstance::Listener overrides:
256 void OnStarted() override
;
257 void OnStopped() override
;
258 void OnReportException(const base::string16
& error_message
,
261 const GURL
& source_url
) override
;
262 void OnReportConsoleMessage(int source_identifier
,
264 const base::string16
& message
,
266 const GURL
& source_url
) override
;
267 bool OnMessageReceived(const IPC::Message
& message
) override
;
269 void OnStartMessageSent(ServiceWorkerStatusCode status
);
271 void DispatchInstallEventAfterStartWorker(int active_version_id
,
272 const StatusCallback
& callback
);
273 void DispatchActivateEventAfterStartWorker(const StatusCallback
& callback
);
275 void DispatchMessageEventInternal(
276 const base::string16
& message
,
277 const std::vector
<int>& sent_message_port_ids
,
278 const StatusCallback
& callback
);
281 void OnGetClientDocuments(int request_id
);
282 void OnActivateEventFinished(int request_id
,
283 blink::WebServiceWorkerEventResult result
);
284 void OnInstallEventFinished(int request_id
,
285 blink::WebServiceWorkerEventResult result
);
286 void OnFetchEventFinished(int request_id
,
287 ServiceWorkerFetchEventResult result
,
288 const ServiceWorkerResponse
& response
);
289 void OnSyncEventFinished(int request_id
);
290 void OnPushEventFinished(int request_id
);
291 void OnGeofencingEventFinished(int request_id
);
292 void OnPostMessageToDocument(int client_id
,
293 const base::string16
& message
,
294 const std::vector
<int>& sent_message_port_ids
);
296 void ScheduleStopWorker();
299 const int64 version_id_
;
300 int64 registration_id_
;
304 scoped_ptr
<EmbeddedWorkerInstance
> embedded_worker_
;
305 scoped_ptr
<ServiceWorkerCacheListener
> cache_listener_
;
306 std::vector
<StatusCallback
> start_callbacks_
;
307 std::vector
<StatusCallback
> stop_callbacks_
;
308 std::vector
<base::Closure
> status_change_callbacks_
;
310 // Message callbacks.
311 IDMap
<StatusCallback
, IDMapOwnPointer
> activate_callbacks_
;
312 IDMap
<StatusCallback
, IDMapOwnPointer
> install_callbacks_
;
313 IDMap
<FetchCallback
, IDMapOwnPointer
> fetch_callbacks_
;
314 IDMap
<StatusCallback
, IDMapOwnPointer
> sync_callbacks_
;
315 IDMap
<StatusCallback
, IDMapOwnPointer
> push_callbacks_
;
316 IDMap
<StatusCallback
, IDMapOwnPointer
> geofencing_callbacks_
;
318 ControlleeMap controllee_map_
;
319 ControlleeByIDMap controllee_by_id_
;
320 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
321 ObserverList
<Listener
> listeners_
;
322 ServiceWorkerScriptCacheMap script_cache_map_
;
323 base::OneShotTimer
<ServiceWorkerVersion
> stop_worker_timer_
;
324 base::OneShotTimer
<ServiceWorkerVersion
> update_timer_
;
327 base::WeakPtrFactory
<ServiceWorkerVersion
> weak_factory_
;
329 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion
);
332 } // namespace content
334 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_