|
@@ -5,100 +5,84 @@
|
|
|
#include "shell/browser/login_handler.h"
|
|
|
|
|
|
#include <memory>
|
|
|
+#include <string>
|
|
|
#include <utility>
|
|
|
+#include <vector>
|
|
|
|
|
|
-#include "base/task/post_task.h"
|
|
|
-#include "base/values.h"
|
|
|
-#include "content/public/browser/browser_task_traits.h"
|
|
|
-#include "content/public/browser/browser_thread.h"
|
|
|
-#include "content/public/browser/web_contents.h"
|
|
|
-#include "net/base/auth.h"
|
|
|
-#include "shell/browser/browser.h"
|
|
|
+#include "base/callback.h"
|
|
|
+#include "native_mate/dictionary.h"
|
|
|
+#include "shell/browser/api/atom_api_web_contents.h"
|
|
|
+#include "shell/common/native_mate_converters/callback_converter_deprecated.h"
|
|
|
#include "shell/common/native_mate_converters/net_converter.h"
|
|
|
+#include "shell/common/native_mate_converters/value_converter.h"
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
-LoginHandler::LoginHandler(net::URLRequest* request,
|
|
|
- const net::AuthChallengeInfo& auth_info,
|
|
|
- // net::NetworkDelegate::AuthCallback callback,
|
|
|
- net::AuthCredentials* credentials)
|
|
|
- : credentials_(credentials),
|
|
|
- auth_info_(std::make_unique<net::AuthChallengeInfo>(auth_info)),
|
|
|
- // auth_callback_(std::move(callback)),
|
|
|
- weak_factory_(this) {
|
|
|
- DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
-
|
|
|
- std::unique_ptr<base::DictionaryValue> request_details(
|
|
|
- new base::DictionaryValue);
|
|
|
- FillRequestDetails(request_details.get(), request);
|
|
|
-
|
|
|
- // TODO(deepak1556): fix with network service
|
|
|
- // tracking issue: #19602
|
|
|
- CHECK(false) << "fix with network service";
|
|
|
- // web_contents_getter_ =
|
|
|
- // resource_request_info->GetWebContentsGetterForRequest();
|
|
|
-
|
|
|
- base::PostTask(
|
|
|
- FROM_HERE, {BrowserThread::UI},
|
|
|
- base::BindOnce(&Browser::RequestLogin, base::Unretained(Browser::Get()),
|
|
|
- base::RetainedRef(this), std::move(request_details)));
|
|
|
-}
|
|
|
-
|
|
|
-LoginHandler::~LoginHandler() = default;
|
|
|
-
|
|
|
-void LoginHandler::Login(const base::string16& username,
|
|
|
- const base::string16& password) {
|
|
|
+LoginHandler::LoginHandler(
|
|
|
+ const net::AuthChallengeInfo& auth_info,
|
|
|
+ content::WebContents* web_contents,
|
|
|
+ bool is_main_frame,
|
|
|
+ const GURL& url,
|
|
|
+ scoped_refptr<net::HttpResponseHeaders> response_headers,
|
|
|
+ bool first_auth_attempt,
|
|
|
+ LoginAuthRequiredCallback auth_required_callback)
|
|
|
+
|
|
|
+ : WebContentsObserver(web_contents),
|
|
|
+ auth_required_callback_(std::move(auth_required_callback)) {
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
|
|
base::PostTask(
|
|
|
- FROM_HERE, {BrowserThread::IO},
|
|
|
- base::BindOnce(&LoginHandler::DoLogin, weak_factory_.GetWeakPtr(),
|
|
|
- username, password));
|
|
|
+ FROM_HERE, {base::CurrentThread()},
|
|
|
+ base::BindOnce(&LoginHandler::EmitEvent, weak_factory_.GetWeakPtr(),
|
|
|
+ auth_info, is_main_frame, url, response_headers,
|
|
|
+ first_auth_attempt));
|
|
|
}
|
|
|
|
|
|
-void LoginHandler::CancelAuth() {
|
|
|
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
+void LoginHandler::EmitEvent(
|
|
|
+ net::AuthChallengeInfo auth_info,
|
|
|
+ bool is_main_frame,
|
|
|
+ const GURL& url,
|
|
|
+ scoped_refptr<net::HttpResponseHeaders> response_headers,
|
|
|
+ bool first_auth_attempt) {
|
|
|
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
+
|
|
|
+ auto api_web_contents = api::WebContents::From(isolate, web_contents());
|
|
|
+ if (api_web_contents.IsEmpty()) {
|
|
|
+ std::move(auth_required_callback_).Run(base::nullopt);
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- base::PostTask(
|
|
|
- FROM_HERE, {BrowserThread::IO},
|
|
|
- base::BindOnce(&LoginHandler::DoCancelAuth, weak_factory_.GetWeakPtr()));
|
|
|
-}
|
|
|
+ v8::HandleScope scope(isolate);
|
|
|
|
|
|
-void LoginHandler::NotifyRequestDestroyed() {
|
|
|
- // auth_callback_.Reset();
|
|
|
- credentials_ = nullptr;
|
|
|
- weak_factory_.InvalidateWeakPtrs();
|
|
|
-}
|
|
|
+ auto details = mate::Dictionary::CreateEmpty(isolate);
|
|
|
+ details.Set("url", url.spec());
|
|
|
|
|
|
-content::WebContents* LoginHandler::GetWebContents() const {
|
|
|
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
- // TODO(deepak1556): fix with network service
|
|
|
- // tracking issue: #19602
|
|
|
- CHECK(false) << "fix with network service";
|
|
|
- return web_contents_getter_.Run();
|
|
|
-}
|
|
|
+ // These parameters aren't documented, and I'm not sure that they're useful,
|
|
|
+ // but we might as well stick 'em on the details object. If it turns out they
|
|
|
+ // are useful, we can add them to the docs :)
|
|
|
+ details.Set("isMainFrame", is_main_frame);
|
|
|
+ details.Set("firstAuthAttempt", first_auth_attempt);
|
|
|
+ details.Set("responseHeaders", response_headers.get());
|
|
|
|
|
|
-void LoginHandler::DoCancelAuth() {
|
|
|
- DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
- /*
|
|
|
- if (!auth_callback_.is_null())
|
|
|
- std::move(auth_callback_)
|
|
|
- .Run(net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
|
|
|
- */
|
|
|
+ bool default_prevented =
|
|
|
+ api_web_contents->Emit("login", std::move(details), auth_info,
|
|
|
+ base::BindRepeating(&LoginHandler::CallbackFromJS,
|
|
|
+ weak_factory_.GetWeakPtr()));
|
|
|
+ if (!default_prevented && auth_required_callback_) {
|
|
|
+ std::move(auth_required_callback_).Run(base::nullopt);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-void LoginHandler::DoLogin(const base::string16& username,
|
|
|
- const base::string16& password) {
|
|
|
- DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
- /*
|
|
|
- if (!auth_callback_.is_null()) {
|
|
|
- credentials_->Set(username, password);
|
|
|
- std::move(auth_callback_)
|
|
|
- .Run(net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
|
|
|
+LoginHandler::~LoginHandler() = default;
|
|
|
+
|
|
|
+void LoginHandler::CallbackFromJS(base::string16 username,
|
|
|
+ base::string16 password) {
|
|
|
+ if (auth_required_callback_) {
|
|
|
+ std::move(auth_required_callback_)
|
|
|
+ .Run(net::AuthCredentials(username, password));
|
|
|
}
|
|
|
- */
|
|
|
}
|
|
|
|
|
|
} // namespace electron
|