Skip to content

Commit 034f4d5

Browse files
authored
fix: implement 'login' event for WebContents (electron#20954)
1 parent 049bd09 commit 034f4d5

16 files changed

Lines changed: 235 additions & 243 deletions

docs/api/app.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,8 @@ Returns:
314314

315315
* `event` Event
316316
* `webContents` [WebContents](web-contents.md)
317-
* `request` Object
318-
* `method` String
317+
* `authenticationResponseDetails` Object
319318
* `url` URL
320-
* `referrer` URL
321319
* `authInfo` Object
322320
* `isProxy` Boolean
323321
* `scheme` String
@@ -337,7 +335,7 @@ should prevent the default behavior with `event.preventDefault()` and call
337335
```javascript
338336
const { app } = require('electron')
339337

340-
app.on('login', (event, webContents, request, authInfo, callback) => {
338+
app.on('login', (event, webContents, details, authInfo, callback) => {
341339
event.preventDefault()
342340
callback('username', 'secret')
343341
})

docs/api/web-contents.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,8 @@ The usage is the same with [the `select-client-certificate` event of
454454
Returns:
455455

456456
* `event` Event
457-
* `request` Object
458-
* `method` String
457+
* `authenticationResponseDetails` Object
459458
* `url` URL
460-
* `referrer` URL
461459
* `authInfo` Object
462460
* `isProxy` Boolean
463461
* `scheme` String

lib/browser/api/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ if (process.platform === 'linux') {
105105
}
106106

107107
// Routes the events to webContents.
108-
const events = ['login', 'certificate-error', 'select-client-certificate']
108+
const events = ['certificate-error', 'select-client-certificate']
109109
for (const name of events) {
110-
app.on(name as 'login', (event, webContents, ...args: any[]) => {
110+
app.on(name as 'certificate-error', (event, webContents, ...args: any[]) => {
111111
webContents.emit(name, event, ...args)
112112
})
113113
}

lib/browser/api/web-contents.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ WebContents.prototype._init = function () {
423423
})
424424
}
425425

426+
this.on('login', (event, ...args) => {
427+
app.emit('login', event, this, ...args)
428+
})
429+
426430
const event = process.electronBinding('event').createEmpty()
427431
app.emit('web-contents-created', event, this)
428432
}

shell/browser/api/atom_api_app.cc

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,6 @@ void OnClientCertificateSelected(
495495
}
496496
}
497497

498-
void PassLoginInformation(scoped_refptr<LoginHandler> login_handler,
499-
gin_helper::Arguments* args) {
500-
base::string16 username, password;
501-
if (args->GetNext(&username) && args->GetNext(&password))
502-
login_handler->Login(username, password);
503-
else
504-
login_handler->CancelAuth();
505-
}
506-
507498
#if defined(USE_NSS_CERTS)
508499
int ImportIntoCertStore(CertificateManagerModel* model,
509500
const base::DictionaryValue& options) {
@@ -667,25 +658,6 @@ void App::OnNewWindowForTab() {
667658
}
668659
#endif
669660

670-
void App::OnLogin(scoped_refptr<LoginHandler> login_handler,
671-
const base::DictionaryValue& request_details) {
672-
v8::Locker locker(isolate());
673-
v8::HandleScope handle_scope(isolate());
674-
bool prevent_default = false;
675-
content::WebContents* web_contents = login_handler->GetWebContents();
676-
if (web_contents) {
677-
prevent_default =
678-
Emit("login", WebContents::FromOrCreate(isolate(), web_contents),
679-
request_details, *login_handler->auth_info(),
680-
base::BindOnce(&PassLoginInformation,
681-
base::RetainedRef(login_handler)));
682-
}
683-
684-
// Default behavior is to always cancel the auth.
685-
if (!prevent_default)
686-
login_handler->CancelAuth();
687-
}
688-
689661
bool App::CanCreateWindow(
690662
content::RenderFrameHost* opener,
691663
const GURL& opener_url,

shell/browser/api/atom_api_app.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ class App : public AtomBrowserClient::Delegate,
8686
void OnActivate(bool has_visible_windows) override;
8787
void OnWillFinishLaunching() override;
8888
void OnFinishLaunching(const base::DictionaryValue& launch_info) override;
89-
void OnLogin(scoped_refptr<LoginHandler> login_handler,
90-
const base::DictionaryValue& request_details) override;
9189
void OnAccessibilitySupportChanged() override;
9290
void OnPreMainMessageLoopRun() override;
9391
#if defined(OS_MACOSX)

shell/browser/atom_browser_client.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "content/public/browser/browser_ppapi_host.h"
3131
#include "content/public/browser/browser_task_traits.h"
3232
#include "content/public/browser/client_certificate_delegate.h"
33+
#include "content/public/browser/login_delegate.h"
3334
#include "content/public/browser/overlay_window.h"
3435
#include "content/public/browser/render_frame_host.h"
3536
#include "content/public/browser/render_process_host.h"
@@ -1104,4 +1105,18 @@ void AtomBrowserClient::BindHostReceiverForRenderer(
11041105
#endif
11051106
}
11061107

1108+
std::unique_ptr<content::LoginDelegate> AtomBrowserClient::CreateLoginDelegate(
1109+
const net::AuthChallengeInfo& auth_info,
1110+
content::WebContents* web_contents,
1111+
const content::GlobalRequestID& request_id,
1112+
bool is_main_frame,
1113+
const GURL& url,
1114+
scoped_refptr<net::HttpResponseHeaders> response_headers,
1115+
bool first_auth_attempt,
1116+
LoginAuthRequiredCallback auth_required_callback) {
1117+
return std::make_unique<LoginHandler>(
1118+
auth_info, web_contents, is_main_frame, url, response_headers,
1119+
first_auth_attempt, std::move(auth_required_callback));
1120+
}
1121+
11071122
} // namespace electron

shell/browser/atom_browser_client.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ class AtomBrowserClient : public content::ContentBrowserClient,
211211
const base::Optional<url::Origin>& initiating_origin,
212212
mojo::PendingRemote<network::mojom::URLLoaderFactory>* out_factory)
213213
override;
214+
std::unique_ptr<content::LoginDelegate> CreateLoginDelegate(
215+
const net::AuthChallengeInfo& auth_info,
216+
content::WebContents* web_contents,
217+
const content::GlobalRequestID& request_id,
218+
bool is_main_frame,
219+
const GURL& url,
220+
scoped_refptr<net::HttpResponseHeaders> response_headers,
221+
bool first_auth_attempt,
222+
LoginAuthRequiredCallback auth_required_callback) override;
214223

215224
// content::RenderProcessHostObserver:
216225
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;

shell/browser/browser.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,6 @@ void Browser::OnAccessibilitySupportChanged() {
181181
observer.OnAccessibilitySupportChanged();
182182
}
183183

184-
void Browser::RequestLogin(
185-
scoped_refptr<LoginHandler> login_handler,
186-
std::unique_ptr<base::DictionaryValue> request_details) {
187-
for (BrowserObserver& observer : observers_)
188-
observer.OnLogin(login_handler, *(request_details.get()));
189-
}
190-
191184
void Browser::PreMainMessageLoopRun() {
192185
for (BrowserObserver& observer : observers_) {
193186
observer.OnPreMainMessageLoopRun();

shell/browser/browser.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,6 @@ class Browser : public WindowListObserver {
250250

251251
void OnAccessibilitySupportChanged();
252252

253-
// Request basic auth login.
254-
void RequestLogin(scoped_refptr<LoginHandler> login_handler,
255-
std::unique_ptr<base::DictionaryValue> request_details);
256-
257253
void PreMainMessageLoopRun();
258254

259255
// Stores the supplied |quit_closure|, to be run when the last Browser

0 commit comments

Comments
 (0)