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 #include <string> |
9 | 10 |
| 11 #include "base/callback_forward.h" |
10 #include "base/macros.h" | 12 #include "base/macros.h" |
11 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
12 #include "base/optional.h" | 14 #include "base/optional.h" |
13 #include "base/time/clock.h" | 15 #include "base/time/clock.h" |
14 #include "base/time/tick_clock.h" | 16 #include "base/time/tick_clock.h" |
15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
16 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
17 #include "components/doodle/doodle_fetcher.h" | 19 #include "components/doodle/doodle_fetcher.h" |
18 #include "components/doodle/doodle_types.h" | 20 #include "components/doodle/doodle_types.h" |
19 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
20 | 22 |
21 class PrefRegistrySimple; | 23 class PrefRegistrySimple; |
22 class PrefService; | 24 class PrefService; |
23 | 25 |
| 26 namespace gfx { |
| 27 class Image; |
| 28 } |
| 29 |
| 30 namespace image_fetcher { |
| 31 class ImageFetcher; |
| 32 struct RequestMetadata; |
| 33 } |
| 34 |
24 namespace doodle { | 35 namespace doodle { |
25 | 36 |
26 class DoodleService : public KeyedService { | 37 class DoodleService : public KeyedService { |
27 public: | 38 public: |
28 class Observer { | 39 class Observer { |
29 public: | 40 public: |
30 virtual void OnDoodleConfigRevalidated(bool from_cache) = 0; | 41 virtual void OnDoodleConfigRevalidated(bool from_cache) = 0; |
31 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0; | 42 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0; |
32 }; | 43 }; |
33 | 44 |
| 45 using ImageCallback = base::Callback<void(const gfx::Image& image)>; |
| 46 |
34 static void RegisterProfilePrefs(PrefRegistrySimple* pref_registry); | 47 static void RegisterProfilePrefs(PrefRegistrySimple* pref_registry); |
35 | 48 |
36 // All pointer parameters must be non-null. If |min_refresh_interval| doesn't | 49 // All pointer parameters must be non-null. If |min_refresh_interval| doesn't |
37 // have a value, the default value is used. | 50 // have a value, the default value is used. |
38 DoodleService(PrefService* pref_service, | 51 DoodleService(PrefService* pref_service, |
39 std::unique_ptr<DoodleFetcher> fetcher, | 52 std::unique_ptr<DoodleFetcher> fetcher, |
40 std::unique_ptr<base::OneShotTimer> expiry_timer, | 53 std::unique_ptr<base::OneShotTimer> expiry_timer, |
41 std::unique_ptr<base::Clock> clock, | 54 std::unique_ptr<base::Clock> clock, |
42 std::unique_ptr<base::TickClock> tick_clock, | 55 std::unique_ptr<base::TickClock> tick_clock, |
43 base::Optional<base::TimeDelta> override_min_refresh_interval); | 56 base::Optional<base::TimeDelta> override_min_refresh_interval, |
| 57 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher); |
44 ~DoodleService() override; | 58 ~DoodleService() override; |
45 | 59 |
46 // KeyedService implementation. | 60 // KeyedService implementation. |
47 void Shutdown() override; | 61 void Shutdown() override; |
48 | 62 |
49 // Returns the current (cached) config, if any. | 63 // Returns the current (cached) config, if any. |
50 const base::Optional<DoodleConfig>& config() const { return cached_config_; } | 64 const base::Optional<DoodleConfig>& config() const { return cached_config_; } |
51 | 65 |
| 66 // Returns the image for the currently-cached doodle config via |callback|, |
| 67 // which may be run synchronously or asynchronously. If the doodle is |
| 68 // animated, this returns the static CTA image. |
| 69 void GetImage(const ImageCallback& callback); |
| 70 |
52 // Adds a new observer to the service. It'll only be called when the config | 71 // 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()|. | 72 // changes; to get the current (cached) config, call |config()|. |
54 void AddObserver(Observer* observer); | 73 void AddObserver(Observer* observer); |
55 | 74 |
56 // Prevents |observer| from receiving future updates. This is safe to call | 75 // Prevents |observer| from receiving future updates. This is safe to call |
57 // even when the observer is being notified of an update. | 76 // even when the observer is being notified of an update. |
58 void RemoveObserver(Observer* observer); | 77 void RemoveObserver(Observer* observer); |
59 | 78 |
60 // Requests an asynchronous refresh of the DoodleConfig from the network. | 79 // Requests an asynchronous refresh of the DoodleConfig from the network. |
61 // After the update completes, the observers will be notified whether the | 80 // After the update completes, the observers will be notified whether the |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 DoodleState state, | 117 DoodleState state, |
99 base::TimeDelta time_to_live, | 118 base::TimeDelta time_to_live, |
100 const base::Optional<DoodleConfig>& doodle_config); | 119 const base::Optional<DoodleConfig>& doodle_config); |
101 | 120 |
102 void UpdateCachedConfig(base::TimeDelta time_to_live, | 121 void UpdateCachedConfig(base::TimeDelta time_to_live, |
103 const base::Optional<DoodleConfig>& new_config); | 122 const base::Optional<DoodleConfig>& new_config); |
104 | 123 |
105 // Callback for the expiry timer. | 124 // Callback for the expiry timer. |
106 void DoodleExpired(); | 125 void DoodleExpired(); |
107 | 126 |
| 127 void ImageFetched(const ImageCallback& callback, |
| 128 const std::string& id, |
| 129 const gfx::Image& image, |
| 130 const image_fetcher::RequestMetadata& metadata); |
| 131 |
108 PrefService* pref_service_; | 132 PrefService* pref_service_; |
109 | 133 |
110 // The fetcher for getting fresh DoodleConfigs from the network. | 134 // The fetcher for getting fresh DoodleConfigs from the network. |
111 std::unique_ptr<DoodleFetcher> fetcher_; | 135 std::unique_ptr<DoodleFetcher> fetcher_; |
112 | 136 |
113 std::unique_ptr<base::OneShotTimer> expiry_timer_; | 137 std::unique_ptr<base::OneShotTimer> expiry_timer_; |
114 std::unique_ptr<base::Clock> clock_; | 138 std::unique_ptr<base::Clock> clock_; |
115 std::unique_ptr<base::TickClock> tick_clock_; | 139 std::unique_ptr<base::TickClock> tick_clock_; |
116 | 140 |
117 // The minimum interval between server fetches. After a successful fetch, | 141 // The minimum interval between server fetches. After a successful fetch, |
118 // refresh requests are ignored for this period. | 142 // refresh requests are ignored for this period. |
119 const base::TimeDelta min_refresh_interval_; | 143 const base::TimeDelta min_refresh_interval_; |
120 | 144 |
| 145 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
| 146 |
121 // The result of the last network fetch. | 147 // The result of the last network fetch. |
122 base::Optional<DoodleConfig> cached_config_; | 148 base::Optional<DoodleConfig> cached_config_; |
123 | 149 |
124 // The time of the most recent successful fetch. | 150 // The time of the most recent successful fetch. |
125 base::TimeTicks last_successful_fetch_; | 151 base::TimeTicks last_successful_fetch_; |
126 | 152 |
127 // The list of observers to be notified when the DoodleConfig changes. | 153 // The list of observers to be notified when the DoodleConfig changes. |
128 base::ObserverList<Observer> observers_; | 154 base::ObserverList<Observer> observers_; |
129 | 155 |
130 DISALLOW_COPY_AND_ASSIGN(DoodleService); | 156 DISALLOW_COPY_AND_ASSIGN(DoodleService); |
131 }; | 157 }; |
132 | 158 |
133 } // namespace doodle | 159 } // namespace doodle |
134 | 160 |
135 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 161 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
OLD | NEW |