Add frameId to MessageSender (extension messaging API).
Moved IPC for extension messaging from RenderView to RenderFrame.

BUG=264286
[email protected]

Review URL: https://codereview.chromium.org/709933002

Cr-Commit-Position: refs/heads/master@{#305148}
diff --git a/extensions/renderer/dispatcher.cc b/extensions/renderer/dispatcher.cc
index 1ebddb6..930b6c3 100644
--- a/extensions/renderer/dispatcher.cc
+++ b/extensions/renderer/dispatcher.cc
@@ -846,28 +846,28 @@
   MessagingBindings::DeliverMessage(script_context_set_,
                                     target_port_id,
                                     message,
-                                    NULL);  // All render views.
+                                    NULL);  // All render frames.
 }
 
 void Dispatcher::OnDispatchOnConnect(
     int target_port_id,
     const std::string& channel_name,
-    const base::DictionaryValue& source_tab,
+    const ExtensionMsg_TabConnectionInfo& source,
     const ExtensionMsg_ExternalConnectionInfo& info,
     const std::string& tls_channel_id) {
   DCHECK(!ContainsKey(port_to_tab_id_map_, target_port_id));
   DCHECK_EQ(1, target_port_id % 2);  // target renderer ports have odd IDs.
   int sender_tab_id = -1;
-  source_tab.GetInteger("id", &sender_tab_id);
+  source.tab.GetInteger("id", &sender_tab_id);
   port_to_tab_id_map_[target_port_id] = sender_tab_id;
 
   MessagingBindings::DispatchOnConnect(script_context_set_,
                                        target_port_id,
                                        channel_name,
-                                       source_tab,
+                                       source,
                                        info,
                                        tls_channel_id,
-                                       NULL);  // All render views.
+                                       NULL);  // All render frames.
 }
 
 void Dispatcher::OnDispatchOnDisconnect(int port_id,
@@ -875,7 +875,7 @@
   MessagingBindings::DispatchOnDisconnect(script_context_set_,
                                           port_id,
                                           error_message,
-                                          NULL);  // All render views.
+                                          NULL);  // All render frames.
 }
 
 void Dispatcher::OnLoaded(
diff --git a/extensions/renderer/dispatcher.h b/extensions/renderer/dispatcher.h
index ca8f0ff..fa3a784 100644
--- a/extensions/renderer/dispatcher.h
+++ b/extensions/renderer/dispatcher.h
@@ -33,6 +33,7 @@
 class URLPattern;
 struct ExtensionMsg_ExternalConnectionInfo;
 struct ExtensionMsg_Loaded_Params;
+struct ExtensionMsg_TabConnectionInfo;
 struct ExtensionMsg_UpdatePermissions_Params;
 
 namespace blink {
@@ -41,7 +42,6 @@
 }
 
 namespace base {
-class DictionaryValue;
 class ListValue;
 }
 
@@ -166,7 +166,7 @@
   void OnDeliverMessage(int target_port_id, const Message& message);
   void OnDispatchOnConnect(int target_port_id,
                            const std::string& channel_name,
-                           const base::DictionaryValue& source_tab,
+                           const ExtensionMsg_TabConnectionInfo& source,
                            const ExtensionMsg_ExternalConnectionInfo& info,
                            const std::string& tls_channel_id);
   void OnDispatchOnDisconnect(int port_id, const std::string& error_message);
diff --git a/extensions/renderer/extension_helper.cc b/extensions/renderer/extension_helper.cc
index d6f5cb8..100d342 100644
--- a/extensions/renderer/extension_helper.cc
+++ b/extensions/renderer/extension_helper.cc
@@ -6,7 +6,6 @@
 
 #include "content/public/renderer/render_view.h"
 #include "content/public/renderer/render_view_visitor.h"
-#include "extensions/common/api/messaging/message.h"
 #include "extensions/common/constants.h"
 #include "extensions/common/extension_messages.h"
 #include "extensions/common/permissions/permissions_data.h"
@@ -14,7 +13,6 @@
 #include "extensions/renderer/api/automation/automation_api_helper.h"
 #include "extensions/renderer/console.h"
 #include "extensions/renderer/dispatcher.h"
-#include "extensions/renderer/messaging_bindings.h"
 #include "third_party/WebKit/public/platform/WebURLRequest.h"
 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
 #include "third_party/WebKit/public/web/WebDocument.h"
@@ -137,11 +135,6 @@
   IPC_BEGIN_MESSAGE_MAP(ExtensionHelper, message)
     IPC_MESSAGE_HANDLER(ExtensionMsg_Response, OnExtensionResponse)
     IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnExtensionMessageInvoke)
