| 
									
										
										
										
											2015-09-30 10:56:42 +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/atom_resource_dispatcher_host_delegate.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-28 15:34:41 +08:00
										 |  |  | #include "atom/browser/login_handler.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-20 22:25:15 +05:30
										 |  |  | #include "atom/browser/web_contents_permission_helper.h"
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:06:53 +05:30
										 |  |  | #include "atom/common/atom_constants.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  | #include "atom/common/platform_util.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-17 22:40:15 +05:30
										 |  |  | #include "base/strings/stringprintf.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:23:54 +05:30
										 |  |  | #include "base/strings/utf_string_conversions.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  | #include "content/public/browser/browser_thread.h"
 | 
					
						
							| 
									
										
										
										
											2017-02-27 11:40:49 +05:30
										 |  |  | #include "content/public/browser/stream_info.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  | #include "net/base/escape.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-06 17:24:37 +09:00
										 |  |  | #include "net/ssl/client_cert_store.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-12 03:33:26 +05:30
										 |  |  | #include "net/url_request/url_request.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  | #include "url/gurl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 17:24:37 +09:00
										 |  |  | #if defined(USE_NSS_CERTS)
 | 
					
						
							|  |  |  | #include "net/ssl/client_cert_store_nss.h"
 | 
					
						
							|  |  |  | #elif defined(OS_WIN)
 | 
					
						
							|  |  |  | #include "net/ssl/client_cert_store_win.h"
 | 
					
						
							|  |  |  | #elif defined(OS_MACOSX)
 | 
					
						
							|  |  |  | #include "net/ssl/client_cert_store_mac.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  | using content::BrowserThread; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace atom { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 22:25:15 +05:30
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void OnOpenExternal(const GURL& escaped_url, | 
					
						
							|  |  |  |                     bool allowed) { | 
					
						
							|  |  |  |   if (allowed) | 
					
						
							| 
									
										
										
										
											2016-07-11 17:11:05 +09:00
										 |  |  |     platform_util::OpenExternal( | 
					
						
							|  |  |  | #if defined(OS_WIN)
 | 
					
						
							|  |  |  |         base::UTF8ToUTF16(escaped_url.spec()), | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |         escaped_url, | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         true); | 
					
						
							| 
									
										
										
										
											2016-04-20 22:25:15 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void HandleExternalProtocolInUI( | 
					
						
							|  |  |  |     const GURL& url, | 
					
						
							|  |  |  |     const content::ResourceRequestInfo::WebContentsGetter& web_contents_getter, | 
					
						
							|  |  |  |     bool has_user_gesture) { | 
					
						
							|  |  |  |   content::WebContents* web_contents = web_contents_getter.Run(); | 
					
						
							|  |  |  |   if (!web_contents) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   auto permission_helper = | 
					
						
							|  |  |  |       WebContentsPermissionHelper::FromWebContents(web_contents); | 
					
						
							| 
									
										
										
										
											2016-06-08 18:04:03 +05:30
										 |  |  |   if (!permission_helper) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GURL escaped_url(net::EscapeExternalHandlerValue(url.spec())); | 
					
						
							|  |  |  |   auto callback = base::Bind(&OnOpenExternal, escaped_url); | 
					
						
							| 
									
										
										
										
											2016-04-20 22:25:15 +05:30
										 |  |  |   permission_helper->RequestOpenExternalPermission(callback, has_user_gesture); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 03:33:26 +05:30
										 |  |  | void OnPdfResourceIntercepted( | 
					
						
							|  |  |  |     const GURL& original_url, | 
					
						
							|  |  |  |     const content::ResourceRequestInfo::WebContentsGetter& | 
					
						
							|  |  |  |         web_contents_getter) { | 
					
						
							| 
									
										
										
										
											2017-01-17 19:53:13 +05:30
										 |  |  |   content::WebContents* web_contents = web_contents_getter.Run(); | 
					
						
							|  |  |  |   if (!web_contents) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-27 11:40:49 +05:30
										 |  |  |   // The URL passes the original pdf resource url, that will be requested
 | 
					
						
							|  |  |  |   // by the webui page.
 | 
					
						
							|  |  |  |   // chrome://pdf-viewer/index.html?src=https://somepage/123.pdf
 | 
					
						
							|  |  |  |   content::NavigationController::LoadURLParams params( | 
					
						
							|  |  |  |       GURL(base::StringPrintf("%sindex.html?%s=%s", kPdfViewerUIOrigin, | 
					
						
							|  |  |  |                               kPdfPluginSrc, original_url.spec().c_str()))); | 
					
						
							| 
									
										
										
										
											2017-01-17 19:53:13 +05:30
										 |  |  |   web_contents->GetController().LoadURLWithParams(params); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 22:25:15 +05:30
										 |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  | AtomResourceDispatcherHostDelegate::AtomResourceDispatcherHostDelegate() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool AtomResourceDispatcherHostDelegate::HandleExternalProtocol( | 
					
						
							|  |  |  |     const GURL& url, | 
					
						
							| 
									
										
										
										
											2017-01-24 12:16:29 +09:00
										 |  |  |     content::ResourceRequestInfo* info) { | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  |   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 
					
						
							| 
									
										
										
										
											2016-04-20 22:25:15 +05:30
										 |  |  |                           base::Bind(&HandleExternalProtocolInUI, | 
					
						
							|  |  |  |                                      url, | 
					
						
							| 
									
										
										
										
											2017-01-24 12:16:29 +09:00
										 |  |  |                                      info->GetWebContentsGetterForRequest(), | 
					
						
							|  |  |  |                                      info->HasUserGesture())); | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-28 15:34:41 +08:00
										 |  |  | content::ResourceDispatcherHostLoginDelegate* | 
					
						
							|  |  |  | AtomResourceDispatcherHostDelegate::CreateLoginDelegate( | 
					
						
							|  |  |  |     net::AuthChallengeInfo* auth_info, | 
					
						
							|  |  |  |     net::URLRequest* request) { | 
					
						
							|  |  |  |   return new LoginHandler(auth_info, request); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 17:24:37 +09:00
										 |  |  | std::unique_ptr<net::ClientCertStore> | 
					
						
							|  |  |  | AtomResourceDispatcherHostDelegate::CreateClientCertStore( | 
					
						
							|  |  |  |     content::ResourceContext* resource_context) { | 
					
						
							|  |  |  |   #if defined(USE_NSS_CERTS)
 | 
					
						
							|  |  |  |     return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreNSS( | 
					
						
							|  |  |  |         net::ClientCertStoreNSS::PasswordDelegateFactory())); | 
					
						
							|  |  |  |   #elif defined(OS_WIN)
 | 
					
						
							|  |  |  |     return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreWin()); | 
					
						
							|  |  |  |   #elif defined(OS_MACOSX)
 | 
					
						
							|  |  |  |     return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreMac()); | 
					
						
							|  |  |  |   #elif defined(USE_OPENSSL)
 | 
					
						
							|  |  |  |     return std::unique_ptr<net::ClientCertStore>(); | 
					
						
							|  |  |  |   #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 19:53:13 +05:30
										 |  |  | bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream( | 
					
						
							|  |  |  |     net::URLRequest* request, | 
					
						
							|  |  |  |     const base::FilePath& plugin_path, | 
					
						
							|  |  |  |     const std::string& mime_type, | 
					
						
							|  |  |  |     GURL* origin, | 
					
						
							|  |  |  |     std::string* payload) { | 
					
						
							| 
									
										
										
										
											2017-03-02 23:11:19 +05:30
										 |  |  |   const content::ResourceRequestInfo* info = | 
					
						
							|  |  |  |       content::ResourceRequestInfo::ForRequest(request); | 
					
						
							|  |  |  |   if (mime_type == "application/pdf" && info->IsMainFrame()) { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:06:53 +05:30
										 |  |  |     *origin = GURL(kPdfViewerUIOrigin); | 
					
						
							| 
									
										
										
										
											2017-03-12 03:33:26 +05:30
										 |  |  |     content::BrowserThread::PostTask( | 
					
						
							|  |  |  |         BrowserThread::UI, FROM_HERE, | 
					
						
							|  |  |  |         base::Bind(&OnPdfResourceIntercepted, request->url(), | 
					
						
							|  |  |  |                    info->GetWebContentsGetterForRequest())); | 
					
						
							| 
									
										
										
										
											2017-01-17 19:53:13 +05:30
										 |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-30 10:56:42 +08:00
										 |  |  | }  // namespace atom
 |