Convert ViewHostMsg_WebUISend so that it is sent via frame (not view).

Bug: 683418, 666525
Change-Id: Id8bf3fa0a69b783b73a075a54ff7154e6d4c7a1e
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_site_isolation
Reviewed-on: https://chromium-review.googlesource.com/701415
Reviewed-by: Nick Carter <nick@chromium.org>
Reviewed-by: Rachel Blum <groby@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Ɓukasz Anforowicz <lukasza@chromium.org>
Cr-Commit-Position: refs/heads/master@{#507414}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index dbcf18d09..8a7f39a4 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -770,13 +770,6 @@
 
 bool WebContentsImpl::OnMessageReceived(RenderViewHostImpl* render_view_host,
                                         const IPC::Message& message) {
-  RenderFrameHost* main_frame = render_view_host->GetMainFrame();
-  if (main_frame) {
-    WebUIImpl* web_ui = static_cast<RenderFrameHostImpl*>(main_frame)->web_ui();
-    if (web_ui && web_ui->OnMessageReceived(message))
-      return true;
-  }
-
   for (auto& observer : observers_) {
     // TODO(nick, creis): https://crbug.com/758026: Replace all uses of this
     // variant of OnMessageReceived with the version that takes a
@@ -816,6 +809,12 @@
 
 bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host,
                                         const IPC::Message& message) {
+  {
+    WebUIImpl* web_ui = render_frame_host->web_ui();
+    if (web_ui && web_ui->OnMessageReceived(message, render_frame_host))
+      return true;
+  }
+
   for (auto& observer : observers_) {
     if (observer.OnMessageReceived(message, render_frame_host))
       return true;
diff --git a/content/browser/webui/web_ui_impl.cc b/content/browser/webui/web_ui_impl.cc
index e886e13..6840e2c 100644
--- a/content/browser/webui/web_ui_impl.cc
+++ b/content/browser/webui/web_ui_impl.cc
@@ -6,6 +6,9 @@
 
 #include <stddef.h>
 
+#include <string>
+#include <utility>
+
 #include "base/debug/dump_without_crashing.h"
 #include "base/json/json_writer.h"
 #include "base/strings/utf_string_conversions.h"
@@ -19,7 +22,7 @@
 #include "content/browser/web_contents/web_contents_impl.h"
 #include "content/browser/web_contents/web_contents_view.h"
 #include "content/browser/webui/web_ui_controller_factory_registry.h"
-#include "content/common/view_messages.h"
+#include "content/common/frame_messages.h"
 #include "content/public/browser/content_browser_client.h"
 #include "content/public/browser/navigation_handle.h"
 #include "content/public/browser/render_frame_host.h"
@@ -91,20 +94,22 @@
 
 // WebUIImpl, public: ----------------------------------------------------------
 
-bool WebUIImpl::OnMessageReceived(const IPC::Message& message) {
+bool WebUIImpl::OnMessageReceived(const IPC::Message& message,
+                                  RenderFrameHost* sender) {
   bool handled = true;
-  IPC_BEGIN_MESSAGE_MAP(WebUIImpl, message)
-    IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend)
+  IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebUIImpl, message, sender)
+    IPC_MESSAGE_HANDLER(FrameHostMsg_WebUISend, OnWebUISend)
     IPC_MESSAGE_UNHANDLED(handled = false)
   IPC_END_MESSAGE_MAP()
   return handled;
 }
 
-void WebUIImpl::OnWebUISend(const GURL& source_url,
+void WebUIImpl::OnWebUISend(RenderFrameHost* sender,
+                            const GURL& source_url,
                             const std::string& message,
                             const base::ListValue& args) {
-  if (!ChildProcessSecurityPolicyImpl::GetInstance()->
-          HasWebUIBindings(web_contents_->GetRenderProcessHost()->GetID()) ||
+  if (!ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
+          sender->GetProcess()->GetID()) ||
       !WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI(
           web_contents_->GetBrowserContext(), source_url)) {
     NOTREACHED() << "Blocked unauthorized use of WebUIBindings.";
diff --git a/content/browser/webui/web_ui_impl.h b/content/browser/webui/web_ui_impl.h
index 0801a5af..b13969e 100644
--- a/content/browser/webui/web_ui_impl.h
+++ b/content/browser/webui/web_ui_impl.h
@@ -8,19 +8,22 @@
 #include <map>
 #include <memory>
 #include <set>
+#include <string>
 #include <vector>
 
 #include "base/compiler_specific.h"
 #include "base/macros.h"
 #include "base/memory/weak_ptr.h"
 #include "content/public/browser/web_ui.h"
-#include "ipc/ipc_listener.h"
+
+namespace IPC {
+class Message;
+}
 
 namespace content {
 class RenderFrameHost;
 
 class CONTENT_EXPORT WebUIImpl : public WebUI,
-                                 public IPC::Listener,
                                  public base::SupportsWeakPtr<WebUIImpl> {
  public:
   WebUIImpl(WebContents* contents);
@@ -76,14 +79,14 @@
   std::vector<std::unique_ptr<WebUIMessageHandler>>* GetHandlersForTesting()
       override;
 
-  // IPC::Listener implementation:
-  bool OnMessageReceived(const IPC::Message& message) override;
+  bool OnMessageReceived(const IPC::Message& message, RenderFrameHost* sender);
 
  private:
   class MainFrameNavigationObserver;
 
   // IPC message handling.
-  void OnWebUISend(const GURL& source_url,
+  void OnWebUISend(RenderFrameHost* sender,
+                   const GURL& source_url,
                    const std::string& message,
                    const base::ListValue& args);
 
diff --git a/content/common/frame_messages.h b/content/common/frame_messages.h
index d415de2..1047ce1e 100644
--- a/content/common/frame_messages.h
+++ b/content/common/frame_messages.h
@@ -1687,6 +1687,13 @@
 IPC_MESSAGE_ROUTED1(FrameHostMsg_UpdateFaviconURL,
                     std::vector<content::FaviconURL> /* candidates */)
 
+// A message from HTML-based UI.  When (trusted) Javascript calls
+// send(message, args), this message is sent to the browser.
+IPC_MESSAGE_ROUTED3(FrameHostMsg_WebUISend,
+                    GURL /* source_url */,
+                    std::string /* message */,
+                    base::ListValue /* args */)
+
 #if BUILDFLAG(USE_EXTERNAL_POPUP_MENU)
 
 // Message to show/hide a popup menu using native controls.
diff --git a/content/common/view_messages.h b/content/common/view_messages.h
index 53b0269..9184f48 100644
--- a/content/common/view_messages.h
+++ b/content/common/view_messages.h
@@ -10,6 +10,10 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include <set>
+#include <string>
+#include <vector>
+
 #include "base/memory/shared_memory.h"
 #include "base/process/process.h"
 #include "base/strings/string16.h"
@@ -669,13 +673,6 @@
 IPC_MESSAGE_ROUTED1(ViewHostMsg_HasTouchEventHandlers,
                     bool /* has_handlers */)
 
-// A message from HTML-based UI.  When (trusted) Javascript calls
-// send(message, args), this message is sent to the browser.
-IPC_MESSAGE_ROUTED3(ViewHostMsg_WebUISend,
-                    GURL /* source_url */,
-                    std::string  /* message */,
-                    base::ListValue /* args */)
-
 #if BUILDFLAG(ENABLE_PLUGINS)
 // A renderer sends this to the browser process when it wants to access a PPAPI
 // broker. In contrast to FrameHostMsg_OpenChannelToPpapiBroker, this is called
diff --git a/content/renderer/web_ui_extension.cc b/content/renderer/web_ui_extension.cc
index 802bd7a..bf9d778 100644
--- a/content/renderer/web_ui_extension.cc
+++ b/content/renderer/web_ui_extension.cc
@@ -9,7 +9,7 @@
 
 #include "base/strings/string_util.h"
 #include "base/values.h"
-#include "content/common/view_messages.h"
+#include "content/common/frame_messages.h"
 #include "content/public/child/v8_value_converter.h"
 #include "content/public/common/bindings_policy.h"
 #include "content/public/common/url_constants.h"
@@ -33,15 +33,11 @@
 namespace {
 
 bool ShouldRespondToRequest(blink::WebLocalFrame** frame_ptr,
-                            RenderView** render_view_ptr) {
+                            RenderFrame** render_frame_ptr) {
   blink::WebLocalFrame* frame = blink::WebLocalFrame::FrameForCurrentContext();
   if (!frame || !frame->View())
     return false;
 
-  RenderView* render_view = RenderView::FromWebView(frame->View());
-  if (!render_view)
-    return false;
-
   GURL frame_url = frame->GetDocument().Url();
 
   RenderFrame* render_frame = RenderFrame::FromWebFrame(frame);
@@ -57,7 +53,7 @@
     return false;
 
   *frame_ptr = frame;
-  *render_view_ptr = render_view;
+  *render_frame_ptr = render_frame;
   return true;
 }
 
@@ -92,8 +88,8 @@
 // static
 void WebUIExtension::Send(gin::Arguments* args) {
   blink::WebLocalFrame* frame;
-  RenderView* render_view;
-  if (!ShouldRespondToRequest(&frame, &render_view))
+  RenderFrame* render_frame;
+  if (!ShouldRespondToRequest(&frame, &render_frame))
     return;
 
   std::string message;
@@ -127,19 +123,19 @@
   }
 
   // Send the message up to the browser.
-  render_view->Send(new ViewHostMsg_WebUISend(render_view->GetRoutingID(),
-                                              frame->GetDocument().Url(),
-                                              message, *content));
+  render_frame->Send(new FrameHostMsg_WebUISend(render_frame->GetRoutingID(),
+                                                frame->GetDocument().Url(),
+                                                message, *content));
 }
 
 // static
 std::string WebUIExtension::GetVariableValue(const std::string& name) {
   blink::WebLocalFrame* frame;
-  RenderView* render_view;
-  if (!ShouldRespondToRequest(&frame, &render_view))
+  RenderFrame* render_frame;
+  if (!ShouldRespondToRequest(&frame, &render_frame))
     return std::string();
 
-  return WebUIExtensionData::Get(render_view)->GetValue(name);
+  return WebUIExtensionData::Get(render_frame->GetRenderView())->GetValue(name);
 }
 
 }  // namespace content
diff --git a/docs/webui_explainer.md b/docs/webui_explainer.md
index e9a9284..1244741 100644
--- a/docs/webui_explainer.md
+++ b/docs/webui_explainer.md
@@ -516,17 +516,17 @@
 ```
 
 The message name and argument list are serialized to JSON and sent via the
-`ViewHostMsg_WebUISend` IPC message from the renderer to the browser.
+`FrameHostMsg_WebUISend` IPC message from the renderer to the browser.
 
 ```c++
 // In the renderer (WebUIExtension::Send()):
-render_view->Send(new ViewHostMsg_WebUISend(render_view->GetRoutingID(),
-                                            frame->GetDocument().Url(),
-                                            message, *content));
+render_frame->Send(new FrameHostMsg_WebUISend(render_frame->GetRoutingID(),
+                                              frame->GetDocument().Url(),
+                                              message, *content));
 ```
 ```c++
 // In the browser (WebUIImpl::OnMessageReceived()):
-IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend)
+IPC_MESSAGE_HANDLER(FrameHostMsg_WebUISend, OnWebUISend)
 ```
 
 The browser-side code does a map lookup for the message name and calls the found