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"
|
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(
|
|
|
|
net::AuthChallengeInfo* auth_info,
|
|
|
|
content::ResourceRequestInfo::WebContentsGetter web_contents_getter,
|
|
|
|
const GURL& url,
|
|
|
|
const base::Callback<void(const base::Optional<net::AuthCredentials>&)>&
|
|
|
|
auth_required_callback)
|
|
|
|
: auth_info_(auth_info),
|
|
|
|
web_contents_getter_(web_contents_getter),
|
|
|
|
auth_required_callback_(auth_required_callback) {
|
2016-06-08 19:22:21 +05:30
|
|
|
// Fill request details on IO thread.
|
2018-04-12 14:56:41 +05:30
|
|
|
// TODO(deepak1556): Fill in method and referrer details to
|
|
|
|
// avoid breaking the app login event.
|
2016-06-08 19:22:21 +05:30
|
|
|
std::unique_ptr<base::DictionaryValue> request_details(
|
|
|
|
new base::DictionaryValue);
|
2018-04-12 15:18:26 +05:30
|
|
|
request_details->SetKey("url", base::Value(url.spec()));
|
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()),
|
|
|
|
base::RetainedRef(WrapRefCounted(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 20:21:56 +08:00
|
|
|
content::WebContents* LoginHandler::GetWebContents() const {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2018-04-12 14:56:41 +05:30
|
|
|
return web_contents_getter_.Run();
|
2015-10-28 20:21:56 +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);
|
2015-10-28 21:20:08 +08:00
|
|
|
if (TestAndSetAuthHandled())
|
|
|
|
return;
|
2015-10-28 19:34:01 +08:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
2018-04-20 16:25:05 +05:30
|
|
|
base::BindOnce(&LoginHandler::DoLogin, this, username, password));
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoginHandler::CancelAuth() {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2015-10-28 21:20:08 +08:00
|
|
|
if (TestAndSetAuthHandled())
|
|
|
|
return;
|
2015-10-28 19:34:01 +08:00
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2018-04-20 16:25:05 +05:30
|
|
|
base::BindOnce(&LoginHandler::DoCancelAuth, this));
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
|
|
|
|
2015-10-28 15:34:41 +08:00
|
|
|
void LoginHandler::OnRequestCancelled() {
|
2015-10-28 21:20:08 +08:00
|
|
|
TestAndSetAuthHandled();
|
2018-04-12 14:56:41 +05:30
|
|
|
auth_required_callback_.Reset();
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
|
|
|
|
2015-10-28 21:20:08 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-10-28 19:34:01 +08:00
|
|
|
void LoginHandler::DoCancelAuth() {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2018-04-12 14:56:41 +05:30
|
|
|
if (!auth_required_callback_.is_null())
|
|
|
|
std::move(auth_required_callback_).Run(base::nullopt);
|
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-04-12 14:56:41 +05:30
|
|
|
if (!auth_required_callback_.is_null()) {
|
|
|
|
std::move(auth_required_callback_)
|
|
|
|
.Run(net::AuthCredentials(username, password));
|
2015-10-28 19:34:01 +08:00
|
|
|
}
|
2015-10-28 15:34:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|