Commit 5b63313e authored by Jonathan Mengedoht's avatar Jonathan Mengedoht Committed by Commit Bot

Integrate WellKnownChangePasswordState

Integrate the WellKnownChangePasswordState in the
WellKnownChangePasswordNavigationThrottle.

Bug: 927473
Change-Id: I376d9f51cf3c06d253aaa28c692e0fcca58e0be7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2346424
Commit-Queue: Jonathan Mengedoht <mengedoht@google.com>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798678}
parent 3a2299a9
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h" #include "content/public/browser/web_contents_user_data.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h" #include "url/gurl.h"
#include "url/origin.h" #include "url/origin.h"
...@@ -28,10 +25,8 @@ namespace { ...@@ -28,10 +25,8 @@ namespace {
using content::NavigationHandle; using content::NavigationHandle;
using content::NavigationThrottle; using content::NavigationThrottle;
using content::WebContents; using content::WebContents;
using password_manager::CreateWellKnownNonExistingResourceURL;
using password_manager::IsWellKnownChangePasswordUrl; using password_manager::IsWellKnownChangePasswordUrl;
using password_manager::kWellKnownChangePasswordPath; using password_manager::WellKnownChangePasswordState;
using password_manager::kWellKnownNotExistingResourcePath;
// Used to scope the posted navigation task to the lifetime of |web_contents|. // Used to scope the posted navigation task to the lifetime of |web_contents|.
class WebContentsLifetimeHelper class WebContentsLifetimeHelper
...@@ -93,75 +88,49 @@ WellKnownChangePasswordNavigationThrottle:: ...@@ -93,75 +88,49 @@ WellKnownChangePasswordNavigationThrottle::
NavigationThrottle::ThrottleCheckResult NavigationThrottle::ThrottleCheckResult
WellKnownChangePasswordNavigationThrottle::WillStartRequest() { WellKnownChangePasswordNavigationThrottle::WillStartRequest() {
FetchNonExistingResource(navigation_handle()); auto url_loader_factory =
content::BrowserContext::GetDefaultStoragePartition(
navigation_handle()->GetWebContents()->GetBrowserContext())
->GetURLLoaderFactoryForBrowserProcess();
well_known_change_password_state_.FetchNonExistingResource(
url_loader_factory.get(), navigation_handle()->GetURL());
return NavigationThrottle::PROCEED; return NavigationThrottle::PROCEED;
} }
NavigationThrottle::ThrottleCheckResult NavigationThrottle::ThrottleCheckResult
WellKnownChangePasswordNavigationThrottle::WillFailRequest() { WellKnownChangePasswordNavigationThrottle::WillFailRequest() {
url_loader_.reset();
return NavigationThrottle::PROCEED; return NavigationThrottle::PROCEED;
} }
NavigationThrottle::ThrottleCheckResult NavigationThrottle::ThrottleCheckResult
WellKnownChangePasswordNavigationThrottle::WillProcessResponse() { WellKnownChangePasswordNavigationThrottle::WillProcessResponse() {
change_password_response_code_ = // PostTask because the Throttle needs to be deferred before the status code
navigation_handle()->GetResponseHeaders()->response_code(); // is set. After setting the status code Resume() can be called synchronous
return BothRequestsFinished() ? ContinueProcessing() // and thereby before the throttle is deferred. This would result in a crash.
: NavigationThrottle::DEFER; // Unretained is safe because the NavigationThrottle is deferred and can only
// be continued after the callback finished.
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(
&WellKnownChangePasswordState::SetChangePasswordResponseCode,
base::Unretained(&well_known_change_password_state_),
navigation_handle()->GetResponseHeaders()->response_code()));
return NavigationThrottle::DEFER;
} }
const char* WellKnownChangePasswordNavigationThrottle::GetNameForLogging() { const char* WellKnownChangePasswordNavigationThrottle::GetNameForLogging() {
return "WellKnownChangePasswordNavigationThrottle"; return "WellKnownChangePasswordNavigationThrottle";
} }
void WellKnownChangePasswordNavigationThrottle::FetchNonExistingResource( void WellKnownChangePasswordNavigationThrottle::OnProcessingFinished(
NavigationHandle* handle) { bool is_supported) {
auto url_loader_factory = content::BrowserContext::GetDefaultStoragePartition( if (is_supported) {
handle->GetWebContents()->GetBrowserContext()) Resume();
->GetURLLoaderFactoryForBrowserProcess();
url_loader_ =
password_manager::CreateResourceRequestToWellKnownNonExistingResourceFor(
handle->GetURL());
// Binding the callback to |this| is safe, because the navigationthrottle
// defers if the request is not received yet. Thereby the throttle still exist
// when the response arrives.
url_loader_->DownloadHeadersOnly(
url_loader_factory.get(),
base::BindOnce(&WellKnownChangePasswordNavigationThrottle::
FetchNonExistingResourceCallback,
base::Unretained(this)));
}
void WellKnownChangePasswordNavigationThrottle::
FetchNonExistingResourceCallback(
scoped_refptr<net::HttpResponseHeaders> headers) {
if (!headers) {
non_existing_resource_response_code_ = -1;
return;
}
non_existing_resource_response_code_ = headers->response_code();
if (BothRequestsFinished()) {
ThrottleAction action = ContinueProcessing();
if (action == NavigationThrottle::PROCEED) {
Resume();
} else if (action == NavigationThrottle::CANCEL) {
CancelDeferredNavigation(NavigationThrottle::CANCEL);
}
}
}
NavigationThrottle::ThrottleAction
WellKnownChangePasswordNavigationThrottle::ContinueProcessing() {
DCHECK(BothRequestsFinished());
if (SupportsChangePasswordUrl()) {
return NavigationThrottle::PROCEED;
} else { } else {
// Redirect call creates PostTask
GURL url = navigation_handle()->GetURL(); GURL url = navigation_handle()->GetURL();
GURL redirect_url = change_password_url_service_->GetChangePasswordUrl(url); GURL redirect_url = change_password_url_service_->GetChangePasswordUrl(url);
Redirect(redirect_url.is_valid() ? redirect_url : url.GetOrigin()); Redirect(redirect_url.is_valid() ? redirect_url : url.GetOrigin());
return NavigationThrottle::CANCEL; CancelDeferredNavigation(NavigationThrottle::CANCEL);
} }
} }
...@@ -183,15 +152,3 @@ void WellKnownChangePasswordNavigationThrottle::Redirect(const GURL& url) { ...@@ -183,15 +152,3 @@ void WellKnownChangePasswordNavigationThrottle::Redirect(const GURL& url) {
helper->GetWeakPtr(), std::move(params))); helper->GetWeakPtr(), std::move(params)));
} }
bool WellKnownChangePasswordNavigationThrottle::BothRequestsFinished() const {
return non_existing_resource_response_code_ != 0 &&
change_password_response_code_ != 0;
}
bool WellKnownChangePasswordNavigationThrottle::SupportsChangePasswordUrl()
const {
DCHECK(BothRequestsFinished());
return 200 <= change_password_response_code_ &&
change_password_response_code_ < 300 &&
non_existing_resource_response_code_ == net::HTTP_NOT_FOUND;
}
...@@ -9,18 +9,13 @@ ...@@ -9,18 +9,13 @@
#include "content/public/browser/navigation_throttle.h" #include "content/public/browser/navigation_throttle.h"
#include "components/password_manager/core/browser/well_known_change_password_state.h"
class GURL; class GURL;
namespace content { namespace content {
class NavigationHandle; class NavigationHandle;
} // namespace content } // namespace content
namespace net {
class HttpResponseHeaders;
} // namespace net
namespace network {
class SimpleURLLoader;
} // namespace network
namespace password_manager { namespace password_manager {
class ChangePasswordUrlService; class ChangePasswordUrlService;
...@@ -34,7 +29,8 @@ class ChangePasswordUrlService; ...@@ -34,7 +29,8 @@ class ChangePasswordUrlService;
// support the change password url, the user gets redirected to the base path // support the change password url, the user gets redirected to the base path
// '/'. // '/'.
class WellKnownChangePasswordNavigationThrottle class WellKnownChangePasswordNavigationThrottle
: public content::NavigationThrottle { : public content::NavigationThrottle,
public password_manager::WellKnownChangePasswordStateDelegate {
public: public:
~WellKnownChangePasswordNavigationThrottle() override; ~WellKnownChangePasswordNavigationThrottle() override;
...@@ -52,24 +48,13 @@ class WellKnownChangePasswordNavigationThrottle ...@@ -52,24 +48,13 @@ class WellKnownChangePasswordNavigationThrottle
private: private:
explicit WellKnownChangePasswordNavigationThrottle( explicit WellKnownChangePasswordNavigationThrottle(
content::NavigationHandle* handle); content::NavigationHandle* handle);
// Request the status code from a path that is expected to return 404. // password_manager::WellKnownChangePasswordStateDelegate:
void FetchNonExistingResource(content::NavigationHandle* handle); void OnProcessingFinished(bool is_supported) override;
// Callback for the request to the "not exist" path.
void FetchNonExistingResourceCallback(
scoped_refptr<net::HttpResponseHeaders> headers);
// Function is called when both requests are finished. Decides to continue or
// redirect to homepage.
ThrottleAction ContinueProcessing();
// Redirects to a given URL in the same tab. // Redirects to a given URL in the same tab.
void Redirect(const GURL& url); void Redirect(const GURL& url);
// Checks if both requests are finished.
bool BothRequestsFinished() const;
// Checks the status codes and returns if change password is supported.
bool SupportsChangePasswordUrl() const;
int non_existing_resource_response_code_ = 0; password_manager::WellKnownChangePasswordState
int change_password_response_code_ = 0; well_known_change_password_state_{this};
std::unique_ptr<network::SimpleURLLoader> url_loader_;
password_manager::ChangePasswordUrlService* change_password_url_service_; password_manager::ChangePasswordUrlService* change_password_url_service_;
}; };
......
...@@ -156,4 +156,4 @@ INSTANTIATE_TEST_SUITE_P(All, ...@@ -156,4 +156,4 @@ INSTANTIATE_TEST_SUITE_P(All,
WellKnownChangePasswordStateTest, WellKnownChangePasswordStateTest,
::testing::ValuesIn(kDelayParams)); ::testing::ValuesIn(kDelayParams));
} // namespace password_manager } // namespace password_manager
\ No newline at end of file
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