Commit 0ce6a13d authored by dpapad's avatar dpapad Committed by Commit Bot

Stop registering content::GenericHandler blindly for all WebUIs.

 - Move GenericHandler from content/ to chrome/
 - Rename to NavigationHandler
 - Stop registering it blindly for all WebUI pages within WebContentsImpl.
 - Register it explicitly in HistoryUI, the only currently known usage of
   it.
 - Change util.js so that a call to listenForPrivilegedLinkClicks() is
   required to trigger that functionality. Previously listeners were added
   as soon as util.js was evaluated.

Note there are probably a few more places that use NavigationHandler to be
registered. The goal of this CL is to narrow down who needs this functionality
and explicitly add it there. So some temporary regressions are expected.

Bug: 1020360
Change-Id: I2e2f5a0fe06cb8268a38380f29547afa94e7450c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1894911
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: default avatarDan Beam <dbeam@chromium.org>
Reviewed-by: default avatarNasko Oskov <nasko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712251}
parent 75a36e67
...@@ -111,6 +111,11 @@ Polymer({ ...@@ -111,6 +111,11 @@ Polymer({
/** @private {?function(!Event)} */ /** @private {?function(!Event)} */
boundOnCommand_: null, boundOnCommand_: null,
/** @override */
created: function() {
listenForPrivilegedLinkClicks();
},
/** @override */ /** @override */
attached: function() { attached: function() {
cr.ui.decorate('command', cr.ui.Command); cr.ui.decorate('command', cr.ui.Command);
......
...@@ -261,6 +261,8 @@ jumbo_static_library("ui") { ...@@ -261,6 +261,8 @@ jumbo_static_library("ui") {
"webui/memory_internals_ui.h", "webui/memory_internals_ui.h",
"webui/metrics_handler.cc", "webui/metrics_handler.cc",
"webui/metrics_handler.h", "webui/metrics_handler.h",
"webui/navigation_handler.cc",
"webui/navigation_handler.h",
"webui/net_export_ui.cc", "webui/net_export_ui.cc",
"webui/net_export_ui.h", "webui/net_export_ui.h",
"webui/net_internals/net_internals_ui.cc", "webui/net_internals/net_internals_ui.cc",
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "chrome/browser/ui/webui/localized_string.h" #include "chrome/browser/ui/webui/localized_string.h"
#include "chrome/browser/ui/webui/managed_ui_handler.h" #include "chrome/browser/ui/webui/managed_ui_handler.h"
#include "chrome/browser/ui/webui/metrics_handler.h" #include "chrome/browser/ui/webui/metrics_handler.h"
#include "chrome/browser/ui/webui/navigation_handler.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/grit/browser_resources.h" #include "chrome/grit/browser_resources.h"
...@@ -181,6 +182,7 @@ HistoryUI::HistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { ...@@ -181,6 +182,7 @@ HistoryUI::HistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) {
ManagedUIHandler::Initialize(web_ui, data_source); ManagedUIHandler::Initialize(web_ui, data_source);
content::WebUIDataSource::Add(profile, data_source); content::WebUIDataSource::Add(profile, data_source);
web_ui->AddMessageHandler(std::make_unique<webui::NavigationHandler>());
web_ui->AddMessageHandler(std::make_unique<BrowsingHistoryHandler>()); web_ui->AddMessageHandler(std::make_unique<BrowsingHistoryHandler>());
web_ui->AddMessageHandler(std::make_unique<MetricsHandler>()); web_ui->AddMessageHandler(std::make_unique<MetricsHandler>());
......
...@@ -2,31 +2,30 @@ ...@@ -2,31 +2,30 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "content/browser/webui/generic_handler.h" #include "chrome/browser/ui/webui/navigation_handler.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/values.h" #include "base/values.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "ui/base/window_open_disposition.h" #include "ui/base/window_open_disposition.h"
namespace content { namespace webui {
GenericHandler::GenericHandler() { NavigationHandler::NavigationHandler() {}
}
GenericHandler::~GenericHandler() { NavigationHandler::~NavigationHandler() {}
}
void GenericHandler::RegisterMessages() { void NavigationHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback( web_ui()->RegisterMessageCallback(
"navigateToUrl", base::BindRepeating(&GenericHandler::HandleNavigateToUrl, "navigateToUrl",
base::BindRepeating(&NavigationHandler::HandleNavigateToUrl,
base::Unretained(this))); base::Unretained(this)));
} }
void GenericHandler::HandleNavigateToUrl(const base::ListValue* args) { void NavigationHandler::HandleNavigateToUrl(const base::ListValue* args) {
std::string url_string; std::string url_string;
std::string target_string; std::string target_string;
double button; double button;
...@@ -52,13 +51,11 @@ void GenericHandler::HandleNavigateToUrl(const base::ListValue* args) { ...@@ -52,13 +51,11 @@ void GenericHandler::HandleNavigateToUrl(const base::ListValue* args) {
target_string == "_blank") target_string == "_blank")
disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
web_ui()->GetWebContents()->OpenURL(OpenURLParams(GURL(url_string), web_ui()->GetWebContents()->OpenURL(
Referrer(), content::OpenURLParams(GURL(url_string), content::Referrer(), disposition,
disposition, ui::PAGE_TRANSITION_LINK, false));
ui::PAGE_TRANSITION_LINK,
false));
// This may delete us! // This may delete us!
} }
} // namespace content } // namespace webui
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CONTENT_BROWSER_WEBUI_GENERIC_HANDLER_H_ #ifndef CHROME_BROWSER_UI_WEBUI_NAVIGATION_HANDLER_H_
#define CONTENT_BROWSER_WEBUI_GENERIC_HANDLER_H_ #define CHROME_BROWSER_UI_WEBUI_NAVIGATION_HANDLER_H_
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -13,13 +13,13 @@ namespace base { ...@@ -13,13 +13,13 @@ namespace base {
class ListValue; class ListValue;
} }
namespace content { namespace webui {
// A place to add handlers for messages shared across all WebUI pages. // A place to add handlers for messages shared across all WebUI pages.
class GenericHandler : public WebUIMessageHandler { class NavigationHandler : public content::WebUIMessageHandler {
public: public:
GenericHandler(); NavigationHandler();
~GenericHandler() override; ~NavigationHandler() override;
// WebUIMessageHandler implementation. // WebUIMessageHandler implementation.
void RegisterMessages() override; void RegisterMessages() override;
...@@ -27,9 +27,9 @@ class GenericHandler : public WebUIMessageHandler { ...@@ -27,9 +27,9 @@ class GenericHandler : public WebUIMessageHandler {
private: private:
void HandleNavigateToUrl(const base::ListValue* args); void HandleNavigateToUrl(const base::ListValue* args);
DISALLOW_COPY_AND_ASSIGN(GenericHandler); DISALLOW_COPY_AND_ASSIGN(NavigationHandler);
}; };
} // namespace content } // namespace webui
#endif // CONTENT_BROWSER_WEBUI_GENERIC_HANDLER_H_ #endif // CHROME_BROWSER_UI_WEBUI_NAVIGATION_HANDLER_H_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import {$, quoteString} from 'chrome://resources/js/util.m.js'; import {$, listenForPrivilegedLinkClicks, quoteString} from 'chrome://resources/js/util.m.js';
suite('UtilModuleTest', function() { suite('UtilModuleTest', function() {
test('quote string', function() { test('quote string', function() {
...@@ -24,6 +24,7 @@ suite('UtilModuleTest', function() { ...@@ -24,6 +24,7 @@ suite('UtilModuleTest', function() {
}); });
test('click handler', function() { test('click handler', function() {
listenForPrivilegedLinkClicks();
document.body.innerHTML = ` document.body.innerHTML = `
<a id="file" href="file:///path/to/file">File</a> <a id="file" href="file:///path/to/file">File</a>
<a id="chrome" href="about:chrome">Chrome</a> <a id="chrome" href="about:chrome">Chrome</a>
......
...@@ -24,6 +24,7 @@ function testQuoteString() { ...@@ -24,6 +24,7 @@ function testQuoteString() {
} }
function testClickHandler() { function testClickHandler() {
listenForPrivilegedLinkClicks();
var clickArgs = null; var clickArgs = null;
var oldSend = chrome.send; var oldSend = chrome.send;
chrome.send = function(message, args) { chrome.send = function(message, args) {
......
...@@ -1922,8 +1922,6 @@ jumbo_source_set("browser") { ...@@ -1922,8 +1922,6 @@ jumbo_source_set("browser") {
"websockets/websocket_handshake_request_info_impl.h", "websockets/websocket_handshake_request_info_impl.h",
"webui/content_web_ui_controller_factory.cc", "webui/content_web_ui_controller_factory.cc",
"webui/content_web_ui_controller_factory.h", "webui/content_web_ui_controller_factory.h",
"webui/generic_handler.cc",
"webui/generic_handler.h",
"webui/network_error_url_loader.cc", "webui/network_error_url_loader.cc",
"webui/network_error_url_loader.h", "webui/network_error_url_loader.h",
"webui/shared_resources_data_source.cc", "webui/shared_resources_data_source.cc",
......
...@@ -86,7 +86,6 @@ ...@@ -86,7 +86,6 @@
#include "content/browser/web_contents/javascript_dialog_navigation_deferrer.h" #include "content/browser/web_contents/javascript_dialog_navigation_deferrer.h"
#include "content/browser/web_contents/web_contents_view_child_frame.h" #include "content/browser/web_contents/web_contents_view_child_frame.h"
#include "content/browser/web_contents/web_contents_view_guest.h" #include "content/browser/web_contents/web_contents_view_guest.h"
#include "content/browser/webui/generic_handler.h"
#include "content/browser/webui/web_ui_controller_factory_registry.h" #include "content/browser/webui/web_ui_controller_factory_registry.h"
#include "content/browser/webui/web_ui_impl.h" #include "content/browser/webui/web_ui_impl.h"
#include "content/common/browser_plugin/browser_plugin_constants.h" #include "content/common/browser_plugin/browser_plugin_constants.h"
...@@ -6822,7 +6821,6 @@ std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) { ...@@ -6822,7 +6821,6 @@ std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) {
WebUIControllerFactoryRegistry::GetInstance() WebUIControllerFactoryRegistry::GetInstance()
->CreateWebUIControllerForURL(web_ui.get(), url)); ->CreateWebUIControllerForURL(web_ui.get(), url));
if (controller) { if (controller) {
web_ui->AddMessageHandler(std::make_unique<GenericHandler>());
web_ui->SetController(std::move(controller)); web_ui->SetController(std::move(controller));
return web_ui; return web_ui;
} }
......
...@@ -155,9 +155,13 @@ ...@@ -155,9 +155,13 @@
element, HTMLElement, 'Missing required element: ' + selectors); element, HTMLElement, 'Missing required element: ' + selectors);
} }
// Handle click on a link. If the link points to a chrome: or file: url, then // Adds click/auxclick listeners for any link on the page. If the link points to
// call into the browser to do the navigation. // a chrome: or file: url, then calls into the browser to do the navigation.
['click', 'auxclick'].forEach(function(eventName) { // Note: This method is *not* re-entrant. Every call to it, will re-add
// listeners on |document|. It's up to callers to ensure this is only called
// once.
/* #export */ function listenForPrivilegedLinkClicks() {
['click', 'auxclick'].forEach(function(eventName) {
document.addEventListener(eventName, function(e) { document.addEventListener(eventName, function(e) {
if (e.button > 1) { if (e.button > 1) {
return; return;
...@@ -202,7 +206,8 @@ ...@@ -202,7 +206,8 @@
e.preventDefault(); e.preventDefault();
} }
}); });
}); });
}
/** /**
* Creates a new URL which is the old URL with a GET param of key=value. * Creates a new URL which is the old URL with a GET param of key=value.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment