// Copyright 2015 The Chromium Authors // 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 GetAffiliationsAndBranding() 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 (that is, prefetch intervals are open // from the right). // * 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 for facet Y is near-stale, that is, it has been fetched more // than |kCacheHardExpiryInHours| hours ago. // // 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. // // Fetches are triggered already when the data gets near-stale, as opposed to // waiting until the data would get stale, in an effort to keep the data fresh // even in face of temporary network errors lasting no more than the difference // between soft and hard expiry times. // // The current fetch scheduling logic, however, can only deal with at most one // such 'early' fetch between taking place between the prior fetch and the // corresponding hard expiry time of the data, therefore it is assumed that: // // kCacheSoftExpiryInHours < kCacheHardExpiryInHours, and // 2 * kCacheSoftExpiryInHours > kCacheHardExpiryInHours. // // // Cache freshness terminology // // // Fetch (t=0) kCacheSoftExpiry kCacheHardExpiry // / / / // ---o------------------------o-----------------------o-----------------> t // | | | // | [-- Cache near-stale --------------------- .. // [--------------- Cache is fresh ----------------)[-- Cache is stale .. // #include "components/affiliations/core/browser/facet_manager.h" #include "base/functional/bind.h" #include "base/location.h" #include "base/task/task_runner.h" #include "base/time/clock.h" #include "base/time/time.h" #include "components/affiliations/core/browser/facet_manager_host.h" namespace affiliations { // statics const int FacetManager::kCacheSoftExpiryInHours = …; const int FacetManager::kCacheHardExpiryInHours = …; static_assert …; static_assert …; // Encapsulates the details of a pending GetAffiliationsAndBranding() request. struct FacetManager::RequestInfo { … }; FacetManager::FacetManager(const FacetURI& facet_uri, FacetManagerHost* backend, base::Clock* clock) : … { … } FacetManager::~FacetManager() { … } void FacetManager::GetAffiliationsAndBranding( StrategyOnCacheMiss cache_miss_strategy, AffiliationService::ResultCallback callback, const scoped_refptr<base::TaskRunner>& callback_task_runner) { … } void FacetManager::Prefetch(const base::Time& keep_fresh_until) { … } void FacetManager::CancelPrefetch(const base::Time& keep_fresh_until) { … } void FacetManager::OnFetchSucceeded( const AffiliatedFacetsWithUpdateTime& affiliation) { … } void FacetManager::OnFetchFailed() { … } void FacetManager::NotifyAtRequestedTime() { … } bool FacetManager::CanBeDiscarded() const { … } bool FacetManager::CanCachedDataBeDiscarded() const { … } bool FacetManager::DoesRequireFetch() const { … } bool FacetManager::IsCachedDataFresh() const { … } bool FacetManager::IsCachedDataNearStale() const { … } base::Time FacetManager::GetCacheSoftExpiryTime() const { … } base::Time FacetManager::GetCacheHardExpiryTime() const { … } base::Time FacetManager::GetMaximumKeepFreshUntilThreshold() const { … } base::Time FacetManager::GetNextRequiredFetchTimeDueToPrefetch() const { … } // static void FacetManager::ServeRequestWithSuccess( RequestInfo request_info, const AffiliatedFacets& affiliation) { … } // static void FacetManager::ServeRequestWithFailure(RequestInfo request_info) { … } } // namespace affiliations