OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_ | 5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_ |
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_ | 6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_ |
7 | 7 |
| 8 #include <set> |
| 9 #include <vector> |
| 10 |
8 #include "base/macros.h" | 11 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
10 #include "base/time/time.h" | 13 #include "base/time/time.h" |
11 #include "components/password_manager/core/browser/affiliation_service.h" | 14 #include "components/password_manager/core/browser/affiliation_service.h" |
12 #include "components/password_manager/core/browser/affiliation_utils.h" | 15 #include "components/password_manager/core/browser/affiliation_utils.h" |
13 | 16 |
14 namespace base { | 17 namespace base { |
| 18 class Clock; |
15 class TaskRunner; | 19 class TaskRunner; |
16 } // namespace base | 20 } // namespace base |
17 | 21 |
18 namespace password_manager { | 22 namespace password_manager { |
19 | 23 |
20 class FacetManagerHost; | 24 class FacetManagerHost; |
21 | 25 |
22 // Part of AffiliationBackend that encapsulates the state and logic required for | 26 // Encapsulates the state and logic required for handling affiliation requests |
23 // handling GetAffiliations() requests in regard to a single facet. | 27 // concerning a single facet. The AffiliationBackend owns one instance for each |
24 // | 28 // facet that requires attention, and it itself implements the FacetManagerHost |
25 // In contrast, the AffiliationBackend itself implements the FacetManagerHost | |
26 // interface to provide shared functionality needed by all FacetManagers. | 29 // interface to provide shared functionality needed by all FacetManagers. |
27 class FacetManager { | 30 class FacetManager { |
28 public: | 31 public: |
29 // The |backend| must outlive this object. | 32 // Both the |backend| and |clock| must outlive this object. |
30 FacetManager(FacetManagerHost* backend, const FacetURI& facet_uri); | 33 FacetManager(const FacetURI& facet_uri, |
| 34 FacetManagerHost* backend, |
| 35 base::Clock* clock); |
31 ~FacetManager(); | 36 ~FacetManager(); |
32 | 37 |
| 38 // Facet-specific implementations for methods in AffiliationService of the |
| 39 // same name. See documentation in affiliation_service.h for details: |
| 40 void GetAffiliations( |
| 41 bool cached_only, |
| 42 const AffiliationService::ResultCallback& callback, |
| 43 const scoped_refptr<base::TaskRunner>& callback_task_runner); |
| 44 void Prefetch(const base::Time& keep_fresh_until); |
| 45 void CancelPrefetch(const base::Time& keep_fresh_until); |
| 46 |
33 // Called when |affiliation| information regarding this facet has just been | 47 // Called when |affiliation| information regarding this facet has just been |
34 // fetched from the Affiliation API. | 48 // fetched from the Affiliation API. |
35 void OnFetchSucceeded(const AffiliatedFacetsWithUpdateTime& affiliation); | 49 void OnFetchSucceeded(const AffiliatedFacetsWithUpdateTime& affiliation); |
36 | 50 |
| 51 // Called by the backend when the time specified in RequestNotificationAtTime |
| 52 // has come to pass, so that |this| can perform delayed administrative tasks. |
| 53 void NotifyAtRequestedTime(); |
| 54 |
37 // Returns whether this instance has becomes redundant, that is, it has no | 55 // Returns whether this instance has becomes redundant, that is, it has no |
38 // more meaningful state than a newly created instance would have. | 56 // more meaningful state than a newly created instance would have. |
39 bool CanBeDiscarded() const; | 57 bool CanBeDiscarded() const; |
40 | 58 |
41 // Returns whether or not affiliation information relating to this facet needs | 59 // Returns whether or not affiliation information relating to this facet needs |
42 // to be fetched right now. | 60 // to be fetched right now. |
43 bool DoesRequireFetch() const; | 61 bool DoesRequireFetch() const; |
44 | 62 |
45 // Facet-specific implementations for methods in AffiliationService of the | 63 // The members below are made public for the sake of tests. |
46 // same name. See documentation in affiliation_service.h for details: | 64 |
47 void GetAffiliations( | 65 // Returns whether or not cached data for this facet is fresh (not stale). |
48 bool cached_only, | 66 bool IsCachedDataFresh() const; |
49 const AffiliationService::ResultCallback& callback, | 67 |
50 const scoped_refptr<base::TaskRunner>& callback_task_runner); | 68 // Returns whether or not cached data for this facet is near-stale or stale. |
| 69 bool IsCachedDataNearStale() const; |
| 70 |
| 71 // The duration after which cached affiliation data is considered near-stale. |
| 72 static const int kCacheSoftExpiryInHours; |
| 73 |
| 74 // The duration after which cached affiliation data is considered stale. |
| 75 static const int kCacheHardExpiryInHours; |
51 | 76 |
52 private: | 77 private: |
53 struct RequestInfo; | 78 struct RequestInfo; |
54 | 79 |
55 // Returns the time when cached data for this facet will expire. The data is | 80 // Returns the time when the cached data for this facet will become stale. |
56 // already considered expired at the returned microsecond. | 81 // The data is considered stale with the returned time value inclusive. |
57 base::Time GetCacheExpirationTime() const; | 82 base::Time GetCacheHardExpiryTime() const; |
58 | 83 |
59 // Returns whether or not the cache has fresh data for this facet. | 84 // Returns the time when cached data for this facet becomes near-stale. |
60 bool IsCachedDataFresh() const; | 85 // The data is considered near-stale with the returned time value inclusive. |
| 86 base::Time GetCacheSoftExpiryTime() const; |
| 87 |
| 88 // Returns the maximum of |keep_fresh_thresholds_|, or the NULL time if the |
| 89 // set is empty. |
| 90 base::Time GetMaximumKeepFreshUntilThreshold() const; |
| 91 |
| 92 // Returns the next time affiliation data for this facet needs to be fetched |
| 93 // due to active prefetch requests, or base::Time::Max() if not at all. |
| 94 base::Time GetNextRequiredFetchTimeDueToPrefetch() const; |
61 | 95 |
62 // Posts the callback of the request described by |request_info| with success. | 96 // Posts the callback of the request described by |request_info| with success. |
63 static void ServeRequestWithSuccess(const RequestInfo& request_info, | 97 static void ServeRequestWithSuccess(const RequestInfo& request_info, |
64 const AffiliatedFacets& affiliation); | 98 const AffiliatedFacets& affiliation); |
65 | 99 |
66 // Posts the callback of the request described by |request_info| with failure. | 100 // Posts the callback of the request described by |request_info| with failure. |
67 static void ServeRequestWithFailure(const RequestInfo& request_info); | 101 static void ServeRequestWithFailure(const RequestInfo& request_info); |
68 | 102 |
| 103 FacetURI facet_uri_; |
69 FacetManagerHost* backend_; | 104 FacetManagerHost* backend_; |
70 FacetURI facet_uri_; | 105 base::Clock* clock_; |
71 | 106 |
72 // The last time affiliation information was fetched for this facet, i.e. the | 107 // The last time affiliation information was fetched for this facet, i.e. the |
73 // freshness of the data in the cache. If there is no corresponding data in | 108 // freshness of the data in the cache. If there is no corresponding data in |
74 // the database, this will contain the NULL time. Otherwise, the update time | 109 // the database, this will contain the NULL time. Otherwise, the update time |
75 // in the database should match this value; it is stored to reduce disk I/O. | 110 // in the database should match this value; it is stored to reduce disk I/O. |
76 base::Time last_update_time_; | 111 base::Time last_update_time_; |
77 | 112 |
78 // Contains information about the GetAffiliations() requests that are waiting | 113 // Contains information about the GetAffiliations() requests that are waiting |
79 // for the result of looking up this facet. | 114 // for the result of looking up this facet. |
80 std::vector<RequestInfo> pending_requests_; | 115 std::vector<RequestInfo> pending_requests_; |
81 | 116 |
| 117 // Keeps track of |keep_fresh_until| thresholds corresponding to Prefetch() |
| 118 // requests for this facet. Affiliation information for this facet must be |
| 119 // kept fresh by periodic refetches until at least the maximum time in this |
| 120 // set (exclusive). |
| 121 // |
| 122 // This is not a single timestamp but rather a multiset so that cancellation |
| 123 // of individual prefetches can be supported even if there are two requests |
| 124 // with the same |keep_fresh_until| threshold. |
| 125 std::multiset<base::Time> keep_fresh_until_thresholds_; |
| 126 |
82 DISALLOW_COPY_AND_ASSIGN(FacetManager); | 127 DISALLOW_COPY_AND_ASSIGN(FacetManager); |
83 }; | 128 }; |
84 | 129 |
85 } // namespace password_manager | 130 } // namespace password_manager |
86 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_ | 131 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_ |
OLD | NEW |