NetworkService: Implement URLLoader::OnAuthRequired
https://chromium-review.googlesource.com/c/chromium/src/+/938960
This commit is contained in:
parent
27cb84eee0
commit
0cbffb15cc
6 changed files with 54 additions and 58 deletions
|
@ -17,6 +17,7 @@
|
||||||
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
|
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
|
||||||
#include "atom/browser/atom_speech_recognition_manager_delegate.h"
|
#include "atom/browser/atom_speech_recognition_manager_delegate.h"
|
||||||
#include "atom/browser/child_web_contents_tracker.h"
|
#include "atom/browser/child_web_contents_tracker.h"
|
||||||
|
#include "atom/browser/login_handler.h"
|
||||||
#include "atom/browser/native_window.h"
|
#include "atom/browser/native_window.h"
|
||||||
#include "atom/browser/session_preferences.h"
|
#include "atom/browser/session_preferences.h"
|
||||||
#include "atom/browser/web_contents_permission_helper.h"
|
#include "atom/browser/web_contents_permission_helper.h"
|
||||||
|
@ -497,6 +498,19 @@ std::unique_ptr<net::ClientCertStore> AtomBrowserClient::CreateClientCertStore(
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
content::ResourceDispatcherHostLoginDelegate*
|
||||||
|
AtomBrowserClient::CreateLoginDelegate(
|
||||||
|
net::AuthChallengeInfo* auth_info,
|
||||||
|
content::ResourceRequestInfo::WebContentsGetter web_contents_getter,
|
||||||
|
bool is_main_frame,
|
||||||
|
const GURL& url,
|
||||||
|
bool first_auth_attempt,
|
||||||
|
const base::Callback<void(const base::Optional<net::AuthCredentials>&)>&
|
||||||
|
auth_required_callback) {
|
||||||
|
return new LoginHandler(auth_info, web_contents_getter, url,
|
||||||
|
auth_required_callback);
|
||||||
|
}
|
||||||
|
|
||||||
brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts(
|
brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts(
|
||||||
const content::MainFunctionParams&) {
|
const content::MainFunctionParams&) {
|
||||||
v8::V8::Initialize(); // Init V8 before creating main parts.
|
v8::V8::Initialize(); // Init V8 before creating main parts.
|
||||||
|
|
|
@ -106,6 +106,14 @@ class AtomBrowserClient : public brightray::BrowserClient,
|
||||||
void SiteInstanceDeleting(content::SiteInstance* site_instance) override;
|
void SiteInstanceDeleting(content::SiteInstance* site_instance) override;
|
||||||
std::unique_ptr<net::ClientCertStore> CreateClientCertStore(
|
std::unique_ptr<net::ClientCertStore> CreateClientCertStore(
|
||||||
content::ResourceContext* resource_context) override;
|
content::ResourceContext* resource_context) override;
|
||||||
|
content::ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
|
||||||
|
net::AuthChallengeInfo* auth_info,
|
||||||
|
content::ResourceRequestInfo::WebContentsGetter web_contents_getter,
|
||||||
|
bool is_main_frame,
|
||||||
|
const GURL& url,
|
||||||
|
bool first_auth_attempt,
|
||||||
|
const base::Callback<void(const base::Optional<net::AuthCredentials>&)>&
|
||||||
|
auth_required_callback) override;
|
||||||
|
|
||||||
// brightray::BrowserClient:
|
// brightray::BrowserClient:
|
||||||
brightray::BrowserMainParts* OverrideCreateBrowserMainParts(
|
brightray::BrowserMainParts* OverrideCreateBrowserMainParts(
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
|
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
|
||||||
|
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/browser/login_handler.h"
|
|
||||||
#include "atom/browser/web_contents_permission_helper.h"
|
#include "atom/browser/web_contents_permission_helper.h"
|
||||||
#include "atom/browser/web_contents_preferences.h"
|
#include "atom/browser/web_contents_preferences.h"
|
||||||
#include "atom/common/platform_util.h"
|
#include "atom/common/platform_util.h"
|
||||||
|
@ -114,13 +113,6 @@ bool AtomResourceDispatcherHostDelegate::HandleExternalProtocol(
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
content::ResourceDispatcherHostLoginDelegate*
|
|
||||||
AtomResourceDispatcherHostDelegate::CreateLoginDelegate(
|
|
||||||
net::AuthChallengeInfo* auth_info,
|
|
||||||
net::URLRequest* request) {
|
|
||||||
return new LoginHandler(auth_info, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
|
bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
|
||||||
net::URLRequest* request,
|
net::URLRequest* request,
|
||||||
const std::string& mime_type,
|
const std::string& mime_type,
|
||||||
|
|
|
@ -19,9 +19,6 @@ class AtomResourceDispatcherHostDelegate
|
||||||
// content::ResourceDispatcherHostDelegate:
|
// content::ResourceDispatcherHostDelegate:
|
||||||
bool HandleExternalProtocol(const GURL& url,
|
bool HandleExternalProtocol(const GURL& url,
|
||||||
content::ResourceRequestInfo* info) override;
|
content::ResourceRequestInfo* info) override;
|
||||||
content::ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
|
|
||||||
net::AuthChallengeInfo* auth_info,
|
|
||||||
net::URLRequest* request) override;
|
|
||||||
bool ShouldInterceptResourceAsStream(net::URLRequest* request,
|
bool ShouldInterceptResourceAsStream(net::URLRequest* request,
|
||||||
const std::string& mime_type,
|
const std::string& mime_type,
|
||||||
GURL* origin,
|
GURL* origin,
|
||||||
|
|
|
@ -5,41 +5,30 @@
|
||||||
#include "atom/browser/login_handler.h"
|
#include "atom/browser/login_handler.h"
|
||||||
|
|
||||||
#include "atom/browser/browser.h"
|
#include "atom/browser/browser.h"
|
||||||
#include "atom/common/native_mate_converters/net_converter.h"
|
|
||||||
#include "base/values.h"
|
#include "base/values.h"
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "content/public/browser/render_frame_host.h"
|
|
||||||
#include "content/public/browser/resource_dispatcher_host.h"
|
|
||||||
#include "content/public/browser/resource_request_info.h"
|
|
||||||
#include "content/public/browser/web_contents.h"
|
#include "content/public/browser/web_contents.h"
|
||||||
#include "net/base/auth.h"
|
#include "net/base/auth.h"
|
||||||
#include "net/url_request/url_request.h"
|
|
||||||
|
|
||||||
using content::BrowserThread;
|
using content::BrowserThread;
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace {
|
LoginHandler::LoginHandler(
|
||||||
|
net::AuthChallengeInfo* auth_info,
|
||||||
// Helper to remove the ref from an net::URLRequest to the LoginHandler.
|
content::ResourceRequestInfo::WebContentsGetter web_contents_getter,
|
||||||
// Should only be called from the IO thread, since it accesses an
|
const GURL& url,
|
||||||
// net::URLRequest.
|
const base::Callback<void(const base::Optional<net::AuthCredentials>&)>&
|
||||||
void ResetLoginHandlerForRequest(net::URLRequest* request) {
|
auth_required_callback)
|
||||||
content::ResourceDispatcherHost::Get()->ClearLoginDelegateForRequest(request);
|
: auth_info_(auth_info),
|
||||||
}
|
web_contents_getter_(web_contents_getter),
|
||||||
|
auth_required_callback_(auth_required_callback) {
|
||||||
} // namespace
|
|
||||||
|
|
||||||
LoginHandler::LoginHandler(net::AuthChallengeInfo* auth_info,
|
|
||||||
net::URLRequest* request)
|
|
||||||
: auth_info_(auth_info), request_(request) {
|
|
||||||
content::ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame(
|
|
||||||
&render_process_host_id_, &render_frame_id_);
|
|
||||||
|
|
||||||
// Fill request details on IO thread.
|
// Fill request details on IO thread.
|
||||||
|
// TODO(deepak1556): Fill in method and referrer details to
|
||||||
|
// avoid breaking the app login event.
|
||||||
std::unique_ptr<base::DictionaryValue> request_details(
|
std::unique_ptr<base::DictionaryValue> request_details(
|
||||||
new base::DictionaryValue);
|
new base::DictionaryValue);
|
||||||
FillRequestDetails(request_details.get(), request_);
|
request_details->SetKey("url", base::Value(url));
|
||||||
|
|
||||||
BrowserThread::PostTask(
|
BrowserThread::PostTask(
|
||||||
BrowserThread::UI, FROM_HERE,
|
BrowserThread::UI, FROM_HERE,
|
||||||
|
@ -52,10 +41,7 @@ LoginHandler::~LoginHandler() {}
|
||||||
|
|
||||||
content::WebContents* LoginHandler::GetWebContents() const {
|
content::WebContents* LoginHandler::GetWebContents() const {
|
||||||
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||||
|
return web_contents_getter_.Run();
|
||||||
content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
|
|
||||||
render_process_host_id_, render_frame_id_);
|
|
||||||
return content::WebContents::FromRenderFrameHost(rfh);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoginHandler::Login(const base::string16& username,
|
void LoginHandler::Login(const base::string16& username,
|
||||||
|
@ -78,7 +64,7 @@ void LoginHandler::CancelAuth() {
|
||||||
|
|
||||||
void LoginHandler::OnRequestCancelled() {
|
void LoginHandler::OnRequestCancelled() {
|
||||||
TestAndSetAuthHandled();
|
TestAndSetAuthHandled();
|
||||||
request_ = nullptr;
|
auth_required_callback_.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marks authentication as handled and returns the previous handled state.
|
// Marks authentication as handled and returns the previous handled state.
|
||||||
|
@ -91,22 +77,16 @@ bool LoginHandler::TestAndSetAuthHandled() {
|
||||||
|
|
||||||
void LoginHandler::DoCancelAuth() {
|
void LoginHandler::DoCancelAuth() {
|
||||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||||
|
if (!auth_required_callback_.is_null())
|
||||||
if (request_) {
|
std::move(auth_required_callback_).Run(base::nullopt);
|
||||||
request_->CancelAuth();
|
|
||||||
// Verify that CancelAuth doesn't destroy the request via our delegate.
|
|
||||||
DCHECK(request_ != nullptr);
|
|
||||||
ResetLoginHandlerForRequest(request_);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoginHandler::DoLogin(const base::string16& username,
|
void LoginHandler::DoLogin(const base::string16& username,
|
||||||
const base::string16& password) {
|
const base::string16& password) {
|
||||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||||
|
if (!auth_required_callback_.is_null()) {
|
||||||
if (request_) {
|
std::move(auth_required_callback_)
|
||||||
request_->SetAuth(net::AuthCredentials(username, password));
|
.Run(net::AuthCredentials(username, password));
|
||||||
ResetLoginHandlerForRequest(request_);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
#ifndef ATOM_BROWSER_LOGIN_HANDLER_H_
|
#ifndef ATOM_BROWSER_LOGIN_HANDLER_H_
|
||||||
#define ATOM_BROWSER_LOGIN_HANDLER_H_
|
#define ATOM_BROWSER_LOGIN_HANDLER_H_
|
||||||
|
|
||||||
|
#include "base/callback.h"
|
||||||
|
#include "base/optional.h"
|
||||||
#include "base/strings/string16.h"
|
#include "base/strings/string16.h"
|
||||||
#include "base/synchronization/lock.h"
|
#include "base/synchronization/lock.h"
|
||||||
#include "content/public/browser/resource_dispatcher_host_login_delegate.h"
|
#include "content/public/browser/resource_dispatcher_host_login_delegate.h"
|
||||||
|
@ -15,7 +17,7 @@ class WebContents;
|
||||||
|
|
||||||
namespace net {
|
namespace net {
|
||||||
class AuthChallengeInfo;
|
class AuthChallengeInfo;
|
||||||
class URLRequest;
|
class AuthCredentials;
|
||||||
} // namespace net
|
} // namespace net
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -23,7 +25,12 @@ namespace atom {
|
||||||
// Handles the HTTP basic auth, must be created on IO thread.
|
// Handles the HTTP basic auth, must be created on IO thread.
|
||||||
class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
|
class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
|
||||||
public:
|
public:
|
||||||
LoginHandler(net::AuthChallengeInfo* auth_info, net::URLRequest* request);
|
LoginHandler(
|
||||||
|
net::AuthChallengeInfo* auth_info,
|
||||||
|
content::ResourceRequestInfo::WebContentsGetter web_contents_getter,
|
||||||
|
const GURL& url,
|
||||||
|
const base::Callback<void(const base::Optional<net::AuthCredentials>&)>&
|
||||||
|
auth_required_callback);
|
||||||
|
|
||||||
// Returns the WebContents associated with the request, must be called on UI
|
// Returns the WebContents associated with the request, must be called on UI
|
||||||
// thread.
|
// thread.
|
||||||
|
@ -59,13 +66,11 @@ class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
|
||||||
// Who/where/what asked for the authentication.
|
// Who/where/what asked for the authentication.
|
||||||
scoped_refptr<net::AuthChallengeInfo> auth_info_;
|
scoped_refptr<net::AuthChallengeInfo> auth_info_;
|
||||||
|
|
||||||
// The request that wants login data.
|
// WebContents associated with the login request.
|
||||||
// This should only be accessed on the IO loop.
|
content::ResourceRequestInfo::WebContentsGetter web_contents_getter_;
|
||||||
net::URLRequest* request_ = nullptr;
|
|
||||||
|
|
||||||
// Cached from the net::URLRequest, in case it goes NULL on us.
|
base::Callback<void(const base::Optional<net::AuthCredentials>&)>
|
||||||
int render_process_host_id_ = 0;
|
auth_required_callback_;
|
||||||
int render_frame_id_ = 0;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(LoginHandler);
|
DISALLOW_COPY_AND_ASSIGN(LoginHandler);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue