OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ |
| 6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 |
| 10 #include <map> |
| 11 #include <unordered_map> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/weak_ptr.h" |
| 18 #include "components/password_manager/core/browser/affiliation_fetch_throttler_d
elegate.h" |
| 19 #include "components/password_manager/core/browser/affiliation_fetcher_delegate.
h" |
| 20 #include "components/password_manager/core/browser/affiliation_service.h" |
| 21 #include "components/password_manager/core/browser/affiliation_utils.h" |
| 22 #include "components/password_manager/core/browser/facet_manager_host.h" |
| 23 |
| 24 namespace base { |
| 25 class Clock; |
| 26 class FilePath; |
| 27 class SingleThreadTaskRunner; |
| 28 class TaskRunner; |
| 29 class ThreadChecker; |
| 30 class TickClock; |
| 31 class Time; |
| 32 } // namespace base |
| 33 |
| 34 namespace net { |
| 35 class URLRequestContextGetter; |
| 36 } // namespace net |
| 37 |
| 38 namespace password_manager { |
| 39 |
| 40 class AffiliationDatabase; |
| 41 class AffiliationFetcher; |
| 42 class AffiliationFetchThrottler; |
| 43 class FacetManager; |
| 44 |
| 45 // The AffiliationBackend is the part of the AffiliationService that lives on a |
| 46 // background thread suitable for performing blocking I/O. As most tasks require |
| 47 // I/O, the backend ends up doing most of the work for the AffiliationService; |
| 48 // the latter being just a thin layer that delegates most tasks to the backend. |
| 49 // |
| 50 // This class is not thread-safe, but it is fine to construct it on one thread |
| 51 // and then transfer it to the background thread for the rest of its life. |
| 52 // Initialize() must be called already on the final (background) thread. |
| 53 class AffiliationBackend : public FacetManagerHost, |
| 54 public AffiliationFetcherDelegate, |
| 55 public AffiliationFetchThrottlerDelegate { |
| 56 public: |
| 57 using StrategyOnCacheMiss = AffiliationService::StrategyOnCacheMiss; |
| 58 |
| 59 // Constructs an instance that will use |request_context_getter| for all |
| 60 // network requests, use |task_runner| for asynchronous tasks, and will rely |
| 61 // on |time_source| and |time_tick_source| to tell the current time/ticks. |
| 62 // Construction is very cheap, expensive steps are deferred to Initialize(). |
| 63 AffiliationBackend( |
| 64 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 65 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 66 std::unique_ptr<base::Clock> time_source, |
| 67 std::unique_ptr<base::TickClock> time_tick_source); |
| 68 ~AffiliationBackend() override; |
| 69 |
| 70 // Performs the I/O-heavy part of initialization. The database used to cache |
| 71 // affiliation information locally will be opened/created at |db_path|. |
| 72 void Initialize(const base::FilePath& db_path); |
| 73 |
| 74 // Implementations for methods of the same name in AffiliationService. They |
| 75 // are not documented here again. See affiliation_service.h for details: |
| 76 void GetAffiliations( |
| 77 const FacetURI& facet_uri, |
| 78 StrategyOnCacheMiss cache_miss_strategy, |
| 79 const AffiliationService::ResultCallback& callback, |
| 80 const scoped_refptr<base::TaskRunner>& callback_task_runner); |
| 81 void Prefetch(const FacetURI& facet_uri, const base::Time& keep_fresh_until); |
| 82 void CancelPrefetch(const FacetURI& facet_uri, |
| 83 const base::Time& keep_fresh_until); |
| 84 void TrimCache(); |
| 85 void TrimCacheForFacet(const FacetURI& facet_uri); |
| 86 |
| 87 // Deletes the cache database file at |db_path|, and all auxiliary files. The |
| 88 // database must be closed before calling this. |
| 89 static void DeleteCache(const base::FilePath& db_path); |
| 90 |
| 91 private: |
| 92 friend class AffiliationBackendTest; |
| 93 FRIEND_TEST_ALL_PREFIXES( |
| 94 AffiliationBackendTest, |
| 95 DiscardCachedDataIfNoLongerNeededWithEmptyAffiliation); |
| 96 |
| 97 // Retrieves the FacetManager corresponding to |facet_uri|, creating it and |
| 98 // storing it into |facet_managers_| if it did not exist. |
| 99 FacetManager* GetOrCreateFacetManager(const FacetURI& facet_uri); |
| 100 |
| 101 // Discards cached data corresponding to |affiliated_facets| unless there are |
| 102 // FacetManagers that still need the data. |
| 103 void DiscardCachedDataIfNoLongerNeeded( |
| 104 const AffiliatedFacets& affiliated_facets); |
| 105 |
| 106 // Scheduled by RequestNotificationAtTime() to be called back at times when a |
| 107 // FacetManager needs to be notified. |
| 108 void OnSendNotification(const FacetURI& facet_uri); |
| 109 |
| 110 // FacetManagerHost: |
| 111 bool ReadAffiliationsFromDatabase( |
| 112 const FacetURI& facet_uri, |
| 113 AffiliatedFacetsWithUpdateTime* affiliations) override; |
| 114 void SignalNeedNetworkRequest() override; |
| 115 void RequestNotificationAtTime(const FacetURI& facet_uri, |
| 116 base::Time time) override; |
| 117 |
| 118 // AffiliationFetcherDelegate: |
| 119 void OnFetchSucceeded( |
| 120 std::unique_ptr<AffiliationFetcherDelegate::Result> result) override; |
| 121 void OnFetchFailed() override; |
| 122 void OnMalformedResponse() override; |
| 123 |
| 124 // AffiliationFetchThrottlerDelegate: |
| 125 bool OnCanSendNetworkRequest() override; |
| 126 |
| 127 // Returns the number of in-memory FacetManagers. Used only for testing. |
| 128 size_t facet_manager_count_for_testing() { return facet_managers_.size(); } |
| 129 |
| 130 // Reports the |requested_facet_uri_count| in a single fetch; and the elapsed |
| 131 // time before the first fetch, and in-between subsequent fetches. |
| 132 void ReportStatistics(size_t requested_facet_uri_count); |
| 133 |
| 134 // To be called after Initialize() to use |throttler| instead of the default |
| 135 // one. Used only for testing. |
| 136 void SetThrottlerForTesting( |
| 137 std::unique_ptr<AffiliationFetchThrottler> throttler); |
| 138 |
| 139 // Created in Initialize(), and ensures that all subsequent methods are called |
| 140 // on the same thread. |
| 141 std::unique_ptr<base::ThreadChecker> thread_checker_; |
| 142 |
| 143 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 144 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 145 std::unique_ptr<base::Clock> clock_; |
| 146 std::unique_ptr<base::TickClock> tick_clock_; |
| 147 |
| 148 std::unique_ptr<AffiliationDatabase> cache_; |
| 149 std::unique_ptr<AffiliationFetcher> fetcher_; |
| 150 std::unique_ptr<AffiliationFetchThrottler> throttler_; |
| 151 |
| 152 base::Time construction_time_; |
| 153 base::Time last_request_time_; |
| 154 |
| 155 // Contains a FacetManager for each facet URI that need ongoing attention. To |
| 156 // save memory, managers are discarded as soon as they become redundant. |
| 157 std::unordered_map<FacetURI, std::unique_ptr<FacetManager>, FacetURIHash> |
| 158 facet_managers_; |
| 159 |
| 160 base::WeakPtrFactory<AffiliationBackend> weak_ptr_factory_; |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(AffiliationBackend); |
| 163 }; |
| 164 |
| 165 } // namespace password_manager |
| 166 |
| 167 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ |
OLD | NEW |