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 // Construction is very cheap, expensive steps are deferred to Initialize(). | |
48 AffiliationBackend( | |
49 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | |
50 scoped_ptr<base::Clock> time_source); | |
51 ~AffiliationBackend() override; | |
29 | 52 |
30 void Initialize(); | 53 // Performs the I/O-heavy part of initialization. The database to cache |
54 // affiliation information locally will be opened/created at |db_path|. | |
55 void Initialize(const base::FilePath& db_path); | |
31 | 56 |
32 // Implementations for functions of the same name in AffiliationService. | 57 // Implementations for methods of the same name in AffiliationService. They |
33 void GetAffiliations(const FacetURI& facet_uri, | 58 // are not documented here again, see affiliation_service.h for details: |
Garrett Casto
2015/02/03 08:20:38
Nit: This colon seems like it should be a period.
engedy
2015/02/03 10:49:23
Done.
| |
34 bool cached_only, | 59 void GetAffiliations( |
35 const AffiliationService::ResultCallback& callback, | 60 const FacetURI& facet_uri, |
36 scoped_refptr<base::TaskRunner> callback_task_runner); | 61 bool cached_only, |
62 const AffiliationService::ResultCallback& callback, | |
63 const scoped_refptr<base::TaskRunner>& callback_task_runner); | |
37 void Prefetch(const FacetURI& facet_uri, const base::Time& keep_fresh_until); | 64 void Prefetch(const FacetURI& facet_uri, const base::Time& keep_fresh_until); |
38 void CancelPrefetch(const FacetURI& facet_uri, | 65 void CancelPrefetch(const FacetURI& facet_uri, |
39 const base::Time& keep_fresh_until); | 66 const base::Time& keep_fresh_until); |
40 void TrimCache(); | 67 void TrimCache(); |
41 | 68 |
42 private: | 69 private: |
70 class FacetManager; | |
71 friend class FacetManager; | |
72 | |
73 // Collects facet URIs that require fetching and issues a network request | |
74 // against the Affiliation API to fetch corresponding affiliation information. | |
75 void SendNetworkRequest(); | |
76 | |
77 // Gets the current time as per |clock_|. Used by FacetManager. | |
78 base::Time GetCurrentTime(); | |
79 | |
80 // Reads and returns the last update time of the equivalence class containing | |
81 // |facet_uri| from the database, or, if no such equivalence class is stored, | |
82 // returns a time sufficiently far in the past to be considered stale. Used by | |
83 // FacetManager. | |
84 base::Time ReadLastUpdateTimeFromDatabase(const FacetURI& facet_uri); | |
85 | |
86 // Reads the equivalence class containing |facet_uri| from the database and | |
87 // returns true if found; returns false otherwise. Used by FacetManager. | |
88 bool ReadAffiliationsFromDatabase( | |
89 const FacetURI& facet_uri, | |
90 AffiliatedFacetsWithUpdateTime* affiliations); | |
91 | |
92 // Signals the fetching logic that there is at least one facet that needs to | |
93 // be fetched immediately. Called by FacetManager. | |
94 void SignalNeedNetworkRequest(); | |
95 | |
96 // AffiliationFetcherDelegate: | |
97 void OnFetchSucceeded( | |
98 scoped_ptr<AffiliationFetcherDelegate::Result> result) override; | |
99 void OnFetchFailed() override; | |
100 void OnMalformedResponse() override; | |
101 | |
102 // Created in Initialize(), and ensures that all subsequent methods are called | |
103 // on the same thread. | |
104 scoped_ptr<base::ThreadChecker> thread_checker_; | |
105 | |
106 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
107 scoped_ptr<base::Clock> clock_; | |
108 | |
109 scoped_ptr<AffiliationDatabase> cache_; | |
110 scoped_ptr<AffiliationFetcher> fetcher_; | |
111 | |
112 // Contains a FacetManager for each facet URI that need ongoing attention. To | |
113 // save memory, managers are discarded as soon as they become redundant. | |
114 base::ScopedPtrHashMap<FacetURI, FacetManager> facet_managers_; | |
115 | |
116 base::WeakPtrFactory<AffiliationBackend> weak_ptr_factory_; | |
117 | |
43 DISALLOW_COPY_AND_ASSIGN(AffiliationBackend); | 118 DISALLOW_COPY_AND_ASSIGN(AffiliationBackend); |
44 }; | 119 }; |
45 | 120 |
46 } // namespace password_manager | 121 } // namespace password_manager |
47 | 122 |
48 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ | 123 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_ |
OLD | NEW |