| 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: |
| 64 // Callback for the fetcher. | 66 // Callback for the fetcher. |
| 65 void DoodleFetched(DoodleState state, | 67 void DoodleFetched(base::TimeTicks start_time, |
| 68 DoodleState state, |
| 66 base::TimeDelta time_to_live, | 69 base::TimeDelta time_to_live, |
| 67 const base::Optional<DoodleConfig>& doodle_config); | 70 const base::Optional<DoodleConfig>& doodle_config); |
| 68 | 71 |
| 69 void UpdateCachedConfig(base::TimeDelta time_to_live, | 72 void UpdateCachedConfig(base::Optional<base::TimeDelta> download_time, |
| 73 DoodleState state, |
| 74 base::TimeDelta time_to_live, |
| 70 const base::Optional<DoodleConfig>& doodle_config); | 75 const base::Optional<DoodleConfig>& doodle_config); |
| 71 | 76 |
| 72 // Callback for the expiry timer. | 77 // Callback for the expiry timer. |
| 73 void DoodleExpired(); | 78 void DoodleExpired(); |
| 74 | 79 |
| 75 PrefService* pref_service_; | 80 PrefService* pref_service_; |
| 76 | 81 |
| 77 // The fetcher for getting fresh DoodleConfigs from the network. | 82 // The fetcher for getting fresh DoodleConfigs from the network. |
| 78 std::unique_ptr<DoodleFetcher> fetcher_; | 83 std::unique_ptr<DoodleFetcher> fetcher_; |
| 79 | 84 |
| 80 std::unique_ptr<base::OneShotTimer> expiry_timer_; | 85 std::unique_ptr<base::OneShotTimer> expiry_timer_; |
| 81 std::unique_ptr<base::Clock> clock_; | 86 std::unique_ptr<base::Clock> clock_; |
| 87 std::unique_ptr<base::TickClock> tick_clock_; |
| 82 | 88 |
| 83 // The result of the last network fetch. | 89 // The result of the last network fetch. |
| 84 base::Optional<DoodleConfig> cached_config_; | 90 base::Optional<DoodleConfig> cached_config_; |
| 85 | 91 |
| 86 // The list of observers to be notified when the DoodleConfig changes. | 92 // The list of observers to be notified when the DoodleConfig changes. |
| 87 base::ObserverList<Observer> observers_; | 93 base::ObserverList<Observer> observers_; |
| 88 | 94 |
| 89 DISALLOW_COPY_AND_ASSIGN(DoodleService); | 95 DISALLOW_COPY_AND_ASSIGN(DoodleService); |
| 90 }; | 96 }; |
| 91 | 97 |
| 92 } // namespace doodle | 98 } // namespace doodle |
| 93 | 99 |
| 94 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 100 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
| OLD | NEW |