-    IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect,
-                        OnExtensionDispatchOnConnect)
-    IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnExtensionDeliverMessage)
-    IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect,
-                        OnExtensionDispatchOnDisconnect)
     IPC_MESSAGE_HANDLER(ExtensionMsg_SetFrameName, OnSetFrameName)
     IPC_MESSAGE_HANDLER(ExtensionMsg_SetTabId, OnSetTabId)
     IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateBrowserWindowId,
@@ -206,34 +199,6 @@
       user_gesture);
 }
 
-void ExtensionHelper::OnExtensionDispatchOnConnect(
-    int target_port_id,
-    const std::string& channel_name,
-    const base::DictionaryValue& source_tab,
-    const ExtensionMsg_ExternalConnectionInfo& info,
-    const std::string& tls_channel_id) {
-  MessagingBindings::DispatchOnConnect(dispatcher_->script_context_set(),
-                                       target_port_id,
-                                       channel_name,
-                                       source_tab,
-                                       info,
-                                       tls_channel_id,
-                                       render_view());
-}
-
-void ExtensionHelper::OnExtensionDeliverMessage(int target_id,
-                                                const Message& message) {
-  MessagingBindings::DeliverMessage(
-      dispatcher_->script_context_set(), target_id, message, render_view());
-}
-
-void ExtensionHelper::OnExtensionDispatchOnDisconnect(
-    int port_id,
-    const std::string& error_message) {
-  MessagingBindings::DispatchOnDisconnect(
-      dispatcher_->script_context_set(), port_id, error_message, render_view());
-}
-
 void ExtensionHelper::OnNotifyRendererViewType(ViewType type) {
   view_type_ = type;
 }
diff --git a/extensions/renderer/extension_helper.h b/extensions/renderer/extension_helper.h
index 4bfee1c..3dba0777d 100644
--- a/extensions/renderer/extension_helper.h
+++ b/extensions/renderer/extension_helper.h
@@ -15,10 +15,8 @@
 
 class GURL;
 class SkBitmap;
-struct ExtensionMsg_ExternalConnectionInfo;
 
 namespace base {
-class DictionaryValue;
 class ListValue;
 }
 
@@ -27,8 +25,6 @@
 class Dispatcher;
 class URLPatternSet;
 
-struct Message;
-
 // RenderView-level plumbing for extension features.
 class ExtensionHelper
     : public content::RenderViewObserver,
@@ -73,16 +69,6 @@
                                 const std::string& function_name,
                                 const base::ListValue& args,
                                 bool user_gesture);
-  void OnExtensionDispatchOnConnect(
-      int target_port_id,
-      const std::string& channel_name,
-      const base::DictionaryValue& source_tab,
-      const ExtensionMsg_ExternalConnectionInfo& info,
-      const std::string& tls_channel_id);
-  void OnExtensionDeliverMessage(int target_port_id,
-                                 const Message& message);
-  void OnExtensionDispatchOnDisconnect(int port_id,
-                                       const std::string& error_message);
   void OnNotifyRendererViewType(ViewType view_type);
   void OnSetTabId(int tab_id);
   void OnUpdateBrowserWindowId(int window_id);
diff --git a/extensions/renderer/messaging_bindings.cc b/extensions/renderer/messaging_bindings.cc
index e7014fa8..5a12f712 100644
--- a/extensions/renderer/messaging_bindings.cc
+++ b/extensions/renderer/messaging_bindings.cc
@@ -13,8 +13,8 @@
 #include "base/lazy_instance.h"
 #include "base/message_loop/message_loop.h"
 #include "base/values.h"
+#include "content/public/renderer/render_frame.h"
 #include "content/public/renderer/render_thread.h"
-#include "content/public/renderer/render_view.h"
 #include "content/public/renderer/v8_value_converter.h"
 #include "extensions/common/api/messaging/message.h"
 #include "extensions/common/extension_messages.h"
@@ -106,8 +106,8 @@
 
   // Sends a message along the given channel.
   void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) {
-    content::RenderView* renderview = context()->GetRenderView();
-    if (!renderview)
+    content::RenderFrame* renderframe = context()->GetRenderFrame();
+    if (!renderframe)
       return;
 
     // Arguments are (int32 port_id, string message).
@@ -120,8 +120,8 @@
       return;
     }
 
