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 affiliation information relating to this facet needs
62 // to be fetched right now.
63 bool DoesRequireFetch() const;
65 // The members below are made public for the sake of tests.
67 // Returns whether or not cached data for this facet is fresh (not stale).
68 bool IsCachedDataFresh() const;
70 // Returns whether or not cached data for this facet is near-stale or stale.
71 bool IsCachedDataNearStale() const;
73 // The duration after which cached affiliation data is considered near-stale.
74 static const int kCacheSoftExpiryInHours
;
76 // The duration after which cached affiliation data is considered stale.
77 static const int kCacheHardExpiryInHours
;
82 // Returns the time when the cached data for this facet will become stale.
83 // The data is considered stale with the returned time value inclusive.
84 base::Time
GetCacheHardExpiryTime() const;
86 // Returns the time when cached data for this facet becomes near-stale.
87 // The data is considered near-stale with the returned time value inclusive.
88 base::Time
GetCacheSoftExpiryTime() const;
90 // Returns the maximum of |keep_fresh_thresholds_|, or the NULL time if the
92 base::Time
GetMaximumKeepFreshUntilThreshold() const;
94 // Returns the next time affiliation data for this facet needs to be fetched
95 // due to active prefetch requests, or base::Time::Max() if not at all.
96 base::Time
GetNextRequiredFetchTimeDueToPrefetch() const;
98 // Posts the callback of the request described by |request_info| with success.
99 static void ServeRequestWithSuccess(const RequestInfo
& request_info
,
100 const AffiliatedFacets
& affiliation
);
102 // Posts the callback of the request described by |request_info| with failure.
103 static void ServeRequestWithFailure(const RequestInfo
& request_info
);
106 FacetManagerHost
* backend_
;
109 // The last time affiliation information was fetched for this facet, i.e. the
110 // freshness of the data in the cache. If there is no corresponding data in
111 // the database, this will contain the NULL time. Otherwise, the update time
112 // in the database should match this value; it is stored to reduce disk I/O.
113 base::Time last_update_time_
;
115 // Contains information about the GetAffiliations() requests that are waiting
116 // for the result of looking up this facet.
117 std::vector
<RequestInfo
> pending_requests_
;
119 // Keeps track of |keep_fresh_until| thresholds corresponding to Prefetch()
120 // requests for this facet. Affiliation information for this facet must be
121 // kept fresh by periodic refetches until at least the maximum time in this
124 // This is not a single timestamp but rather a multiset so that cancellation
125 // of individual prefetches can be supported even if there are two requests
126 // with the same |keep_fresh_until| threshold.
127 std::multiset
<base::Time
> keep_fresh_until_thresholds_
;
129 DISALLOW_COPY_AND_ASSIGN(FacetManager
);
132 } // namespace password_manager
133 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_