cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / password_manager / core / browser / facet_manager.cc
bloba0e9e8667371d41689ca49145dd1e0df4b6efc9c
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
3 // found in the LICENSE file.
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.
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.
21 // Proactive fetching strategy
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.
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.
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.
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.
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:
50 // kCacheSoftExpiryInHours < kCacheHardExpiryInHours, and
51 // 2 * kCacheSoftExpiryInHours > kCacheHardExpiryInHours.
54 // Cache freshness terminology
57 // Fetch (t=0) kCacheSoftExpiry kCacheHardExpiry
58 // / / /
59 // ---o------------------------o-----------------------o-----------------> t
60 // | | |
61 // | [-- Cache near-stale --------------------- ..
62 // [--------------- Cache is fresh ----------------)[-- Cache is stale ..
65 #include "components/password_manager/core/browser/facet_manager.h"
67 #include "base/bind.h"
68 #include "base/location.h"
69 #include "base/task_runner.h"
70 #include "base/time/clock.h"
71 #include "base/time/time.h"
72 #include "components/password_manager/core/browser/facet_manager_host.h"
74 namespace password_manager {
76 // statics
77 const int FacetManager::kCacheSoftExpiryInHours = 21;
78 const int FacetManager::kCacheHardExpiryInHours = 24;
80 static_assert(
81 FacetManager::kCacheSoftExpiryInHours <
82 FacetManager::kCacheHardExpiryInHours,
83 "Soft expiry period must be shorter than the hard expiry period.");
85 static_assert(
86 2 * FacetManager::kCacheSoftExpiryInHours >
87 FacetManager::kCacheHardExpiryInHours,
88 "Soft expiry period must be longer than half of the hard expiry period.");
90 // Encapsulates the details of a pending GetAffiliations() request.
91 struct FacetManager::RequestInfo {
92 AffiliationService::ResultCallback callback;
93 scoped_refptr<base::TaskRunner> callback_task_runner;
96 FacetManager::FacetManager(const FacetURI& facet_uri,
97 FacetManagerHost* backend,
98 base::Clock* clock)
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;
105 FacetManager::~FacetManager() {
106 // The manager will be destroyed while there are pending requests only if the
107 // entire backend is going away. Fail pending requests in this case.
108 for (const auto& request_info : pending_requests_)
109 ServeRequestWithFailure(request_info);
112 void FacetManager::GetAffiliations(
113 StrategyOnCacheMiss cache_miss_strategy,
114 const AffiliationService::ResultCallback& callback,
115 const scoped_refptr<base::TaskRunner>& callback_task_runner) {
116 RequestInfo request_info;
117 request_info.callback = callback;
118 request_info.callback_task_runner = callback_task_runner;
119 if (IsCachedDataFresh()) {
120 AffiliatedFacetsWithUpdateTime affiliation;
121 if (!backend_->ReadAffiliationsFromDatabase(facet_uri_, &affiliation)) {
122 ServeRequestWithFailure(request_info);
123 return;
125 DCHECK_EQ(affiliation.last_update_time, last_update_time_) << facet_uri_;
126 ServeRequestWithSuccess(request_info, affiliation.facets);
127 } else if (cache_miss_strategy == StrategyOnCacheMiss::FETCH_OVER_NETWORK) {
128 pending_requests_.push_back(request_info);
129 backend_->SignalNeedNetworkRequest();
130 } else {
131 ServeRequestWithFailure(request_info);
135 void FacetManager::Prefetch(const base::Time& keep_fresh_until) {
136 keep_fresh_until_thresholds_.insert(keep_fresh_until);
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);
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);
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);
159 void FacetManager::OnFetchSucceeded(
160 const AffiliatedFacetsWithUpdateTime& affiliation) {
161 last_update_time_ = affiliation.last_update_time;
162 DCHECK(IsCachedDataFresh()) << facet_uri_;
163 for (const auto& request_info : pending_requests_)
164 ServeRequestWithSuccess(request_info, affiliation.facets);
165 pending_requests_.clear();
167 base::Time next_required_fetch(GetNextRequiredFetchTimeDueToPrefetch());
168 if (next_required_fetch < base::Time::Max())
169 backend_->RequestNotificationAtTime(facet_uri_, next_required_fetch);
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);
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);
185 bool FacetManager::CanBeDiscarded() const {
186 return pending_requests_.empty() &&
187 GetMaximumKeepFreshUntilThreshold() <= clock_->Now();
190 bool FacetManager::CanCachedDataBeDiscarded() const {
191 return GetMaximumKeepFreshUntilThreshold() <= clock_->Now() ||
192 !IsCachedDataFresh();
195 bool FacetManager::DoesRequireFetch() const {
196 return (!pending_requests_.empty() && !IsCachedDataFresh()) ||
197 GetNextRequiredFetchTimeDueToPrefetch() <= clock_->Now();
200 bool FacetManager::IsCachedDataFresh() const {
201 return clock_->Now() < GetCacheHardExpiryTime();
204 bool FacetManager::IsCachedDataNearStale() const {
205 return GetCacheSoftExpiryTime() <= clock_->Now();
208 base::Time FacetManager::GetCacheSoftExpiryTime() const {
209 return last_update_time_ +
210 base::TimeDelta::FromHours(kCacheSoftExpiryInHours);
213 base::Time FacetManager::GetCacheHardExpiryTime() const {
214 return last_update_time_ +
215 base::TimeDelta::FromHours(kCacheHardExpiryInHours);
218 base::Time FacetManager::GetMaximumKeepFreshUntilThreshold() const {
219 return !keep_fresh_until_thresholds_.empty()
220 ? *keep_fresh_until_thresholds_.rbegin()
221 : base::Time();
224 base::Time FacetManager::GetNextRequiredFetchTimeDueToPrefetch() const {
225 // If there is at least one non-expired Prefetch() request that requires the
226 // data to be kept fresh until some time later than its current hard expiry
227 // time, then a fetch is needed once the cached data becomes near-stale.
228 if (clock_->Now() < GetMaximumKeepFreshUntilThreshold() &&
229 GetCacheHardExpiryTime() < GetMaximumKeepFreshUntilThreshold()) {
230 return GetCacheSoftExpiryTime();
232 return base::Time::Max();
235 // static
236 void FacetManager::ServeRequestWithSuccess(
237 const RequestInfo& request_info,
238 const AffiliatedFacets& affiliation) {
239 request_info.callback_task_runner->PostTask(
240 FROM_HERE, base::Bind(request_info.callback, affiliation, true));
243 // static
244 void FacetManager::ServeRequestWithFailure(const RequestInfo& request_info) {
245 request_info.callback_task_runner->PostTask(
246 FROM_HERE, base::Bind(request_info.callback, AffiliatedFacets(), false));
249 } // namespace password_manager