#include "atom/browser/api/atom_api_menu.h"
#include "atom/browser/api/atom_api_session.h"
+#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/browser.h"
-#include "atom/browser/api/atom_api_web_contents.h"
+#include "atom/browser/login_handler.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/node_includes.h"
v8::Isolate* isolate,
std::shared_ptr<content::ClientCertificateDelegate> delegate,
mate::Arguments* args) {
- v8::Locker locker(isolate);
- v8::HandleScope handle_scope(isolate);
mate::Dictionary cert_data;
if (!(args->Length() == 1 && args->GetNext(&cert_data))) {
args->ThrowError();
net::X509Certificate::CreateCertificateListFromBytes(
encoded_data.data(), encoded_data.size(),
net::X509Certificate::FORMAT_AUTO);
-
delegate->ContinueWithCertificate(certs[0].get());
}
+void PassLoginInformation(scoped_refptr<LoginHandler> login_handler,
+ mate::Arguments* args) {
+ base::string16 username, password;
+ if (args->GetNext(&username) && args->GetNext(&password))
+ login_handler->Login(username, password);
+ else
+ login_handler->CancelAuth();
+}
+
} // namespace
App::App() {
cert_request_info->client_certs[0].get());
}
+void App::OnLogin(LoginHandler* login_handler) {
+ bool prevent_default =
+ Emit("login", base::Bind(&PassLoginInformation,
+ make_scoped_refptr(login_handler)));
+
+ // Default behavior is to alwasy cancel the auth.
+ if (!prevent_default)
+ login_handler->CancelAuth();
+}
+
void App::OnGpuProcessCrashed(base::TerminationStatus exit_code) {
Emit("gpu-process-crashed");
}
content::WebContents* web_contents,
net::SSLCertRequestInfo* cert_request_info,
scoped_ptr<content::ClientCertificateDelegate> delegate) override;
+ void OnLogin(LoginHandler* login_handler) override;
// content::GpuDataManagerObserver:
void OnGpuProcessCrashed(base::TerminationStatus exit_code) override;
delegate.Pass()));
}
+void Browser::RequestLogin(LoginHandler* login_handler) {
+ FOR_EACH_OBSERVER(BrowserObserver, observers_, OnLogin(login_handler));
+}
+
void Browser::NotifyAndShutdown() {
if (is_shutdown_)
return;
namespace atom {
+class LoginHandler;
+
// This class is used for control application-wide operations.
class Browser : public WindowListObserver {
public:
net::SSLCertRequestInfo* cert_request_info,
scoped_ptr<content::ClientCertificateDelegate> delegate);
+ // Request basic auth login.
+ void RequestLogin(LoginHandler* login_handler);
+
void AddObserver(BrowserObserver* obs) {
observers_.AddObserver(obs);
}
namespace atom {
+class LoginHandler;
+
class BrowserObserver {
public:
// The browser is about to close all windows.
net::SSLCertRequestInfo* cert_request_info,
scoped_ptr<content::ClientCertificateDelegate> delegate) {}
+ // The browser requests HTTP login.
+ virtual void OnLogin(LoginHandler* login_handler) {}
+
protected:
virtual ~BrowserObserver() {}
};
#include "atom/browser/login_handler.h"
+#include "atom/browser/browser.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/resource_dispatcher_host.h"
#include "net/base/auth.h"
+#include "net/url_request/url_request.h"
+
+using content::BrowserThread;
namespace atom {
+namespace {
+
+// Helper to remove the ref from an net::URLRequest to the LoginHandler.
+// Should only be called from the IO thread, since it accesses an
+// net::URLRequest.
+void ResetLoginHandlerForRequest(net::URLRequest* request) {
+ content::ResourceDispatcherHost::Get()->ClearLoginDelegateForRequest(request);
+}
+
+} // namespace
+
LoginHandler::LoginHandler(net::AuthChallengeInfo* auth_info,
net::URLRequest* request)
- : auth_info_(auth_info), request_(request), weak_factory_(this) {
+ : auth_info_(auth_info), request_(request) {
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&Browser::RequestLogin,
+ base::Unretained(Browser::Get()),
+ make_scoped_refptr(this)));
}
LoginHandler::~LoginHandler() {
}
+void LoginHandler::Login(const base::string16& username,
+ const base::string16& password) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&LoginHandler::DoLogin, this, username, password));
+}
+
+void LoginHandler::CancelAuth() {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&LoginHandler::DoCancelAuth, this));
+}
+
void LoginHandler::OnRequestCancelled() {
+ request_ = nullptr;
+}
+
+void LoginHandler::DoCancelAuth() {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ if (request_) {
+ 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,
+ const base::string16& password) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ if (request_) {
+ request_->SetAuth(net::AuthCredentials(username, password));
+ ResetLoginHandlerForRequest(request_);
+ }
}
} // namespace atom
#ifndef ATOM_BROWSER_LOGIN_HANDLER_H_
#define ATOM_BROWSER_LOGIN_HANDLER_H_
-#include "base/memory/weak_ptr.h"
+#include "base/strings/string16.h"
#include "content/public/browser/resource_dispatcher_host_login_delegate.h"
namespace net {
namespace atom {
+// Handles the HTTP basic auth, must be created on IO thread.
class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
public:
LoginHandler(net::AuthChallengeInfo* auth_info, net::URLRequest* request);
+ // The auth is cancelled, must be called on UI thread.
+ void CancelAuth();
+
+ // Login with |username| and |password|, must be called on UI thread.
+ void Login(const base::string16& username, const base::string16& password);
+
protected:
~LoginHandler() override;
void OnRequestCancelled() override;
private:
+ // Must be called on IO thread.
+ void DoCancelAuth();
+ void DoLogin(const base::string16& username, const base::string16& password);
+
// Who/where/what asked for the authentication.
scoped_refptr<net::AuthChallengeInfo> auth_info_;
// This should only be accessed on the IO loop.
net::URLRequest* request_;
- base::WeakPtrFactory<LoginHandler> weak_factory_;
-
DISALLOW_COPY_AND_ASSIGN(LoginHandler);
};