Commit 10a710c2 authored by Nela Kaczmarek's avatar Nela Kaczmarek Committed by Commit Bot

Add AffiliationFetcherFactory interface and implementation.

This change provides an interface and implementation of AffiliationFetcherFactory. It modifies the ScopedFakeAffiliationFetcherFactory to extend this interface and changes the creation of Affiliation Fetcher in Affiliation Service.

Bug: 1117447
Change-Id: I900c34e09461e870ea144f20e209cec973976568
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2409899Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Commit-Queue: Nela Kaczmarek <nelakaczmarek@google.com>
Cr-Commit-Position: refs/heads/master@{#806910}
parent 414e027d
...@@ -51,7 +51,6 @@ static_library("browser") { ...@@ -51,7 +51,6 @@ static_library("browser") {
"android_affiliation/facet_manager_host.h", "android_affiliation/facet_manager_host.h",
"android_affiliation/lookup_affiliation_response_parser.cc", "android_affiliation/lookup_affiliation_response_parser.cc",
"android_affiliation/lookup_affiliation_response_parser.h", "android_affiliation/lookup_affiliation_response_parser.h",
"android_affiliation/test_affiliation_fetcher_factory.h",
"biometric_authenticator.h", "biometric_authenticator.h",
"browser_save_password_progress_logger.cc", "browser_save_password_progress_logger.cc",
"browser_save_password_progress_logger.h", "browser_save_password_progress_logger.h",
...@@ -205,6 +204,9 @@ static_library("browser") { ...@@ -205,6 +204,9 @@ static_library("browser") {
"psl_matching_helper.cc", "psl_matching_helper.cc",
"psl_matching_helper.h", "psl_matching_helper.h",
"reauth_purpose.h", "reauth_purpose.h",
"site_affiliation/affiliation_fetcher_factory.h",
"site_affiliation/affiliation_fetcher_factory_impl.cc",
"site_affiliation/affiliation_fetcher_factory_impl.h",
"site_affiliation/affiliation_service.h", "site_affiliation/affiliation_service.h",
"site_affiliation/affiliation_service_impl.cc", "site_affiliation/affiliation_service_impl.cc",
"site_affiliation/affiliation_service_impl.h", "site_affiliation/affiliation_service_impl.h",
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "components/password_manager/core/browser/android_affiliation/affiliation_api.pb.h" #include "components/password_manager/core/browser/android_affiliation/affiliation_api.pb.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_utils.h" #include "components/password_manager/core/browser/android_affiliation/affiliation_utils.h"
#include "components/password_manager/core/browser/android_affiliation/lookup_affiliation_response_parser.h" #include "components/password_manager/core/browser/android_affiliation/lookup_affiliation_response_parser.h"
#include "components/password_manager/core/browser/android_affiliation/test_affiliation_fetcher_factory.h" #include "components/password_manager/core/browser/site_affiliation/affiliation_fetcher_factory.h"
#include "google_apis/google_api_keys.h" #include "google_apis/google_api_keys.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/base/url_util.h" #include "net/base/url_util.h"
...@@ -40,7 +40,7 @@ enum AffiliationFetchResult { ...@@ -40,7 +40,7 @@ enum AffiliationFetchResult {
AFFILIATION_FETCH_RESULT_MAX AFFILIATION_FETCH_RESULT_MAX
}; };
static TestAffiliationFetcherFactory* g_testing_factory = nullptr; static AffiliationFetcherFactory* g_testing_factory = nullptr;
AffiliationFetcher::AffiliationFetcher( AffiliationFetcher::AffiliationFetcher(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
...@@ -59,8 +59,8 @@ std::unique_ptr<AffiliationFetcherInterface> AffiliationFetcher::Create( ...@@ -59,8 +59,8 @@ std::unique_ptr<AffiliationFetcherInterface> AffiliationFetcher::Create(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
AffiliationFetcherDelegate* delegate) { AffiliationFetcherDelegate* delegate) {
if (g_testing_factory) { if (g_testing_factory) {
return base::WrapUnique(g_testing_factory->CreateInstance( return g_testing_factory->CreateInstance(std::move(url_loader_factory),
std::move(url_loader_factory), delegate)); delegate);
} }
// Using `new` to access a non-public constructor. // Using `new` to access a non-public constructor.
// (https://abseil.io/tips/134#recommendations) // (https://abseil.io/tips/134#recommendations)
...@@ -70,7 +70,7 @@ std::unique_ptr<AffiliationFetcherInterface> AffiliationFetcher::Create( ...@@ -70,7 +70,7 @@ std::unique_ptr<AffiliationFetcherInterface> AffiliationFetcher::Create(
// static // static
void AffiliationFetcher::SetFactoryForTesting( void AffiliationFetcher::SetFactoryForTesting(
TestAffiliationFetcherFactory* factory) { AffiliationFetcherFactory* factory) {
g_testing_factory = factory; g_testing_factory = factory;
} }
......
...@@ -23,7 +23,7 @@ class SimpleURLLoader; ...@@ -23,7 +23,7 @@ class SimpleURLLoader;
namespace password_manager { namespace password_manager {
class TestAffiliationFetcherFactory; class AffiliationFetcherFactory;
// Fetches authoritative information regarding which facets are affiliated with // Fetches authoritative information regarding which facets are affiliated with
// each other, that is, which facets belong to the same logical application. // each other, that is, which facets belong to the same logical application.
...@@ -35,6 +35,9 @@ class TestAffiliationFetcherFactory; ...@@ -35,6 +35,9 @@ class TestAffiliationFetcherFactory;
// moved to a factory responsible for creating AffiliationFetcher instances. // moved to a factory responsible for creating AffiliationFetcher instances.
class AffiliationFetcher : public AffiliationFetcherInterface { class AffiliationFetcher : public AffiliationFetcherInterface {
public: public:
AffiliationFetcher(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
AffiliationFetcherDelegate* delegate);
~AffiliationFetcher() override; ~AffiliationFetcher() override;
// Constructs a fetcher using the specified |url_loader_factory|, and will // Constructs a fetcher using the specified |url_loader_factory|, and will
...@@ -52,7 +55,7 @@ class AffiliationFetcher : public AffiliationFetcherInterface { ...@@ -52,7 +55,7 @@ class AffiliationFetcher : public AffiliationFetcherInterface {
// //
// The caller must ensure that the |factory| outlives all potential Create() // The caller must ensure that the |factory| outlives all potential Create()
// calls. The caller may pass in NULL to resume using the default factory. // calls. The caller may pass in NULL to resume using the default factory.
static void SetFactoryForTesting(TestAffiliationFetcherFactory* factory); static void SetFactoryForTesting(AffiliationFetcherFactory* factory);
// Actually starts the request to retrieve affiliations and optionally // Actually starts the request to retrieve affiliations and optionally
// groupings for each facet in |facet_uris| along with the details based on // groupings for each facet in |facet_uris| along with the details based on
...@@ -69,11 +72,6 @@ class AffiliationFetcher : public AffiliationFetcherInterface { ...@@ -69,11 +72,6 @@ class AffiliationFetcher : public AffiliationFetcherInterface {
AffiliationFetcherDelegate* delegate() const; AffiliationFetcherDelegate* delegate() const;
protected:
AffiliationFetcher(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
AffiliationFetcherDelegate* delegate);
private: private:
// Prepares and returns the serialized protocol buffer message that will be // Prepares and returns the serialized protocol buffer message that will be
// the payload of the POST request. Sets mask request based on |request_info|. // the payload of the POST request. Sets mask request based on |request_info|.
......
...@@ -47,12 +47,13 @@ FakeAffiliationFetcher* ScopedFakeAffiliationFetcherFactory::PeekNextFetcher() { ...@@ -47,12 +47,13 @@ FakeAffiliationFetcher* ScopedFakeAffiliationFetcherFactory::PeekNextFetcher() {
return pending_fetchers_.front(); return pending_fetchers_.front();
} }
FakeAffiliationFetcher* ScopedFakeAffiliationFetcherFactory::CreateInstance( std::unique_ptr<AffiliationFetcherInterface>
ScopedFakeAffiliationFetcherFactory::CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
AffiliationFetcherDelegate* delegate) { AffiliationFetcherDelegate* delegate) {
FakeAffiliationFetcher* fetcher = auto fetcher = std::make_unique<FakeAffiliationFetcher>(
new FakeAffiliationFetcher(std::move(url_loader_factory), delegate); std::move(url_loader_factory), delegate);
pending_fetchers_.push(fetcher); pending_fetchers_.push(fetcher.get());
return fetcher; return fetcher;
} }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher.h" #include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_delegate.h" #include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_delegate.h"
#include "components/password_manager/core/browser/android_affiliation/test_affiliation_fetcher_factory.h" #include "components/password_manager/core/browser/site_affiliation/affiliation_fetcher_factory.h"
namespace password_manager { namespace password_manager {
...@@ -42,8 +42,7 @@ class FakeAffiliationFetcher : public AffiliationFetcher { ...@@ -42,8 +42,7 @@ class FakeAffiliationFetcher : public AffiliationFetcher {
// While this factory is in scope, calls to AffiliationFetcher::Create() will // While this factory is in scope, calls to AffiliationFetcher::Create() will
// produce FakeAffiliationFetchers that can be used in tests to return fake API // produce FakeAffiliationFetchers that can be used in tests to return fake API
// responses to users of AffiliationFetcher. Nesting is not supported. // responses to users of AffiliationFetcher. Nesting is not supported.
class ScopedFakeAffiliationFetcherFactory class ScopedFakeAffiliationFetcherFactory : public AffiliationFetcherFactory {
: public TestAffiliationFetcherFactory {
public: public:
ScopedFakeAffiliationFetcherFactory(); ScopedFakeAffiliationFetcherFactory();
~ScopedFakeAffiliationFetcherFactory() override; ~ScopedFakeAffiliationFetcherFactory() override;
...@@ -65,7 +64,7 @@ class ScopedFakeAffiliationFetcherFactory ...@@ -65,7 +64,7 @@ class ScopedFakeAffiliationFetcherFactory
bool has_pending_fetchers() const { return !pending_fetchers_.empty(); } bool has_pending_fetchers() const { return !pending_fetchers_.empty(); }
// AffiliationFetcherFactory: // AffiliationFetcherFactory:
FakeAffiliationFetcher* CreateInstance( std::unique_ptr<AffiliationFetcherInterface> CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
AffiliationFetcherDelegate* delegate) override; AffiliationFetcherDelegate* delegate) override;
......
// Copyright 2015 The Chromium Authors. All rights reserved. // Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_ANDROID_AFFILIATION_TEST_AFFILIATION_FETCHER_FACTORY_H_ #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_SITE_AFFILIATION_AFFILIATION_FETCHER_FACTORY_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_ANDROID_AFFILIATION_TEST_AFFILIATION_FETCHER_FACTORY_H_ #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_SITE_AFFILIATION_AFFILIATION_FETCHER_FACTORY_H_
#include <memory>
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
...@@ -16,28 +18,27 @@ namespace password_manager { ...@@ -16,28 +18,27 @@ namespace password_manager {
class AffiliationFetcherInterface; class AffiliationFetcherInterface;
class AffiliationFetcherDelegate; class AffiliationFetcherDelegate;
// Interface for a factory to be used by AffiliationFetcher::Create() in tests // Interface for a factory to construct instances of AffiliationFetcher
// to construct instances of test-specific AffiliationFetcher subclasses. // subclasses.
// class AffiliationFetcherFactory {
// The factory is registered with AffiliationFetcher::SetFactoryForTesting().
class TestAffiliationFetcherFactory {
public: public:
TestAffiliationFetcherFactory(const TestAffiliationFetcherFactory&) = delete; AffiliationFetcherFactory() = default;
TestAffiliationFetcherFactory& operator=( virtual ~AffiliationFetcherFactory() = default;
const TestAffiliationFetcherFactory&) = delete;
AffiliationFetcherFactory(const AffiliationFetcherFactory&) = delete;
AffiliationFetcherFactory& operator=(const AffiliationFetcherFactory&) =
delete;
AffiliationFetcherFactory(AffiliationFetcherFactory&&) = delete;
AffiliationFetcherFactory& operator=(AffiliationFetcherFactory&&) = delete;
// Constructs a fetcher to retrieve affiliations for each facet in |facet_ids| // Constructs a fetcher to retrieve affiliations for requested facets
// using the specified |url_loader_factory|, and will provide the results // using the specified |url_loader_factory|, and will provide the results
// to the |delegate| on the same thread that creates the instance. // to the |delegate| on the same thread that creates the instance.
virtual AffiliationFetcherInterface* CreateInstance( virtual std::unique_ptr<AffiliationFetcherInterface> CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
AffiliationFetcherDelegate* delegate) = 0; AffiliationFetcherDelegate* delegate) = 0;
protected:
TestAffiliationFetcherFactory() = default;
virtual ~TestAffiliationFetcherFactory() = default;
}; };
} // namespace password_manager } // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_ANDROID_AFFILIATION_TEST_AFFILIATION_FETCHER_FACTORY_H_ #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_SITE_AFFILIATION_AFFILIATION_FETCHER_FACTORY_H_
// Copyright 2020 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/site_affiliation/affiliation_fetcher_factory_impl.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace password_manager {
AffiliationFetcherFactoryImpl::AffiliationFetcherFactoryImpl() = default;
AffiliationFetcherFactoryImpl::~AffiliationFetcherFactoryImpl() = default;
std::unique_ptr<AffiliationFetcherInterface>
AffiliationFetcherFactoryImpl::CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
AffiliationFetcherDelegate* delegate) {
return std::make_unique<AffiliationFetcher>(std::move(url_loader_factory),
delegate);
}
} // namespace password_manager
// Copyright 2020 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_SITE_AFFILIATION_AFFILIATION_FETCHER_FACTORY_IMPL_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_SITE_AFFILIATION_AFFILIATION_FETCHER_FACTORY_IMPL_H_
#include "components/password_manager/core/browser/site_affiliation/affiliation_fetcher_factory.h"
namespace password_manager {
class AffiliationFetcherFactoryImpl : public AffiliationFetcherFactory {
public:
AffiliationFetcherFactoryImpl();
~AffiliationFetcherFactoryImpl() override;
std::unique_ptr<AffiliationFetcherInterface> CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
AffiliationFetcherDelegate* delegate) override;
};
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_SITE_AFFILIATION_AFFILIATION_FETCHER_FACTORY_IMPL_H_
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "base/metrics/histogram_functions.h" #include "base/metrics/histogram_functions.h"
#include "base/ranges/algorithm.h" #include "base/ranges/algorithm.h"
#include "base/threading/sequenced_task_runner_handle.h" #include "base/threading/sequenced_task_runner_handle.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher.h"
#include "components/password_manager/core/browser/password_store_factory_util.h" #include "components/password_manager/core/browser/password_store_factory_util.h"
#include "components/sync/driver/sync_service.h" #include "components/sync/driver/sync_service.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
...@@ -59,7 +58,8 @@ AffiliationServiceImpl::AffiliationServiceImpl( ...@@ -59,7 +58,8 @@ AffiliationServiceImpl::AffiliationServiceImpl(
syncer::SyncService* sync_service, syncer::SyncService* sync_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: sync_service_(sync_service), : sync_service_(sync_service),
url_loader_factory_(std::move(url_loader_factory)) {} url_loader_factory_(std::move(url_loader_factory)),
fetcher_factory_(std::make_unique<AffiliationFetcherFactoryImpl>()) {}
AffiliationServiceImpl::~AffiliationServiceImpl() = default; AffiliationServiceImpl::~AffiliationServiceImpl() = default;
...@@ -157,7 +157,7 @@ void AffiliationServiceImpl::RequestFacetsAffiliations( ...@@ -157,7 +157,7 @@ void AffiliationServiceImpl::RequestFacetsAffiliations(
const std::vector<FacetURI>& facets, const std::vector<FacetURI>& facets,
const AffiliationFetcherInterface::RequestInfo request_info) { const AffiliationFetcherInterface::RequestInfo request_info) {
if (!facets.empty()) { if (!facets.empty()) {
fetcher_ = AffiliationFetcher::Create(url_loader_factory_, this); fetcher_ = fetcher_factory_->CreateInstance(url_loader_factory_, this);
fetcher_->StartRequest(facets, request_info); fetcher_->StartRequest(facets, request_info);
} }
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_delegate.h" #include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_delegate.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_interface.h" #include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_interface.h"
#include "components/password_manager/core/browser/password_manager_metrics_util.h" #include "components/password_manager/core/browser/password_manager_metrics_util.h"
#include "components/password_manager/core/browser/site_affiliation/affiliation_fetcher_factory_impl.h"
namespace network { namespace network {
class SharedURLLoaderFactory; class SharedURLLoaderFactory;
...@@ -65,6 +66,11 @@ class AffiliationServiceImpl : public AffiliationService, ...@@ -65,6 +66,11 @@ class AffiliationServiceImpl : public AffiliationService,
url_loader_factory_ = std::move(url_loader_factory); url_loader_factory_ = std::move(url_loader_factory);
} }
void SetFetcherFactoryForTesting(
std::unique_ptr<AffiliationFetcherFactory> fetcher_factory) {
fetcher_factory_ = std::move(fetcher_factory);
}
void SetSyncServiceForTesting(syncer::SyncService* sync_service) { void SetSyncServiceForTesting(syncer::SyncService* sync_service) {
sync_service_ = sync_service; sync_service_ = sync_service;
} }
...@@ -92,6 +98,7 @@ class AffiliationServiceImpl : public AffiliationService, ...@@ -92,6 +98,7 @@ class AffiliationServiceImpl : public AffiliationService,
std::map<url::SchemeHostPort, ChangePasswordUrlMatch> change_password_urls_; std::map<url::SchemeHostPort, ChangePasswordUrlMatch> change_password_urls_;
// TODO(crbug.com/1117045): A vector of pending fetchers to be created. // TODO(crbug.com/1117045): A vector of pending fetchers to be created.
std::unique_ptr<AffiliationFetcherInterface> fetcher_; std::unique_ptr<AffiliationFetcherInterface> fetcher_;
std::unique_ptr<AffiliationFetcherFactory> fetcher_factory_;
// Callback is passed in PrefetchChangePasswordURLs and is run in // Callback is passed in PrefetchChangePasswordURLs and is run in
// OnFetchSucceeded, OnFetchMalformed, OnFetchFailed to indicate the prefetch // OnFetchSucceeded, OnFetchMalformed, OnFetchFailed to indicate the prefetch
// has finished. // has finished.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment