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 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/time/time.h"
14 #include "components/password_manager/core/browser/affiliation_service.h"
15 #include "components/password_manager/core/browser/affiliation_utils.h"
22 namespace password_manager
{
24 class FacetManagerHost
;
26 // Encapsulates the state and logic required for handling affiliation requests
27 // concerning a single facet. The AffiliationBackend owns one instance for each
28 // facet that requires attention, and it itself implements the FacetManagerHost
29 // interface to provide shared functionality needed by all FacetManagers.
32 using StrategyOnCacheMiss
= AffiliationService::StrategyOnCacheMiss
;
34 // Both the |backend| and |clock| must outlive this object.
35 FacetManager(const FacetURI
& facet_uri
,
36 FacetManagerHost
* backend
,
40 // Facet-specific implementations for methods in AffiliationService of the
41 // same name. See documentation in affiliation_service.h for details:
43 StrategyOnCacheMiss cache_miss_strategy
,
44 const AffiliationService::ResultCallback
& callback
,
45 const scoped_refptr
<base::TaskRunner
>& callback_task_runner
);
46 void Prefetch(const base::Time
& keep_fresh_until
);
47 void CancelPrefetch(const base::Time
& keep_fresh_until
);
49 // Called when |affiliation| information regarding this facet has just been
50 // fetched from the Affiliation API.
51 void OnFetchSucceeded(const AffiliatedFacetsWithUpdateTime
& affiliation
);
53 // Called by the backend when the time specified in RequestNotificationAtTime
54 // has come to pass, so that |this| can perform delayed administrative tasks.
55 void NotifyAtRequestedTime();
57 // Returns whether this instance has becomes redundant, that is, it has no
58 // more meaningful state than a newly created instance would have.
59 bool CanBeDiscarded() const;
61 // Returns whether or not cached data for this facet can be discarded without
62 // harm when trimming the database.
63 bool CanCachedDataBeDiscarded() const;
65 // Returns whether or not affiliation information relating to this facet needs
66 // to be fetched right now.
67 bool DoesRequireFetch() const;
69 // The members below are made public for the sake of tests.
71 // Returns whether or not cached data for this facet is fresh (not stale).
72 bool IsCachedDataFresh() const;
74 // Returns whether or not cached data for this facet is near-stale or stale.
75 bool IsCachedDataNearStale() const;
77 // The duration after which cached affiliation data is considered near-stale.
78 static const int kCacheSoftExpiryInHours
;
80 // The duration after which cached affiliation data is considered stale.
81 static const int kCacheHardExpiryInHours
;
86 // Returns the time when the cached data for this facet will become stale.
87 // The data is considered stale with the returned time value inclusive.
88 base::Time
GetCacheHardExpiryTime() const;
90 // Returns the time when cached data for this facet becomes near-stale.
91 // The data is considered near-stale with the returned time value inclusive.
92 base::Time
GetCacheSoftExpiryTime() const;
94 // Returns the maximum of |keep_fresh_thresholds_|, or the NULL time if the
96 base::Time
GetMaximumKeepFreshUntilThreshold() const;
98 // Returns the next time affiliation data for this facet needs to be fetched
99 // due to active prefetch requests, or base::Time::Max() if not at all.
100 base::Time
GetNextRequiredFetchTimeDueToPrefetch() const;
102 // Posts the callback of the request described by |request_info| with success.
103 static void ServeRequestWithSuccess(const RequestInfo
& request_info
,
104 const AffiliatedFacets
& affiliation
);
106 // Posts the callback of the request described by |request_info| with failure.
107 static void ServeRequestWithFailure(const RequestInfo
& request_info
);
110 FacetManagerHost
* backend_
;
113 // The last time affiliation information was fetched for this facet, i.e. the
114 // freshness of the data in the cache. If there is no corresponding data in
115 // the database, this will contain the NULL time. Otherwise, the update time
116 // in the database should match this value; it is stored to reduce disk I/O.
117 base::Time last_update_time_
;
119 // Contains information about the GetAffiliations() requests that are waiting
120 // for the result of looking up this facet.
121 std::vector
<RequestInfo
> pending_requests_
;
123 // Keeps track of |keep_fresh_until| thresholds corresponding to Prefetch()
124 // requests for this facet. Affiliation information for this facet must be
125 // kept fresh by periodic refetches until at least the maximum time in this
128 // This is not a single timestamp but rather a multiset so that cancellation
129 // of individual prefetches can be supported even if there are two requests
130 // with the same |keep_fresh_until| threshold.
131 std::multiset
<base::Time
> keep_fresh_until_thresholds_
;
133 DISALLOW_COPY_AND_ASSIGN(FacetManager
);
136 } // namespace password_manager
137 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_