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

Side by Side Diff: components/password_manager/core/browser/facet_manager.cc

Issue 947563002: Add prefetch support to AffiliationBackend. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove 'stale' accessor, cleaned up timelines for tests, extended/fixed some tests. Created 5 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 // Note: Read the class comment of AffiliationService for the definition of the
6 // terms used below.
7 //
8 // On-demand fetching strategy
9 //
10 // A GetAffiliations() request concerning facet X will be served from the cache
11 // as long as the cache contains fresh affiliation information for facet X, that
12 // is, if there is an equivalence class in the cache that contains X and has
13 // been fetched less than |kCacheHardExpiryInHours| hours ago.
14 //
15 // Otherwise, a network request is issued against the Affiliation API as soon as
16 // possible, that is, immediately if there is no fetch in flight, or right after
17 // completion of the fetch in flight if there is one, provided that the required
18 // data is not incidentally returned by the first fetch.
19 //
20 //
21 // Proactive fetching strategy
22 //
23 // A Prefetch() request concerning facet Y can trigger an initial network fetch,
24 // or periodic refetches only when:
25 // * The prefetch request is not already expired, i.e., its |keep_fresh_until|
26 // threshold is strictly in the future (that is, prefetch intervals are open
27 // from the right).
28 // * Affiliation information in the cache pertaining to facet Y will get stale
29 // strictly before the specified |keep_fresh_until| threshold.
30 //
31 // An initial fetch will be issued as soon as possible if, in addition to the
32 // two necessery conditions above, and at the time of the Prefetch() call, the
33 // cache contains no affiliation information regarding facet Y, or if the data
34 // in the cache for facet Y is near-stale, that is, it has been fetched more
35 // than |kCacheHardExpiryInHours| hours ago.
36 //
37 // A refetch will be issued every time the data in the cache regarding facet Y
38 // becomes near-stale, that is, exactly |kCacheSoftExpiry| hours after the last
39 // fetch, provided that the above two necessary conditions are also met.
40 //
41 // Fetches are triggered already when the data gets near-stale, as opposed to
42 // waiting until the data would get stale, in an effort to keep the data fresh
43 // even in face of temporary network errors lasting no more than the difference
44 // between soft and hard expiry times.
45 //
46 // The current fetch scheduling logic, however, can only deal with at most one
47 // such 'early' fetch between taking place between the prior fetch and the
48 // corresponding hard expiry time of the data, therefore it is assumed that:
49 //
50 // kCacheSoftExpiryInHours < kCacheHardExpiryInHours, and
51 // 2 * kCacheSoftExpiryInHours > kCacheHardExpiryInHours.
52 //
53 //
54 // Cache freshness terminology
55 //
56 //
57 // Fetch (t=0) kCacheSoftExpiry kCacheHardExpiry
58 // / / /
59 // ---o------------------------o-----------------------o-----------------> t
60 // | | |
61 // | [-- Cache near-stale --------------------- ..
62 // [--------------- Cache is fresh ----------------)[-- Cache is stale ..
63 //
64
5 #include "components/password_manager/core/browser/facet_manager.h" 65 #include "components/password_manager/core/browser/facet_manager.h"
6 66
7 #include "base/bind.h" 67 #include "base/bind.h"
8 #include "base/location.h" 68 #include "base/location.h"
9 #include "base/task_runner.h" 69 #include "base/task_runner.h"
70 #include "base/time/clock.h"
71 #include "base/time/time.h"
10 #include "components/password_manager/core/browser/facet_manager_host.h" 72 #include "components/password_manager/core/browser/facet_manager_host.h"
11 73
12 namespace password_manager { 74 namespace password_manager {
13 75
14 namespace { 76 // statics
77 const int FacetManager::kCacheSoftExpiryInHours = 21;
78 const int FacetManager::kCacheHardExpiryInHours = 24;
15 79
16 // The duration after which cached affiliation data is considered stale and will 80 static_assert(
17 // not be used to serve requests any longer. 81 FacetManager::kCacheSoftExpiryInHours <
18 const int kCacheLifetimeInHours = 24; 82 FacetManager::kCacheHardExpiryInHours,
83 "Soft expiry period must be shorter than the hard expiry period.");
19 84
20 } // namespace 85 static_assert(
86 2 * FacetManager::kCacheSoftExpiryInHours >
87 FacetManager::kCacheHardExpiryInHours,
88 "Soft expiry period must be longer than half of the hard expiry period.");
21 89
22 // Encapsulates the details of a pending GetAffiliations() request. 90 // Encapsulates the details of a pending GetAffiliations() request.
23 struct FacetManager::RequestInfo { 91 struct FacetManager::RequestInfo {
24 AffiliationService::ResultCallback callback; 92 AffiliationService::ResultCallback callback;
25 scoped_refptr<base::TaskRunner> callback_task_runner; 93 scoped_refptr<base::TaskRunner> callback_task_runner;
26 }; 94 };
27 95
28 FacetManager::FacetManager(FacetManagerHost* host, const FacetURI& facet_uri) 96 FacetManager::FacetManager(const FacetURI& facet_uri,
29 : backend_(host), 97 FacetManagerHost* backend,
30 facet_uri_(facet_uri), 98 base::Clock* clock)
31 last_update_time_(backend_->ReadLastUpdateTimeFromDatabase(facet_uri)) { 99 : facet_uri_(facet_uri), backend_(backend), clock_(clock) {
100 AffiliatedFacetsWithUpdateTime affiliations;
101 if (backend_->ReadAffiliationsFromDatabase(facet_uri_, &affiliations))
102 last_update_time_ = affiliations.last_update_time;
32 } 103 }
33 104
34 FacetManager::~FacetManager() { 105 FacetManager::~FacetManager() {
35 // The manager will only be destroyed while there are pending requests if the 106 // The manager will be destroyed while there are pending requests only if the
36 // entire backend is going. Call failure on pending requests in this case. 107 // entire backend is going away. Fail pending requests in this case.
37 for (const auto& request_info : pending_requests_) 108 for (const auto& request_info : pending_requests_)
38 ServeRequestWithFailure(request_info); 109 ServeRequestWithFailure(request_info);
39 } 110 }
40 111
41 void FacetManager::GetAffiliations( 112 void FacetManager::GetAffiliations(
42 bool cached_only, 113 bool cached_only,
43 const AffiliationService::ResultCallback& callback, 114 const AffiliationService::ResultCallback& callback,
44 const scoped_refptr<base::TaskRunner>& callback_task_runner) { 115 const scoped_refptr<base::TaskRunner>& callback_task_runner) {
45 RequestInfo request_info; 116 RequestInfo request_info;
46 request_info.callback = callback; 117 request_info.callback = callback;
47 request_info.callback_task_runner = callback_task_runner; 118 request_info.callback_task_runner = callback_task_runner;
48 if (IsCachedDataFresh()) { 119 if (IsCachedDataFresh()) {
49 AffiliatedFacetsWithUpdateTime affiliation; 120 AffiliatedFacetsWithUpdateTime affiliation;
50 if (!backend_->ReadAffiliationsFromDatabase(facet_uri_, &affiliation)) { 121 if (!backend_->ReadAffiliationsFromDatabase(facet_uri_, &affiliation)) {
51 // TODO(engedy): Implement this. crbug.com/437865. 122 // TODO(engedy): Implement this. crbug.com/437865.
52 NOTIMPLEMENTED(); 123 NOTIMPLEMENTED();
53 } 124 }
54 DCHECK_EQ(affiliation.last_update_time, last_update_time_) << facet_uri_; 125 DCHECK_EQ(affiliation.last_update_time, last_update_time_) << facet_uri_;
55 ServeRequestWithSuccess(request_info, affiliation.facets); 126 ServeRequestWithSuccess(request_info, affiliation.facets);
56 } else if (cached_only) { 127 } else if (cached_only) {
57 ServeRequestWithFailure(request_info); 128 ServeRequestWithFailure(request_info);
58 } else { 129 } else {
59 pending_requests_.push_back(request_info); 130 pending_requests_.push_back(request_info);
60 backend_->SignalNeedNetworkRequest(); 131 backend_->SignalNeedNetworkRequest();
61 } 132 }
62 } 133 }
63 134
135 void FacetManager::Prefetch(const base::Time& keep_fresh_until) {
136 keep_fresh_until_thresholds_.insert(keep_fresh_until);
137
138 // If an initial fetch if needed, trigger that (the refetch will be scheduled
139 // once the initial fetch completes). Otherwise schedule the next refetch.
140 base::Time next_required_fetch(GetNextRequiredFetchTimeDueToPrefetch());
141 if (next_required_fetch <= clock_->Now())
142 backend_->SignalNeedNetworkRequest();
143 else if (next_required_fetch < base::Time::Max())
144 backend_->RequestNotificationAtTime(facet_uri_, next_required_fetch);
145
146 // For a finite |keep_fresh_until|, schedule a callback so that once the
147 // prefetch expires, it can be removed from |keep_fresh_untils_|, and also the
148 // manager can get a chance to be destroyed unless it is otherwise needed.
149 if (keep_fresh_until > clock_->Now() && keep_fresh_until < base::Time::Max())
150 backend_->RequestNotificationAtTime(facet_uri_, keep_fresh_until);
151 }
152
153 void FacetManager::CancelPrefetch(const base::Time& keep_fresh_until) {
154 auto iter = keep_fresh_until_thresholds_.find(keep_fresh_until);
155 if (iter != keep_fresh_until_thresholds_.end())
156 keep_fresh_until_thresholds_.erase(iter);
157 }
158
64 void FacetManager::OnFetchSucceeded( 159 void FacetManager::OnFetchSucceeded(
65 const AffiliatedFacetsWithUpdateTime& affiliation) { 160 const AffiliatedFacetsWithUpdateTime& affiliation) {
66 last_update_time_ = affiliation.last_update_time; 161 last_update_time_ = affiliation.last_update_time;
67 DCHECK(IsCachedDataFresh()) << facet_uri_; 162 DCHECK(IsCachedDataFresh()) << facet_uri_;
68 for (const auto& request_info : pending_requests_) 163 for (const auto& request_info : pending_requests_)
69 ServeRequestWithSuccess(request_info, affiliation.facets); 164 ServeRequestWithSuccess(request_info, affiliation.facets);
70 pending_requests_.clear(); 165 pending_requests_.clear();
166
167 base::Time next_required_fetch(GetNextRequiredFetchTimeDueToPrefetch());
168 if (next_required_fetch < base::Time::Max())
169 backend_->RequestNotificationAtTime(facet_uri_, next_required_fetch);
170 }
171
172 void FacetManager::NotifyAtRequestedTime() {
173 base::Time next_required_fetch(GetNextRequiredFetchTimeDueToPrefetch());
174 if (next_required_fetch <= clock_->Now())
175 backend_->SignalNeedNetworkRequest();
176 else if (next_required_fetch < base::Time::Max())
177 backend_->RequestNotificationAtTime(facet_uri_, next_required_fetch);
178
179 auto iter_first_non_expired =
180 keep_fresh_until_thresholds_.upper_bound(clock_->Now());
181 keep_fresh_until_thresholds_.erase(keep_fresh_until_thresholds_.begin(),
182 iter_first_non_expired);
71 } 183 }
72 184
73 bool FacetManager::CanBeDiscarded() const { 185 bool FacetManager::CanBeDiscarded() const {
74 return pending_requests_.empty(); 186 return pending_requests_.empty() &&
187 GetMaximumKeepFreshUntilThreshold() <= clock_->Now();
75 } 188 }
76 189
77 bool FacetManager::DoesRequireFetch() const { 190 bool FacetManager::DoesRequireFetch() const {
78 return !pending_requests_.empty() && !IsCachedDataFresh(); 191 return (!pending_requests_.empty() && !IsCachedDataFresh()) ||
79 } 192 GetNextRequiredFetchTimeDueToPrefetch() <= clock_->Now();
80
81 base::Time FacetManager::GetCacheExpirationTime() const {
82 if (last_update_time_.is_null())
83 return base::Time();
84 return last_update_time_ + base::TimeDelta::FromHours(kCacheLifetimeInHours);
85 } 193 }
86 194
87 bool FacetManager::IsCachedDataFresh() const { 195 bool FacetManager::IsCachedDataFresh() const {
88 return backend_->GetCurrentTime() < GetCacheExpirationTime(); 196 return clock_->Now() < GetCacheHardExpiryTime();
197 }
198
199 bool FacetManager::IsCachedDataNearStale() const {
200 return GetCacheSoftExpiryTime() <= clock_->Now();
201 }
202
203 base::Time FacetManager::GetCacheSoftExpiryTime() const {
204 return last_update_time_ +
205 base::TimeDelta::FromHours(kCacheSoftExpiryInHours);
206 }
207
208 base::Time FacetManager::GetCacheHardExpiryTime() const {
209 return last_update_time_ +
210 base::TimeDelta::FromHours(kCacheHardExpiryInHours);
211 }
212
213 base::Time FacetManager::GetMaximumKeepFreshUntilThreshold() const {
214 return !keep_fresh_until_thresholds_.empty()
215 ? *keep_fresh_until_thresholds_.rbegin()
216 : base::Time();
217 }
218
219 base::Time FacetManager::GetNextRequiredFetchTimeDueToPrefetch() const {
220 // If there is at least one non-expired Prefetch() request that requires the
221 // data to be kept fresh until some time later than its current hard expiry
222 // time, then a fetch is needed once the cached data becomes near-stale.
223 if (clock_->Now() < GetMaximumKeepFreshUntilThreshold() &&
224 GetCacheHardExpiryTime() < GetMaximumKeepFreshUntilThreshold()) {
225 return GetCacheSoftExpiryTime();
226 }
227 return base::Time::Max();
89 } 228 }
90 229
91 // static 230 // static
92 void FacetManager::ServeRequestWithSuccess( 231 void FacetManager::ServeRequestWithSuccess(
93 const RequestInfo& request_info, 232 const RequestInfo& request_info,
94 const AffiliatedFacets& affiliation) { 233 const AffiliatedFacets& affiliation) {
95 request_info.callback_task_runner->PostTask( 234 request_info.callback_task_runner->PostTask(
96 FROM_HERE, base::Bind(request_info.callback, affiliation, true)); 235 FROM_HERE, base::Bind(request_info.callback, affiliation, true));
97 } 236 }
98 237
99 // static 238 // static
100 void FacetManager::ServeRequestWithFailure(const RequestInfo& request_info) { 239 void FacetManager::ServeRequestWithFailure(const RequestInfo& request_info) {
101 request_info.callback_task_runner->PostTask( 240 request_info.callback_task_runner->PostTask(
102 FROM_HERE, base::Bind(request_info.callback, AffiliatedFacets(), false)); 241 FROM_HERE, base::Bind(request_info.callback, AffiliatedFacets(), false));
103 } 242 }
104 243
105 } // namespace password_manager 244 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698