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_SERVICE_H_ | 5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ |
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ | 6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 // | 34 // |
35 // The service must be accessed from the UI thread, and it can be utilized in | 35 // The service must be accessed from the UI thread, and it can be utilized in |
36 // two ways: | 36 // two ways: |
37 // | 37 // |
38 // 1.) On-demand fetching: For the one-off query that wishes to learn | 38 // 1.) On-demand fetching: For the one-off query that wishes to learn |
39 // affiliations of facet X when (potentially) issuing an on-demand | 39 // affiliations of facet X when (potentially) issuing an on-demand |
40 // network request to the Affiliation API containing the URI of facet X | 40 // network request to the Affiliation API containing the URI of facet X |
41 // is acceptable from the privacy and/or performance perspective. | 41 // is acceptable from the privacy and/or performance perspective. |
42 // | 42 // |
43 // This mode of operation is achieved by invoking GetAffiliations() with | 43 // This mode of operation is achieved by invoking GetAffiliations() with |
44 // |cached_only| set to false. | 44 // StrategyOnCacheMiss::FETCH_OVER_NETWORK. |
45 // | 45 // |
46 // 2.) Proactive fetching: For the compound query that is concerned with | 46 // 2.) Proactive fetching: For the compound query that is concerned with |
47 // checking, over time, whether or not each element in a sequence of | 47 // checking, over time, whether or not each element in a sequence of |
48 // facets, W_1, W_2, ..., W_n, is affiliated with a fixed facet Y; and | 48 // facets, W_1, W_2, ..., W_n, is affiliated with a fixed facet Y; and |
49 // when it is desired, for privacy and/or performance reasons, that only | 49 // when it is desired, for privacy and/or performance reasons, that only |
50 // facet Y be looked up against the Affiliation API and that subsequent | 50 // facet Y be looked up against the Affiliation API and that subsequent |
51 // requests regarding each W_i not trigger additional requests. | 51 // requests regarding each W_i not trigger additional requests. |
52 // | 52 // |
53 // This mode of operation can be useful when, for example, the password | 53 // This mode of operation can be useful when, for example, the password |
54 // manager has credentials stored for facet Y and wishes to check, for | 54 // manager has credentials stored for facet Y and wishes to check, for |
55 // each visited web site W_i, whether these credentials should be offered | 55 // each visited web site W_i, whether these credentials should be offered |
56 // to be autofilled onto W_i. | 56 // to be autofilled onto W_i. |
57 // | 57 // |
58 // Example code: | 58 // Example code: |
59 // | 59 // |
60 // class ExampleAffiliatedCredentialFiller | 60 // class ExampleAffiliatedCredentialFiller |
61 // : public base::SupportsWeakPtr<...> { | 61 // : public base::SupportsWeakPtr<...> { |
62 // public: | 62 // public: |
63 // ExampleAffiliatedCredentialFiller(AffiliationService* service, | 63 // ExampleAffiliatedCredentialFiller(AffiliationService* service, |
64 // const FacetURI& y) | 64 // const FacetURI& y) |
65 // : service_(service), y_(y) { | 65 // : service_(service), y_(y) { |
66 // cancel_handle_ = service_->Prefetch(y_, base::Time::Max()); | 66 // cancel_handle_ = service_->Prefetch(y_, base::Time::Max()); |
67 // } | 67 // } |
68 // | 68 // |
69 // ~ExampleAffiliatedCredentialFiller() { cancel_handle_.Run(); } | 69 // ~ExampleAffiliatedCredentialFiller() { cancel_handle_.Run(); } |
70 // | 70 // |
71 // void ShouldFillInto(const FacetURI& wi, FillDelegate* delegate) { | 71 // void ShouldFillInto(const FacetURI& wi, FillDelegate* delegate) { |
72 // service_->GetAffiliations(wi, false, base::Bind( | 72 // service_->GetAffiliations(wi, StrategyOnCacheMiss::FAIL, |
73 // &ExampleAffiliatedCredentialFiller::OnAffiliationResult, | 73 // base::Bind( |
74 // AsWeakPtr(), | 74 // &ExampleAffiliatedCredentialFiller::OnAffiliationResult, |
75 // delegate)); | 75 // AsWeakPtr(), |
76 // delegate)); | |
76 // } | 77 // } |
77 // | 78 // |
78 // void OnAffiliationResult(FillDelegate* delegate, | 79 // void OnAffiliationResult(FillDelegate* delegate, |
79 // const AffiliatedFacets& results, | 80 // const AffiliatedFacets& results, |
80 // bool success) { | 81 // bool success) { |
81 // if (success && std::count(results.begin(), results.end(), y_)) | 82 // if (success && std::count(results.begin(), results.end(), y_)) |
82 // delegate->FillCredentialsFor(y_); | 83 // delegate->FillCredentialsFor(y_); |
83 // } | 84 // } |
84 // | 85 // |
85 // private: | 86 // private: |
86 // AffiliationService* service_; | 87 // AffiliationService* service_; |
87 // const FacetURI& y_; | 88 // const FacetURI& y_; |
88 // CancelPrefetchingHandle cancel_handle_; | 89 // CancelPrefetchingHandle cancel_handle_; |
89 // }; | 90 // }; |
90 class AffiliationService : public KeyedService { | 91 class AffiliationService : public KeyedService { |
91 public: | 92 public: |
92 typedef base::Callback<void(const AffiliatedFacets& /* results */, | 93 typedef base::Callback<void(const AffiliatedFacets& /* results */, |
93 bool /* success */)> ResultCallback; | 94 bool /* success */)> ResultCallback; |
94 | 95 |
96 // Controls whether to send a network request or fail on a cache miss. | |
97 enum class StrategyOnCacheMiss { FETCH_OVER_NETWORK, FAIL }; | |
Mike West
2015/03/17 13:47:10
These names seem fine to me. Failure is totally a
engedy
2015/03/17 13:54:07
I dwelled on it for a bit more, and while a bit we
| |
98 | |
95 // The |backend_task_runner| should be a task runner corresponding to a thread | 99 // The |backend_task_runner| should be a task runner corresponding to a thread |
96 // that can take blocking I/O, and is normally Chrome's DB thread. | 100 // that can take blocking I/O, and is normally Chrome's DB thread. |
97 AffiliationService( | 101 AffiliationService( |
98 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner); | 102 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner); |
99 ~AffiliationService() override; | 103 ~AffiliationService() override; |
100 | 104 |
101 // Initializes the service by creating its backend and transferring it to the | 105 // Initializes the service by creating its backend and transferring it to the |
102 // thread corresponding to |backend_task_runner_|. | 106 // thread corresponding to |backend_task_runner_|. |
103 void Initialize(net::URLRequestContextGetter* request_context_getter, | 107 void Initialize(net::URLRequestContextGetter* request_context_getter, |
104 const base::FilePath& db_path); | 108 const base::FilePath& db_path); |
105 | 109 |
106 // Looks up facets affiliated with the facet identified by |facet_uri|. If | 110 // Looks up facets affiliated with the facet identified by |facet_uri|, and |
107 // |cached_only| is true, the results will be based solely on prefetched | 111 // invokes |result_callback| with the results. |
108 // information already stored in the cache. Otherwise, on-demand network | 112 // |
109 // requests will be issued if there is no up-to-date data in the cache. | 113 // If the local cache contains fresh affiliation information for |facet_uri|, |
114 // the request will be served from cache. Otherwise, |cache_miss_policy| | |
115 // controls whether to issue an on-demand network request, or to fail the | |
116 // request without fetching. | |
110 virtual void GetAffiliations(const FacetURI& facet_uri, | 117 virtual void GetAffiliations(const FacetURI& facet_uri, |
111 bool cached_only, | 118 StrategyOnCacheMiss cache_miss_strategy, |
112 const ResultCallback& result_callback); | 119 const ResultCallback& result_callback); |
113 | 120 |
114 // Prefetches affiliation information for the facet identified by |facet_uri|, | 121 // Prefetches affiliation information for the facet identified by |facet_uri|, |
115 // and keeps the information fresh by periodic re-fetches (as needed) until | 122 // and keeps the information fresh by periodic re-fetches (as needed) until |
116 // the clock strikes |keep_fresh_until| (exclusive), until a matching call to | 123 // the clock strikes |keep_fresh_until| (exclusive), until a matching call to |
117 // CancelPrefetch(), or until Chrome is shut down, whichever is sooner. It is | 124 // CancelPrefetch(), or until Chrome is shut down, whichever is sooner. It is |
118 // a supported use-case to pass base::Time::Max() as |keep_fresh_until|. | 125 // a supported use-case to pass base::Time::Max() as |keep_fresh_until|. |
119 // | 126 // |
120 // Canceling can be useful when a password is deleted, so that resources are | 127 // Canceling can be useful when a password is deleted, so that resources are |
121 // no longer wasted on repeatedly refreshing affiliation information. Note | 128 // no longer wasted on repeatedly refreshing affiliation information. Note |
(...skipping 24 matching lines...) Expand all Loading... | |
146 | 153 |
147 base::ThreadChecker thread_checker_; | 154 base::ThreadChecker thread_checker_; |
148 base::WeakPtrFactory<AffiliationService> weak_ptr_factory_; | 155 base::WeakPtrFactory<AffiliationService> weak_ptr_factory_; |
149 | 156 |
150 DISALLOW_COPY_AND_ASSIGN(AffiliationService); | 157 DISALLOW_COPY_AND_ASSIGN(AffiliationService); |
151 }; | 158 }; |
152 | 159 |
153 } // namespace password_manager | 160 } // namespace password_manager |
154 | 161 |
155 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ | 162 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ |
OLD | NEW |