Commit be4b7f80 authored by Jonathan Mengedoht's avatar Jonathan Mengedoht Committed by Commit Bot

Add ChangePasswordUrlService

Bug: 1086141
Change-Id: I6681eebcc8b14c649dac7622bf9c32c3e7b2eb98
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2283595
Commit-Queue: Jonathan Mengedoht <mengedoht@google.com>
Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789432}
parent 6f6e253a
......@@ -1032,6 +1032,8 @@ static_library("browser") {
"password_manager/account_storage/account_password_store_factory.h",
"password_manager/bulk_leak_check_service_factory.cc",
"password_manager/bulk_leak_check_service_factory.h",
"password_manager/change_password_url_service_factory.cc",
"password_manager/change_password_url_service_factory.h",
"password_manager/chrome_biometric_authenticator.h",
"password_manager/chrome_password_manager_client.cc",
"password_manager/chrome_password_manager_client.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 "chrome/browser/password_manager/change_password_url_service_factory.h"
#include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/password_manager/core/browser/change_password_url_service_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
ChangePasswordUrlServiceFactory::ChangePasswordUrlServiceFactory()
: BrowserContextKeyedServiceFactory(
"ChangePasswordUrlService",
BrowserContextDependencyManager::GetInstance()) {}
ChangePasswordUrlServiceFactory::~ChangePasswordUrlServiceFactory() = default;
// static
ChangePasswordUrlServiceFactory*
ChangePasswordUrlServiceFactory::GetInstance() {
static base::NoDestructor<ChangePasswordUrlServiceFactory> instance;
return instance.get();
}
// static
password_manager::ChangePasswordUrlService*
ChangePasswordUrlServiceFactory::GetForBrowserContext(
content::BrowserContext* browser_context) {
return static_cast<password_manager::ChangePasswordUrlService*>(
GetInstance()->GetServiceForBrowserContext(browser_context, true));
}
KeyedService* ChangePasswordUrlServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new password_manager::ChangePasswordUrlServiceImpl(
content::BrowserContext::GetDefaultStoragePartition(context)
->GetURLLoaderFactoryForBrowserProcess());
}
// 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 CHROME_BROWSER_PASSWORD_MANAGER_CHANGE_PASSWORD_URL_SERVICE_FACTORY_H_
#define CHROME_BROWSER_PASSWORD_MANAGER_CHANGE_PASSWORD_URL_SERVICE_FACTORY_H_
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
namespace password_manager {
class ChangePasswordUrlService;
}
namespace content {
class BrowserContext;
}
// Creates instances of ChangePasswordUrl per BrowserContext.
class ChangePasswordUrlServiceFactory
: public BrowserContextKeyedServiceFactory {
public:
ChangePasswordUrlServiceFactory();
~ChangePasswordUrlServiceFactory() override;
static ChangePasswordUrlServiceFactory* GetInstance();
static password_manager::ChangePasswordUrlService* GetForBrowserContext(
content::BrowserContext* browser_context);
private:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
};
#endif // CHROME_BROWSER_PASSWORD_MANAGER_CHANGE_PASSWORD_URL_SERVICE_FACTORY_H_
......@@ -60,6 +60,9 @@ jumbo_static_library("browser") {
"bulk_leak_check_service.h",
"bulk_leak_check_service_interface.cc",
"bulk_leak_check_service_interface.h",
"change_password_url_service.h",
"change_password_url_service_impl.cc",
"change_password_url_service_impl.h",
"compromised_credentials_consumer.cc",
"compromised_credentials_consumer.h",
"compromised_credentials_observer.cc",
......@@ -533,6 +536,7 @@ source_set("unit_tests") {
"android_affiliation/facet_manager_unittest.cc",
"browser_save_password_progress_logger_unittest.cc",
"bulk_leak_check_service_unittest.cc",
"change_password_url_service_impl_unittest.cc",
"compromised_credentials_observer_unittest.cc",
"compromised_credentials_table_unittest.cc",
"credential_cache_unittest.cc",
......
// 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_CHANGE_PASSWORD_URL_SERVICE_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_CHANGE_PASSWORD_URL_SERVICE_H_
#include "base/callback_forward.h"
#include "components/keyed_service/core/keyed_service.h"
class GURL;
namespace url {
class Origin;
}
namespace password_manager {
class ChangePasswordUrlService : public KeyedService {
public:
using UrlCallback = base::OnceCallback<void(GURL)>;
// Initializes the service.
virtual void Initialize() = 0;
// Returns the change password URL for `origin` via `callback`.
virtual void GetChangePasswordUrl(const url::Origin& origin,
UrlCallback callback) = 0;
};
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_CHANGE_PASSWORD_URL_SERVICE_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/change_password_url_service_impl.h"
#include "base/callback.h"
#include "base/containers/flat_map.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace password_manager {
ChangePasswordUrlServiceImpl::ChangePasswordUrlServiceImpl(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: url_loader_factory_(std::move(url_loader_factory)) {}
ChangePasswordUrlServiceImpl::~ChangePasswordUrlServiceImpl() = default;
void ChangePasswordUrlServiceImpl::Initialize() {
if (started_fetching_) {
return;
}
started_fetching_ = true;
// TODO(crbug.com/1086141): make request to gstatic.
OnFetchComplete(std::make_unique<std::string>("{}"));
}
void ChangePasswordUrlServiceImpl::GetChangePasswordUrl(
const url::Origin& origin,
UrlCallback callback) {
// TODO(crbug.com/1086141): call callback if response available, otherwise
// save callback.
url_callbacks_.emplace_back(std::make_pair(origin, std::move(callback)));
}
void ChangePasswordUrlServiceImpl::OnFetchComplete(
std::unique_ptr<std::string> response_body) {
// TODO(crbug.com/1086141): handle response and convert JSON.
change_password_url_map_ = {};
for (auto& url_callback : std::exchange(url_callbacks_, {})) {
GURL url = ChangePasswordUrlFor(url_callback.first);
std::move(url_callback.second).Run(std::move(url));
}
}
GURL ChangePasswordUrlServiceImpl::ChangePasswordUrlFor(
const url::Origin& origin) {
// TODO(crbug.com/1086141): lookup url override from map.
// Fallback if no change-password url available or request failed
return origin.GetURL();
}
} // 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_CHANGE_PASSWORD_URL_SERVICE_IMPL_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_CHANGE_PASSWORD_URL_SERVICE_IMPL_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/callback_forward.h"
#include "base/containers/flat_map.h"
#include "base/memory/scoped_refptr.h"
#include "components/password_manager/core/browser/change_password_url_service.h"
class GURL;
namespace url {
class Origin;
}
namespace network {
class SharedURLLoaderFactory;
}
namespace password_manager {
class ChangePasswordUrlServiceImpl
: public password_manager::ChangePasswordUrlService {
public:
explicit ChangePasswordUrlServiceImpl(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
~ChangePasswordUrlServiceImpl() override;
void Initialize() override;
// When the gstatic response arrives the callback is called with the override
// url for the given |url|. If no override is there the origin is returned.
void GetChangePasswordUrl(const url::Origin& origin,
UrlCallback callback) override;
private:
// Callback for the the request to gstatic.
void OnFetchComplete(std::unique_ptr<std::string> response_body);
// Retrieves the url override from the |change_password_url_map| for a given
// origin. It uses eTLD+1 for the lookup but also checks if overrides for
// eTLD+1+N exist.
GURL ChangePasswordUrlFor(const url::Origin& origin);
// Stores if the request is already started to only fetch once.
bool started_fetching_ = false;
// Stores the JSON result for the url overrides.
base::flat_map<std::string, GURL> change_password_url_map_;
// Stores the callbacks that are waiting for the request to finish.
std::vector<std::pair<url::Origin, base::OnceCallback<void(GURL)>>>
url_callbacks_;
// SharedURLLoaderFactory for the gstatic request, argument in the
// constructor.
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
};
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_CHANGE_PASSWORD_URL_SERVICE_IMPL_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/change_password_url_service_impl.h"
#include "base/logging.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace password_manager {
class ChangePasswordUrlServiceTest : public testing::Test {
private:
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_);
};
} // namespace password_manager
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