Index: components/password_manager/core/browser/facet_manager.cc |
diff --git a/components/password_manager/core/browser/facet_manager.cc b/components/password_manager/core/browser/facet_manager.cc |
index 6de77ad98547efd53476732d059da578a76388f5..89df50ca05e4924a40834e9e678d87a0481fbe2f 100644 |
--- a/components/password_manager/core/browser/facet_manager.cc |
+++ b/components/password_manager/core/browser/facet_manager.cc |
@@ -2,20 +2,70 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+// Note: Read the class comment of AffiliationService for the definition of the |
+// terms used below. |
+// |
+// On-demand fetching strategy |
+// |
+// A GetAffiliations() request concerning facet X will be served from the cache |
+// as long as the cache contains fresh affiliation information for facet X, that |
+// is, if there is an equivalence class in the cache that contains X and has |
+// been fetched less than |kCacheHardExpiryInHours| hours ago. |
+// |
+// Otherwise, a network request is issued against the Affiliation API as soon as |
+// possible, that is, immediately if there is no fetch in flight, or right after |
+// completion of the fetch in flight if there is one, provided that the required |
+// data is not incidentally returned by the first fetch. |
+// |
+// |
+// Proactive fetching strategy |
+// |
+// A Prefetch() request concerning facet Y can trigger an initial network fetch, |
+// or periodic refetches only when: |
+// * The prefetch request is not already expired, i.e., its |keep_fresh_until| |
+// threshold is strictly in the future. |
+// * Affiliation information in the cache pertaining to facet Y will get stale |
+// strictly before the specified |keep_fresh_until| threshold. |
+// |
+// An initial fetch will be issued as soon as possible if, in addition to the |
+// two necessery conditions above, and at the time of the Prefetch() call, the |
+// cache contains no affiliation information regarding facet Y, or if the data |
+// in the cache is already near-stale or stale. |
+// |
+// A refetch will be issued every time the data in the cache regarding facet Y |
+// becomes near-stale, that is, exactly |kCacheSoftExpiry| hours after the last |
+// fetch, provided that the above two necessary conditions are also met. |
+// |
+// |
+// Cache freshness terminology |
+// |
+// |
+// Fetch (t=0) kCacheSoftExpiry kCacheHardExpiry |
+// / / / |
+// ---o------------------------o-----------------------o-----------------> t |
+// | | | |
+// | [-- Cache near-stale --)| |
+// [--------------- Cache is fresh ----------------)[-- Cache is stale .. |
+// |
+ |
#include "components/password_manager/core/browser/facet_manager.h" |
#include "base/bind.h" |
#include "base/location.h" |
#include "base/task_runner.h" |
+#include "base/time/clock.h" |
+#include "base/time/time.h" |
#include "components/password_manager/core/browser/facet_manager_host.h" |
namespace password_manager { |
namespace { |
-// The duration after which cached affiliation data is considered stale and will |
-// not be used to serve requests any longer. |
-const int kCacheLifetimeInHours = 24; |
+// The duration after which cached affiliation data is considered near-stale. |
+const int kCacheSoftExpiryInHours = 21; |
+ |
+// The duration after which cached affiliation data is considered stale. |
+const int kCacheHardExpiryInHours = 24; |
} // namespace |
@@ -25,15 +75,18 @@ struct FacetManager::RequestInfo { |
scoped_refptr<base::TaskRunner> callback_task_runner; |
}; |
-FacetManager::FacetManager(FacetManagerHost* host, const FacetURI& facet_uri) |
- : backend_(host), |
- facet_uri_(facet_uri), |
- last_update_time_(backend_->ReadLastUpdateTimeFromDatabase(facet_uri)) { |
+FacetManager::FacetManager(const FacetURI& facet_uri, |
+ FacetManagerHost* backend, |
+ base::Clock* clock) |
+ : facet_uri_(facet_uri), backend_(backend), clock_(clock) { |
+ AffiliatedFacetsWithUpdateTime affiliations; |
+ if (backend_->ReadAffiliationsFromDatabase(facet_uri_, &affiliations)) |
+ last_update_time_ = affiliations.last_update_time; |
} |
FacetManager::~FacetManager() { |
- // The manager will only be destroyed while there are pending requests if the |
- // entire backend is going. Call failure on pending requests in this case. |
+ // The manager will be destroyed while there are pending requests only if the |
+ // entire backend is going away. Fail pending requests in this case. |
for (const auto& request_info : pending_requests_) |
ServeRequestWithFailure(request_info); |
} |
@@ -61,6 +114,30 @@ void FacetManager::GetAffiliations( |
} |
} |
+void FacetManager::Prefetch(const base::Time& keep_fresh_until) { |
+ keep_fresh_untils_.insert(keep_fresh_until); |
+ |
+ // If an initial fetch if needed, trigger that (the refetch will be scheduled |
+ // once the initial fetch completes). Otherwise schedule the next refetch. |
+ base::Time next_required_fetch(GetNextRequiredFetchTimeDueToPrefetch()); |
+ if (next_required_fetch <= clock_->Now()) |
+ backend_->SignalNeedNetworkRequest(); |
+ else if (next_required_fetch < base::Time::Max()) |
+ backend_->RequestNotificationAtTime(facet_uri_, next_required_fetch); |
+ |
+ // For a finite |keep_fresh_until|, schedule a callback so that once the |
+ // prefetch expires, it can be removed from |keep_fresh_untils_|, and also the |
+ // manager can get a chance to be destroyed unless it is otherwise needed. |
+ if (keep_fresh_until > clock_->Now() && keep_fresh_until < base::Time::Max()) |
+ backend_->RequestNotificationAtTime(facet_uri_, keep_fresh_until); |
+} |
+ |
+void FacetManager::CancelPrefetch(const base::Time& keep_fresh_until) { |
+ auto iter = keep_fresh_untils_.find(keep_fresh_until); |
+ if (iter != keep_fresh_untils_.end()) |
+ keep_fresh_untils_.erase(iter); |
+} |
+ |
void FacetManager::OnFetchSucceeded( |
const AffiliatedFacetsWithUpdateTime& affiliation) { |
last_update_time_ = affiliation.last_update_time; |
@@ -68,24 +145,63 @@ void FacetManager::OnFetchSucceeded( |
for (const auto& request_info : pending_requests_) |
ServeRequestWithSuccess(request_info, affiliation.facets); |
pending_requests_.clear(); |
+ |
+ base::Time next_required_fetch(GetNextRequiredFetchTimeDueToPrefetch()); |
+ if (next_required_fetch < base::Time::Max()) |
+ backend_->RequestNotificationAtTime(facet_uri_, next_required_fetch); |
+} |
+ |
+void FacetManager::NotifyAtRequestedTime() { |
+ base::Time next_required_fetch(GetNextRequiredFetchTimeDueToPrefetch()); |
+ if (next_required_fetch <= clock_->Now()) |
+ backend_->SignalNeedNetworkRequest(); |
+ else if (next_required_fetch < base::Time::Max()) |
+ backend_->RequestNotificationAtTime(facet_uri_, next_required_fetch); |
+ |
+ auto iter_first_non_expired = keep_fresh_untils_.upper_bound(clock_->Now()); |
Mike West
2015/02/25 10:33:06
Shouldn't you clear out the list of expired "keep-
engedy
2015/03/10 11:01:00
Normally they should never expire at those time, b
|
+ keep_fresh_untils_.erase(keep_fresh_untils_.begin(), iter_first_non_expired); |
} |
bool FacetManager::CanBeDiscarded() const { |
- return pending_requests_.empty(); |
+ return pending_requests_.empty() && |
+ GetMaximumKeepFreshUntil() <= clock_->Now(); |
} |
bool FacetManager::DoesRequireFetch() const { |
- return !pending_requests_.empty() && !IsCachedDataFresh(); |
+ return (!pending_requests_.empty() && !IsCachedDataFresh()) || |
+ GetNextRequiredFetchTimeDueToPrefetch() <= clock_->Now(); |
+} |
+ |
+base::Time FacetManager::GetCacheSoftExpiryTime() const { |
+ base::TimeDelta expiry(base::TimeDelta::FromHours(kCacheSoftExpiryInHours)); |
+ return !last_update_time_.is_null() ? last_update_time_ + expiry |
+ : base::Time(); |
} |
-base::Time FacetManager::GetCacheExpirationTime() const { |
- if (last_update_time_.is_null()) |
- return base::Time(); |
- return last_update_time_ + base::TimeDelta::FromHours(kCacheLifetimeInHours); |
+base::Time FacetManager::GetCacheHardExpiryTime() const { |
+ base::TimeDelta expiry(base::TimeDelta::FromHours(kCacheHardExpiryInHours)); |
+ return !last_update_time_.is_null() ? last_update_time_ + expiry |
Mike West
2015/02/25 10:33:06
Do you need to distinguish between the first fetch
engedy
2015/03/10 11:01:00
Done.
|
+ : base::Time(); |
} |
bool FacetManager::IsCachedDataFresh() const { |
- return backend_->GetCurrentTime() < GetCacheExpirationTime(); |
+ return clock_->Now() < GetCacheHardExpiryTime(); |
+} |
+ |
+base::Time FacetManager::GetMaximumKeepFreshUntil() const { |
+ return !keep_fresh_untils_.empty() ? *keep_fresh_untils_.rbegin() |
Mike West
2015/02/25 10:33:06
Nit: My brain refuses to believe that 'untils' is
engedy
2015/03/10 11:01:00
I have renamed to |keep_fresh_until_thresholds_|.
|
+ : base::Time(); |
+} |
+ |
+base::Time FacetManager::GetNextRequiredFetchTimeDueToPrefetch() const { |
+ // If there is at least one non-expired Prefetch() request that requires the |
+ // data to be kept fresh until some time later than its current hard expiry |
+ // time, then a fetch is needed once the cached data becomes near-stale. |
+ if (clock_->Now() < GetMaximumKeepFreshUntil() && |
Mike West
2015/02/25 10:33:06
`<=`?
engedy
2015/03/10 11:01:00
I have clarified in the comment in affiliation_ser
|
+ GetCacheHardExpiryTime() < GetMaximumKeepFreshUntil()) { |
Mike West
2015/02/25 10:33:06
`<=`?
engedy
2015/03/10 11:01:00
Same here.
|
+ return GetCacheSoftExpiryTime(); |
+ } |
+ return base::Time::Max(); |
} |
// static |