electron/atom/browser/login_handler.cc

96 lines
3 KiB
C++
Raw Normal View History

2015-10-28 15:34:41 +08:00
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/login_handler.h"
2015-10-28 19:34:01 +08:00
#include "atom/browser/browser.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
2015-10-28 19:34:01 +08:00
#include "content/public/browser/resource_dispatcher_host.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/web_contents.h"
2015-10-28 15:34:41 +08:00
#include "net/base/auth.h"
2015-10-28 19:34:01 +08:00
#include "net/url_request/url_request.h"
using content::BrowserThread;
2015-10-28 15:34:41 +08:00
namespace atom {
2015-10-28 19:34:01 +08:00
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
2015-10-28 15:34:41 +08:00
LoginHandler::LoginHandler(net::AuthChallengeInfo* auth_info,
net::URLRequest* request)
: auth_info_(auth_info),
request_(request),
render_process_host_id_(0),
render_frame_id_(0) {
content::ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame(
&render_process_host_id_, &render_frame_id_);
2015-10-28 19:34:01 +08:00
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&Browser::RequestLogin,
base::Unretained(Browser::Get()),
make_scoped_refptr(this)));
2015-10-28 15:34:41 +08:00
}
LoginHandler::~LoginHandler() {
}
content::WebContents* LoginHandler::GetWebContents() const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
render_process_host_id_, render_frame_id_);
return content::WebContents::FromRenderFrameHost(rfh);
}
2015-10-28 19:34:01 +08:00
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));
}
2015-10-28 15:34:41 +08:00
void LoginHandler::OnRequestCancelled() {
2015-10-28 19:34:01 +08:00
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_);
}
2015-10-28 15:34:41 +08:00
}
} // namespace atom