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.
5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_
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"
20 class SingleThreadTaskRunner
;
24 class URLRequestContextGetter
;
27 namespace password_manager
{
29 class AffiliationBackend
;
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.
35 // The service must be accessed from the UI thread, and it can be utilized in
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.
43 // This mode of operation is achieved by invoking GetAffiliations() with
44 // StrategyOnCacheMiss::FETCH_OVER_NETWORK.
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.
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.
60 // class ExampleAffiliatedCredentialFiller
61 // : public base::SupportsWeakPtr<...> {
63 // ExampleAffiliatedCredentialFiller(AffiliationService* service,
65 // : service_(service), y_(y) {
66 // cancel_handle_ = service_->Prefetch(y_, base::Time::Max());
69 // ~ExampleAffiliatedCredentialFiller() { cancel_handle_.Run(); }
71 // void ShouldFillInto(const FacetURI& wi, FillDelegate* delegate) {
72 // service_->GetAffiliations(wi, StrategyOnCacheMiss::FAIL,
74 // &ExampleAffiliatedCredentialFiller::OnAffiliationResult,
79 // void OnAffiliationResult(FillDelegate* delegate,
80 // const AffiliatedFacets& results,
82 // if (success && std::count(results.begin(), results.end(), y_))
83 // delegate->FillCredentialsFor(y_);
87 // AffiliationService* service_;
88 // const FacetURI& y_;
89 // CancelPrefetchingHandle cancel_handle_;
91 class AffiliationService
: public KeyedService
{
93 typedef base::Callback
<void(const AffiliatedFacets
& /* results */,
94 bool /* success */)> ResultCallback
;
96 // Controls whether to send a network request or fail on a cache miss.
97 enum class StrategyOnCacheMiss
{ FETCH_OVER_NETWORK
, FAIL
};
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.
102 scoped_refptr
<base::SingleThreadTaskRunner
> backend_task_runner
);
103 ~AffiliationService() override
;
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
);
110 // Looks up facets affiliated with the facet identified by |facet_uri|, and
111 // invokes |result_callback| with the results.
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
);
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|.
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
131 virtual void Prefetch(const FacetURI
& facet_uri
,
132 const base::Time
& keep_fresh_until
);
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
);
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.
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
);
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
152 static void DeleteCache(const base::FilePath
& db_path
,
153 base::SingleThreadTaskRunner
* backend_task_runner
);
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_
;
161 // TaskRunner to be used to run the |backend_| (usually the DB thread).
162 scoped_refptr
<base::SingleThreadTaskRunner
> backend_task_runner_
;
164 base::ThreadChecker thread_checker_
;
165 base::WeakPtrFactory
<AffiliationService
> weak_ptr_factory_
;
167 DISALLOW_COPY_AND_ASSIGN(AffiliationService
);
170 } // namespace password_manager
172 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_