Add login event for "app" module

This commit is contained in:
Cheng Zhao 2015-10-28 19:34:01 +08:00
parent 54dad72d92
commit d05255179a
7 changed files with 106 additions and 8 deletions

View file

@ -13,10 +13,11 @@
#include "atom/browser/api/atom_api_menu.h" #include "atom/browser/api/atom_api_menu.h"
#include "atom/browser/api/atom_api_session.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_context.h"
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/browser.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/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
@ -132,8 +133,6 @@ void OnClientCertificateSelected(
v8::Isolate* isolate, v8::Isolate* isolate,
std::shared_ptr<content::ClientCertificateDelegate> delegate, std::shared_ptr<content::ClientCertificateDelegate> delegate,
mate::Arguments* args) { mate::Arguments* args) {
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
mate::Dictionary cert_data; mate::Dictionary cert_data;
if (!(args->Length() == 1 && args->GetNext(&cert_data))) { if (!(args->Length() == 1 && args->GetNext(&cert_data))) {
args->ThrowError(); args->ThrowError();
@ -147,10 +146,18 @@ void OnClientCertificateSelected(
net::X509Certificate::CreateCertificateListFromBytes( net::X509Certificate::CreateCertificateListFromBytes(
encoded_data.data(), encoded_data.size(), encoded_data.data(), encoded_data.size(),
net::X509Certificate::FORMAT_AUTO); net::X509Certificate::FORMAT_AUTO);
delegate->ContinueWithCertificate(certs[0].get()); 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 } // namespace
App::App() { App::App() {
@ -233,6 +240,16 @@ void App::OnSelectCertificate(
cert_request_info->client_certs[0].get()); 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) { void App::OnGpuProcessCrashed(base::TerminationStatus exit_code) {
Emit("gpu-process-crashed"); Emit("gpu-process-crashed");
} }

View file

@ -50,6 +50,7 @@ class App : public mate::EventEmitter,
content::WebContents* web_contents, content::WebContents* web_contents,
net::SSLCertRequestInfo* cert_request_info, net::SSLCertRequestInfo* cert_request_info,
scoped_ptr<content::ClientCertificateDelegate> delegate) override; scoped_ptr<content::ClientCertificateDelegate> delegate) override;
void OnLogin(LoginHandler* login_handler) override;
// content::GpuDataManagerObserver: // content::GpuDataManagerObserver:
void OnGpuProcessCrashed(base::TerminationStatus exit_code) override; void OnGpuProcessCrashed(base::TerminationStatus exit_code) override;

View file

@ -134,6 +134,10 @@ void Browser::ClientCertificateSelector(
delegate.Pass())); delegate.Pass()));
} }
void Browser::RequestLogin(LoginHandler* login_handler) {
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnLogin(login_handler));
}
void Browser::NotifyAndShutdown() { void Browser::NotifyAndShutdown() {
if (is_shutdown_) if (is_shutdown_)
return; return;

View file

@ -29,6 +29,8 @@ class MenuModel;
namespace atom { namespace atom {
class LoginHandler;
// This class is used for control application-wide operations. // This class is used for control application-wide operations.
class Browser : public WindowListObserver { class Browser : public WindowListObserver {
public: public:
@ -122,6 +124,9 @@ class Browser : public WindowListObserver {
net::SSLCertRequestInfo* cert_request_info, net::SSLCertRequestInfo* cert_request_info,
scoped_ptr<content::ClientCertificateDelegate> delegate); scoped_ptr<content::ClientCertificateDelegate> delegate);
// Request basic auth login.
void RequestLogin(LoginHandler* login_handler);
void AddObserver(BrowserObserver* obs) { void AddObserver(BrowserObserver* obs) {
observers_.AddObserver(obs); observers_.AddObserver(obs);
} }

View file

@ -20,6 +20,8 @@ class SSLCertRequestInfo;
namespace atom { namespace atom {
class LoginHandler;
class BrowserObserver { class BrowserObserver {
public: public:
// The browser is about to close all windows. // The browser is about to close all windows.
@ -57,6 +59,9 @@ class BrowserObserver {
net::SSLCertRequestInfo* cert_request_info, net::SSLCertRequestInfo* cert_request_info,
scoped_ptr<content::ClientCertificateDelegate> delegate) {} scoped_ptr<content::ClientCertificateDelegate> delegate) {}
// The browser requests HTTP login.
virtual void OnLogin(LoginHandler* login_handler) {}
protected: protected:
virtual ~BrowserObserver() {} virtual ~BrowserObserver() {}
}; };

View file

@ -4,19 +4,76 @@
#include "atom/browser/login_handler.h" #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/base/auth.h"
#include "net/url_request/url_request.h"
using content::BrowserThread;
namespace atom { 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, LoginHandler::LoginHandler(net::AuthChallengeInfo* auth_info,
net::URLRequest* request) 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() { 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() { 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 } // namespace atom

View file

@ -5,7 +5,7 @@
#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/memory/weak_ptr.h" #include "base/strings/string16.h"
#include "content/public/browser/resource_dispatcher_host_login_delegate.h" #include "content/public/browser/resource_dispatcher_host_login_delegate.h"
namespace net { namespace net {
@ -15,10 +15,17 @@ class URLRequest;
namespace atom { namespace atom {
// 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, 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: protected:
~LoginHandler() override; ~LoginHandler() override;
@ -26,6 +33,10 @@ class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
void OnRequestCancelled() override; void OnRequestCancelled() override;
private: 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. // Who/where/what asked for the authentication.
scoped_refptr<net::AuthChallengeInfo> auth_info_; scoped_refptr<net::AuthChallengeInfo> auth_info_;
@ -33,8 +44,6 @@ class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
// This should only be accessed on the IO loop. // This should only be accessed on the IO loop.
net::URLRequest* request_; net::URLRequest* request_;
base::WeakPtrFactory<LoginHandler> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(LoginHandler); DISALLOW_COPY_AND_ASSIGN(LoginHandler);
}; };