| 
									
										
										
										
											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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 13:46:59 -07:00
										 |  |  | #include "shell/browser/login_handler.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-28 15:34:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 19:25:56 -05:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2019-10-31 16:56:00 +09:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2018-09-12 19:25:56 -05:00
										 |  |  | #include <utility>
 | 
					
						
							| 
									
										
										
										
											2019-10-31 16:56:00 +09:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2018-09-12 19:25:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  | #include "base/callback.h"
 | 
					
						
							|  |  |  | #include "gin/dictionary.h"
 | 
					
						
							|  |  |  | #include "shell/browser/api/atom_api_web_contents.h"
 | 
					
						
							|  |  |  | #include "shell/common/gin_converters/callback_converter.h"
 | 
					
						
							| 
									
										
										
										
											2019-10-31 16:56:00 +09:00
										 |  |  | #include "shell/common/gin_converters/net_converter.h"
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  | #include "shell/common/gin_converters/value_converter.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-28 19:34:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using content::BrowserThread; | 
					
						
							| 
									
										
										
										
											2015-10-28 15:34:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  | namespace electron { | 
					
						
							| 
									
										
										
										
											2015-10-28 15:34:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  | 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)) { | 
					
						
							| 
									
										
										
										
											2015-10-28 19:34:01 +08:00
										 |  |  |   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
					
						
							| 
									
										
										
										
											2018-08-23 15:25:13 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 15:58:00 -04:00
										 |  |  |   base::PostTask( | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  |       FROM_HERE, {base::CurrentThread()}, | 
					
						
							|  |  |  |       base::BindOnce(&LoginHandler::EmitEvent, weak_factory_.GetWeakPtr(), | 
					
						
							|  |  |  |                      auth_info, is_main_frame, url, response_headers, | 
					
						
							|  |  |  |                      first_auth_attempt)); | 
					
						
							| 
									
										
										
										
											2015-10-28 19:34:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  | 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; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-08-23 15:25:13 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  |   v8::HandleScope scope(isolate); | 
					
						
							| 
									
										
										
										
											2015-10-28 19:34:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  |   auto details = gin::Dictionary::CreateEmpty(isolate); | 
					
						
							|  |  |  |   details.Set("url", url.spec()); | 
					
						
							| 
									
										
										
										
											2015-10-28 19:34:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  |   // 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()); | 
					
						
							| 
									
										
										
										
											2015-10-28 21:20:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  |   bool default_prevented = | 
					
						
							|  |  |  |       api_web_contents->Emit("login", std::move(details), auth_info, | 
					
						
							|  |  |  |                              base::BindOnce(&LoginHandler::CallbackFromJS, | 
					
						
							|  |  |  |                                             weak_factory_.GetWeakPtr())); | 
					
						
							|  |  |  |   if (!default_prevented) { | 
					
						
							|  |  |  |     std::move(auth_required_callback_).Run(base::nullopt); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-28 19:34:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 09:47:01 -08:00
										 |  |  | LoginHandler::~LoginHandler() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LoginHandler::CallbackFromJS(base::string16 username, | 
					
						
							|  |  |  |                                   base::string16 password) { | 
					
						
							|  |  |  |   std::move(auth_required_callback_) | 
					
						
							|  |  |  |       .Run(net::AuthCredentials(username, password)); | 
					
						
							| 
									
										
										
										
											2015-10-28 15:34:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 |  |  | }  // namespace electron
 |