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/timer/timer.h" | 15 #include "base/timer/timer.h" |
15 #include "components/doodle/doodle_fetcher.h" | 16 #include "components/doodle/doodle_fetcher.h" |
16 #include "components/doodle/doodle_types.h" | 17 #include "components/doodle/doodle_types.h" |
17 #include "components/keyed_service/core/keyed_service.h" | 18 #include "components/keyed_service/core/keyed_service.h" |
18 | 19 |
19 class PrefRegistrySimple; | 20 class PrefRegistrySimple; |
20 class PrefService; | 21 class PrefService; |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 class TimeDelta; | 24 class TimeDelta; |
24 } | 25 } |
25 | 26 |
26 namespace doodle { | 27 namespace doodle { |
27 | 28 |
28 class DoodleService : public KeyedService { | 29 class DoodleService : public KeyedService { |
29 public: | 30 public: |
30 class Observer { | 31 class Observer { |
31 public: | 32 public: |
32 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0; | 33 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0; |
33 }; | 34 }; |
34 | 35 |
35 static void RegisterProfilePrefs(PrefRegistrySimple* pref_registry); | 36 static void RegisterProfilePrefs(PrefRegistrySimple* pref_registry); |
36 | 37 |
37 // All parameters must be non-null. | 38 // All parameters must be non-null. |
38 DoodleService(PrefService* pref_service, | 39 DoodleService(PrefService* pref_service, |
39 std::unique_ptr<DoodleFetcher> fetcher, | 40 std::unique_ptr<DoodleFetcher> fetcher, |
40 std::unique_ptr<base::OneShotTimer> expiry_timer, | 41 std::unique_ptr<base::OneShotTimer> expiry_timer, |
41 std::unique_ptr<base::Clock> clock); | 42 std::unique_ptr<base::Clock> clock, |
43 std::unique_ptr<base::TickClock> tick_clock); | |
42 ~DoodleService() override; | 44 ~DoodleService() override; |
43 | 45 |
44 // KeyedService implementation. | 46 // KeyedService implementation. |
45 void Shutdown() override; | 47 void Shutdown() override; |
46 | 48 |
47 // Returns the current (cached) config, if any. | 49 // Returns the current (cached) config, if any. |
48 const base::Optional<DoodleConfig>& config() const { return cached_config_; } | 50 const base::Optional<DoodleConfig>& config() const { return cached_config_; } |
49 | 51 |
50 // Adds a new observer to the service. It'll only be called when the config | 52 // Adds a new observer to the service. It'll only be called when the config |
51 // changes; to get the current (cached) config, call |config()|. | 53 // changes; to get the current (cached) config, call |config()|. |
52 void AddObserver(Observer* observer); | 54 void AddObserver(Observer* observer); |
53 | 55 |
54 // Prevents |observer| from receiving future updates. This is safe to call | 56 // Prevents |observer| from receiving future updates. This is safe to call |
55 // even when the observer is being notified of an update. | 57 // even when the observer is being notified of an update. |
56 void RemoveObserver(Observer* observer); | 58 void RemoveObserver(Observer* observer); |
57 | 59 |
58 // Requests an asynchronous refresh of the DoodleConfig from the network. | 60 // Requests an asynchronous refresh of the DoodleConfig from the network. |
59 // After the update completes, the observers will be notified only if the | 61 // After the update completes, the observers will be notified only if the |
60 // config changed. | 62 // config changed. |
61 void Refresh(); | 63 void Refresh(); |
62 | 64 |
63 private: | 65 private: |
66 // Note: Keep in sync with the corresponding enum in histograms.xml. Never | |
67 // remove values, and only insert new values at the end. | |
68 enum DownloadOutcome { | |
69 OUTCOME_NEW_DOODLE = 0, | |
70 OUTCOME_REVALIDATED_DOODLE = 1, | |
71 OUTCOME_CHANGED_DOODLE = 2, | |
72 OUTCOME_NO_DOODLE = 3, | |
73 OUTCOME_EXPIRED = 4, | |
74 OUTCOME_DOWNLOAD_ERROR = 5, | |
75 OUTCOME_PARSING_ERROR = 6, | |
76 // Insert new values here! | |
77 OUTCOME_COUNT = 7 | |
78 }; | |
79 | |
80 static bool DownloadOutcomeIsSuccess(DownloadOutcome outcome); | |
81 static void RecordDownloadMetrics(DownloadOutcome outcome, | |
82 base::TimeDelta download_time); | |
83 | |
84 static DownloadOutcome DetermineDownloadOutcome( | |
85 const base::Optional<DoodleConfig>& old_config, | |
86 const base::Optional<DoodleConfig>& new_config, | |
87 DoodleState state, | |
88 bool expired); | |
89 | |
64 // Callback for the fetcher. | 90 // Callback for the fetcher. |
65 void DoodleFetched(DoodleState state, | 91 void DoodleFetched(base::TimeTicks start_time, |
fhorschig
2017/03/22 09:47:10
nit: const base::TimeTicks& ?
(I guess like for Ti
Marc Treib
2017/03/22 10:47:22
Yup, all the Time types are just an int64, so they
| |
92 DoodleState state, | |
66 base::TimeDelta time_to_live, | 93 base::TimeDelta time_to_live, |
67 const base::Optional<DoodleConfig>& doodle_config); | 94 const base::Optional<DoodleConfig>& doodle_config); |
68 | 95 |
96 DownloadOutcome HandleNewConfig( | |
97 DoodleState state, | |
98 base::TimeDelta time_to_live, | |
99 const base::Optional<DoodleConfig>& doodle_config); | |
100 | |
69 void UpdateCachedConfig(base::TimeDelta time_to_live, | 101 void UpdateCachedConfig(base::TimeDelta time_to_live, |
70 const base::Optional<DoodleConfig>& doodle_config); | 102 const base::Optional<DoodleConfig>& new_config); |
71 | 103 |
72 // Callback for the expiry timer. | 104 // Callback for the expiry timer. |
73 void DoodleExpired(); | 105 void DoodleExpired(); |
74 | 106 |
75 PrefService* pref_service_; | 107 PrefService* pref_service_; |
76 | 108 |
77 // The fetcher for getting fresh DoodleConfigs from the network. | 109 // The fetcher for getting fresh DoodleConfigs from the network. |
78 std::unique_ptr<DoodleFetcher> fetcher_; | 110 std::unique_ptr<DoodleFetcher> fetcher_; |
79 | 111 |
80 std::unique_ptr<base::OneShotTimer> expiry_timer_; | 112 std::unique_ptr<base::OneShotTimer> expiry_timer_; |
81 std::unique_ptr<base::Clock> clock_; | 113 std::unique_ptr<base::Clock> clock_; |
114 std::unique_ptr<base::TickClock> tick_clock_; | |
82 | 115 |
83 // The result of the last network fetch. | 116 // The result of the last network fetch. |
84 base::Optional<DoodleConfig> cached_config_; | 117 base::Optional<DoodleConfig> cached_config_; |
85 | 118 |
86 // The list of observers to be notified when the DoodleConfig changes. | 119 // The list of observers to be notified when the DoodleConfig changes. |
87 base::ObserverList<Observer> observers_; | 120 base::ObserverList<Observer> observers_; |
88 | 121 |
89 DISALLOW_COPY_AND_ASSIGN(DoodleService); | 122 DISALLOW_COPY_AND_ASSIGN(DoodleService); |
90 }; | 123 }; |
91 | 124 |
92 } // namespace doodle | 125 } // namespace doodle |
93 | 126 |
94 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 127 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
OLD | NEW |