-    renderview->Send(new ExtensionHostMsg_PostMessage(
-        renderview->GetRoutingID(), port_id,
+    renderframe->Send(new ExtensionHostMsg_PostMessage(
+        renderframe->GetRoutingID(), port_id,
         Message(*v8::String::Utf8Value(args[1]),
                 blink::WebUserGestureIndicator::isProcessingUserGesture())));
   }
@@ -137,7 +137,7 @@
     if (!HasPortData(port_id))
       return;
 
-    // Send via the RenderThread because the RenderView might be closing.
+    // Send via the RenderThread because the RenderFrame might be closing.
     bool notify_browser = args[1]->BooleanValue();
     if (notify_browser) {
       content::RenderThread::Get()->Send(
@@ -168,7 +168,7 @@
 
     int port_id = args[0]->Int32Value();
     if (HasPortData(port_id) && --GetPortData(port_id).ref_count == 0) {
-      // Send via the RenderThread because the RenderView might be closing.
+      // Send via the RenderThread because the RenderFrame might be closing.
       content::RenderThread::Get()->Send(
           new ExtensionHostMsg_CloseChannel(port_id, std::string()));
       ClearPortDataAndNotifyDispatcher(port_id);
@@ -240,7 +240,7 @@
 void DispatchOnConnectToScriptContext(
     int target_port_id,
     const std::string& channel_name,
-    const base::DictionaryValue* source_tab,
+    const ExtensionMsg_TabConnectionInfo* source,
     const ExtensionMsg_ExternalConnectionInfo& info,
     const std::string& tls_channel_id,
     bool* port_created,
@@ -258,8 +258,8 @@
   v8::Handle<v8::Value> tls_channel_id_value = v8::Undefined(isolate);
 
   if (extension) {
-    if (!source_tab->empty() && !extension->is_platform_app())
-      tab = converter->ToV8Value(source_tab, script_context->v8_context());
+    if (!source->tab.empty() && !extension->is_platform_app())
+      tab = converter->ToV8Value(&source->tab, script_context->v8_context());
 
     ExternallyConnectableInfo* externally_connectable =
         ExternallyConnectableInfo::Get(extension);
@@ -282,6 +282,8 @@
                               channel_name.size()),
       // sourceTab
       tab,
+      // source_frame_id
+      v8::Integer::New(isolate, source->frame_id),
       // sourceExtensionId
       v8::String::NewFromUtf8(isolate,
                               info.source_id.c_str(),
@@ -372,20 +374,19 @@
     const ScriptContextSet& context_set,
     int target_port_id,
     const std::string& channel_name,
-    const base::DictionaryValue& source_tab,
+    const ExtensionMsg_TabConnectionInfo& source,
     const ExtensionMsg_ExternalConnectionInfo& info,
     const std::string& tls_channel_id,
-    content::RenderView* restrict_to_render_view) {
+    content::RenderFrame* restrict_to_render_frame) {
+  // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*.
+  content::RenderView* restrict_to_render_view =
+      restrict_to_render_frame ? restrict_to_render_frame->GetRenderView()
+                               : NULL;
   bool port_created = false;
-  context_set.ForEach(info.target_id,
-                      restrict_to_render_view,
-                      base::Bind(&DispatchOnConnectToScriptContext,
-                                 target_port_id,
-                                 channel_name,
-                                 &source_tab,
-                                 info,
-                                 tls_channel_id,
-                                 &port_created));
+  context_set.ForEach(
+      info.target_id, restrict_to_render_view,
+      base::Bind(&DispatchOnConnectToScriptContext, target_port_id,
+                 channel_name, &source, info, tls_channel_id, &port_created));
 
   // If we didn't create a port, notify the other end of the channel (treat it
   // as a disconnect).
@@ -400,7 +401,7 @@
     const ScriptContextSet& context_set,
     int target_port_id,
     const Message& message,
-    content::RenderView* restrict_to_render_view) {
+    content::RenderFrame* restrict_to_render_frame) {
   scoped_ptr<blink::WebScopedUserGesture> web_user_gesture;
   scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus;
   if (message.user_gesture) {
@@ -408,6 +409,10 @@
     allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator);
   }
 
+  // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*.
+  content::RenderView* restrict_to_render_view =
+      restrict_to_render_frame ? restrict_to_render_frame->GetRenderView()
+                               : NULL;
   context_set.ForEach(
       restrict_to_render_view,
       base::Bind(&DeliverMessageToScriptContext, message.data, target_port_id));
@@ -418,7 +423,11 @@
     const ScriptContextSet& context_set,
     int port_id,
     const std::string& error_message,
-    content::RenderView* restrict_to_render_view) {
+    content::RenderFrame* restrict_to_render_frame) {
+  // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*.
+  content::RenderView* restrict_to_render_view =
+      restrict_to_render_frame ? restrict_to_render_frame->GetRenderView()
+                               : NULL;
   context_set.ForEach(
       restrict_to_render_view,
       base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message));
diff --git a/extensions/renderer/messaging_bindings.h b/extensions/renderer/messaging_bindings.h
index 422dcae..a7a1c6fe 100644
--- a/extensions/renderer/messaging_bindings.h
+++ b/extensions/renderer/messaging_bindings.h
@@ -10,13 +10,14 @@
 #include "extensions/renderer/script_context_set.h"
 
 struct ExtensionMsg_ExternalConnectionInfo;
+struct ExtensionMsg_TabConnectionInfo;
 
 namespace base {
 class DictionaryValue;
 }
 
 namespace content {
-class RenderView;
+class RenderFrame;
 }
 
 namespace v8 {
@@ -41,30 +42,30 @@
                                         ScriptContext* context);
 
   // Dispatches the onConnect content script messaging event to some contexts
-  // in |context_set|. If |restrict_to_render_view| is specified, only contexts
-  // in that render view will receive the message.
+  // in |context_set|. If |restrict_to_render_frame| is specified, only contexts
+  // in that render frame will receive the message.
   static void DispatchOnConnect(const ScriptContextSet& context_set,
                                 int target_port_id,
                                 const std::string& channel_name,
-                                const base::DictionaryValue& source_tab,
+                                const ExtensionMsg_TabConnectionInfo& source,
                                 const ExtensionMsg_ExternalConnectionInfo& info,
                                 const std::string& tls_channel_id,
-                                content::RenderView* restrict_to_render_view);
+                                content::RenderFrame* restrict_to_render_frame);
 
   // Delivers a message sent using content script messaging to some of the
-  // contexts in |bindings_context_set|. If |restrict_to_render_view| is
+  // contexts in |bindings_context_set|. If |restrict_to_render_frame| is
   // specified, only contexts in that render view will receive the message.
   static void DeliverMessage(const ScriptContextSet& context_set,
                              int target_port_id,
                              const Message& message,
-                             content::RenderView* restrict_to_render_view);
+                             content::RenderFrame* restrict_to_render_frame);
 
   // Dispatches the onDisconnect event in response to the channel being closed.
   static void DispatchOnDisconnect(
       const ScriptContextSet& context_set,
       int port_id,
       const std::string& error_message,
-      content::RenderView* restrict_to_render_view);
+      content::RenderFrame* restrict_to_render_frame);
 };
 
 }  // namespace extensions
