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 <queue> |
10 #include <set> | 10 #include <set> |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 // |callback| with the response from the worker. | 207 // |callback| with the response from the worker. |
208 // | 208 // |
209 // This must be called when the status() is ACTIVATED. Calling this in other | 209 // This must be called when the status() is ACTIVATED. Calling this in other |
210 // statuses will result in an error SERVICE_WORKER_ERROR_FAILED. | 210 // statuses will result in an error SERVICE_WORKER_ERROR_FAILED. |
211 void DispatchFetchEvent(const ServiceWorkerFetchRequest& request, | 211 void DispatchFetchEvent(const ServiceWorkerFetchRequest& request, |
212 const base::Closure& prepare_callback, | 212 const base::Closure& prepare_callback, |
213 const FetchCallback& fetch_callback); | 213 const FetchCallback& fetch_callback); |
214 | 214 |
215 // Sends sync event to the associated embedded worker and asynchronously calls | 215 // Sends sync event to the associated embedded worker and asynchronously calls |
216 // |callback| when it errors out or it gets a response from the worker to | 216 // |callback| when it errors out or it gets a response from the worker to |
217 // notify completion. | 217 // notify completion. |max_duration| is how long the event is allowed to run |
| 218 // for before it times out. |
218 // | 219 // |
219 // This must be called when the status() is ACTIVATED. | 220 // This must be called when the status() is ACTIVATED. |
220 void DispatchSyncEvent(BackgroundSyncRegistrationHandle::HandleId handle_id, | 221 void DispatchSyncEvent(BackgroundSyncRegistrationHandle::HandleId handle_id, |
221 BackgroundSyncEventLastChance last_chance, | 222 BackgroundSyncEventLastChance last_chance, |
| 223 base::TimeDelta max_duration, |
222 const StatusCallback& callback); | 224 const StatusCallback& callback); |
223 | 225 |
224 // Sends notificationclick event to the associated embedded worker and | 226 // Sends notificationclick event to the associated embedded worker and |
225 // asynchronously calls |callback| when it errors out or it gets a response | 227 // asynchronously calls |callback| when it errors out or it gets a response |
226 // from the worker to notify completion. | 228 // from the worker to notify completion. |
227 // | 229 // |
228 // This must be called when the status() is ACTIVATED. | 230 // This must be called when the status() is ACTIVATED. |
229 void DispatchNotificationClickEvent( | 231 void DispatchNotificationClickEvent( |
230 const StatusCallback& callback, | 232 const StatusCallback& callback, |
231 int64_t persistent_notification_id, | 233 int64_t persistent_notification_id, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 | 338 |
337 // Simulate ping timeout. Should be used for tests-only. | 339 // Simulate ping timeout. Should be used for tests-only. |
338 void SimulatePingTimeoutForTesting(); | 340 void SimulatePingTimeoutForTesting(); |
339 | 341 |
340 private: | 342 private: |
341 friend class base::RefCounted<ServiceWorkerVersion>; | 343 friend class base::RefCounted<ServiceWorkerVersion>; |
342 friend class ServiceWorkerMetrics; | 344 friend class ServiceWorkerMetrics; |
343 friend class ServiceWorkerURLRequestJobTest; | 345 friend class ServiceWorkerURLRequestJobTest; |
344 friend class ServiceWorkerStallInStoppingTest; | 346 friend class ServiceWorkerStallInStoppingTest; |
345 friend class ServiceWorkerVersionBrowserTest; | 347 friend class ServiceWorkerVersionBrowserTest; |
| 348 friend class ServiceWorkerVersionTest; |
346 | 349 |
347 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest, | 350 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerControlleeRequestHandlerTest, |
348 ActivateWaitingVersion); | 351 ActivateWaitingVersion); |
349 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, IdleTimeout); | 352 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, IdleTimeout); |
350 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, SetDevToolsAttached); | 353 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, SetDevToolsAttached); |
351 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_FreshWorker); | 354 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_FreshWorker); |
352 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, | 355 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, |
353 StaleUpdate_NonActiveWorker); | 356 StaleUpdate_NonActiveWorker); |
354 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_StartWorker); | 357 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_StartWorker); |
355 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_RunningWorker); | 358 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, StaleUpdate_RunningWorker); |
356 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, | 359 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, |
357 StaleUpdate_DoNotDeferTimer); | 360 StaleUpdate_DoNotDeferTimer); |
358 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWaitForeverInFetchTest, RequestTimeout); | 361 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWaitForeverInFetchTest, RequestTimeout); |
359 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerFailToStartTest, Timeout); | 362 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerFailToStartTest, Timeout); |
360 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, | 363 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, |
361 TimeoutStartingWorker); | 364 TimeoutStartingWorker); |
362 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, | 365 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, |
363 TimeoutWorkerInEvent); | 366 TimeoutWorkerInEvent); |
364 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); | 367 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); |
365 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); | 368 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); |
366 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, | 369 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, |
367 RegisterForeignFetchScopes); | 370 RegisterForeignFetchScopes); |
| 371 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTest, RequestCustomizedTimeout); |
| 372 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerWaitForeverInFetchTest, |
| 373 MixedRequestTimeouts); |
368 | 374 |
369 class Metrics; | 375 class Metrics; |
370 class PingController; | 376 class PingController; |
371 | 377 |
372 typedef ServiceWorkerVersion self; | |
373 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; | |
374 | |
375 // Used for UMA; add new entries to the end, before NUM_REQUEST_TYPES. | 378 // Used for UMA; add new entries to the end, before NUM_REQUEST_TYPES. |
376 enum RequestType { | 379 enum RequestType { |
377 REQUEST_ACTIVATE, | 380 REQUEST_ACTIVATE, |
378 REQUEST_INSTALL, | 381 REQUEST_INSTALL, |
379 REQUEST_FETCH, | 382 REQUEST_FETCH, |
380 REQUEST_SYNC, | 383 REQUEST_SYNC, |
381 REQUEST_NOTIFICATION_CLICK, | 384 REQUEST_NOTIFICATION_CLICK, |
382 REQUEST_PUSH, | 385 REQUEST_PUSH, |
383 REQUEST_GEOFENCING, | 386 REQUEST_GEOFENCING, |
384 REQUEST_SERVICE_PORT_CONNECT, | 387 REQUEST_SERVICE_PORT_CONNECT, |
385 NUM_REQUEST_TYPES | 388 NUM_REQUEST_TYPES |
386 }; | 389 }; |
387 | 390 |
388 struct RequestInfo { | 391 struct RequestInfo { |
389 RequestInfo(int id, RequestType type, const base::TimeTicks& time); | 392 RequestInfo(int id, RequestType type, const base::TimeTicks& expiration); |
390 ~RequestInfo(); | 393 ~RequestInfo(); |
| 394 bool operator>(const RequestInfo& other) const; |
391 int id; | 395 int id; |
392 RequestType type; | 396 RequestType type; |
393 base::TimeTicks time; | 397 base::TimeTicks expiration; |
394 }; | 398 }; |
395 | 399 |
396 template <typename CallbackType> | 400 template <typename CallbackType> |
397 struct PendingRequest { | 401 struct PendingRequest { |
398 PendingRequest(const CallbackType& callback, const base::TimeTicks& time); | 402 PendingRequest(const CallbackType& callback, const base::TimeTicks& time); |
399 ~PendingRequest(); | 403 ~PendingRequest(); |
400 | 404 |
401 CallbackType callback; | 405 CallbackType callback; |
402 base::TimeTicks start_time; | 406 base::TimeTicks start_time; |
403 }; | 407 }; |
404 | 408 |
| 409 typedef ServiceWorkerVersion self; |
| 410 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; |
| 411 using RequestInfoPriorityQueue = |
| 412 std::priority_queue<RequestInfo, |
| 413 std::vector<RequestInfo>, |
| 414 std::greater<RequestInfo>>; |
| 415 |
405 // The timeout timer interval. | 416 // The timeout timer interval. |
406 static const int kTimeoutTimerDelaySeconds; | 417 static const int kTimeoutTimerDelaySeconds; |
407 // Timeout for an installed worker to start. | 418 // Timeout for an installed worker to start. |
408 static const int kStartInstalledWorkerTimeoutSeconds; | 419 static const int kStartInstalledWorkerTimeoutSeconds; |
409 // Timeout for a new worker to start. | 420 // Timeout for a new worker to start. |
410 static const int kStartNewWorkerTimeoutMinutes; | 421 static const int kStartNewWorkerTimeoutMinutes; |
411 // Timeout for a request to be handled. | 422 // Timeout for a request to be handled. |
412 static const int kRequestTimeoutMinutes; | 423 static const int kRequestTimeoutMinutes; |
413 // Timeout for the worker to stop. | 424 // Timeout for the worker to stop. |
414 static const int kStopWorkerTimeoutSeconds; | 425 static const int kStopWorkerTimeoutSeconds; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 | 555 |
545 template <typename IDMAP> | 556 template <typename IDMAP> |
546 void RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, int request_id); | 557 void RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, int request_id); |
547 | 558 |
548 template <typename CallbackType> | 559 template <typename CallbackType> |
549 int AddRequest( | 560 int AddRequest( |
550 const CallbackType& callback, | 561 const CallbackType& callback, |
551 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, | 562 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, |
552 RequestType request_type); | 563 RequestType request_type); |
553 | 564 |
| 565 template <typename CallbackType> |
| 566 int AddRequestWithExpiration( |
| 567 const CallbackType& callback, |
| 568 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, |
| 569 RequestType request_type, |
| 570 base::TimeTicks expiration); |
| 571 |
554 bool MaybeTimeOutRequest(const RequestInfo& info); | 572 bool MaybeTimeOutRequest(const RequestInfo& info); |
555 void SetAllRequestTimes(const base::TimeTicks& ticks); | 573 bool ShouldStopIfRequestTimesOut(const RequestInfo& info); |
| 574 void SetAllRequestExpirations(const base::TimeTicks& expiration); |
556 | 575 |
557 // Returns the reason the embedded worker failed to start, using information | 576 // Returns the reason the embedded worker failed to start, using information |
558 // inaccessible to EmbeddedWorkerInstance. Returns |default_code| if it can't | 577 // inaccessible to EmbeddedWorkerInstance. Returns |default_code| if it can't |
559 // deduce a reason. | 578 // deduce a reason. |
560 ServiceWorkerStatusCode DeduceStartWorkerFailureReason( | 579 ServiceWorkerStatusCode DeduceStartWorkerFailureReason( |
561 ServiceWorkerStatusCode default_code); | 580 ServiceWorkerStatusCode default_code); |
562 | 581 |
563 // Sets |stale_time_| if this worker is stale, causing an update to eventually | 582 // Sets |stale_time_| if this worker is stale, causing an update to eventually |
564 // occur once the worker stops or is running too long. | 583 // occur once the worker stops or is running too long. |
565 void MarkIfStale(); | 584 void MarkIfStale(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 // Holds the time that the outstanding StartWorker() request started. | 644 // Holds the time that the outstanding StartWorker() request started. |
626 base::TimeTicks start_time_; | 645 base::TimeTicks start_time_; |
627 // Holds the time the worker entered STOPPING status. | 646 // Holds the time the worker entered STOPPING status. |
628 base::TimeTicks stop_time_; | 647 base::TimeTicks stop_time_; |
629 // Holds the time the worker was detected as stale and needs updating. We try | 648 // Holds the time the worker was detected as stale and needs updating. We try |
630 // to update once the worker stops, but will also update if it stays alive too | 649 // to update once the worker stops, but will also update if it stays alive too |
631 // long. | 650 // long. |
632 base::TimeTicks stale_time_; | 651 base::TimeTicks stale_time_; |
633 | 652 |
634 // New requests are added to |requests_| along with their entry in a callback | 653 // New requests are added to |requests_| along with their entry in a callback |
635 // map. The timeout timer periodically checks |requests_| for entries that | 654 // map. Requests are sorted by their expiration time (soonest to expire on top |
636 // should time out or have already been fulfilled (i.e., removed from the | 655 // of the priority queue). The timeout timer periodically checks |requests_| |
637 // callback map). | 656 // for entries that should time out or have already been fulfilled (i.e., |
638 std::queue<RequestInfo> requests_; | 657 // removed from the callback map). |
| 658 RequestInfoPriorityQueue requests_; |
639 | 659 |
640 bool skip_waiting_ = false; | 660 bool skip_waiting_ = false; |
641 bool skip_recording_startup_time_ = false; | 661 bool skip_recording_startup_time_ = false; |
642 bool force_bypass_cache_for_scripts_ = false; | 662 bool force_bypass_cache_for_scripts_ = false; |
643 bool skip_script_comparison_ = false; | 663 bool skip_script_comparison_ = false; |
644 bool is_update_scheduled_ = false; | 664 bool is_update_scheduled_ = false; |
645 bool in_dtor_ = false; | 665 bool in_dtor_ = false; |
646 | 666 |
647 std::vector<int> pending_skip_waiting_requests_; | 667 std::vector<int> pending_skip_waiting_requests_; |
648 scoped_ptr<net::HttpResponseInfo> main_script_http_info_; | 668 scoped_ptr<net::HttpResponseInfo> main_script_http_info_; |
649 | 669 |
650 // The status when StartWorker was invoked. Used for UMA. | 670 // The status when StartWorker was invoked. Used for UMA. |
651 Status prestart_status_ = NEW; | 671 Status prestart_status_ = NEW; |
652 // If not OK, the reason that StartWorker failed. Used for | 672 // If not OK, the reason that StartWorker failed. Used for |
653 // running |start_callbacks_|. | 673 // running |start_callbacks_|. |
654 ServiceWorkerStatusCode start_worker_status_ = SERVICE_WORKER_OK; | 674 ServiceWorkerStatusCode start_worker_status_ = SERVICE_WORKER_OK; |
655 | 675 |
656 scoped_ptr<PingController> ping_controller_; | 676 scoped_ptr<PingController> ping_controller_; |
657 scoped_ptr<Metrics> metrics_; | 677 scoped_ptr<Metrics> metrics_; |
658 const bool should_exclude_from_uma_ = false; | 678 const bool should_exclude_from_uma_ = false; |
659 | 679 |
660 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; | 680 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
661 | 681 |
662 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 682 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
663 }; | 683 }; |
664 | 684 |
665 } // namespace content | 685 } // namespace content |
666 | 686 |
667 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 687 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
OLD | NEW |