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

[iOS] Integrate ChangePasswordUrlService in TabHelper

Integrate ChangePasswordUrlService in WellKnownChangepasswordTabHelper.

Bug: 927473
Change-Id: I7a836f1946e18fa434873d0b112e85015659bac6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2358741
Commit-Queue: Jonathan Mengedoht <mengedoht@google.com>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798794}
parent bbe968ce
...@@ -11,6 +11,8 @@ source_set("passwords") { ...@@ -11,6 +11,8 @@ source_set("passwords") {
"credential_manager.mm", "credential_manager.mm",
"ios_chrome_bulk_leak_check_service_factory.cc", "ios_chrome_bulk_leak_check_service_factory.cc",
"ios_chrome_bulk_leak_check_service_factory.h", "ios_chrome_bulk_leak_check_service_factory.h",
"ios_chrome_change_password_url_service_factory.cc",
"ios_chrome_change_password_url_service_factory.h",
"ios_chrome_password_check_manager.h", "ios_chrome_password_check_manager.h",
"ios_chrome_password_check_manager.mm", "ios_chrome_password_check_manager.mm",
"ios_chrome_password_check_manager_factory.h", "ios_chrome_password_check_manager_factory.h",
......
// 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 "ios/chrome/browser/passwords/ios_chrome_change_password_url_service_factory.h"
#include <memory>
#include <utility>
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
#include "components/password_manager/core/browser/change_password_url_service_impl.h"
#include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
// static
IOSChromeChangePasswordUrlServiceFactory*
IOSChromeChangePasswordUrlServiceFactory::GetInstance() {
static base::NoDestructor<IOSChromeChangePasswordUrlServiceFactory> instance;
return instance.get();
}
// static
password_manager::ChangePasswordUrlService*
IOSChromeChangePasswordUrlServiceFactory::GetForBrowserState(
web::BrowserState* browser_state) {
return static_cast<password_manager::ChangePasswordUrlService*>(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}
IOSChromeChangePasswordUrlServiceFactory::
IOSChromeChangePasswordUrlServiceFactory()
: BrowserStateKeyedServiceFactory(
"ChangePasswordUrlService",
BrowserStateDependencyManager::GetInstance()) {}
IOSChromeChangePasswordUrlServiceFactory::
~IOSChromeChangePasswordUrlServiceFactory() = default;
std::unique_ptr<KeyedService>
IOSChromeChangePasswordUrlServiceFactory::BuildServiceInstanceFor(
web::BrowserState* context) const {
ChromeBrowserState* browser_state =
ChromeBrowserState::FromBrowserState(context);
return std::make_unique<password_manager::ChangePasswordUrlServiceImpl>(
context->GetSharedURLLoaderFactory(), browser_state->GetPrefs());
}
// Copyright 2019 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 IOS_CHROME_BROWSER_PASSWORDS_IOS_CHROME_CHANGE_PASSWORD_URL_SERVICE_FACTORY_H_
#define IOS_CHROME_BROWSER_PASSWORDS_IOS_CHROME_CHANGE_PASSWORD_URL_SERVICE_FACTORY_H_
#include "base/macros.h"
#include "base/no_destructor.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
#include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
class ChromeBrowserState;
enum class ServiceAccessType;
namespace password_manager {
class ChangePasswordUrlService;
}
// Singleton that owns all ChangePasswordUrlServices and associates them with
// ChromeBrowserState.
class IOSChromeChangePasswordUrlServiceFactory
: public BrowserStateKeyedServiceFactory {
public:
static IOSChromeChangePasswordUrlServiceFactory* GetInstance();
static password_manager::ChangePasswordUrlService* GetForBrowserState(
web::BrowserState* browser_state);
private:
friend class base::NoDestructor<IOSChromeChangePasswordUrlServiceFactory>;
IOSChromeChangePasswordUrlServiceFactory();
~IOSChromeChangePasswordUrlServiceFactory() override;
std::unique_ptr<KeyedService> BuildServiceInstanceFor(
web::BrowserState* context) const override;
};
#endif // IOS_CHROME_BROWSER_PASSWORDS_IOS_CHROME_CHANGE_PASSWORD_URL_SERVICE_FACTORY_H_
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#ifndef IOS_CHROME_BROWSER_PASSWORDS_WELL_KNOWN_CHANGE_PASSWORD_TAB_HELPER_H_ #ifndef IOS_CHROME_BROWSER_PASSWORDS_WELL_KNOWN_CHANGE_PASSWORD_TAB_HELPER_H_
#define IOS_CHROME_BROWSER_PASSWORDS_WELL_KNOWN_CHANGE_PASSWORD_TAB_HELPER_H_ #define IOS_CHROME_BROWSER_PASSWORDS_WELL_KNOWN_CHANGE_PASSWORD_TAB_HELPER_H_
#include "components/password_manager/core/browser/change_password_url_service.h"
#include "components/password_manager/core/browser/well_known_change_password_state.h" #include "components/password_manager/core/browser/well_known_change_password_state.h"
#include "ios/web/public/navigation/web_state_policy_decider.h" #include "ios/web/public/navigation/web_state_policy_decider.h"
#include "ios/web/public/web_state_observer.h" #include "ios/web/public/web_state_observer.h"
...@@ -77,6 +78,7 @@ class WellKnownChangePasswordTabHelper ...@@ -77,6 +78,7 @@ class WellKnownChangePasswordTabHelper
web::WebStatePolicyDecider::PolicyDecisionCallback response_policy_callback_; web::WebStatePolicyDecider::PolicyDecisionCallback response_policy_callback_;
password_manager::WellKnownChangePasswordState password_manager::WellKnownChangePasswordState
well_known_change_password_state_{this}; well_known_change_password_state_{this};
ChangePasswordUrlService* change_password_url_service_;
WEB_STATE_USER_DATA_KEY_DECL(); WEB_STATE_USER_DATA_KEY_DECL();
}; };
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "components/password_manager/core/browser/well_known_change_password_util.h" #include "components/password_manager/core/browser/well_known_change_password_util.h"
#include "components/password_manager/core/common/password_manager_features.h" #include "components/password_manager/core/common/password_manager_features.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h" #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/passwords/ios_chrome_change_password_url_service_factory.h"
#import "ios/web/public/navigation/navigation_context.h" #import "ios/web/public/navigation/navigation_context.h"
#import "net/base/mac/url_conversions.h" #import "net/base/mac/url_conversions.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
...@@ -22,7 +23,11 @@ using password_manager::WellKnownChangePasswordTabHelper; ...@@ -22,7 +23,11 @@ using password_manager::WellKnownChangePasswordTabHelper;
WellKnownChangePasswordTabHelper::WellKnownChangePasswordTabHelper( WellKnownChangePasswordTabHelper::WellKnownChangePasswordTabHelper(
web::WebState* web_state) web::WebState* web_state)
: web::WebStatePolicyDecider(web_state), web_state_(web_state) { : web::WebStatePolicyDecider(web_state),
web_state_(web_state),
change_password_url_service_(
IOSChromeChangePasswordUrlServiceFactory::GetForBrowserState(
web_state->GetBrowserState())) {
web_state->AddObserver(this); web_state->AddObserver(this);
} }
...@@ -57,7 +62,7 @@ WellKnownChangePasswordTabHelper::ShouldAllowRequest( ...@@ -57,7 +62,7 @@ WellKnownChangePasswordTabHelper::ShouldAllowRequest(
base::FeatureList::IsEnabled( base::FeatureList::IsEnabled(
password_manager::features::kWellKnownChangePassword)) { password_manager::features::kWellKnownChangePassword)) {
request_url_ = request_url; request_url_ = request_url;
change_password_url_service_->PrefetchURLs();
auto url_loader_factory = auto url_loader_factory =
web_state_->GetBrowserState()->GetSharedURLLoaderFactory(); web_state_->GetBrowserState()->GetSharedURLLoaderFactory();
well_known_change_password_state_.FetchNonExistingResource( well_known_change_password_state_.FetchNonExistingResource(
...@@ -122,7 +127,9 @@ void WellKnownChangePasswordTabHelper::OnProcessingFinished(bool is_supported) { ...@@ -122,7 +127,9 @@ void WellKnownChangePasswordTabHelper::OnProcessingFinished(bool is_supported) {
} else { } else {
std::move(response_policy_callback_) std::move(response_policy_callback_)
.Run(web::WebStatePolicyDecider::PolicyDecision::Cancel()); .Run(web::WebStatePolicyDecider::PolicyDecision::Cancel());
Redirect(request_url_.GetOrigin()); GURL redirect_url =
change_password_url_service_->GetChangePasswordUrl(request_url_);
Redirect(redirect_url.is_valid() ? redirect_url : request_url_.GetOrigin());
} }
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "components/password_manager/core/browser/well_known_change_password_util.h" #include "components/password_manager/core/browser/well_known_change_password_util.h"
#include "components/password_manager/core/common/password_manager_features.h" #include "components/password_manager/core/common/password_manager_features.h"
#include "ios/chrome/browser/passwords/ios_chrome_change_password_url_service_factory.h"
#import "ios/web/public/navigation/navigation_manager.h" #import "ios/web/public/navigation/navigation_manager.h"
#import "ios/web/public/test/fakes/test_web_client.h" #import "ios/web/public/test/fakes/test_web_client.h"
#import "ios/web/public/test/fakes/test_web_state_delegate.h" #import "ios/web/public/test/fakes/test_web_state_delegate.h"
...@@ -47,8 +48,30 @@ struct ServerResponse { ...@@ -47,8 +48,30 @@ struct ServerResponse {
std::vector<std::pair<std::string, std::string>> headers; std::vector<std::pair<std::string, std::string>> headers;
}; };
constexpr char kMockChangePasswordPath[] = "/change-password-override";
} // namespace } // namespace
class TestChangePasswordUrlService
: public password_manager::ChangePasswordUrlService {
public:
void PrefetchURLs() override {}
GURL GetChangePasswordUrl(const GURL& url) override {
if (override_available_) {
GURL::Replacements replacement;
replacement.SetPathStr(kMockChangePasswordPath);
return url.ReplaceComponents(replacement);
}
return GURL();
}
void SetOverrideAvailable(bool available) { override_available_ = available; }
private:
bool override_available_ = false;
};
// This test uses a mockserver to simulate different response. To handle the // This test uses a mockserver to simulate different response. To handle the
// url_loader requests we also mock the response for the url_loader_factory. // url_loader requests we also mock the response for the url_loader_factory.
class WellKnownChangePasswordTabHelperTest : public web::TestWebClient, class WellKnownChangePasswordTabHelperTest : public web::TestWebClient,
...@@ -67,6 +90,14 @@ class WellKnownChangePasswordTabHelperTest : public web::TestWebClient, ...@@ -67,6 +90,14 @@ class WellKnownChangePasswordTabHelperTest : public web::TestWebClient,
EXPECT_TRUE(test_server_->InitializeAndListen()); EXPECT_TRUE(test_server_->InitializeAndListen());
test_server_->StartAcceptingConnections(); test_server_->StartAcceptingConnections();
url_service_ = static_cast<TestChangePasswordUrlService*>(
IOSChromeChangePasswordUrlServiceFactory::GetInstance()
->SetTestingFactoryAndUse(
web_state()->GetBrowserState(),
base::BindRepeating([](web::BrowserState* browser_state) {
return std::unique_ptr<KeyedService>(
std::make_unique<TestChangePasswordUrlService>());
})));
web_state()->SetDelegate(&delegate_); web_state()->SetDelegate(&delegate_);
password_manager::WellKnownChangePasswordTabHelper::CreateForWebState( password_manager::WellKnownChangePasswordTabHelper::CreateForWebState(
web_state()); web_state());
...@@ -92,6 +123,7 @@ class WellKnownChangePasswordTabHelperTest : public web::TestWebClient, ...@@ -92,6 +123,7 @@ class WellKnownChangePasswordTabHelperTest : public web::TestWebClient,
base::flat_map<std::string, ServerResponse> path_response_map_; base::flat_map<std::string, ServerResponse> path_response_map_;
std::unique_ptr<EmbeddedTestServer> test_server_ = std::unique_ptr<EmbeddedTestServer> test_server_ =
std::make_unique<EmbeddedTestServer>(); std::make_unique<EmbeddedTestServer>();
TestChangePasswordUrlService* url_service_ = nullptr;
private: private:
// Returns a response for the given request. Uses |path_response_map_| to // Returns a response for the given request. Uses |path_response_map_| to
...@@ -204,3 +236,16 @@ TEST_F(WellKnownChangePasswordTabHelperTest, ...@@ -204,3 +236,16 @@ TEST_F(WellKnownChangePasswordTabHelperTest,
ASSERT_TRUE(WaitUntilLoaded()); ASSERT_TRUE(WaitUntilLoaded());
EXPECT_EQ(GetNavigatedUrl().path(), "/"); EXPECT_EQ(GetNavigatedUrl().path(), "/");
} }
TEST_F(WellKnownChangePasswordTabHelperTest,
NoSupportForChangePassword_WithOverride) {
url_service_->SetOverrideAvailable(true);
path_response_map_[kWellKnownChangePasswordPath] = {
net::HTTP_PERMANENT_REDIRECT, {std::make_pair("Location", "/not-found")}};
path_response_map_["/not-found"] = {net::HTTP_NOT_FOUND, {}};
SetUrlLoaderResponse(kWellKnownNotExistingResourcePath, net::HTTP_OK);
web::test::LoadUrl(web_state(),
test_server_->GetURL(kWellKnownChangePasswordPath));
ASSERT_TRUE(WaitUntilLoaded());
EXPECT_EQ(GetNavigatedUrl().path(), kMockChangePasswordPath);
}
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