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

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

Issue 2729923003: [Doodle] Automatically expire Doodles after their TTL (Closed)
Patch Set: 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
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 #include "base/time/time.h"
11 11
12 namespace doodle { 12 namespace doodle {
13 13
14 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) 14 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher,
15 : fetcher_(std::move(fetcher)) { 15 std::unique_ptr<base::OneShotTimer> expiry_timer)
16 : fetcher_(std::move(fetcher)), expiry_timer_(std::move(expiry_timer)) {
16 DCHECK(fetcher_); 17 DCHECK(fetcher_);
mastiz 2017/03/03 11:13:15 According to my earlier comment, you might want a
Marc Treib 2017/03/03 13:01:48 Done.
17 } 18 }
18 19
19 DoodleService::~DoodleService() = default; 20 DoodleService::~DoodleService() = default;
20 21
21 void DoodleService::AddObserver(Observer* observer) { 22 void DoodleService::AddObserver(Observer* observer) {
22 observers_.AddObserver(observer); 23 observers_.AddObserver(observer);
23 } 24 }
24 25
25 void DoodleService::RemoveObserver(Observer* observer) { 26 void DoodleService::RemoveObserver(Observer* observer) {
26 observers_.RemoveObserver(observer); 27 observers_.RemoveObserver(observer);
27 } 28 }
28 29
29 void DoodleService::Refresh() { 30 void DoodleService::Refresh() {
30 fetcher_->FetchDoodle( 31 fetcher_->FetchDoodle(
31 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); 32 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this)));
32 } 33 }
33 34
34 void DoodleService::DoodleFetched( 35 void DoodleService::DoodleFetched(
35 DoodleState state, 36 DoodleState state,
36 base::TimeDelta time_to_live, 37 base::TimeDelta time_to_live,
37 const base::Optional<DoodleConfig>& doodle_config) { 38 const base::Optional<DoodleConfig>& doodle_config) {
38 // If nothing changed, then there's nothing to do. Note that this checks both 39 // Handle the case where the new config is already expired.
39 // for existence changes as well as changes of the configs themselves. 40 bool expired = time_to_live <= base::TimeDelta();
40 if (cached_config_ == doodle_config) { 41 const base::Optional<DoodleConfig>& new_config =
41 // TODO(treib): Once we support expiry, update the time to live here. 42 expired ? base::nullopt : doodle_config;
mastiz 2017/03/03 11:13:15 This seems legit but I'm always scared when refere
Marc Treib 2017/03/03 13:01:48 Yup, I have an explicit test for the already-expir
42 return; 43
44 // If the config changed, update our cache and notify observers.
45 // Note that this checks both for existence changes as well as changes of the
46 // configs themselves.
47 if (cached_config_ != new_config) {
48 cached_config_ = new_config;
49 for (auto& observer : observers_) {
50 observer.OnDoodleConfigUpdated(cached_config_);
51 }
43 } 52 }
44 53
45 // Update the cache and notify observers. 54 // Even if the configs are identical, the time-to-live might have changed.
46 cached_config_ = doodle_config; 55 UpdateTimeToLive(time_to_live);
56 }
57
58 void DoodleService::UpdateTimeToLive(base::TimeDelta time_to_live) {
59 // (Re-)schedule the cache expiry.
60 if (cached_config_.has_value()) {
61 expiry_timer_->Start(
62 FROM_HERE, time_to_live,
63 base::Bind(&DoodleService::DoodleExpired, base::Unretained(this)));
64 } else {
65 expiry_timer_->Stop();
66 }
67 }
68
69 void DoodleService::DoodleExpired() {
70 DCHECK(cached_config_.has_value());
71 cached_config_.reset();
47 for (auto& observer : observers_) { 72 for (auto& observer : observers_) {
48 observer.OnDoodleConfigUpdated(cached_config_); 73 observer.OnDoodleConfigUpdated(cached_config_);
49 } 74 }
50 } 75 }
51 76
52 } // namespace doodle 77 } // namespace doodle
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698