From 8f5fdc1f9adb0aa81f9db4c3f0d74d08d554ccc5 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Mon, 15 Feb 2021 18:24:28 -0500 Subject: [PATCH] chrome: Add CEF-specific WebUI handling (see issue #2969) To avoid conflicting IDs between Alloy (which uses cef.pak) and Chrome (which uses chrome_100_percent.pak) the cef/LICENSE.txt file is now included in both cef/libcef/resources/cef_resources.grd and chrome/app/theme/chrome_unscaled_resources.grd with different ID values. The cef.pak file currently contains both CEF-specific resources and Chrome resources that are already included in the default *.pak files distributed with Chrome. In the future we should remove this duplication and just distribute the same *.pak files as Chrome for the majority of resources. --- .../chrome_browser_main_extra_parts_cef.cc | 3 ++ .../chrome_content_browser_client_cef.cc | 12 ++++++++ .../chrome_content_browser_client_cef.h | 2 ++ libcef/browser/net/chrome_scheme_handler.cc | 30 +++++++++++++++++-- patch/patches/chrome_runtime.patch | 12 ++++++++ 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/libcef/browser/chrome/chrome_browser_main_extra_parts_cef.cc b/libcef/browser/chrome/chrome_browser_main_extra_parts_cef.cc index 2ad9760f6..bd54e5730 100644 --- a/libcef/browser/chrome/chrome_browser_main_extra_parts_cef.cc +++ b/libcef/browser/chrome/chrome_browser_main_extra_parts_cef.cc @@ -5,6 +5,7 @@ #include "libcef/browser/chrome/chrome_browser_main_extra_parts_cef.h" #include "libcef/browser/context.h" +#include "libcef/browser/net/chrome_scheme_handler.h" #include "base/task/post_task.h" @@ -31,4 +32,6 @@ void ChromeBrowserMainExtraPartsCef::PreMainMessageLoopRun() { user_blocking_task_runner_ = base::CreateSingleThreadTaskRunner( {base::ThreadPool(), base::TaskPriority::USER_BLOCKING, base::TaskShutdownBehavior::BLOCK_SHUTDOWN, base::MayBlock()}); + + scheme::RegisterWebUIControllerFactory(); } diff --git a/libcef/browser/chrome/chrome_content_browser_client_cef.cc b/libcef/browser/chrome/chrome_content_browser_client_cef.cc index 25242c587..2b6bfc989 100644 --- a/libcef/browser/chrome/chrome_content_browser_client_cef.cc +++ b/libcef/browser/chrome/chrome_content_browser_client_cef.cc @@ -8,6 +8,7 @@ #include "libcef/browser/browser_info_manager.h" #include "libcef/browser/browser_message_filter.h" #include "libcef/browser/chrome/chrome_browser_main_extra_parts_cef.h" +#include "libcef/browser/net/chrome_scheme_handler.h" #include "libcef/browser/net/throttle_handler.h" #include "libcef/browser/net_service/cookie_manager_impl.h" #include "libcef/browser/net_service/login_delegate.h" @@ -283,6 +284,17 @@ ChromeContentBrowserClientCef::CreateLoginDelegate( response_headers, first_auth_attempt, std::move(auth_required_callback)); } +void ChromeContentBrowserClientCef::BrowserURLHandlerCreated( + content::BrowserURLHandler* handler) { + scheme::BrowserURLHandlerCreated(handler); + ChromeContentBrowserClient::BrowserURLHandlerCreated(handler); +} + +bool ChromeContentBrowserClientCef::IsWebUIAllowedToMakeNetworkRequests( + const url::Origin& origin) { + return scheme::IsWebUIAllowedToMakeNetworkRequests(origin); +} + CefRefPtr ChromeContentBrowserClientCef::request_context() const { return browser_main_parts_->request_context(); diff --git a/libcef/browser/chrome/chrome_content_browser_client_cef.h b/libcef/browser/chrome/chrome_content_browser_client_cef.h index 20b66da2b..ce013666a 100644 --- a/libcef/browser/chrome/chrome_content_browser_client_cef.h +++ b/libcef/browser/chrome/chrome_content_browser_client_cef.h @@ -91,6 +91,8 @@ class ChromeContentBrowserClientCef : public ChromeContentBrowserClient { scoped_refptr response_headers, bool first_auth_attempt, LoginAuthRequiredCallback auth_required_callback) override; + void BrowserURLHandlerCreated(content::BrowserURLHandler* handler) override; + bool IsWebUIAllowedToMakeNetworkRequests(const url::Origin& origin) override; CefRefPtr request_context() const; diff --git a/libcef/browser/net/chrome_scheme_handler.cc b/libcef/browser/net/chrome_scheme_handler.cc index 388353f45..c2bd7c042 100644 --- a/libcef/browser/net/chrome_scheme_handler.cc +++ b/libcef/browser/net/chrome_scheme_handler.cc @@ -17,6 +17,7 @@ #include "libcef/browser/net/internal_scheme_handler.h" #include "libcef/browser/thread_util.h" #include "libcef/common/app_manager.h" +#include "libcef/features/runtime.h" #include "base/command_line.h" #include "base/files/file_util.h" @@ -34,6 +35,7 @@ #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" #include "chrome/browser/ui/webui/theme_source.h" #include "chrome/common/url_constants.h" +#include "chrome/grit/chrome_unscaled_resources.h" #include "content/browser/renderer_host/debug_urls.h" #include "content/browser/webui/content_web_ui_controller_factory.h" #include "content/public/browser/browser_url_handler.h" @@ -164,6 +166,10 @@ bool IsUnlistedHost(const std::string& host) { // Returns true if a host is WebUI and should be allowed to load. bool IsAllowedWebUIHost(const std::string& host) { + // Chrome runtime allows all WebUI hosts. + if (cef::IsChromeRuntimeEnabled()) + return true; + // Explicitly whitelisted WebUI hosts. for (size_t i = 0; i < sizeof(kAllowedWebUIHosts) / sizeof(kAllowedWebUIHosts[0]); ++i) { @@ -285,6 +291,18 @@ class TemplateParser { }; bool OnExtensionsSupportUI(std::string* mime_type, std::string* output) { + *mime_type = "text/html"; + + if (cef::IsChromeRuntimeEnabled()) { + // Redirect to the Chrome documentation. + *output = + "\n" + "\n" + "\n"; + return true; + } + static const char kDevURL[] = "https://developer.chrome.com/extensions/"; std::string html = @@ -331,16 +349,22 @@ bool OnExtensionsSupportUI(std::string* mime_type, std::string* output) { html += "\n\n"; - *mime_type = "text/html"; *output = html; - return true; } bool OnLicenseUI(std::string* mime_type, std::string* output) { + // TODO(chrome): Currently, CEF-specific resources for the Alloy runtime come + // from cef/libcef/resources/cef_resources.grd via cef.pak and CEF-specific + // resources for the Chrome runtime come from + // chrome/app/theme/chrome_unscaled_resources.grd via chrome_100_percent.pak. + // It would be better to have a single pak file (and single ID value) for + // CEF-specific resources and share the same *.pak files as Chrome for the + // other resources. std::string piece = ui::ResourceBundle::GetSharedInstance().LoadDataResourceString( - IDR_CEF_LICENSE_TXT); + cef::IsChromeRuntimeEnabled() ? IDR_CHROME_CEF_LICENSE_TXT + : IDR_CEF_LICENSE_TXT); if (piece.empty()) { NOTREACHED() << "Failed to load license txt resource."; return false; diff --git a/patch/patches/chrome_runtime.patch b/patch/patches/chrome_runtime.patch index aecd11fa1..a97e02a14 100644 --- a/patch/patches/chrome_runtime.patch +++ b/patch/patches/chrome_runtime.patch @@ -77,6 +77,18 @@ index db85b6b2fcf5..bcf6867e8f4e 100644 } #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) +diff --git chrome/app/theme/chrome_unscaled_resources.grd chrome/app/theme/chrome_unscaled_resources.grd +index b4e4545e3520..29335f1614e2 100644 +--- chrome/app/theme/chrome_unscaled_resources.grd ++++ chrome/app/theme/chrome_unscaled_resources.grd +@@ -62,6 +62,7 @@ + + + ++ + + + diff --git chrome/browser/chrome_browser_main.cc chrome/browser/chrome_browser_main.cc index fee4d5f1a8ca..2e4fb506d851 100644 --- chrome/browser/chrome_browser_main.cc