diff options
author | engedy <engedy@chromium.org> | 2015-02-16 06:29:49 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-16 14:30:46 +0000 |
commit | 11fce3fc8ed1e1d4b065933e626e3cf24d520191 (patch) | |
tree | 7b99a4f280578cf83029697de519ac3ce39a9921 | |
parent | 98003ec1b124286a470e1295a118885a99850f6b (diff) | |
download | chromium_src-11fce3fc8ed1e1d4b065933e626e3cf24d520191.zip chromium_src-11fce3fc8ed1e1d4b065933e626e3cf24d520191.tar.gz chromium_src-11fce3fc8ed1e1d4b065933e626e3cf24d520191.tar.bz2 |
Refactor FacetManager into a separate file.
BUG=437865
Review URL: https://codereview.chromium.org/919093004
Cr-Commit-Position: refs/heads/master@{#316478}
7 files changed, 249 insertions, 174 deletions
diff --git a/components/password_manager.gypi b/components/password_manager.gypi index 90d7f3b..977b167 100644 --- a/components/password_manager.gypi +++ b/components/password_manager.gypi @@ -45,6 +45,9 @@ 'password_manager/core/browser/browser_save_password_progress_logger.h', 'password_manager/core/browser/export/csv_writer.cc', 'password_manager/core/browser/export/csv_writer.h', + 'password_manager/core/browser/facet_manager_host.h', + 'password_manager/core/browser/facet_manager.cc', + 'password_manager/core/browser/facet_manager.h', 'password_manager/core/browser/import/csv_reader.cc', 'password_manager/core/browser/import/csv_reader.h', 'password_manager/core/browser/log_receiver.h', diff --git a/components/password_manager/core/browser/BUILD.gn b/components/password_manager/core/browser/BUILD.gn index 2f1b1c0..d6601bf 100644 --- a/components/password_manager/core/browser/BUILD.gn +++ b/components/password_manager/core/browser/BUILD.gn @@ -33,6 +33,9 @@ static_library("browser") { "affiliation_utils.h", "browser_save_password_progress_logger.cc", "browser_save_password_progress_logger.h", + "facet_manager.cc", + "facet_manager.h", + "facet_manager_host.h", "log_receiver.h", "log_router.cc", "log_router.h", diff --git a/components/password_manager/core/browser/affiliation_backend.cc b/components/password_manager/core/browser/affiliation_backend.cc index 2b8dcc1..9b74b8e 100644 --- a/components/password_manager/core/browser/affiliation_backend.cc +++ b/components/password_manager/core/browser/affiliation_backend.cc @@ -6,165 +6,16 @@ #include <stdint.h> -#include "base/bind.h" -#include "base/location.h" #include "base/task_runner.h" #include "base/threading/thread_checker.h" #include "base/time/clock.h" #include "base/time/time.h" #include "components/password_manager/core/browser/affiliation_database.h" #include "components/password_manager/core/browser/affiliation_fetcher.h" +#include "components/password_manager/core/browser/facet_manager.h" #include "net/url_request/url_request_context_getter.h" namespace password_manager { -namespace { - -// The duration after which cached affiliation data is considered stale and will -// not be used to serve requests any longer. -const int kCacheLifetimeInHours = 24; - -// RequestInfo ---------------------------------------------------------------- - -// Encapsulates the details of a pending GetAffiliations() request. -struct RequestInfo { - AffiliationService::ResultCallback callback; - scoped_refptr<base::TaskRunner> callback_task_runner; -}; - -} // namespace - -// AffiliationBackend::FacetManager ------------------------------------------- - -// Manages and performs ongoing tasks concerning a single facet. -class AffiliationBackend::FacetManager { - public: - // The |backend| must outlive this object. - FacetManager(AffiliationBackend* backend, const FacetURI& facet_uri); - ~FacetManager(); - - // Called when |affiliation| information regarding this facet has just been - // fetched from the Affiliation API. - void OnFetchSucceeded(const AffiliatedFacetsWithUpdateTime& affiliation); - - // Returns whether this instance has becomes redundant, that is, it has no - // more meaningful state than a newly created instance would have. - bool CanBeDiscarded() const; - - // Returns whether or not affiliation information relating to this facet needs - // to be fetched right now. - bool DoesRequireFetch() const; - - // Facet-specific implementations for methods in AffiliationService of the - // same name. See documentation in affiliation_service.h for details: - void GetAffiliations(bool cached_only, const RequestInfo& request_info); - - private: - // Returns the time when cached data for this facet will expire. The data is - // already considered expired at the returned microsecond. - base::Time GetCacheExpirationTime() const; - - // Returns whether or not the cache has fresh data for this facet. - bool IsCachedDataFresh() const; - - // Posts the callback of the request described by |request_info| with success. - static void ServeRequestWithSuccess(const RequestInfo& request_info, - const AffiliatedFacets& affiliation); - - // Posts the callback of the request described by |request_info| with failure. - static void ServeRequestWithFailure(const RequestInfo& request_info); - - AffiliationBackend* backend_; - FacetURI facet_uri_; - - // The last time affiliation information was fetched for this facet, i.e. the - // freshness of the data in the cache. If there is no corresponding data in - // the database, this will contain the NULL time. Otherwise, the update time - // in the database should match this value; it is stored to reduce disk I/O. - base::Time last_update_time_; - - // Contains information about the GetAffiliations() requests that are waiting - // for the result of looking up this facet. - std::vector<RequestInfo> pending_requests_; - - DISALLOW_COPY_AND_ASSIGN(FacetManager); -}; - -AffiliationBackend::FacetManager::FacetManager(AffiliationBackend* backend, - const FacetURI& facet_uri) - : backend_(backend), - facet_uri_(facet_uri), - last_update_time_(backend_->ReadLastUpdateTimeFromDatabase(facet_uri)) { -} - -AffiliationBackend::FacetManager::~FacetManager() { - // The manager will only be destroyed while there are pending requests if the - // entire backend is going. Call failure on pending requests in this case. - for (const auto& request_info : pending_requests_) - ServeRequestWithFailure(request_info); -} - -void AffiliationBackend::FacetManager::GetAffiliations( - bool cached_only, - const RequestInfo& request_info) { - if (IsCachedDataFresh()) { - AffiliatedFacetsWithUpdateTime affiliation; - if (!backend_->ReadAffiliationsFromDatabase(facet_uri_, &affiliation)) { - // TODO(engedy): Implement this. crbug.com/437865. - NOTIMPLEMENTED(); - } - DCHECK_EQ(affiliation.last_update_time, last_update_time_) << facet_uri_; - ServeRequestWithSuccess(request_info, affiliation.facets); - } else if (cached_only) { - ServeRequestWithFailure(request_info); - } else { - pending_requests_.push_back(request_info); - backend_->SignalNeedNetworkRequest(); - } -} - -void AffiliationBackend::FacetManager::OnFetchSucceeded( - const AffiliatedFacetsWithUpdateTime& affiliation) { - last_update_time_ = affiliation.last_update_time; - DCHECK(IsCachedDataFresh()) << facet_uri_; - for (const auto& request_info : pending_requests_) - ServeRequestWithSuccess(request_info, affiliation.facets); - pending_requests_.clear(); -} - -bool AffiliationBackend::FacetManager::CanBeDiscarded() const { - return pending_requests_.empty(); -} - -bool AffiliationBackend::FacetManager::DoesRequireFetch() const { - return !pending_requests_.empty() && !IsCachedDataFresh(); -} - -base::Time AffiliationBackend::FacetManager::GetCacheExpirationTime() const { - if (last_update_time_.is_null()) - return base::Time(); - return last_update_time_ + base::TimeDelta::FromHours(kCacheLifetimeInHours); -} - -bool AffiliationBackend::FacetManager::IsCachedDataFresh() const { - return backend_->GetCurrentTime() < GetCacheExpirationTime(); -} - -// static -void AffiliationBackend::FacetManager::ServeRequestWithSuccess( - const RequestInfo& request_info, - const AffiliatedFacets& affiliation) { - request_info.callback_task_runner->PostTask( - FROM_HERE, base::Bind(request_info.callback, affiliation, true)); -} - -// static -void AffiliationBackend::FacetManager::ServeRequestWithFailure( - const RequestInfo& request_info) { - request_info.callback_task_runner->PostTask( - FROM_HERE, base::Bind(request_info.callback, AffiliatedFacets(), false)); -} - -// AffiliationBackend --------------------------------------------------------- AffiliationBackend::AffiliationBackend( const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, @@ -200,10 +51,7 @@ void AffiliationBackend::GetAffiliations( FacetManager* facet_manager = facet_managers_.get(facet_uri); DCHECK(facet_manager); - RequestInfo request_info; - request_info.callback = callback; - request_info.callback_task_runner = callback_task_runner; - facet_manager->GetAffiliations(cached_only, request_info); + facet_manager->GetAffiliations(cached_only, callback, callback_task_runner); if (facet_manager->CanBeDiscarded()) facet_managers_.erase(facet_uri); diff --git a/components/password_manager/core/browser/affiliation_backend.h b/components/password_manager/core/browser/affiliation_backend.h index d668375..eea5e3d 100644 --- a/components/password_manager/core/browser/affiliation_backend.h +++ b/components/password_manager/core/browser/affiliation_backend.h @@ -15,6 +15,7 @@ #include "components/password_manager/core/browser/affiliation_fetcher_delegate.h" #include "components/password_manager/core/browser/affiliation_service.h" #include "components/password_manager/core/browser/affiliation_utils.h" +#include "components/password_manager/core/browser/facet_manager_host.h" namespace base { class Clock; @@ -31,6 +32,7 @@ namespace password_manager { class AffiliationDatabase; class AffiliationFetcher; +class FacetManager; // The AffiliationBackend is the part of the AffiliationService that lives on a // background thread suitable for performing blocking I/O. As most tasks require @@ -40,7 +42,8 @@ class AffiliationFetcher; // This class is not thread-safe, but it is fine to construct it on one thread // and then transfer it to the background thread for the rest of its life. // Initialize() must be called already on the background thread. -class AffiliationBackend : public AffiliationFetcherDelegate { +class AffiliationBackend : public FacetManagerHost, + public AffiliationFetcherDelegate { public: // Constructs an instance that will use |request_context_getter| for all // network requests, and will rely on |time_source| to tell the current time, @@ -68,31 +71,17 @@ class AffiliationBackend : public AffiliationFetcherDelegate { void TrimCache(); private: - class FacetManager; - friend class FacetManager; - // Collects facet URIs that require fetching and issues a network request // against the Affiliation API to fetch corresponding affiliation information. void SendNetworkRequest(); - // Gets the current time as per |clock_|. The returned time will always be - // strictly greater than the NULL time. Used by FacetManager. - base::Time GetCurrentTime(); - - // Reads and returns the last update time of the equivalence class containing - // |facet_uri| from the database, or, if no such equivalence class is stored, - // returns the NULL time. Used by FacetManager. - base::Time ReadLastUpdateTimeFromDatabase(const FacetURI& facet_uri); - - // Reads the equivalence class containing |facet_uri| from the database and - // returns true if found; returns false otherwise. Used by FacetManager. + // FacetManagerHost: + base::Time GetCurrentTime() override; + base::Time ReadLastUpdateTimeFromDatabase(const FacetURI& facet_uri) override; bool ReadAffiliationsFromDatabase( const FacetURI& facet_uri, - AffiliatedFacetsWithUpdateTime* affiliations); - - // Signals the fetching logic that there is at least one facet that needs to - // be fetched immediately. Called by FacetManager. - void SignalNeedNetworkRequest(); + AffiliatedFacetsWithUpdateTime* affiliations) override; + void SignalNeedNetworkRequest() override; // AffiliationFetcherDelegate: void OnFetchSucceeded( diff --git a/components/password_manager/core/browser/facet_manager.cc b/components/password_manager/core/browser/facet_manager.cc new file mode 100644 index 0000000..6de77ad --- /dev/null +++ b/components/password_manager/core/browser/facet_manager.cc @@ -0,0 +1,105 @@ +// 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. + +#include "components/password_manager/core/browser/facet_manager.h" + +#include "base/bind.h" +#include "base/location.h" +#include "base/task_runner.h" +#include "components/password_manager/core/browser/facet_manager_host.h" + +namespace password_manager { + +namespace { + +// The duration after which cached affiliation data is considered stale and will +// not be used to serve requests any longer. +const int kCacheLifetimeInHours = 24; + +} // namespace + +// Encapsulates the details of a pending GetAffiliations() request. +struct FacetManager::RequestInfo { + AffiliationService::ResultCallback callback; + scoped_refptr<base::TaskRunner> callback_task_runner; +}; + +FacetManager::FacetManager(FacetManagerHost* host, const FacetURI& facet_uri) + : backend_(host), + facet_uri_(facet_uri), + last_update_time_(backend_->ReadLastUpdateTimeFromDatabase(facet_uri)) { +} + +FacetManager::~FacetManager() { + // The manager will only be destroyed while there are pending requests if the + // entire backend is going. Call failure on pending requests in this case. + for (const auto& request_info : pending_requests_) + ServeRequestWithFailure(request_info); +} + +void FacetManager::GetAffiliations( + bool cached_only, + const AffiliationService::ResultCallback& callback, + const scoped_refptr<base::TaskRunner>& callback_task_runner) { + RequestInfo request_info; + request_info.callback = callback; + request_info.callback_task_runner = callback_task_runner; + if (IsCachedDataFresh()) { + AffiliatedFacetsWithUpdateTime affiliation; + if (!backend_->ReadAffiliationsFromDatabase(facet_uri_, &affiliation)) { + // TODO(engedy): Implement this. crbug.com/437865. + NOTIMPLEMENTED(); + } + DCHECK_EQ(affiliation.last_update_time, last_update_time_) << facet_uri_; + ServeRequestWithSuccess(request_info, affiliation.facets); + } else if (cached_only) { + ServeRequestWithFailure(request_info); + } else { + pending_requests_.push_back(request_info); + backend_->SignalNeedNetworkRequest(); + } +} + +void FacetManager::OnFetchSucceeded( + const AffiliatedFacetsWithUpdateTime& affiliation) { + last_update_time_ = affiliation.last_update_time; + DCHECK(IsCachedDataFresh()) << facet_uri_; + for (const auto& request_info : pending_requests_) + ServeRequestWithSuccess(request_info, affiliation.facets); + pending_requests_.clear(); +} + +bool FacetManager::CanBeDiscarded() const { + return pending_requests_.empty(); +} + +bool FacetManager::DoesRequireFetch() const { + return !pending_requests_.empty() && !IsCachedDataFresh(); +} + +base::Time FacetManager::GetCacheExpirationTime() const { + if (last_update_time_.is_null()) + return base::Time(); + return last_update_time_ + base::TimeDelta::FromHours(kCacheLifetimeInHours); +} + +bool FacetManager::IsCachedDataFresh() const { + return backend_->GetCurrentTime() < GetCacheExpirationTime(); +} + +// static +void FacetManager::ServeRequestWithSuccess( + const RequestInfo& request_info, + const AffiliatedFacets& affiliation) { + request_info.callback_task_runner->PostTask( + FROM_HERE, base::Bind(request_info.callback, affiliation, true)); +} + +// static +void FacetManager::ServeRequestWithFailure(const RequestInfo& request_info) { + request_info.callback_task_runner->PostTask( + FROM_HERE, base::Bind(request_info.callback, AffiliatedFacets(), false)); +} + +} // namespace password_manager diff --git a/components/password_manager/core/browser/facet_manager.h b/components/password_manager/core/browser/facet_manager.h new file mode 100644 index 0000000..f27e9f2 --- /dev/null +++ b/components/password_manager/core/browser/facet_manager.h @@ -0,0 +1,86 @@ +// 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_FACET_MANAGER_H_ +#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_ + +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/time/time.h" +#include "components/password_manager/core/browser/affiliation_service.h" +#include "components/password_manager/core/browser/affiliation_utils.h" + +namespace base { +class TaskRunner; +} // namespace base + +namespace password_manager { + +class FacetManagerHost; + +// Part of AffiliationBackend that encapsulates the state and logic required for +// handling GetAffiliations() requests in regard to a single facet. +// +// In contrast, the AffiliationBackend itself implements the FacetManagerHost +// interface to provide shared functionality needed by all FacetManagers. +class FacetManager { + public: + // The |backend| must outlive this object. + FacetManager(FacetManagerHost* backend, const FacetURI& facet_uri); + ~FacetManager(); + + // Called when |affiliation| information regarding this facet has just been + // fetched from the Affiliation API. + void OnFetchSucceeded(const AffiliatedFacetsWithUpdateTime& affiliation); + + // Returns whether this instance has becomes redundant, that is, it has no + // more meaningful state than a newly created instance would have. + bool CanBeDiscarded() const; + + // Returns whether or not affiliation information relating to this facet needs + // to be fetched right now. + bool DoesRequireFetch() const; + + // Facet-specific implementations for methods in AffiliationService of the + // same name. See documentation in affiliation_service.h for details: + void GetAffiliations( + bool cached_only, + const AffiliationService::ResultCallback& callback, + const scoped_refptr<base::TaskRunner>& callback_task_runner); + + private: + struct RequestInfo; + + // Returns the time when cached data for this facet will expire. The data is + // already considered expired at the returned microsecond. + base::Time GetCacheExpirationTime() const; + + // Returns whether or not the cache has fresh data for this facet. + bool IsCachedDataFresh() const; + + // Posts the callback of the request described by |request_info| with success. + static void ServeRequestWithSuccess(const RequestInfo& request_info, + const AffiliatedFacets& affiliation); + + // Posts the callback of the request described by |request_info| with failure. + static void ServeRequestWithFailure(const RequestInfo& request_info); + + FacetManagerHost* backend_; + FacetURI facet_uri_; + + // The last time affiliation information was fetched for this facet, i.e. the + // freshness of the data in the cache. If there is no corresponding data in + // the database, this will contain the NULL time. Otherwise, the update time + // in the database should match this value; it is stored to reduce disk I/O. + base::Time last_update_time_; + + // Contains information about the GetAffiliations() requests that are waiting + // for the result of looking up this facet. + std::vector<RequestInfo> pending_requests_; + + DISALLOW_COPY_AND_ASSIGN(FacetManager); +}; + +} // namespace password_manager +#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_ diff --git a/components/password_manager/core/browser/facet_manager_host.h b/components/password_manager/core/browser/facet_manager_host.h new file mode 100644 index 0000000..d6122a9 --- /dev/null +++ b/components/password_manager/core/browser/facet_manager_host.h @@ -0,0 +1,41 @@ +// 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_FACET_MANAGER_HOST_H_ +#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_HOST_H_ + +#include "base/macros.h" + +namespace password_manager { + +// Interface through which FacetManagers can access shared functionality +// provided by the AffiliationBackend. +class FacetManagerHost { + public: + virtual ~FacetManagerHost() {} + + // Gets the current time. The returned time will always be strictly greater + // than the NULL time. + virtual base::Time GetCurrentTime() = 0; + + // Reads and returns the last update time of the equivalence class containing + // |facet_uri| from the database, or, if no such equivalence class is stored, + // returns the NULL time. + virtual base::Time ReadLastUpdateTimeFromDatabase( + const FacetURI& facet_uri) = 0; + + // Reads the equivalence class containing |facet_uri| from the database and + // returns true if found; returns false otherwise. + virtual bool ReadAffiliationsFromDatabase( + const FacetURI& facet_uri, + AffiliatedFacetsWithUpdateTime* affiliations) = 0; + + // Signals the fetching logic that affiliation information for a facet needs + // to be fetched immediately. + virtual void SignalNeedNetworkRequest() = 0; +}; + +} // namespace password_manager + +#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_HOST_H_ |