From aa6d76922942314e8e4aee9071da55580f42b055 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Sun, 22 Jul 2018 01:05:21 +1000 Subject: [PATCH] Handle external protocols with the network service. https://chromium-review.googlesource.com/c/chromium/src/+/956091 --- atom/browser/atom_browser_client.cc | 47 +++++++++++++++++++ atom/browser/atom_browser_client.h | 8 ++++ .../atom_resource_dispatcher_host_delegate.cc | 42 ----------------- .../atom_resource_dispatcher_host_delegate.h | 2 - 4 files changed, 55 insertions(+), 44 deletions(-) diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index 77af55eaa53c..48d5cbf46204 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -24,6 +24,7 @@ #include "atom/browser/window_list.h" #include "atom/common/google_api_key.h" #include "atom/common/options_switches.h" +#include "atom/common/platform_util.h" #include "base/command_line.h" #include "base/environment.h" #include "base/files/file_util.h" @@ -45,6 +46,7 @@ #include "content/public/common/url_constants.h" #include "content/public/common/web_preferences.h" #include "device/geolocation/public/cpp/location_provider.h" +#include "net/base/escape.h" #include "net/ssl/ssl_cert_request_info.h" #include "ppapi/host/ppapi_host.h" #include "services/network/public/cpp/resource_request_body.h" @@ -561,4 +563,49 @@ void AtomBrowserClient::RenderProcessExited(content::RenderProcessHost* host, } } +void OnOpenExternal(const GURL& escaped_url, bool allowed) { + if (allowed) + platform_util::OpenExternal( +#if defined(OS_WIN) + base::UTF8ToUTF16(escaped_url.spec()), +#else + escaped_url, +#endif + true); +} + +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); + if (!permission_helper) + return; + + GURL escaped_url(net::EscapeExternalHandlerValue(url.spec())); + auto callback = base::Bind(&OnOpenExternal, escaped_url); + permission_helper->RequestOpenExternalPermission(callback, has_user_gesture, + url); +} + +bool AtomBrowserClient::HandleExternalProtocol( + const GURL& url, + content::ResourceRequestInfo::WebContentsGetter web_contents_getter, + int child_id, + content::NavigationUIData* navigation_data, + bool is_main_frame, + ui::PageTransition page_transition, + bool has_user_gesture) { + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, + base::BindOnce(&HandleExternalProtocolInUI, url, web_contents_getter, + has_user_gesture)); + return true; +} + } // namespace atom diff --git a/atom/browser/atom_browser_client.h b/atom/browser/atom_browser_client.h index 95211d72e51f..82977292237f 100644 --- a/atom/browser/atom_browser_client.h +++ b/atom/browser/atom_browser_client.h @@ -122,6 +122,14 @@ class AtomBrowserClient : public brightray::BrowserClient, void RenderProcessExited(content::RenderProcessHost* host, base::TerminationStatus status, int exit_code) override; + bool HandleExternalProtocol( + const GURL& url, + content::ResourceRequestInfo::WebContentsGetter web_contents_getter, + int child_id, + content::NavigationUIData* navigation_data, + bool is_main_frame, + ui::PageTransition page_transition, + bool has_user_gesture) override; private: struct ProcessPreferences { diff --git a/atom/browser/atom_resource_dispatcher_host_delegate.cc b/atom/browser/atom_resource_dispatcher_host_delegate.cc index 934b0ebaa49b..0a59a0dc8f4b 100644 --- a/atom/browser/atom_resource_dispatcher_host_delegate.cc +++ b/atom/browser/atom_resource_dispatcher_host_delegate.cc @@ -5,9 +5,7 @@ #include "atom/browser/atom_resource_dispatcher_host_delegate.h" #include "atom/browser/atom_browser_context.h" -#include "atom/browser/web_contents_permission_helper.h" #include "atom/browser/web_contents_preferences.h" -#include "atom/common/platform_util.h" #include "base/strings/utf_string_conversions.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_manager.h" @@ -28,36 +26,6 @@ namespace atom { namespace { -void OnOpenExternal(const GURL& escaped_url, bool allowed) { - if (allowed) - platform_util::OpenExternal( -#if defined(OS_WIN) - base::UTF8ToUTF16(escaped_url.spec()), -#else - escaped_url, -#endif - true); -} - -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); - if (!permission_helper) - return; - - GURL escaped_url(net::EscapeExternalHandlerValue(url.spec())); - auto callback = base::Bind(&OnOpenExternal, escaped_url); - permission_helper->RequestOpenExternalPermission(callback, has_user_gesture, - url); -} - #if defined(ENABLE_PDF_VIEWER) void OnPdfResourceIntercepted( const GURL& original_url, @@ -103,16 +71,6 @@ void OnPdfResourceIntercepted( AtomResourceDispatcherHostDelegate::AtomResourceDispatcherHostDelegate() {} -bool AtomResourceDispatcherHostDelegate::HandleExternalProtocol( - const GURL& url, - content::ResourceRequestInfo* info) { - BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, - base::BindOnce(&HandleExternalProtocolInUI, url, - info->GetWebContentsGetterForRequest(), - info->HasUserGesture())); - return true; -} - bool AtomResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream( net::URLRequest* request, const std::string& mime_type, diff --git a/atom/browser/atom_resource_dispatcher_host_delegate.h b/atom/browser/atom_resource_dispatcher_host_delegate.h index eb91b27921e2..252cfa39e9df 100644 --- a/atom/browser/atom_resource_dispatcher_host_delegate.h +++ b/atom/browser/atom_resource_dispatcher_host_delegate.h @@ -17,8 +17,6 @@ class AtomResourceDispatcherHostDelegate AtomResourceDispatcherHostDelegate(); // content::ResourceDispatcherHostDelegate: - bool HandleExternalProtocol(const GURL& url, - content::ResourceRequestInfo* info) override; bool ShouldInterceptResourceAsStream(net::URLRequest* request, const std::string& mime_type, GURL* origin,