Commit df743f35 authored by Joel Hockey's avatar Joel Hockey Committed by Commit Bot

Convert IPC ChromeViewMsg_WebUIJavaScript to mojo

WebUI preload JS handling is only done on main frame, so it
makes more sense to be moved from ChromeRenderViewObserver to
ChromeRenderFrameObserver where it can also access binding
registry.

Using Associated Interface since tests were failing / flaky
without it, showing errors such as:
* encountered javascript console error(s):
* Uncaught ReferenceError: runTest is not defined
Calls to this mojo method that preloads javascript libraries must
be executed prior to the call to run JS tests.

Bug: 577685
Change-Id: I431158867f60eb40824cbdf9db7d5d5681cbfe5b
Reviewed-on: https://chromium-review.googlesource.com/595019Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491617}
parent fc79b092
......@@ -25,6 +25,7 @@
"chrome::mojom::InsecureContentRenderer",
"chrome::mojom::PhishingDetector",
"chrome::mojom::ThumbnailCapturer",
"chrome::mojom::WebUITester",
"contextual_search::mojom::OverlayPageNotifierService",
"dom_distiller::mojom::DistillerPageNotifierService",
"spellcheck::mojom::SpellCheckPanel"
......
......@@ -11,6 +11,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/web_ui_tester.mojom.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_types.h"
......@@ -18,6 +19,7 @@
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/common/associated_interface_provider.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -34,8 +36,10 @@ WebUITestHandler::WebUITestHandler()
void WebUITestHandler::PreloadJavaScript(const base::string16& js_text,
RenderViewHost* preload_host) {
DCHECK(preload_host);
preload_host->Send(new ChromeViewMsg_WebUIJavaScript(
preload_host->GetRoutingID(), js_text));
chrome::mojom::WebUITesterAssociatedPtr web_ui_tester;
preload_host->GetMainFrame()->GetRemoteAssociatedInterfaces()->GetInterface(
&web_ui_tester);
web_ui_tester->ExecuteWebUIJavaScript(js_text);
}
void WebUITestHandler::RunJavaScript(const base::string16& js_text) {
......
......@@ -703,6 +703,10 @@ mojom("mojo_bindings") {
]
}
if (!is_android) {
sources += [ "web_ui_tester.mojom" ]
}
public_deps = [
"//components/content_settings/core/common:mojo_bindings",
"//mojo/common:common_custom_types",
......
......@@ -114,14 +114,6 @@ IPC_STRUCT_TRAITS_END()
// RenderView messages
// These are messages sent from the browser to the renderer process.
#if !defined(OS_ANDROID)
// For WebUI testing, this message requests JavaScript to be executed at a time
// which is late enough to not be thrown out, and early enough to be before
// onload events are fired.
IPC_MESSAGE_ROUTED1(ChromeViewMsg_WebUIJavaScript,
base::string16 /* javascript */)
#endif
// Tells the render frame to load all blocked plugins with the given identifier.
IPC_MESSAGE_ROUTED1(ChromeViewMsg_LoadBlockedPlugins,
std::string /* identifier */)
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
module chrome.mojom;
import "mojo/common/string16.mojom";
interface WebUITester {
// For WebUI testing, this message requests JavaScript to be executed at a
// time which is late enough to not be thrown out, and early enough to be
// before onload events are fired.
ExecuteWebUIJavaScript(mojo.common.mojom.String16 javascript);
};
......@@ -28,6 +28,8 @@
#include "chrome/renderer/web_apps.h"
#include "components/translate/content/renderer/translate_helper.h"
#include "content/public/common/associated_interface_provider.h"
#include "content/public/common/associated_interface_registry.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_view.h"
#include "extensions/common/constants.h"
......@@ -141,6 +143,11 @@ ChromeRenderFrameObserver::ChromeRenderFrameObserver(
*base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kDisableClientSidePhishingDetection))
SetClientSidePhishingDetection(true);
#endif
#if !defined(OS_ANDROID)
render_frame->GetAssociatedInterfaceRegistry()->AddInterface(
base::Bind(&ChromeRenderFrameObserver::OnWebUITesterRequest,
base::Unretained(this)));
#endif
translate_helper_ = new translate::TranslateHelper(
render_frame, chrome::ISOLATED_WORLD_ID_TRANSLATE,
......@@ -313,6 +320,13 @@ void ChromeRenderFrameObserver::SetClientSidePhishingDetection(
}
#endif
#if !defined(OS_ANDROID)
void ChromeRenderFrameObserver::ExecuteWebUIJavaScript(
const base::string16& javascript) {
webui_javascript_.push_back(javascript);
}
#endif
void ChromeRenderFrameObserver::DidFinishLoad() {
WebLocalFrame* frame = render_frame()->GetWebFrame();
// Don't do anything for subframes.
......@@ -352,6 +366,15 @@ void ChromeRenderFrameObserver::DidCommitProvisionalLoad(
base::debug::SetCrashKeyValue(
crash_keys::kViewCount,
base::SizeTToString(content::RenderView::GetRenderViewCount()));
#if !defined(OS_ANDROID)
if ((render_frame()->GetEnabledBindings() &
content::BINDINGS_POLICY_WEB_UI)) {
for (const auto& script : webui_javascript_)
render_frame()->ExecuteJavaScript(script);
webui_javascript_.clear();
}
#endif
}
void ChromeRenderFrameObserver::CapturePageText(TextCaptureType capture_type) {
......@@ -439,6 +462,13 @@ void ChromeRenderFrameObserver::OnPhishingDetectorRequest(
}
#endif
#if !defined(OS_ANDROID)
void ChromeRenderFrameObserver::OnWebUITesterRequest(
chrome::mojom::WebUITesterAssociatedRequest request) {
web_ui_tester_bindings_.AddBinding(this, std::move(request));
}
#endif
void ChromeRenderFrameObserver::OnThumbnailCapturerRequest(
chrome::mojom::ThumbnailCapturerRequest request) {
thumbnail_capturer_bindings_.AddBinding(this, std::move(request));
......
......@@ -7,6 +7,7 @@
#include "base/macros.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "chrome/common/image_context_menu_renderer.mojom.h"
#include "chrome/common/prerender_types.h"
#include "chrome/common/thumbnail_capturer.mojom.h"
......@@ -18,6 +19,11 @@
#include "chrome/common/safe_browsing/phishing_detector.mojom.h"
#endif
#if !defined(OS_ANDROID)
#include "chrome/common/web_ui_tester.mojom.h"
#include "mojo/public/cpp/bindings/associated_binding_set.h"
#endif
namespace gfx {
class Size;
}
......@@ -37,6 +43,9 @@ class ChromeRenderFrameObserver
public chrome::mojom::ImageContextMenuRenderer,
#if defined(SAFE_BROWSING_CSD)
public chrome::mojom::PhishingDetector,
#endif
#if !defined(OS_ANDROID)
public chrome::mojom::WebUITester,
#endif
public chrome::mojom::ThumbnailCapturer {
public:
......@@ -68,6 +77,11 @@ class ChromeRenderFrameObserver
void SetClientSidePhishingDetection(bool enable_phishing_detection) override;
#endif
#if !defined(OS_ANDROID)
// chrome::mojom::WebUITester:
void ExecuteWebUIJavaScript(const base::string16& javascript) override;
#endif
// chrome::mojom::ThumbnailCapturer:
void RequestThumbnailForContextNode(
int32_t thumbnail_min_area_pixels,
......@@ -81,6 +95,10 @@ class ChromeRenderFrameObserver
#if defined(SAFE_BROWSING_CSD)
void OnPhishingDetectorRequest(
chrome::mojom::PhishingDetectorRequest request);
#endif
#if !defined(OS_ANDROID)
void OnWebUITesterRequest(
chrome::mojom::WebUITesterAssociatedRequest request);
#endif
void OnThumbnailCapturerRequest(
chrome::mojom::ThumbnailCapturerRequest request);
......@@ -116,6 +134,13 @@ class ChromeRenderFrameObserver
mojo::BindingSet<chrome::mojom::PhishingDetector> phishing_detector_bindings_;
#endif
#if !defined(OS_ANDROID)
// Save the JavaScript to preload if ExecuteWebUIJavaScript is invoked.
std::vector<base::string16> webui_javascript_;
mojo::AssociatedBindingSet<chrome::mojom::WebUITester>
web_ui_tester_bindings_;
#endif
mojo::BindingSet<chrome::mojom::ThumbnailCapturer>
thumbnail_capturer_bindings_;
......
......@@ -52,9 +52,6 @@ ChromeRenderViewObserver::~ChromeRenderViewObserver() {
bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message)
#if !defined(OS_ANDROID)
IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript)
#endif
#if defined(OS_ANDROID)
IPC_MESSAGE_HANDLER(ChromeViewMsg_UpdateBrowserControlsState,
OnUpdateBrowserControlsState)
......@@ -66,13 +63,6 @@ bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
return handled;
}
#if !defined(OS_ANDROID)
void ChromeRenderViewObserver::OnWebUIJavaScript(
const base::string16& javascript) {
webui_javascript_.push_back(javascript);
}
#endif
#if defined(OS_ANDROID)
void ChromeRenderViewObserver::OnUpdateBrowserControlsState(
content::BrowserControlsState constraints,
......@@ -95,19 +85,6 @@ void ChromeRenderViewObserver::Navigate(const GURL& url) {
web_cache_impl_->ExecutePendingClearCache();
}
void ChromeRenderViewObserver::DidCommitProvisionalLoad(
blink::WebLocalFrame* frame,
bool is_new_navigation) {
auto* render_frame = content::RenderFrame::FromWebFrame(frame);
if (render_frame->IsMainFrame() &&
(render_frame->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI) &&
!webui_javascript_.empty()) {
for (const auto& script : webui_javascript_)
render_view()->GetMainRenderFrame()->ExecuteJavaScript(script);
webui_javascript_.clear();
}
}
void ChromeRenderViewObserver::OnDestruct() {
delete this;
}
......@@ -35,14 +35,9 @@ class ChromeRenderViewObserver : public content::RenderViewObserver {
private:
// RenderViewObserver implementation.
bool OnMessageReceived(const IPC::Message& message) override;
void DidCommitProvisionalLoad(blink::WebLocalFrame* frame,
bool is_new_navigation) override;
void Navigate(const GURL& url) override;
void OnDestruct() override;
#if !defined(OS_ANDROID)
void OnWebUIJavaScript(const base::string16& javascript);
#endif
#if defined(OS_ANDROID)
void OnUpdateBrowserControlsState(content::BrowserControlsState constraints,
content::BrowserControlsState current,
......@@ -53,9 +48,6 @@ class ChromeRenderViewObserver : public content::RenderViewObserver {
// Determines if a host is in the strict security host set.
bool IsStrictSecurityHost(const std::string& host);
// Save the JavaScript to preload if a ViewMsg_WebUIJavaScript is received.
std::vector<base::string16> webui_javascript_;
// Owned by ChromeContentRendererClient and outlive us.
web_cache::WebCacheImpl* web_cache_impl_;
......
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