Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: content/browser/service_worker/service_worker_version.h

Issue 2504663002: Added a local tick clock to ServiceWorkerVersion. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h> 8 #include <stdint.h>
9 9
10 #include <functional> 10 #include <functional>
11 #include <map> 11 #include <map>
12 #include <memory> 12 #include <memory>
13 #include <queue> 13 #include <queue>
14 #include <set> 14 #include <set>
15 #include <string> 15 #include <string>
16 #include <utility> 16 #include <utility>
17 #include <vector> 17 #include <vector>
18 18
19 #include "base/callback.h" 19 #include "base/callback.h"
20 #include "base/containers/scoped_ptr_hash_map.h" 20 #include "base/containers/scoped_ptr_hash_map.h"
21 #include "base/gtest_prod_util.h" 21 #include "base/gtest_prod_util.h"
22 #include "base/id_map.h" 22 #include "base/id_map.h"
23 #include "base/macros.h" 23 #include "base/macros.h"
24 #include "base/memory/ptr_util.h" 24 #include "base/memory/ptr_util.h"
25 #include "base/memory/ref_counted.h" 25 #include "base/memory/ref_counted.h"
26 #include "base/observer_list.h" 26 #include "base/observer_list.h"
27 #include "base/optional.h" 27 #include "base/optional.h"
28 #include "base/threading/thread_task_runner_handle.h" 28 #include "base/threading/thread_task_runner_handle.h"
29 #include "base/time/tick_clock.h"
29 #include "base/time/time.h" 30 #include "base/time/time.h"
30 #include "base/timer/timer.h" 31 #include "base/timer/timer.h"
31 #include "content/browser/service_worker/embedded_worker_instance.h" 32 #include "content/browser/service_worker/embedded_worker_instance.h"
32 #include "content/browser/service_worker/embedded_worker_status.h" 33 #include "content/browser/service_worker/embedded_worker_status.h"
33 #include "content/browser/service_worker/service_worker_metrics.h" 34 #include "content/browser/service_worker/service_worker_metrics.h"
34 #include "content/browser/service_worker/service_worker_script_cache_map.h" 35 #include "content/browser/service_worker/service_worker_script_cache_map.h"
35 #include "content/common/content_export.h" 36 #include "content/common/content_export.h"
36 #include "content/common/origin_trials/trial_token_validator.h" 37 #include "content/common/origin_trials/trial_token_validator.h"
37 #include "content/common/service_worker/service_worker_status_code.h" 38 #include "content/common/service_worker/service_worker_status_code.h"
38 #include "content/common/service_worker/service_worker_types.h" 39 #include "content/common/service_worker/service_worker_types.h"
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 // Sets the HttpResponseInfo used to load the main script. 394 // Sets the HttpResponseInfo used to load the main script.
394 // This HttpResponseInfo will be used for all responses sent back from the 395 // This HttpResponseInfo will be used for all responses sent back from the
395 // service worker, as the effective security of these responses is equivalent 396 // service worker, as the effective security of these responses is equivalent
396 // to that of the ServiceWorker. 397 // to that of the ServiceWorker.
397 void SetMainScriptHttpResponseInfo(const net::HttpResponseInfo& http_info); 398 void SetMainScriptHttpResponseInfo(const net::HttpResponseInfo& http_info);
398 const net::HttpResponseInfo* GetMainScriptHttpResponseInfo(); 399 const net::HttpResponseInfo* GetMainScriptHttpResponseInfo();
399 400
400 // Simulate ping timeout. Should be used for tests-only. 401 // Simulate ping timeout. Should be used for tests-only.
401 void SimulatePingTimeoutForTesting(); 402 void SimulatePingTimeoutForTesting();
402 403
404 // Used to allow tests to change time for testing.
405 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock);
406
403 // Returns true if the service worker has work to do: it has pending 407 // Returns true if the service worker has work to do: it has pending
404 // requests, in-progress streaming URLRequestJobs, or pending start callbacks. 408 // requests, in-progress streaming URLRequestJobs, or pending start callbacks.
405 bool HasWork() const; 409 bool HasWork() const;
406 410
407 // Returns the number of pending external request count of this worker. 411 // Returns the number of pending external request count of this worker.
408 size_t GetExternalRequestCountForTest() const { 412 size_t GetExternalRequestCountForTest() const {
409 return external_request_uuid_to_request_id_.size(); 413 return external_request_uuid_to_request_id_.size();
410 } 414 }
411 415
412 private: 416 private:
(...skipping 22 matching lines...) Expand all
435 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, RequestTimeout); 439 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, RequestTimeout);
436 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerFailToStartTest, Timeout); 440 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerFailToStartTest, Timeout);
437 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, 441 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest,
438 TimeoutStartingWorker); 442 TimeoutStartingWorker);
439 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest, 443 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionBrowserTest,
440 TimeoutWorkerInEvent); 444 TimeoutWorkerInEvent);
441 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart); 445 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenStart);
442 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart); 446 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStallInStoppingTest, DetachThenRestart);
443 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, 447 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP,
444 RegisterForeignFetchScopes); 448 RegisterForeignFetchScopes);
449 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, RequestNowTimeout);
450 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, RequestNowTimeoutKill);
445 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, RequestCustomizedTimeout); 451 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, RequestCustomizedTimeout);
446 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP,
447 RequestCustomizedTimeoutKill);
448 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, MixedRequestTimeouts); 452 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerVersionTestP, MixedRequestTimeouts);
449 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerURLRequestJobTest, EarlyResponse); 453 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerURLRequestJobTest, EarlyResponse);
450 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerURLRequestJobTest, CancelRequest); 454 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerURLRequestJobTest, CancelRequest);
451 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerActivationTest, SkipWaiting); 455 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerActivationTest, SkipWaiting);
452 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerActivationTest, 456 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerActivationTest,
453 SkipWaitingWithInflightRequest); 457 SkipWaitingWithInflightRequest);
454 458
455 class Metrics; 459 class Metrics;
456 class PingController; 460 class PingController;
457 461
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 static const int kStartInstalledWorkerTimeoutSeconds; 584 static const int kStartInstalledWorkerTimeoutSeconds;
581 // Timeout for a new worker to start. 585 // Timeout for a new worker to start.
582 static const int kStartNewWorkerTimeoutMinutes; 586 static const int kStartNewWorkerTimeoutMinutes;
583 // Timeout for a request to be handled. 587 // Timeout for a request to be handled.
584 static const int kRequestTimeoutMinutes; 588 static const int kRequestTimeoutMinutes;
585 // Timeout for the worker to stop. 589 // Timeout for the worker to stop.
586 static const int kStopWorkerTimeoutSeconds; 590 static const int kStopWorkerTimeoutSeconds;
587 591
588 ~ServiceWorkerVersion() override; 592 ~ServiceWorkerVersion() override;
589 593
594 // The following methods all rely on the internal clock for the current time.
falken 2016/12/02 09:12:28 nit: add "|tick_clock_|" for clarity?
harkness 2016/12/02 10:15:33 Done.
595 void RestartTick(base::TimeTicks* time) const;
596 bool RequestExpired(const base::TimeTicks& expiration) const;
597 base::TimeDelta GetTickDuration(const base::TimeTicks& time) const;
598
590 // EmbeddedWorkerInstance::Listener overrides: 599 // EmbeddedWorkerInstance::Listener overrides:
591 void OnThreadStarted() override; 600 void OnThreadStarted() override;
592 void OnStarting() override; 601 void OnStarting() override;
593 void OnStarted() override; 602 void OnStarted() override;
594 void OnStopping() override; 603 void OnStopping() override;
595 void OnStopped(EmbeddedWorkerStatus old_status) override; 604 void OnStopped(EmbeddedWorkerStatus old_status) override;
596 void OnDetached(EmbeddedWorkerStatus old_status) override; 605 void OnDetached(EmbeddedWorkerStatus old_status) override;
597 void OnScriptLoaded() override; 606 void OnScriptLoaded() override;
598 void OnScriptLoadFailed() override; 607 void OnScriptLoadFailed() override;
599 void OnRegisteredToDevToolsManager() override; 608 void OnRegisteredToDevToolsManager() override;
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 811
803 std::vector<int> pending_skip_waiting_requests_; 812 std::vector<int> pending_skip_waiting_requests_;
804 std::unique_ptr<net::HttpResponseInfo> main_script_http_info_; 813 std::unique_ptr<net::HttpResponseInfo> main_script_http_info_;
805 814
806 std::unique_ptr<TrialTokenValidator::FeatureToTokensMap> origin_trial_tokens_; 815 std::unique_ptr<TrialTokenValidator::FeatureToTokensMap> origin_trial_tokens_;
807 816
808 // If not OK, the reason that StartWorker failed. Used for 817 // If not OK, the reason that StartWorker failed. Used for
809 // running |start_callbacks_|. 818 // running |start_callbacks_|.
810 ServiceWorkerStatusCode start_worker_status_ = SERVICE_WORKER_OK; 819 ServiceWorkerStatusCode start_worker_status_ = SERVICE_WORKER_OK;
811 820
821 // The clock used to vend tick time.
822 std::unique_ptr<base::TickClock> tick_clock_;
823
812 std::unique_ptr<PingController> ping_controller_; 824 std::unique_ptr<PingController> ping_controller_;
813 std::unique_ptr<Metrics> metrics_; 825 std::unique_ptr<Metrics> metrics_;
814 const bool should_exclude_from_uma_ = false; 826 const bool should_exclude_from_uma_ = false;
815 827
816 bool stop_when_devtools_detached_ = false; 828 bool stop_when_devtools_detached_ = false;
817 829
818 // Keeps the first purpose of starting the worker for UMA. Cleared in 830 // Keeps the first purpose of starting the worker for UMA. Cleared in
819 // FinishStartWorker(). 831 // FinishStartWorker().
820 base::Optional<ServiceWorkerMetrics::EventType> start_worker_first_purpose_; 832 base::Optional<ServiceWorkerMetrics::EventType> start_worker_first_purpose_;
821 833
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 907
896 // At this point |this| can have been deleted, so don't do anything other 908 // At this point |this| can have been deleted, so don't do anything other
897 // than returning. 909 // than returning.
898 910
899 return true; 911 return true;
900 } 912 }
901 913
902 } // namespace content 914 } // namespace content
903 915
904 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ 916 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698