Chromium Code Reviews| 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 DoodleService(std::unique_ptr<DoodleFetcher> fetcher, |
| 31 std::unique_ptr<base::OneShotTimer> expiry_timer); | |
|
mastiz
2017/03/03 11:13:16
Can you document whether either of these two must
Marc Treib
2017/03/03 13:01:48
Neither may be null. Comment added.
| |
| 30 ~DoodleService(); | 32 ~DoodleService(); |
| 31 | 33 |
| 32 // Returns the current (cached) config, if any. | 34 // Returns the current (cached) config, if any. |
| 33 const base::Optional<DoodleConfig>& config() const { return cached_config_; } | 35 const base::Optional<DoodleConfig>& config() const { return cached_config_; } |
| 34 | 36 |
| 35 // Adds a new observer to the service. It'll only be called when the config | 37 // 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()|. | 38 // changes; to get the current (cached) config, call |config()|. |
| 37 void AddObserver(Observer* observer); | 39 void AddObserver(Observer* observer); |
| 38 | 40 |
| 39 // Prevents |observer| from receiving future updates. This is safe to call | 41 // Prevents |observer| from receiving future updates. This is safe to call |
| 40 // even when the observer is being notified of an update. | 42 // even when the observer is being notified of an update. |
| 41 void RemoveObserver(Observer* observer); | 43 void RemoveObserver(Observer* observer); |
| 42 | 44 |
| 43 // Requests an asynchronous refresh of the DoodleConfig from the network. | 45 // Requests an asynchronous refresh of the DoodleConfig from the network. |
| 44 // After the update completes, the observers will be notified only if the | 46 // After the update completes, the observers will be notified only if the |
| 45 // config changed. | 47 // config changed. |
| 46 void Refresh(); | 48 void Refresh(); |
| 47 | 49 |
| 48 private: | 50 private: |
| 51 // Callback for the fetcher. | |
| 49 void DoodleFetched(DoodleState state, | 52 void DoodleFetched(DoodleState state, |
| 50 base::TimeDelta time_to_live, | 53 base::TimeDelta time_to_live, |
| 51 const base::Optional<DoodleConfig>& doodle_config); | 54 const base::Optional<DoodleConfig>& doodle_config); |
| 52 | 55 |
| 56 void UpdateTimeToLive(base::TimeDelta time_to_live); | |
| 57 | |
| 58 // Callback for the expiry timer. | |
| 59 void DoodleExpired(); | |
| 60 | |
| 53 // The fetcher for getting fresh DoodleConfigs from the network. | 61 // The fetcher for getting fresh DoodleConfigs from the network. |
| 54 std::unique_ptr<DoodleFetcher> fetcher_; | 62 std::unique_ptr<DoodleFetcher> fetcher_; |
| 55 | 63 |
| 56 // The result of the last network fetch. | 64 // The result of the last network fetch. |
| 57 base::Optional<DoodleConfig> cached_config_; | 65 base::Optional<DoodleConfig> cached_config_; |
| 58 | 66 |
| 67 std::unique_ptr<base::OneShotTimer> expiry_timer_; | |
|
mastiz
2017/03/03 11:13:16
Nit: move up before cached_config_, together with
Marc Treib
2017/03/03 13:01:48
Done.
| |
| 68 | |
| 59 // The list of observers to be notified when the DoodleConfig changes. | 69 // The list of observers to be notified when the DoodleConfig changes. |
| 60 base::ObserverList<Observer> observers_; | 70 base::ObserverList<Observer> observers_; |
| 61 | 71 |
| 62 DISALLOW_COPY_AND_ASSIGN(DoodleService); | 72 DISALLOW_COPY_AND_ASSIGN(DoodleService); |
| 63 }; | 73 }; |
| 64 | 74 |
| 65 } // namespace doodle | 75 } // namespace doodle |
| 66 | 76 |
| 67 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ | 77 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_ |
| OLD | NEW |