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"
|
2018-08-23 15:25:13 +05:30
|
|
|
#include "atom/common/native_mate_converters/net_converter.h"
|
2016-06-08 19:22:21 +05:30
|
|
|
#include "base/values.h"
|
2015-10-28 19:34:01 +08:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2015-10-28 20:21:56 +08:00
|
|
|
#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
|
|
|
|
|
|
|
using content::BrowserThread;
|
2015-10-28 15:34:41 +08:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2018-04-12 14:56:41 +05:30
|
|
|
LoginHandler::LoginHandler(
|
2018-08-23 15:25:13 +05:30
|
|
|
net::URLRequest* request,
|
|
|
|
const net::AuthChallengeInfo& auth_info,
|
|
|
|
const net::NetworkDelegate::AuthCallback& callback,
|
|
|
|
net::AuthCredentials* credentials,
|
|
|
|
const content::ResourceRequestInfo* resource_request_info)
|
|
|
|
: credentials_(credentials),
|
|
|
|
auth_info_(auth_info),
|
|
|
|
auth_callback_(std::move(callback)),
|
|
|
|
weak_factory_(this) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
|
|
2016-06-08 19:22:21 +05:30
|
|
|
std::unique_ptr<base::DictionaryValue> request_details(
|
|
|
|
new base::DictionaryValue);
|
2018-08-23 15:25:13 +05:30
|
|
|
FillRequestDetails(request_details.get(), request);
|
|
|
|
|
|
|
|
web_contents_getter_ =
|
|
|
|
resource_request_info->GetWebContentsGetterForRequest();
|
2016-06-08 19:22:21 +05:30
|
|
|
|
2016-05-23 12:28:59 +09:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI, FROM_HERE,
|
2018-04-20 16:25:05 +05:30
|
|
|
base::BindOnce(&Browser::RequestLogin, base::Unretained(Browser::Get()),
|
2018-08-23 15:25:13 +05:30
|
|
|
base::RetainedRef(this), std::move(request_details)));
|
2015-10-28 15:34:41 +08:00
|
|
|
}
|
|
|
|
|
2018-04-17 21:55:30 -04:00
|
|
|
LoginHandler::~LoginHandler() {}
|
2015-10-28 15:34:41 +08:00
|
|
|
|
2015-10-28 19:34:01 +08:00
|
|
|
void LoginHandler::Login(const base::string16& username,
|
|
|
|
const base::string16& password) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2018-08-23 15:25:13 +05:30
|
|
|
|
2015-10-28 19:34:01 +08:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
2018-08-23 15:25:13 +05:30
|
|
|
base::BindOnce(&LoginHandler::DoLogin, weak_factory_.GetWeakPtr(),
|
|
|
|
username, password));
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoginHandler::CancelAuth() {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2018-08-23 15:25:13 +05:30
|
|
|
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
|
|
|
base::BindOnce(&LoginHandler::DoCancelAuth, weak_factory_.GetWeakPtr()));
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:25:13 +05:30
|
|
|
void LoginHandler::NotifyRequestDestroyed() {
|
|
|
|
auth_callback_.Reset();
|
|
|
|
credentials_ = nullptr;
|
|
|
|
weak_factory_.InvalidateWeakPtrs();
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:25:13 +05:30
|
|
|
content::WebContents* LoginHandler::GetWebContents() const {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
return web_contents_getter_.Run();
|
2015-10-28 21:20:08 +08:00
|
|
|
}
|
|
|
|
|
2015-10-28 19:34:01 +08:00
|
|
|
void LoginHandler::DoCancelAuth() {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2018-08-23 15:25:13 +05:30
|
|
|
if (!auth_callback_.is_null())
|
|
|
|
std::move(auth_callback_)
|
|
|
|
.Run(net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoginHandler::DoLogin(const base::string16& username,
|
|
|
|
const base::string16& password) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2018-08-23 15:25:13 +05:30
|
|
|
if (!auth_callback_.is_null()) {
|
|
|
|
credentials_->Set(username, password);
|
|
|
|
std::move(auth_callback_)
|
|
|
|
.Run(net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
2015-10-28 15:34:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|