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