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_SERVICE_H_ |
| 6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "components/keyed_service/core/keyed_service.h" |
| 16 #include "components/password_manager/core/browser/affiliation_utils.h" |
| 17 |
| 18 namespace base { |
| 19 class FilePath; |
| 20 class SingleThreadTaskRunner; |
| 21 } // namespace base |
| 22 |
| 23 namespace net { |
| 24 class URLRequestContextGetter; |
| 25 } // namespace net |
| 26 |
| 27 namespace password_manager { |
| 28 |
| 29 class AffiliationBackend; |
| 30 |
| 31 // A service that can be used to query the list of facets that are affiliated |
| 32 // with a given facet, i.e., facets that belong to the same logical application. |
| 33 // See affiliation_utils.h for details of what this means. |
| 34 // |
| 35 // The service must be accessed from the UI thread, and it can be utilized in |
| 36 // two ways: |
| 37 // |
| 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 |
| 40 // network request to the Affiliation API containing the URI of facet X |
| 41 // is acceptable from the privacy and/or performance perspective. |
| 42 // |
| 43 // This mode of operation is achieved by invoking GetAffiliations() with |
| 44 // StrategyOnCacheMiss::FETCH_OVER_NETWORK. |
| 45 // |
| 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 |
| 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 |
| 50 // facet Y be looked up against the Affiliation API and that subsequent |
| 51 // requests regarding each W_i not trigger additional requests. |
| 52 // |
| 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 |
| 55 // each visited web site W_i, whether these credentials should be offered |
| 56 // to be autofilled onto W_i. |
| 57 // |
| 58 // Example code: |
| 59 // |
| 60 // class ExampleAffiliatedCredentialFiller |
| 61 // : public base::SupportsWeakPtr<...> { |
| 62 // public: |
| 63 // ExampleAffiliatedCredentialFiller(AffiliationService* service, |
| 64 // const FacetURI& y) |
| 65 // : service_(service), y_(y) { |
| 66 // cancel_handle_ = service_->Prefetch(y_, base::Time::Max()); |
| 67 // } |
| 68 // |
| 69 // ~ExampleAffiliatedCredentialFiller() { cancel_handle_.Run(); } |
| 70 // |
| 71 // void ShouldFillInto(const FacetURI& wi, FillDelegate* delegate) { |
| 72 // service_->GetAffiliations(wi, StrategyOnCacheMiss::FAIL, |
| 73 // base::Bind( |
| 74 // &ExampleAffiliatedCredentialFiller::OnAffiliationResult, |
| 75 // AsWeakPtr(), |
| 76 // delegate)); |
| 77 // } |
| 78 // |
| 79 // void OnAffiliationResult(FillDelegate* delegate, |
| 80 // const AffiliatedFacets& results, |
| 81 // bool success) { |
| 82 // if (success && std::count(results.begin(), results.end(), y_)) |
| 83 // delegate->FillCredentialsFor(y_); |
| 84 // } |
| 85 // |
| 86 // private: |
| 87 // AffiliationService* service_; |
| 88 // const FacetURI& y_; |
| 89 // CancelPrefetchingHandle cancel_handle_; |
| 90 // }; |
| 91 class AffiliationService : public KeyedService { |
| 92 public: |
| 93 typedef base::Callback<void(const AffiliatedFacets& /* results */, |
| 94 bool /* success */)> ResultCallback; |
| 95 |
| 96 // Controls whether to send a network request or fail on a cache miss. |
| 97 enum class StrategyOnCacheMiss { FETCH_OVER_NETWORK, FAIL }; |
| 98 |
| 99 // The |backend_task_runner| should be a task runner corresponding to a thread |
| 100 // that can take blocking I/O, and is normally Chrome's DB thread. |
| 101 AffiliationService( |
| 102 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner); |
| 103 ~AffiliationService() override; |
| 104 |
| 105 // Initializes the service by creating its backend and transferring it to the |
| 106 // thread corresponding to |backend_task_runner_|. |
| 107 void Initialize(net::URLRequestContextGetter* request_context_getter, |
| 108 const base::FilePath& db_path); |
| 109 |
| 110 // Looks up facets affiliated with the facet identified by |facet_uri|, and |
| 111 // invokes |result_callback| with the results. |
| 112 // |
| 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. |
| 117 virtual void GetAffiliations(const FacetURI& facet_uri, |
| 118 StrategyOnCacheMiss cache_miss_strategy, |
| 119 const ResultCallback& result_callback); |
| 120 |
| 121 // Prefetches affiliation information for the facet identified by |facet_uri|, |
| 122 // and keeps the information fresh by periodic re-fetches (as needed) until |
| 123 // the clock strikes |keep_fresh_until| (exclusive), until a matching call to |
| 124 // CancelPrefetch(), or until Chrome is shut down, whichever is sooner. It is |
| 125 // a supported use-case to pass base::Time::Max() as |keep_fresh_until|. |
| 126 // |
| 127 // Canceling can be useful when a password is deleted, so that resources are |
| 128 // no longer wasted on repeatedly refreshing affiliation information. Note |
| 129 // that canceling will not blow away data already stored in the cache unless |
| 130 // it becomes stale. |
| 131 virtual void Prefetch(const FacetURI& facet_uri, |
| 132 const base::Time& keep_fresh_until); |
| 133 |
| 134 // Cancels the corresponding prefetch command, i.e., the one issued for the |
| 135 // same |facet_uri| and with the same |keep_fresh_until|. |
| 136 virtual void CancelPrefetch(const FacetURI& facet_uri, |
| 137 const base::Time& keep_fresh_until); |
| 138 |
| 139 // Wipes results of on-demand fetches and expired prefetches from the cache, |
| 140 // but retains information corresponding to facets that are being kept fresh. |
| 141 // As no required data is deleted, there will be no network requests directly |
| 142 // triggered by this call. |
| 143 // |
| 144 // The second version will only potentially remove data corresponding to the |
| 145 // given |facet_uri|, but still only as long as the data is no longer needed. |
| 146 virtual void TrimCache(); |
| 147 virtual void TrimCacheForFacet(const FacetURI& facet_uri); |
| 148 |
| 149 // Posts a task to the |backend_task_runner| to delete the cache database file |
| 150 // at |db_path|, and all auxiliary files. The database must be closed before |
| 151 // calling this. |
| 152 static void DeleteCache(const base::FilePath& db_path, |
| 153 base::SingleThreadTaskRunner* backend_task_runner); |
| 154 |
| 155 private: |
| 156 // The backend, owned by this AffiliationService instance, but living on the |
| 157 // DB thread. It will be deleted asynchronously during shutdown on the DB |
| 158 // thread, so it will outlive |this| along with all its in-flight tasks. |
| 159 AffiliationBackend* backend_; |
| 160 |
| 161 // TaskRunner to be used to run the |backend_| (usually the DB thread). |
| 162 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner_; |
| 163 |
| 164 base::ThreadChecker thread_checker_; |
| 165 base::WeakPtrFactory<AffiliationService> weak_ptr_factory_; |
| 166 |
| 167 DISALLOW_COPY_AND_ASSIGN(AffiliationService); |
| 168 }; |
| 169 |
| 170 } // namespace password_manager |
| 171 |
| 172 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ |
OLD | NEW |