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