]> khamidou — GitHub mirrors - electron.git/commitdiff
Guard against multiple calls of auth
authorCheng Zhao <zcbenz@gmail.com>
Wed, 28 Oct 2015 13:20:08 +0000 (21:20 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 28 Oct 2015 13:20:08 +0000 (21:20 +0800)
atom/browser/login_handler.cc
atom/browser/login_handler.h

index ca6e5de57df097106cfaa158d8a3f60a091ff598..7a1a77cc2b1333ccfd56c77b98f8cfa7c5d8631d 100644 (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);
 
index 899bc8ca450e3b47d42e77afcd37410f00130a7a..52ec1abf5b1a8e5353dd294b946275dc229ef6ab 100644 (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_;