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_AFFILIATION_BACKEND_H_ | 5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ |
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ | 6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/containers/scoped_ptr_hash_map.h" | 11 #include "base/containers/scoped_ptr_hash_map.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "components/password_manager/core/browser/affiliation_fetch_throttler_d
elegate.h" |
15 #include "components/password_manager/core/browser/affiliation_fetcher_delegate.
h" | 16 #include "components/password_manager/core/browser/affiliation_fetcher_delegate.
h" |
16 #include "components/password_manager/core/browser/affiliation_service.h" | 17 #include "components/password_manager/core/browser/affiliation_service.h" |
17 #include "components/password_manager/core/browser/affiliation_utils.h" | 18 #include "components/password_manager/core/browser/affiliation_utils.h" |
18 #include "components/password_manager/core/browser/facet_manager_host.h" | 19 #include "components/password_manager/core/browser/facet_manager_host.h" |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 class Clock; | 22 class Clock; |
22 class FilePath; | 23 class FilePath; |
| 24 class SingleThreadTaskRunner; |
23 class ThreadChecker; | 25 class ThreadChecker; |
| 26 class TickClock; |
24 class Time; | 27 class Time; |
25 } // namespace base | 28 } // namespace base |
26 | 29 |
27 namespace net { | 30 namespace net { |
28 class URLRequestContextGetter; | 31 class URLRequestContextGetter; |
29 } // namespace net | 32 } // namespace net |
30 | 33 |
31 namespace password_manager { | 34 namespace password_manager { |
32 | 35 |
33 class AffiliationDatabase; | 36 class AffiliationDatabase; |
34 class AffiliationFetcher; | 37 class AffiliationFetcher; |
| 38 class AffiliationFetchThrottler; |
35 class FacetManager; | 39 class FacetManager; |
36 | 40 |
37 // The AffiliationBackend is the part of the AffiliationService that lives on a | 41 // The AffiliationBackend is the part of the AffiliationService that lives on a |
38 // background thread suitable for performing blocking I/O. As most tasks require | 42 // background thread suitable for performing blocking I/O. As most tasks require |
39 // I/O, the backend ends up doing most of the work for the AffiliationService; | 43 // I/O, the backend ends up doing most of the work for the AffiliationService; |
40 // the latter being just a thin layer that delegates most tasks to the backend. | 44 // the latter being just a thin layer that delegates most tasks to the backend. |
41 // | 45 // |
42 // This class is not thread-safe, but it is fine to construct it on one thread | 46 // This class is not thread-safe, but it is fine to construct it on one thread |
43 // and then transfer it to the background thread for the rest of its life. | 47 // and then transfer it to the background thread for the rest of its life. |
44 // Initialize() must be called already on the final (background) thread. | 48 // Initialize() must be called already on the final (background) thread. |
45 class AffiliationBackend : public FacetManagerHost, | 49 class AffiliationBackend : public FacetManagerHost, |
46 public AffiliationFetcherDelegate { | 50 public AffiliationFetcherDelegate, |
| 51 public AffiliationFetchThrottlerDelegate { |
47 public: | 52 public: |
48 // Constructs an instance that will use |request_context_getter| for all | 53 // Constructs an instance that will use |request_context_getter| for all |
49 // network requests, and will rely on |time_source| to tell the current time, | 54 // network requests, use |task_runner| for asynchronous tasks, and will rely |
50 // which is expected to always be no less than the Unix epoch. | 55 // on |time_source| and |time_tick_source| to tell the current time/ticks. |
51 // Construction is very cheap, expensive steps are deferred to Initialize(). | 56 // Construction is very cheap, expensive steps are deferred to Initialize(). |
52 AffiliationBackend( | 57 AffiliationBackend( |
53 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 58 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
54 scoped_ptr<base::Clock> time_source); | 59 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 60 scoped_ptr<base::Clock> time_source, |
| 61 scoped_ptr<base::TickClock> time_tick_source); |
55 ~AffiliationBackend() override; | 62 ~AffiliationBackend() override; |
56 | 63 |
57 // Performs the I/O-heavy part of initialization. The database used to cache | 64 // Performs the I/O-heavy part of initialization. The database used to cache |
58 // affiliation information locally will be opened/created at |db_path|. | 65 // affiliation information locally will be opened/created at |db_path|. |
59 void Initialize(const base::FilePath& db_path); | 66 void Initialize(const base::FilePath& db_path); |
60 | 67 |
61 // Implementations for methods of the same name in AffiliationService. They | 68 // Implementations for methods of the same name in AffiliationService. They |
62 // are not documented here again. See affiliation_service.h for details: | 69 // are not documented here again. See affiliation_service.h for details: |
63 void GetAffiliations( | 70 void GetAffiliations( |
64 const FacetURI& facet_uri, | 71 const FacetURI& facet_uri, |
65 bool cached_only, | 72 bool cached_only, |
66 const AffiliationService::ResultCallback& callback, | 73 const AffiliationService::ResultCallback& callback, |
67 const scoped_refptr<base::TaskRunner>& callback_task_runner); | 74 const scoped_refptr<base::TaskRunner>& callback_task_runner); |
68 void Prefetch(const FacetURI& facet_uri, const base::Time& keep_fresh_until); | 75 void Prefetch(const FacetURI& facet_uri, const base::Time& keep_fresh_until); |
69 void CancelPrefetch(const FacetURI& facet_uri, | 76 void CancelPrefetch(const FacetURI& facet_uri, |
70 const base::Time& keep_fresh_until); | 77 const base::Time& keep_fresh_until); |
71 void TrimCache(); | 78 void TrimCache(); |
72 | 79 |
73 private: | 80 private: |
74 friend class AffiliationBackendTest; | 81 friend class AffiliationBackendTest; |
75 | 82 |
76 // Retrieves the FacetManager corresponding to |facet_uri|, creating it and | 83 // Retrieves the FacetManager corresponding to |facet_uri|, creating it and |
77 // storing it into |facet_managers_| if it did not exist. | 84 // storing it into |facet_managers_| if it did not exist. |
78 FacetManager* GetOrCreateFacetManager(const FacetURI& facet_uri); | 85 FacetManager* GetOrCreateFacetManager(const FacetURI& facet_uri); |
79 | 86 |
80 // Collects facet URIs that require fetching and issues a network request | |
81 // against the Affiliation API to fetch corresponding affiliation information. | |
82 void SendNetworkRequest(); | |
83 | |
84 // Scheduled by RequestNotificationAtTime() to be called back at times when a | 87 // Scheduled by RequestNotificationAtTime() to be called back at times when a |
85 // FacetManager needs to be notified. | 88 // FacetManager needs to be notified. |
86 void OnSendNotification(const FacetURI& facet_uri); | 89 void OnSendNotification(const FacetURI& facet_uri); |
87 | 90 |
88 // FacetManagerHost: | 91 // FacetManagerHost: |
89 bool ReadAffiliationsFromDatabase( | 92 bool ReadAffiliationsFromDatabase( |
90 const FacetURI& facet_uri, | 93 const FacetURI& facet_uri, |
91 AffiliatedFacetsWithUpdateTime* affiliations) override; | 94 AffiliatedFacetsWithUpdateTime* affiliations) override; |
92 void SignalNeedNetworkRequest() override; | 95 void SignalNeedNetworkRequest() override; |
93 void RequestNotificationAtTime(const FacetURI& facet_uri, | 96 void RequestNotificationAtTime(const FacetURI& facet_uri, |
94 base::Time time) override; | 97 base::Time time) override; |
95 | 98 |
96 // AffiliationFetcherDelegate: | 99 // AffiliationFetcherDelegate: |
97 void OnFetchSucceeded( | 100 void OnFetchSucceeded( |
98 scoped_ptr<AffiliationFetcherDelegate::Result> result) override; | 101 scoped_ptr<AffiliationFetcherDelegate::Result> result) override; |
99 void OnFetchFailed() override; | 102 void OnFetchFailed() override; |
100 void OnMalformedResponse() override; | 103 void OnMalformedResponse() override; |
101 | 104 |
102 // Used only for testing. | 105 // AffiliationFetchThrottlerDelegate: |
| 106 bool OnCanSendNetworkRequest() override; |
| 107 |
| 108 // Returns the number of in-memory FacetManagers. Used only for testing. |
103 size_t facet_manager_count_for_testing() { return facet_managers_.size(); } | 109 size_t facet_manager_count_for_testing() { return facet_managers_.size(); } |
104 | 110 |
| 111 // To be called after Initialize() to use |throttler| instead of the default |
| 112 // one. Used only for testing. |
| 113 void SetThrottlerForTesting(scoped_ptr<AffiliationFetchThrottler> throttler); |
| 114 |
105 // Created in Initialize(), and ensures that all subsequent methods are called | 115 // Created in Initialize(), and ensures that all subsequent methods are called |
106 // on the same thread. | 116 // on the same thread. |
107 scoped_ptr<base::ThreadChecker> thread_checker_; | 117 scoped_ptr<base::ThreadChecker> thread_checker_; |
108 | 118 |
109 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 119 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
110 | 120 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
111 // Will always return a Now() that is strictly greater than the NULL time. | |
112 scoped_ptr<base::Clock> clock_; | 121 scoped_ptr<base::Clock> clock_; |
| 122 scoped_ptr<base::TickClock> tick_clock_; |
113 | 123 |
114 scoped_ptr<AffiliationDatabase> cache_; | 124 scoped_ptr<AffiliationDatabase> cache_; |
115 scoped_ptr<AffiliationFetcher> fetcher_; | 125 scoped_ptr<AffiliationFetcher> fetcher_; |
| 126 scoped_ptr<AffiliationFetchThrottler> throttler_; |
116 | 127 |
117 // Contains a FacetManager for each facet URI that need ongoing attention. To | 128 // Contains a FacetManager for each facet URI that need ongoing attention. To |
118 // save memory, managers are discarded as soon as they become redundant. | 129 // save memory, managers are discarded as soon as they become redundant. |
119 base::ScopedPtrHashMap<FacetURI, FacetManager> facet_managers_; | 130 base::ScopedPtrHashMap<FacetURI, FacetManager> facet_managers_; |
120 | 131 |
121 base::WeakPtrFactory<AffiliationBackend> weak_ptr_factory_; | 132 base::WeakPtrFactory<AffiliationBackend> weak_ptr_factory_; |
122 | 133 |
123 DISALLOW_COPY_AND_ASSIGN(AffiliationBackend); | 134 DISALLOW_COPY_AND_ASSIGN(AffiliationBackend); |
124 }; | 135 }; |
125 | 136 |
126 } // namespace password_manager | 137 } // namespace password_manager |
127 | 138 |
128 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ | 139 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ |
OLD | NEW |