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 { | |
fhorschig
2017/03/21 16:41:34
Question: could we use enum class DownloadOutcome:
Marc Treib
2017/03/21 16:48:59
Yes.
| |
69 OUTCOME_NEW_DOODLE, | |
70 OUTCOME_REVALIDATED_DOODLE, | |
71 OUTCOME_CHANGED_DOODLE, | |
72 OUTCOME_NO_DOODLE, | |
73 OUTCOME_EXPIRED, | |
74 OUTCOME_DOWNLOAD_ERROR, | |
75 OUTCOME_PARSING_ERROR, | |
76 // Insert new values here! | |
77 OUTCOME_COUNT | |
78 }; | |
79 | |
80 static bool DownloadOutcomeIsSuccess(DownloadOutcome outcome); | |
fhorschig
2017/03/21 16:41:34
It's static because it couldn't access DownloadOut
Marc Treib
2017/03/21 16:48:59
It's a *member* because otherwise it couldn't see
| |
81 static void RecordDownloadMetrics(DownloadOutcome outcome, | |
82 base::TimeDelta download_time); | |
83 | |
64 // Callback for the fetcher. | 84 // Callback for the fetcher. |
65 void DoodleFetched(DoodleState state, | 85 void DoodleFetched(base::TimeTicks start_time, |
86 DoodleState state, | |
66 base::TimeDelta time_to_live, | 87 base::TimeDelta time_to_live, |
67 const base::Optional<DoodleConfig>& doodle_config); | 88 const base::Optional<DoodleConfig>& doodle_config); |
68 | 89 |
69 void UpdateCachedConfig(base::TimeDelta time_to_live, | 90 DownloadOutcome UpdateCachedConfig( |
70 const base::Optional<DoodleConfig>& doodle_config); | 91 DoodleState state, |
92 base::TimeDelta time_to_live, | |
93 const base::Optional<DoodleConfig>& doodle_config); | |
71 | 94 |
72 // Callback for the expiry timer. | 95 // Callback for the expiry timer. |
73 void DoodleExpired(); | 96 void DoodleExpired(); |
74 | 97 |
75 PrefService* pref_service_; | 98 PrefService* pref_service_; |
76 | 99 |
77 // The fetcher for getting fresh DoodleConfigs from the network. | 100 // The fetcher for getting fresh DoodleConfigs from the network. |
78 std::unique_ptr<DoodleFetcher> fetcher_; | 101 std::unique_ptr<DoodleFetcher> fetcher_; |
79 | 102 |
80 std::unique_ptr<base::OneShotTimer> expiry_timer_; | 103 std::unique_ptr<base::OneShotTimer> expiry_timer_; |
81 std::unique_ptr<base::Clock> clock_; | 104 std::unique_ptr<base::Clock> clock_; |
105 std::unique_ptr<base::TickClock> tick_clock_; | |
82 | 106 |
83 // The result of the last network fetch. | 107 // The result of the last network fetch. |
84 base::Optional<DoodleConfig> cached_config_; | 108 base::Optional<DoodleConfig> cached_config_; |
85 | 109 |
86 // The list of observers to be notified when the DoodleConfig changes. | 110 // The list of observers to be notified when the DoodleConfig changes. |
87 base::ObserverList<Observer> observers_; | 111 base::ObserverList<Observer> observers_; |
88 | 112 |
89 DISALLOW_COPY_AND_ASSIGN(DoodleService); | 113 DISALLOW_COPY_AND_ASSIGN(DoodleService); |
90 }; | 114 }; |
91 | 115 |
92 } // namespace doodle | 116 } // namespace doodle |
93 | 117 |
94 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 118 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
OLD | NEW |