| 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/timer/timer.h" | 14 #include "base/timer/timer.h" |
| 14 #include "components/doodle/doodle_fetcher.h" | 15 #include "components/doodle/doodle_fetcher.h" |
| 15 #include "components/doodle/doodle_types.h" | 16 #include "components/doodle/doodle_types.h" |
| 16 | 17 |
| 18 class PrefRegistrySimple; |
| 19 class PrefService; |
| 20 |
| 17 namespace base { | 21 namespace base { |
| 18 class TimeDelta; | 22 class TimeDelta; |
| 19 } | 23 } |
| 20 | 24 |
| 21 namespace doodle { | 25 namespace doodle { |
| 22 | 26 |
| 23 class DoodleService { | 27 class DoodleService { |
| 24 public: | 28 public: |
| 25 class Observer { | 29 class Observer { |
| 26 public: | 30 public: |
| 27 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0; | 31 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0; |
| 28 }; | 32 }; |
| 29 | 33 |
| 30 // Both |fetcher| and |expiry_timer| must be non-null. | 34 static void RegisterProfilePrefs(PrefRegistrySimple* pref_registry); |
| 31 DoodleService(std::unique_ptr<DoodleFetcher> fetcher, | 35 |
| 32 std::unique_ptr<base::OneShotTimer> expiry_timer); | 36 // All parameters must be non-null. |
| 37 DoodleService(PrefService* pref_service, |
| 38 std::unique_ptr<DoodleFetcher> fetcher, |
| 39 std::unique_ptr<base::OneShotTimer> expiry_timer, |
| 40 std::unique_ptr<base::Clock> clock); |
| 33 ~DoodleService(); | 41 ~DoodleService(); |
| 34 | 42 |
| 35 // Returns the current (cached) config, if any. | 43 // Returns the current (cached) config, if any. |
| 36 const base::Optional<DoodleConfig>& config() const { return cached_config_; } | 44 const base::Optional<DoodleConfig>& config() const { return cached_config_; } |
| 37 | 45 |
| 38 // Adds a new observer to the service. It'll only be called when the config | 46 // Adds a new observer to the service. It'll only be called when the config |
| 39 // changes; to get the current (cached) config, call |config()|. | 47 // changes; to get the current (cached) config, call |config()|. |
| 40 void AddObserver(Observer* observer); | 48 void AddObserver(Observer* observer); |
| 41 | 49 |
| 42 // Prevents |observer| from receiving future updates. This is safe to call | 50 // Prevents |observer| from receiving future updates. This is safe to call |
| 43 // even when the observer is being notified of an update. | 51 // even when the observer is being notified of an update. |
| 44 void RemoveObserver(Observer* observer); | 52 void RemoveObserver(Observer* observer); |
| 45 | 53 |
| 46 // Requests an asynchronous refresh of the DoodleConfig from the network. | 54 // Requests an asynchronous refresh of the DoodleConfig from the network. |
| 47 // After the update completes, the observers will be notified only if the | 55 // After the update completes, the observers will be notified only if the |
| 48 // config changed. | 56 // config changed. |
| 49 void Refresh(); | 57 void Refresh(); |
| 50 | 58 |
| 51 private: | 59 private: |
| 52 // Callback for the fetcher. | 60 // Callback for the fetcher. |
| 53 void DoodleFetched(DoodleState state, | 61 void DoodleFetched(DoodleState state, |
| 54 base::TimeDelta time_to_live, | 62 base::TimeDelta time_to_live, |
| 55 const base::Optional<DoodleConfig>& doodle_config); | 63 const base::Optional<DoodleConfig>& doodle_config); |
| 56 | 64 |
| 57 void UpdateTimeToLive(base::TimeDelta time_to_live); | 65 void UpdateCachedConfig(base::TimeDelta time_to_live, |
| 66 const base::Optional<DoodleConfig>& doodle_config); |
| 58 | 67 |
| 59 // Callback for the expiry timer. | 68 // Callback for the expiry timer. |
| 60 void DoodleExpired(); | 69 void DoodleExpired(); |
| 61 | 70 |
| 71 PrefService* pref_service_; |
| 72 |
| 62 // The fetcher for getting fresh DoodleConfigs from the network. | 73 // The fetcher for getting fresh DoodleConfigs from the network. |
| 63 std::unique_ptr<DoodleFetcher> fetcher_; | 74 std::unique_ptr<DoodleFetcher> fetcher_; |
| 64 | 75 |
| 65 std::unique_ptr<base::OneShotTimer> expiry_timer_; | 76 std::unique_ptr<base::OneShotTimer> expiry_timer_; |
| 77 std::unique_ptr<base::Clock> clock_; |
| 66 | 78 |
| 67 // The result of the last network fetch. | 79 // The result of the last network fetch. |
| 68 base::Optional<DoodleConfig> cached_config_; | 80 base::Optional<DoodleConfig> cached_config_; |
| 69 | 81 |
| 70 // The list of observers to be notified when the DoodleConfig changes. | 82 // The list of observers to be notified when the DoodleConfig changes. |
| 71 base::ObserverList<Observer> observers_; | 83 base::ObserverList<Observer> observers_; |
| 72 | 84 |
| 73 DISALLOW_COPY_AND_ASSIGN(DoodleService); | 85 DISALLOW_COPY_AND_ASSIGN(DoodleService); |
| 74 }; | 86 }; |
| 75 | 87 |
| 76 } // namespace doodle | 88 } // namespace doodle |
| 77 | 89 |
| 78 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 90 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
| OLD | NEW |