Guard against multiple calls of auth

This commit is contained in:
Cheng Zhao 2015-10-28 21:20:08 +08:00
parent 862c3187f5
commit 51ba37440d
2 changed files with 24 additions and 1 deletions

View file

@ -30,7 +30,8 @@ void ResetLoginHandlerForRequest(net::URLRequest* request) {
LoginHandler::LoginHandler(net::AuthChallengeInfo* auth_info,
net::URLRequest* request)
: auth_info_(auth_info),
: handled_auth_(false),
auth_info_(auth_info),
request_(request),
render_process_host_id_(0),
render_frame_id_(0) {
@ -56,6 +57,8 @@ content::WebContents* LoginHandler::GetWebContents() const {
void LoginHandler::Login(const base::string16& username,
const base::string16& password) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (TestAndSetAuthHandled())
return;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&LoginHandler::DoLogin, this, username, password));
@ -63,14 +66,25 @@ void LoginHandler::Login(const base::string16& username,
void LoginHandler::CancelAuth() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (TestAndSetAuthHandled())
return;
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&LoginHandler::DoCancelAuth, this));
}
void LoginHandler::OnRequestCancelled() {
TestAndSetAuthHandled();
request_ = nullptr;
}
// Marks authentication as handled and returns the previous handled state.
bool LoginHandler::TestAndSetAuthHandled() {
base::AutoLock lock(handled_auth_lock_);
bool was_handled = handled_auth_;
handled_auth_ = true;
return was_handled;
}
void LoginHandler::DoCancelAuth() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);

View file

@ -6,6 +6,7 @@
#define ATOM_BROWSER_LOGIN_HANDLER_H_
#include "base/strings/string16.h"
#include "base/synchronization/lock.h"
#include "content/public/browser/resource_dispatcher_host_login_delegate.h"
namespace content {
@ -48,6 +49,14 @@ class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
void DoCancelAuth();
void DoLogin(const base::string16& username, const base::string16& password);
// Marks authentication as handled and returns the previous handled
// state.
bool TestAndSetAuthHandled();
// True if we've handled auth (Login or CancelAuth has been called).
bool handled_auth_;
mutable base::Lock handled_auth_lock_;
// Who/where/what asked for the authentication.
scoped_refptr<net::AuthChallengeInfo> auth_info_;