Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / components / password_manager / core / browser / affiliation_service.h
blob0ace7907831220354a8e4950a2ec2f5a7786b0d7
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_
8 #include <string>
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"
18 namespace base {
19 class FilePath;
20 class SingleThreadTaskRunner;
21 } // namespace base
23 namespace net {
24 class URLRequestContextGetter;
25 } // namespace net
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
36 // two ways:
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 // |cached_only| set to false.
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.
58 // Example code:
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 // }
69 // ~ExampleAffiliatedCredentialFiller() { cancel_handle_.Run(); }
71 // void ShouldFillInto(const FacetURI& wi, FillDelegate* delegate) {
72 // service_->GetAffiliations(wi, false, base::Bind(
73 // &ExampleAffiliatedCredentialFiller::OnAffiliationResult,
74 // AsWeakPtr(),
75 // delegate));
76 // }
78 // void OnAffiliationResult(FillDelegate* delegate,
79 // const AffiliatedFacets& results,
80 // bool success) {
81 // if (success && std::count(results.begin(), results.end(), y_))
82 // delegate->FillCredentialsFor(y_);
83 // }
85 // private:
86 // AffiliationService* service_;
87 // const FacetURI& y_;
88 // CancelPrefetchingHandle cancel_handle_;
89 // };
90 class AffiliationService : public KeyedService {
91 public:
92 typedef base::Callback<void(const AffiliatedFacets& /* results */,
93 bool /* success */)> ResultCallback;
95 typedef base::Closure CancelPrefetchingHandle;
97 // The |backend_task_runner| should be a task runner corresponding to a thread
98 // that can take blocking I/O, and is normally Chrome's DB thread.
99 AffiliationService(
100 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner);
101 ~AffiliationService() override;
103 // Initializes the service by creating its backend and transferring it to the
104 // thread corresponding to |backend_task_runner_|.
105 void Initialize(net::URLRequestContextGetter* request_context_getter,
106 const base::FilePath& db_path);
108 // Looks up facets affiliated with the facet identified by |facet_uri|. If
109 // |cached_only| is true, the results will be based solely on prefetched
110 // information already stored in the cache. Otherwise, on-demand network
111 // requests will be issued if there is no up-to-date data in the cache.
112 void GetAffiliations(const FacetURI& facet_uri,
113 bool cached_only,
114 const ResultCallback& result_callback);
116 // Prefetches affiliation information for the facet identified by |facet_uri|,
117 // and keeps the information fresh by periodic re-fetches (as needed) until at
118 // least |keep_fresh_until|, the returned cancel handle is called, or until
119 // Chrome is shut down, whichever is sooner. It is a supported use-case to
120 // pass base::Time::Max() as |keep_fresh_until|.
122 // Canceling can be useful when a password is deleted, so that resources are
123 // no longer wasted on repeatedly refreshing affiliation information. Note
124 // that canceling will not blow away data already stored in the cache unless
125 // it becomes stale.
126 CancelPrefetchingHandle Prefetch(const FacetURI& facet_uri,
127 const base::Time& keep_fresh_until);
129 // Wipes results of on-demand fetches and expired prefetches from the cache,
130 // but retains information corresponding to facets that are being kept fresh.
131 // As no required data is deleted, there will be no network requests directly
132 // triggered by this call.
133 void TrimCache();
135 private:
136 // Cancels the corresponding prefetch command, i.e., the one issued for the
137 // same |facet_uri| and with the same |keep_fresh_until|.
138 void CancelPrefetch(const FacetURI& facet_uri, const base::Time&);
140 // The backend, owned by this AffiliationService instance, but living on the
141 // DB thread. It will be deleted asynchronously during shutdown on the DB
142 // thread, so it will outlive |this| along with all its in-flight tasks.
143 AffiliationBackend* backend_;
145 // TaskRunner to be used to run the |backend_| (usually the DB thread).
146 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner_;
148 base::ThreadChecker thread_checker_;
149 base::WeakPtrFactory<AffiliationService> weak_ptr_factory_;
151 DISALLOW_COPY_AND_ASSIGN(AffiliationService);
154 } // namespace password_manager
156 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_