OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 5 #ifndef COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
6 #define COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 6 #define COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
12 #include "base/optional.h" | 12 #include "base/optional.h" |
13 #include "base/time/clock.h" | 13 #include "base/time/clock.h" |
14 #include "base/time/tick_clock.h" | 14 #include "base/time/tick_clock.h" |
| 15 #include "base/time/time.h" |
15 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
16 #include "components/doodle/doodle_fetcher.h" | 17 #include "components/doodle/doodle_fetcher.h" |
17 #include "components/doodle/doodle_types.h" | 18 #include "components/doodle/doodle_types.h" |
18 #include "components/keyed_service/core/keyed_service.h" | 19 #include "components/keyed_service/core/keyed_service.h" |
19 | 20 |
20 class PrefRegistrySimple; | 21 class PrefRegistrySimple; |
21 class PrefService; | 22 class PrefService; |
22 | 23 |
23 namespace base { | |
24 class TimeDelta; | |
25 } | |
26 | |
27 namespace doodle { | 24 namespace doodle { |
28 | 25 |
29 class DoodleService : public KeyedService { | 26 class DoodleService : public KeyedService { |
30 public: | 27 public: |
31 class Observer { | 28 class Observer { |
32 public: | 29 public: |
33 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0; | 30 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0; |
34 }; | 31 }; |
35 | 32 |
36 static void RegisterProfilePrefs(PrefRegistrySimple* pref_registry); | 33 static void RegisterProfilePrefs(PrefRegistrySimple* pref_registry); |
37 | 34 |
38 // All parameters must be non-null. | 35 // All pointer parameters must be non-null. If |min_refresh_interval| doesn't |
| 36 // have a value, the default value is used. |
39 DoodleService(PrefService* pref_service, | 37 DoodleService(PrefService* pref_service, |
40 std::unique_ptr<DoodleFetcher> fetcher, | 38 std::unique_ptr<DoodleFetcher> fetcher, |
41 std::unique_ptr<base::OneShotTimer> expiry_timer, | 39 std::unique_ptr<base::OneShotTimer> expiry_timer, |
42 std::unique_ptr<base::Clock> clock, | 40 std::unique_ptr<base::Clock> clock, |
43 std::unique_ptr<base::TickClock> tick_clock); | 41 std::unique_ptr<base::TickClock> tick_clock, |
| 42 base::Optional<base::TimeDelta> override_min_refresh_interval); |
44 ~DoodleService() override; | 43 ~DoodleService() override; |
45 | 44 |
46 // KeyedService implementation. | 45 // KeyedService implementation. |
47 void Shutdown() override; | 46 void Shutdown() override; |
48 | 47 |
49 // Returns the current (cached) config, if any. | 48 // Returns the current (cached) config, if any. |
50 const base::Optional<DoodleConfig>& config() const { return cached_config_; } | 49 const base::Optional<DoodleConfig>& config() const { return cached_config_; } |
51 | 50 |
52 // Adds a new observer to the service. It'll only be called when the config | 51 // Adds a new observer to the service. It'll only be called when the config |
53 // changes; to get the current (cached) config, call |config()|. | 52 // changes; to get the current (cached) config, call |config()|. |
(...skipping 12 matching lines...) Expand all Loading... |
66 // Note: Keep in sync with the corresponding enum in histograms.xml. Never | 65 // Note: Keep in sync with the corresponding enum in histograms.xml. Never |
67 // remove values, and only insert new values at the end. | 66 // remove values, and only insert new values at the end. |
68 enum DownloadOutcome { | 67 enum DownloadOutcome { |
69 OUTCOME_NEW_DOODLE = 0, | 68 OUTCOME_NEW_DOODLE = 0, |
70 OUTCOME_REVALIDATED_DOODLE = 1, | 69 OUTCOME_REVALIDATED_DOODLE = 1, |
71 OUTCOME_CHANGED_DOODLE = 2, | 70 OUTCOME_CHANGED_DOODLE = 2, |
72 OUTCOME_NO_DOODLE = 3, | 71 OUTCOME_NO_DOODLE = 3, |
73 OUTCOME_EXPIRED = 4, | 72 OUTCOME_EXPIRED = 4, |
74 OUTCOME_DOWNLOAD_ERROR = 5, | 73 OUTCOME_DOWNLOAD_ERROR = 5, |
75 OUTCOME_PARSING_ERROR = 6, | 74 OUTCOME_PARSING_ERROR = 6, |
| 75 OUTCOME_REFRESH_INTERVAL_NOT_PASSED = 7, |
76 // Insert new values here! | 76 // Insert new values here! |
77 OUTCOME_COUNT = 7 | 77 OUTCOME_COUNT = 8 |
78 }; | 78 }; |
79 | 79 |
80 static bool DownloadOutcomeIsSuccess(DownloadOutcome outcome); | 80 static bool DownloadOutcomeIsSuccess(DownloadOutcome outcome); |
81 static void RecordDownloadMetrics(DownloadOutcome outcome, | 81 static void RecordDownloadMetrics(DownloadOutcome outcome, |
82 base::TimeDelta download_time); | 82 base::TimeDelta download_time); |
83 | 83 |
84 static DownloadOutcome DetermineDownloadOutcome( | 84 static DownloadOutcome DetermineDownloadOutcome( |
85 const base::Optional<DoodleConfig>& old_config, | 85 const base::Optional<DoodleConfig>& old_config, |
86 const base::Optional<DoodleConfig>& new_config, | 86 const base::Optional<DoodleConfig>& new_config, |
87 DoodleState state, | 87 DoodleState state, |
(...skipping 18 matching lines...) Expand all Loading... |
106 | 106 |
107 PrefService* pref_service_; | 107 PrefService* pref_service_; |
108 | 108 |
109 // The fetcher for getting fresh DoodleConfigs from the network. | 109 // The fetcher for getting fresh DoodleConfigs from the network. |
110 std::unique_ptr<DoodleFetcher> fetcher_; | 110 std::unique_ptr<DoodleFetcher> fetcher_; |
111 | 111 |
112 std::unique_ptr<base::OneShotTimer> expiry_timer_; | 112 std::unique_ptr<base::OneShotTimer> expiry_timer_; |
113 std::unique_ptr<base::Clock> clock_; | 113 std::unique_ptr<base::Clock> clock_; |
114 std::unique_ptr<base::TickClock> tick_clock_; | 114 std::unique_ptr<base::TickClock> tick_clock_; |
115 | 115 |
| 116 // The minimum interval between server fetches. After a successful fetch, |
| 117 // refresh requests are ignored for this period. |
| 118 const base::TimeDelta min_refresh_interval_; |
| 119 |
116 // The result of the last network fetch. | 120 // The result of the last network fetch. |
117 base::Optional<DoodleConfig> cached_config_; | 121 base::Optional<DoodleConfig> cached_config_; |
118 | 122 |
| 123 // The time of the most recent successful fetch. |
| 124 base::TimeTicks last_successful_fetch_; |
| 125 |
119 // The list of observers to be notified when the DoodleConfig changes. | 126 // The list of observers to be notified when the DoodleConfig changes. |
120 base::ObserverList<Observer> observers_; | 127 base::ObserverList<Observer> observers_; |
121 | 128 |
122 DISALLOW_COPY_AND_ASSIGN(DoodleService); | 129 DISALLOW_COPY_AND_ASSIGN(DoodleService); |
123 }; | 130 }; |
124 | 131 |
125 } // namespace doodle | 132 } // namespace doodle |
126 | 133 |
127 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 134 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
OLD | NEW |