Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(572)

Side by Side Diff: components/doodle/doodle_service.cc

Issue 2725143003: [Doodle] Pull time_to_live out of DoodleConfig (Closed)
Patch Set: mastiz review Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/doodle/doodle_service.h ('k') | components/doodle/doodle_service_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "components/doodle/doodle_service.h" 5 #include "components/doodle/doodle_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/time/time.h"
10 11
11 namespace doodle { 12 namespace doodle {
12 13
13 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) 14 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher)
14 : fetcher_(std::move(fetcher)) { 15 : fetcher_(std::move(fetcher)) {
15 DCHECK(fetcher_); 16 DCHECK(fetcher_);
16 } 17 }
17 18
18 DoodleService::~DoodleService() = default; 19 DoodleService::~DoodleService() = default;
19 20
20 void DoodleService::AddObserver(Observer* observer) { 21 void DoodleService::AddObserver(Observer* observer) {
21 observers_.AddObserver(observer); 22 observers_.AddObserver(observer);
22 } 23 }
23 24
24 void DoodleService::RemoveObserver(Observer* observer) { 25 void DoodleService::RemoveObserver(Observer* observer) {
25 observers_.RemoveObserver(observer); 26 observers_.RemoveObserver(observer);
26 } 27 }
27 28
28 void DoodleService::Refresh() { 29 void DoodleService::Refresh() {
29 fetcher_->FetchDoodle( 30 fetcher_->FetchDoodle(
30 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); 31 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this)));
31 } 32 }
32 33
33 void DoodleService::DoodleFetched( 34 void DoodleService::DoodleFetched(
34 DoodleState state, 35 DoodleState state,
36 base::TimeDelta time_to_live,
35 const base::Optional<DoodleConfig>& doodle_config) { 37 const base::Optional<DoodleConfig>& doodle_config) {
36 if (!cached_config_.has_value() && !doodle_config.has_value()) { 38 // If nothing changed, then there's nothing to do. Note that this checks both
37 // There was no config before and we didn't get a new one, so there's 39 // for existence changes as well as changes of the configs themselves.
38 // nothing to do. 40 if (cached_config_ == doodle_config) {
41 // TODO(treib): Once we support expiry, update the time to live here.
39 return; 42 return;
40 } 43 }
41 44
42 bool notify = false; 45 // Update the cache and notify observers.
43 if (cached_config_.has_value() != doodle_config.has_value()) {
44 // We got a new config, or an existing one went away.
45 notify = true;
46 } else {
47 // There was a config both before and after the update. Notify observers
48 // only if something relevant changed.
49 notify = !cached_config_.value().IsEquivalent(doodle_config.value());
50 }
51
52 // In any case, update the cache.
53 cached_config_ = doodle_config; 46 cached_config_ = doodle_config;
54 47 for (auto& observer : observers_) {
55 if (notify) { 48 observer.OnDoodleConfigUpdated(cached_config_);
56 for (auto& observer : observers_) {
57 observer.OnDoodleConfigUpdated(cached_config_);
58 }
59 } 49 }
60 } 50 }
61 51
62 } // namespace doodle 52 } // namespace doodle
OLDNEW
« no previous file with comments | « components/doodle/doodle_service.h ('k') | components/doodle/doodle_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698