2019-07-12 22:44:43 +02:00
|
|
|
// Copyright (c) 2019 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/net_service/login_delegate.h"
|
2019-07-12 22:44:43 +02:00
|
|
|
|
|
|
|
#include "base/memory/scoped_refptr.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/task/sequenced_task_runner.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/browser_host_base.h"
|
|
|
|
#include "cef/libcef/browser/net_service/browser_urlrequest_impl.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
2019-07-12 22:44:43 +02:00
|
|
|
#include "content/public/browser/global_request_id.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
|
|
|
|
namespace net_service {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AuthCallbackImpl : public CefAuthCallback {
|
|
|
|
public:
|
|
|
|
explicit AuthCallbackImpl(base::WeakPtr<LoginDelegate> delegate)
|
|
|
|
: delegate_(delegate),
|
2023-01-30 18:43:54 +01:00
|
|
|
task_runner_(base::SequencedTaskRunner::GetCurrentDefault()) {}
|
2019-07-12 22:44:43 +02:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
AuthCallbackImpl(const AuthCallbackImpl&) = delete;
|
|
|
|
AuthCallbackImpl& operator=(const AuthCallbackImpl&) = delete;
|
|
|
|
|
2019-07-12 22:44:43 +02:00
|
|
|
~AuthCallbackImpl() override {
|
|
|
|
if (delegate_.MaybeValid()) {
|
|
|
|
// If |delegate_| isn't valid this will be a no-op.
|
|
|
|
task_runner_->PostTask(FROM_HERE,
|
|
|
|
base::BindOnce(&LoginDelegate::Cancel, delegate_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Continue(const CefString& username, const CefString& password) override {
|
|
|
|
if (!task_runner_->RunsTasksInCurrentSequence()) {
|
|
|
|
task_runner_->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(&AuthCallbackImpl::Continue, this, username,
|
|
|
|
password));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delegate_) {
|
|
|
|
delegate_->Continue(username, password);
|
2020-01-15 14:36:24 +01:00
|
|
|
delegate_ = nullptr;
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override {
|
|
|
|
if (!task_runner_->RunsTasksInCurrentSequence()) {
|
|
|
|
task_runner_->PostTask(FROM_HERE,
|
|
|
|
base::BindOnce(&AuthCallbackImpl::Cancel, this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delegate_) {
|
|
|
|
delegate_->Cancel();
|
2020-01-15 14:36:24 +01:00
|
|
|
delegate_ = nullptr;
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::WeakPtr<LoginDelegate> delegate_;
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(AuthCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
void RunCallbackOnIOThread(
|
2020-09-25 03:40:47 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser,
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<CefBrowserURLRequest::RequestInfo> url_request_info,
|
2019-07-12 22:44:43 +02:00
|
|
|
const net::AuthChallengeInfo& auth_info,
|
|
|
|
const GURL& origin_url,
|
|
|
|
CefRefPtr<AuthCallbackImpl> callback_impl) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
// TODO(network): After the old network code path is deleted move this
|
|
|
|
// callback to the BrowserURLRequest's context thread.
|
|
|
|
if (url_request_info) {
|
|
|
|
bool handled = url_request_info->second->GetAuthCredentials(
|
|
|
|
auth_info.is_proxy, auth_info.challenger.host(),
|
|
|
|
auth_info.challenger.port(), auth_info.realm, auth_info.scheme,
|
|
|
|
callback_impl.get());
|
|
|
|
if (handled) {
|
|
|
|
// The user will execute the callback, or the request will be canceled on
|
|
|
|
// AuthCallbackImpl destruction.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browser) {
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client) {
|
|
|
|
CefRefPtr<CefRequestHandler> handler = client->GetRequestHandler();
|
|
|
|
if (handler) {
|
|
|
|
bool handled = handler->GetAuthCredentials(
|
|
|
|
browser.get(), origin_url.spec(), auth_info.is_proxy,
|
|
|
|
auth_info.challenger.host(), auth_info.challenger.port(),
|
|
|
|
auth_info.realm, auth_info.scheme, callback_impl.get());
|
|
|
|
if (handled) {
|
|
|
|
// The user will execute the callback, or the request will be canceled
|
|
|
|
// on AuthCallbackImpl destruction.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback_impl->Cancel();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
LoginDelegate::LoginDelegate(const net::AuthChallengeInfo& auth_info,
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
const content::GlobalRequestID& request_id,
|
|
|
|
const GURL& origin_url,
|
|
|
|
LoginAuthRequiredCallback callback)
|
|
|
|
: callback_(std::move(callback)), weak_ptr_factory_(this) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
// May be nullptr for requests originating from CefURLRequest.
|
2020-09-25 03:40:47 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser;
|
2019-07-12 22:44:43 +02:00
|
|
|
if (web_contents) {
|
2020-09-25 03:40:47 +02:00
|
|
|
browser = CefBrowserHostBase::GetBrowserForContents(web_contents);
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// |callback| needs to be executed asynchronously.
|
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(&LoginDelegate::Start,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), browser,
|
|
|
|
auth_info, request_id, origin_url));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoginDelegate::Continue(const CefString& username,
|
|
|
|
const CefString& password) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
std::move(callback_).Run(
|
|
|
|
net::AuthCredentials(username.ToString16(), password.ToString16()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoginDelegate::Cancel() {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (!callback_.is_null()) {
|
2024-03-19 22:11:42 +01:00
|
|
|
std::move(callback_).Run(std::nullopt);
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
void LoginDelegate::Start(CefRefPtr<CefBrowserHostBase> browser,
|
2019-07-12 22:44:43 +02:00
|
|
|
const net::AuthChallengeInfo& auth_info,
|
|
|
|
const content::GlobalRequestID& request_id,
|
|
|
|
const GURL& origin_url) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
auto url_request_info = CefBrowserURLRequest::FromRequestID(request_id);
|
|
|
|
|
|
|
|
if (browser || url_request_info) {
|
|
|
|
// AuthCallbackImpl is bound to the current thread.
|
|
|
|
CefRefPtr<AuthCallbackImpl> callbackImpl =
|
|
|
|
new AuthCallbackImpl(weak_ptr_factory_.GetWeakPtr());
|
|
|
|
|
|
|
|
// Execute callbacks on the IO thread to maintain the "old"
|
|
|
|
// network_delegate callback behaviour.
|
|
|
|
CEF_POST_TASK(CEF_IOT, base::BindOnce(&RunCallbackOnIOThread, browser,
|
|
|
|
url_request_info, auth_info,
|
|
|
|
origin_url, callbackImpl));
|
|
|
|
} else {
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:11:42 +01:00
|
|
|
} // namespace net_service
|