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 <string> | 8 #include <map> |
| 9 #include <vector> |
9 | 10 |
| 11 #include "base/containers/scoped_ptr_hash_map.h" |
10 #include "base/macros.h" | 12 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "components/password_manager/core/browser/affiliation_fetcher_delegate.
h" |
12 #include "components/password_manager/core/browser/affiliation_service.h" | 16 #include "components/password_manager/core/browser/affiliation_service.h" |
| 17 #include "components/password_manager/core/browser/affiliation_utils.h" |
13 | 18 |
14 namespace base { | 19 namespace base { |
| 20 class Clock; |
| 21 class FilePath; |
| 22 class ThreadChecker; |
15 class Time; | 23 class Time; |
16 class FilePath; | 24 } // namespace base |
17 class TaskRunner; | 25 |
18 } | 26 namespace net { |
| 27 class URLRequestContextGetter; |
| 28 } // namespace net |
19 | 29 |
20 namespace password_manager { | 30 namespace password_manager { |
21 | 31 |
22 class FacetURI; | 32 class AffiliationDatabase; |
| 33 class AffiliationFetcher; |
23 | 34 |
24 // Implements the bulk of AffiliationService; runs on a background thread. | 35 // The AffiliationBackend is the part of the AffiliationService that lives on a |
25 class AffiliationBackend { | 36 // background thread suitable for performing blocking I/O. As most tasks require |
| 37 // I/O, the backend ends up doing most of the work for the AffiliationService; |
| 38 // the latter being just a thin layer that delegates most tasks to the backend. |
| 39 // |
| 40 // This class is not thread-safe, but it is fine to construct it on one thread |
| 41 // and then transfer it to the background thread for the rest of its life. |
| 42 // Initialize() must be called already on the background thread. |
| 43 class AffiliationBackend : public AffiliationFetcherDelegate { |
26 public: | 44 public: |
27 AffiliationBackend(); | 45 // Constructs an instance that will use |request_context_getter| for all |
28 ~AffiliationBackend(); | 46 // network requests, and will rely on |time_source| to tell the current time, |
| 47 // which is expected to always be strictly greater than the NULL time. |
| 48 // Construction is very cheap, expensive steps are deferred to Initialize(). |
| 49 AffiliationBackend( |
| 50 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 51 scoped_ptr<base::Clock> time_source); |
| 52 ~AffiliationBackend() override; |
29 | 53 |
30 void Initialize(); | 54 // Performs the I/O-heavy part of initialization. The database to cache |
| 55 // affiliation information locally will be opened/created at |db_path|. |
| 56 void Initialize(const base::FilePath& db_path); |
31 | 57 |
32 // Implementations for functions of the same name in AffiliationService. | 58 // Implementations for methods of the same name in AffiliationService. They |
33 void GetAffiliations(const FacetURI& facet_uri, | 59 // are not documented here again. See affiliation_service.h for details: |
34 bool cached_only, | 60 void GetAffiliations( |
35 const AffiliationService::ResultCallback& callback, | 61 const FacetURI& facet_uri, |
36 scoped_refptr<base::TaskRunner> callback_task_runner); | 62 bool cached_only, |
| 63 const AffiliationService::ResultCallback& callback, |
| 64 const scoped_refptr<base::TaskRunner>& callback_task_runner); |
37 void Prefetch(const FacetURI& facet_uri, const base::Time& keep_fresh_until); | 65 void Prefetch(const FacetURI& facet_uri, const base::Time& keep_fresh_until); |
38 void CancelPrefetch(const FacetURI& facet_uri, | 66 void CancelPrefetch(const FacetURI& facet_uri, |
39 const base::Time& keep_fresh_until); | 67 const base::Time& keep_fresh_until); |
40 void TrimCache(); | 68 void TrimCache(); |
41 | 69 |
42 private: | 70 private: |
| 71 class FacetManager; |
| 72 friend class FacetManager; |
| 73 |
| 74 // Collects facet URIs that require fetching and issues a network request |
| 75 // against the Affiliation API to fetch corresponding affiliation information. |
| 76 void SendNetworkRequest(); |
| 77 |
| 78 // Gets the current time as per |clock_|. The returned time will always be |
| 79 // strictly greater than the NULL time. Used by FacetManager. |
| 80 base::Time GetCurrentTime(); |
| 81 |
| 82 // Reads and returns the last update time of the equivalence class containing |
| 83 // |facet_uri| from the database, or, if no such equivalence class is stored, |
| 84 // returns the NULL time. Used by FacetManager. |
| 85 base::Time ReadLastUpdateTimeFromDatabase(const FacetURI& facet_uri); |
| 86 |
| 87 // Reads the equivalence class containing |facet_uri| from the database and |
| 88 // returns true if found; returns false otherwise. Used by FacetManager. |
| 89 bool ReadAffiliationsFromDatabase( |
| 90 const FacetURI& facet_uri, |
| 91 AffiliatedFacetsWithUpdateTime* affiliations); |
| 92 |
| 93 // Signals the fetching logic that there is at least one facet that needs to |
| 94 // be fetched immediately. Called by FacetManager. |
| 95 void SignalNeedNetworkRequest(); |
| 96 |
| 97 // AffiliationFetcherDelegate: |
| 98 void OnFetchSucceeded( |
| 99 scoped_ptr<AffiliationFetcherDelegate::Result> result) override; |
| 100 void OnFetchFailed() override; |
| 101 void OnMalformedResponse() override; |
| 102 |
| 103 // Created in Initialize(), and ensures that all subsequent methods are called |
| 104 // on the same thread. |
| 105 scoped_ptr<base::ThreadChecker> thread_checker_; |
| 106 |
| 107 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 108 |
| 109 // Will always return a Now() that is strictly greater than the NULL time. |
| 110 scoped_ptr<base::Clock> clock_; |
| 111 |
| 112 scoped_ptr<AffiliationDatabase> cache_; |
| 113 scoped_ptr<AffiliationFetcher> fetcher_; |
| 114 |
| 115 // Contains a FacetManager for each facet URI that need ongoing attention. To |
| 116 // save memory, managers are discarded as soon as they become redundant. |
| 117 base::ScopedPtrHashMap<FacetURI, FacetManager> facet_managers_; |
| 118 |
| 119 base::WeakPtrFactory<AffiliationBackend> weak_ptr_factory_; |
| 120 |
43 DISALLOW_COPY_AND_ASSIGN(AffiliationBackend); | 121 DISALLOW_COPY_AND_ASSIGN(AffiliationBackend); |
44 }; | 122 }; |
45 | 123 |
46 } // namespace password_manager | 124 } // namespace password_manager |
47 | 125 |
48 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ | 126 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ |
OLD | NEW |