| // Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ |
| #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ |
| |
| #include <string> |
| |
| #include "base/callback.h" |
| #include "base/macros.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/memory/weak_ptr.h" |
| #include "base/threading/thread_checker.h" |
| #include "components/keyed_service/core/keyed_service.h" |
| #include "components/password_manager/core/browser/affiliation_utils.h" |
| |
| namespace base { |
| class SingleThreadTaskRunner; |
| } |
| |
| namespace password_manager { |
| |
| class AffiliationBackend; |
| |
| // A service that can be used to query the list of facets that are affiliated |
| // with a given facet, i.e., facets that belong to the same logical application. |
| // See affiliation_utils.h for details of what this means. |
| // |
| // The service must be accessed from the UI thread, and it can be utilized in |
| // two ways: |
| // |
| // 1.) On-demand fetching: For the one-off query that wishes to learn |
| // affiliations of facet X when (potentially) issuing an on-demand |
| // network request to the Affiliation API containing the URI of facet X |
| // is acceptable from the privacy and/or performance perspective. |
| // |
| // This mode of operation is achieved by invoking GetAffiliations() with |
| // |cached_only| set to false. |
| // |
| // 2.) Proactive fetching: For the compound query that is concerned with |
| // checking, over time, whether or not each element in a sequence of |
| // facets, W_1, W_2, ..., W_n, is affiliated with a fixed facet Y; and |
| // when it is desired, for privacy and/or performance reasons, that only |
| // facet Y be looked up against the Affiliation API and that subsequent |
| // requests regarding each W_i not trigger additional requests. |
| // |
| // This mode of operation can be useful when, for example, the password |
| // manager has credentials stored for facet Y and wishes to check, for |
| // each visited web site W_i, whether these credentials should be offered |
| // to be autofilled onto W_i. |
| // |
| // Example code: |
| // |
| // class ExampleAffiliatedCredentialFiller |
| // : public base::SupportsWeakPtr<...> { |
| // public: |
| // ExampleAffiliatedCredentialFiller(AffiliationService* service, |
| // const FacetURI& y) |
| // : service_(service), y_(y) { |
| // cancel_handle_ = service_->Prefetch(y_, base::Time::Max()); |
| // } |
| // |
| // ~ExampleAffiliatedCredentialFiller() { cancel_handle_.Run(); } |
| // |
| // void ShouldFillInto(const FacetURI& wi, FillDelegate* delegate) { |
| // service_->GetAffiliations(wi, false, base::Bind( |
| // &ExampleAffiliatedCredentialFiller::OnAffiliationResult, |
| // AsWeakPtr(), |
| // delegate)); |
| // } |
| // |
| // void OnAffiliationResult(FillDelegate* delegate, |
| // const AffiliatedFacets& results, |
| // bool success) { |
| // if (success && std::count(results.begin(), results.end(), y_)) |
| // delegate->FillCredentialsFor(y_); |
| // } |
| // |
| // private: |
| // AffiliationService* service_; |
| // const FacetURI& y_; |
| // CancelPrefetchingHandle cancel_handle_; |
| // }; |
| class AffiliationService : public KeyedService { |
| public: |
| typedef base::Callback<void(const AffiliatedFacets& /* results */, |
| bool /* success */)> ResultCallback; |
| |
| typedef base::Closure CancelPrefetchingHandle; |
| |
| // The |backend_task_runner| should be a task runner corresponding to a thread |
| // that can take blocking I/O, and is normally Chrome's DB thread. |
| AffiliationService( |
| scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner); |
| ~AffiliationService() override; |
| |
| // Initializes the service by creating its backend and transferring it to the |
| // thread corresponding to |backend_task_runner_|. |
| void Initialize(); |
| |
| // Looks up facets affiliated with the facet identified by |facet_uri|. If |
| // |cached_only| is true, the results will be based solely on prefetched |
| // information already stored in the cache. Otherwise, on-demand network |
| // requests will be issued if there is no up-to-date data in the cache. |
| void GetAffiliations(const FacetURI& facet_uri, |
| bool cached_only, |
| const ResultCallback& result_callback); |
| |
| // Prefetches affiliation information for the facet identified by |facet_uri|, |
| // and keeps the information fresh by periodic re-fetches (as needed) until at |
| // least |keep_fresh_until|, the returned cancel handle is called, or until |
| // Chrome is shut down, whichever is sooner. It is a supported use-case to |
| // pass base::Time::Max() as |keep_fresh_until|. |
| // |
| // Canceling can be useful when a password is deleted, so that resources are |
| // no longer wasted on repeatedly refreshing affiliation information. Note |
| // that canceling will not blow away data already stored in the cache unless |
| // it becomes stale. |
| CancelPrefetchingHandle Prefetch(const FacetURI& facet_uri, |
| const base::Time& keep_fresh_until); |
| |
| // Wipes results of on-demand fetches and expired prefetches from the cache, |
| // but retains information corresponding to facets that are being kept fresh. |
| // As no required data is deleted, there will be no network requests directly |
| // triggered by this call. |
| void TrimCache(); |
| |
| private: |
| // Cancels the corresponding prefetch command, i.e., the one issued for the |
| // same |facet_uri| and with the same |keep_fresh_until|. |
| void CancelPrefetch(const FacetURI& facet_uri, const base::Time&); |
| |
| // The backend, owned by this AffiliationService instance, but living on the |
| // DB thread. It will be deleted asynchronously during shutdown on the DB |
| // thread, so it will outlive |this| along with all its in-flight tasks. |
| AffiliationBackend* backend_; |
| |
| // TaskRunner to be used to run the |backend_| (usually the DB thread). |
| scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner_; |
| |
| base::ThreadChecker thread_checker_; |
| base::WeakPtrFactory<AffiliationService> weak_ptr_factory_; |
| |
| DISALLOW_COPY_AND_ASSIGN(AffiliationService); |
| }; |
| |
| } // namespace password_manager |
| |
| #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_SERVICE_H_ |