OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <queue> |
9 #include <set> | 10 #include <set> |
10 #include <string> | 11 #include <string> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
14 #include "base/callback.h" | 15 #include "base/callback.h" |
15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
16 #include "base/id_map.h" | 17 #include "base/id_map.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // one of them is activated. This class connects the actual script with a | 62 // one of them is activated. This class connects the actual script with a |
62 // running worker. | 63 // running worker. |
63 class CONTENT_EXPORT ServiceWorkerVersion | 64 class CONTENT_EXPORT ServiceWorkerVersion |
64 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), | 65 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), |
65 public EmbeddedWorkerInstance::Listener { | 66 public EmbeddedWorkerInstance::Listener { |
66 public: | 67 public: |
67 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | 68 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
68 typedef base::Callback<void(ServiceWorkerStatusCode, | 69 typedef base::Callback<void(ServiceWorkerStatusCode, |
69 ServiceWorkerFetchEventResult, | 70 ServiceWorkerFetchEventResult, |
70 const ServiceWorkerResponse&)> FetchCallback; | 71 const ServiceWorkerResponse&)> FetchCallback; |
71 typedef base::Callback<void(ServiceWorkerStatusCode, bool)> | 72 typedef base::Callback<void(ServiceWorkerStatusCode, |
| 73 bool /* accept_connction */)> |
72 CrossOriginConnectCallback; | 74 CrossOriginConnectCallback; |
73 | 75 |
74 enum RunningStatus { | 76 enum RunningStatus { |
75 STOPPED = EmbeddedWorkerInstance::STOPPED, | 77 STOPPED = EmbeddedWorkerInstance::STOPPED, |
76 STARTING = EmbeddedWorkerInstance::STARTING, | 78 STARTING = EmbeddedWorkerInstance::STARTING, |
77 RUNNING = EmbeddedWorkerInstance::RUNNING, | 79 RUNNING = EmbeddedWorkerInstance::RUNNING, |
78 STOPPING = EmbeddedWorkerInstance::STOPPING, | 80 STOPPING = EmbeddedWorkerInstance::STOPPING, |
79 }; | 81 }; |
80 | 82 |
81 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) | 83 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 300 |
299 private: | 301 private: |
300 friend class base::RefCounted<ServiceWorkerVersion>; | 302 friend class base::RefCounted<ServiceWorkerVersion>; |
301 friend class ServiceWorkerURLRequestJobTest; | 303 friend class ServiceWorkerURLRequestJobTest; |
302 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest, | 304 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest, |
303 ActivateWaitingVersion); | 305 ActivateWaitingVersion); |
304 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, ScheduleStopWorker); | 306 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, ScheduleStopWorker); |
305 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, KeepAlive); | 307 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, KeepAlive); |
306 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, ListenerAvailability); | 308 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, ListenerAvailability); |
307 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, SetDevToolsAttached); | 309 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, SetDevToolsAttached); |
| 310 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWaitForeverInFetchTest, RequestTimeout); |
308 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerFailToStartTest, Timeout); | 311 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerFailToStartTest, Timeout); |
309 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, | 312 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, |
310 TimeoutStartingWorker); | 313 TimeoutStartingWorker); |
311 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, | 314 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, |
312 TimeoutWorkerInEvent); | 315 TimeoutWorkerInEvent); |
313 friend class ServiceWorkerVersionBrowserTest; | 316 friend class ServiceWorkerVersionBrowserTest; |
314 | 317 |
315 typedef ServiceWorkerVersion self; | 318 typedef ServiceWorkerVersion self; |
316 | 319 |
| 320 enum RequestType { |
| 321 REQUEST_ACTIVATE, |
| 322 REQUEST_INSTALL, |
| 323 REQUEST_FETCH, |
| 324 REQUEST_SYNC, |
| 325 REQUEST_NOTIFICATION_CLICK, |
| 326 REQUEST_PUSH, |
| 327 REQUEST_GEOFENCING, |
| 328 REQUEST_CROSS_ORIGIN_CONNECT |
| 329 }; |
317 enum PingState { NOT_PINGING, PINGING, PING_TIMED_OUT }; | 330 enum PingState { NOT_PINGING, PINGING, PING_TIMED_OUT }; |
318 | 331 |
| 332 struct RequestInfo { |
| 333 RequestInfo(int id, RequestType type); |
| 334 ~RequestInfo(); |
| 335 int id; |
| 336 RequestType type; |
| 337 base::TimeTicks time; |
| 338 }; |
| 339 |
319 // Timeout for the worker to start. | 340 // Timeout for the worker to start. |
320 static const int kStartWorkerTimeoutMinutes; | 341 static const int kStartWorkerTimeoutMinutes; |
| 342 // Timeout for a request to be handled. |
| 343 static const int kRequestTimeoutMinutes; |
321 | 344 |
322 ~ServiceWorkerVersion() override; | 345 ~ServiceWorkerVersion() override; |
323 | 346 |
324 // EmbeddedWorkerInstance::Listener overrides: | 347 // EmbeddedWorkerInstance::Listener overrides: |
325 void OnScriptLoaded() override; | 348 void OnScriptLoaded() override; |
326 void OnStarting() override; | 349 void OnStarting() override; |
327 void OnStarted() override; | 350 void OnStarted() override; |
328 void OnStopping() override; | 351 void OnStopping() override; |
329 void OnStopped(EmbeddedWorkerInstance::Status old_status) override; | 352 void OnStopped(EmbeddedWorkerInstance::Status old_status) override; |
330 void OnReportException(const base::string16& error_message, | 353 void OnReportException(const base::string16& error_message, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 | 446 |
424 // RecordStartWorkerResult is added as a start callback by StartTimeoutTimer | 447 // RecordStartWorkerResult is added as a start callback by StartTimeoutTimer |
425 // and records metrics about startup. | 448 // and records metrics about startup. |
426 void RecordStartWorkerResult(ServiceWorkerStatusCode status); | 449 void RecordStartWorkerResult(ServiceWorkerStatusCode status); |
427 | 450 |
428 void DoomInternal(); | 451 void DoomInternal(); |
429 | 452 |
430 template <typename IDMAP> | 453 template <typename IDMAP> |
431 void RemoveCallbackAndStopIfDoomed(IDMAP* callbacks, int request_id); | 454 void RemoveCallbackAndStopIfDoomed(IDMAP* callbacks, int request_id); |
432 | 455 |
| 456 template <typename CallbackType> |
| 457 int AddRequest(const CallbackType& callback, |
| 458 IDMap<CallbackType, IDMapOwnPointer>* callback_map, |
| 459 RequestType request_type); |
| 460 |
| 461 bool OnRequestTimeout(const RequestInfo& info); |
| 462 void SetAllRequestTimes(const base::TimeTicks& ticks); |
| 463 |
433 const int64 version_id_; | 464 const int64 version_id_; |
434 int64 registration_id_; | 465 int64 registration_id_; |
435 GURL script_url_; | 466 GURL script_url_; |
436 GURL scope_; | 467 GURL scope_; |
437 Status status_; | 468 Status status_; |
438 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; | 469 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; |
439 scoped_ptr<ServiceWorkerCacheListener> cache_listener_; | 470 scoped_ptr<ServiceWorkerCacheListener> cache_listener_; |
440 std::vector<StatusCallback> start_callbacks_; | 471 std::vector<StatusCallback> start_callbacks_; |
441 std::vector<StatusCallback> stop_callbacks_; | 472 std::vector<StatusCallback> stop_callbacks_; |
442 std::vector<base::Closure> status_change_callbacks_; | 473 std::vector<base::Closure> status_change_callbacks_; |
(...skipping 23 matching lines...) Expand all Loading... |
466 base::RepeatingTimer<ServiceWorkerVersion> timeout_timer_; | 497 base::RepeatingTimer<ServiceWorkerVersion> timeout_timer_; |
467 // Holds the time the worker last started being considered idle. | 498 // Holds the time the worker last started being considered idle. |
468 base::TimeTicks idle_time_; | 499 base::TimeTicks idle_time_; |
469 // Holds the time that an outstanding ping was sent to the worker. | 500 // Holds the time that an outstanding ping was sent to the worker. |
470 base::TimeTicks ping_time_; | 501 base::TimeTicks ping_time_; |
471 // The state of the ping protocol. | 502 // The state of the ping protocol. |
472 PingState ping_state_; | 503 PingState ping_state_; |
473 // Holds the time that the outstanding StartWorker() request started. | 504 // Holds the time that the outstanding StartWorker() request started. |
474 base::TimeTicks start_time_; | 505 base::TimeTicks start_time_; |
475 | 506 |
| 507 // New requests are added to |requests_| along with their entry in a callback |
| 508 // map. The timeout timer periodically checks |requests_| for entries that |
| 509 // should time out or have already been fulfilled (i.e., removed from the |
| 510 // callback map). |
| 511 std::queue<RequestInfo> requests_; |
| 512 |
476 bool is_doomed_ = false; | 513 bool is_doomed_ = false; |
477 bool skip_waiting_ = false; | 514 bool skip_waiting_ = false; |
478 bool skip_recording_startup_time_ = false; | 515 bool skip_recording_startup_time_ = false; |
479 | 516 |
480 std::vector<int> pending_skip_waiting_requests_; | 517 std::vector<int> pending_skip_waiting_requests_; |
481 scoped_ptr<net::HttpResponseInfo> main_script_http_info_; | 518 scoped_ptr<net::HttpResponseInfo> main_script_http_info_; |
482 | 519 |
483 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; | 520 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
484 | 521 |
485 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 522 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
486 }; | 523 }; |
487 | 524 |
488 } // namespace content | 525 } // namespace content |
489 | 526 |
490 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 527 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
OLD | NEW |