diff --git a/extensions/renderer/resources/messaging.js b/extensions/renderer/resources/messaging.js
index 5a83654c..d0491c0 100644
--- a/extensions/renderer/resources/messaging.js
+++ b/extensions/renderer/resources/messaging.js
@@ -216,6 +216,7 @@
   function dispatchOnConnect(portId,
                              channelName,
                              sourceTab,
+                             sourceFrameId,
                              sourceExtensionId,
                              targetExtensionId,
                              sourceUrl,
@@ -244,6 +245,8 @@
       sender.url = sourceUrl;
     if (sourceTab)
       sender.tab = sourceTab;
+    if (sourceFrameId >= 0)
+      sender.frameId = sourceFrameId;
     if (tlsChannelId !== undefined)
       sender.tlsChannelId = tlsChannelId;
 
diff --git a/extensions/renderer/runtime_custom_bindings.cc b/extensions/renderer/runtime_custom_bindings.cc
index 3d1d3c2..a6131e7 100644
--- a/extensions/renderer/runtime_custom_bindings.cc
+++ b/extensions/renderer/runtime_custom_bindings.cc
@@ -7,6 +7,7 @@
 #include "base/bind.h"
 #include "base/memory/scoped_ptr.h"
 #include "base/values.h"
+#include "content/public/renderer/render_frame.h"
 #include "content/public/renderer/render_view.h"
 #include "content/public/renderer/v8_value_converter.h"
 #include "extensions/common/extension.h"
@@ -46,10 +47,10 @@
 
 void RuntimeCustomBindings::OpenChannelToExtension(
     const v8::FunctionCallbackInfo<v8::Value>& args) {
-  // Get the current RenderView so that we can send a routed IPC message from
+  // Get the current RenderFrame so that we can send a routed IPC message from
   // the correct source.
-  content::RenderView* renderview = context()->GetRenderView();
-  if (!renderview)
+  content::RenderFrame* renderframe = context()->GetRenderFrame();
+  if (!renderframe)
     return;
 
   // The Javascript code should validate/fill the arguments.
@@ -70,12 +71,9 @@
   bool include_tls_channel_id =
       args.Length() > 2 ? args[2]->BooleanValue() : false;
   int port_id = -1;
-  renderview->Send(
-      new ExtensionHostMsg_OpenChannelToExtension(renderview->GetRoutingID(),
-                                                  info,
-                                                  channel_name,
-                                                  include_tls_channel_id,
-                                                  &port_id));
+  renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
+      renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id,
+      &port_id));
   args.GetReturnValue().Set(static_cast<int32_t>(port_id));
 }