Support extension API requests going directly from Pepper plugin processes to the browser process.

- For each plugin instance which uses Apps v2 APIs, an ExtensionFunctionDispatcher instance is created to dispatch requests coming from plugin process.
- ExtensionFunctionDispatcher and ExtensionFunction are refactored, so that we can override how/where to send function execution result.

BUG=226303
TEST=None

Review URL: https://chromiumcodereview.appspot.com/14851012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@200211 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/extensions/extension_function_dispatcher.h b/chrome/browser/extensions/extension_function_dispatcher.h
index 1591dcf..b6cebd6 100644
--- a/chrome/browser/extensions/extension_function_dispatcher.h
+++ b/chrome/browser/extensions/extension_function_dispatcher.h
@@ -5,15 +5,16 @@
 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
 
+#include <map>
 #include <string>
 #include <vector>
 
 #include "base/memory/weak_ptr.h"
+#include "chrome/browser/extensions/extension_function.h"
 #include "ipc/ipc_sender.h"
 #include "googleurl/src/gurl.h"
 
 class ChromeRenderMessageFilter;
-class ExtensionFunction;
 class ExtensionInfoMap;
 class Profile;
 struct ExtensionHostMsg_Request_Params;
@@ -97,8 +98,15 @@
   Delegate* delegate() { return delegate_; }
 
   // Message handlers.
+  // The response is sent to the corresponding render view in an
+  // ExtensionMsg_Response message.
   void Dispatch(const ExtensionHostMsg_Request_Params& params,
-                content::RenderViewHost* sender);
+                content::RenderViewHost* render_view_host);
+  // |callback| is called when the function execution completes.
+  void DispatchWithCallback(
+      const ExtensionHostMsg_Request_Params& params,
+      content::RenderViewHost* render_view_host,
+      const ExtensionFunction::ResponseCallback& callback);
 
   // Called when an ExtensionFunction is done executing, after it has sent
   // a response (if any) to the extension.
@@ -108,14 +116,22 @@
   Profile* profile() { return profile_; }
 
  private:
+  // For a given RenderViewHost instance, UIThreadResponseCallbackWrapper
+  // creates ExtensionFunction::ResponseCallback instances which send responses
+  // to the corresponding render view in ExtensionMsg_Response messages.
+  // This class tracks the lifespan of the RenderViewHost instance, and will be
+  // destroyed automatically when it goes away.
+  class UIThreadResponseCallbackWrapper;
+
   // Helper to check whether an ExtensionFunction has the required permissions.
   // This should be called after the function is fully initialized.
+  // If the check fails, |callback| is run with an access-denied error and false
+  // is returned. |function| must not be run in that case.
   static bool CheckPermissions(
       ExtensionFunction* function,
       const extensions::Extension* extension,
       const ExtensionHostMsg_Request_Params& params,
-      IPC::Sender* ipc_sender,
-      int routing_id);
+      const ExtensionFunction::ResponseCallback& callback);
 
   // Helper to create an ExtensionFunction to handle the function given by
   // |params|. Can be called on any thread.
@@ -127,19 +143,23 @@
       const extensions::ProcessMap& process_map,
       extensions::ExtensionAPI* api,
       void* profile,
-      IPC::Sender* ipc_sender,
-      content::RenderViewHost* render_view_host,
-      int routing_id);
+      const ExtensionFunction::ResponseCallback& callback);
 
-  // Helper to send an access denied error to the requesting renderer. Can be
+  // Helper to run the response callback with an access denied error. Can be
   // called on any thread.
-  static void SendAccessDenied(IPC::Sender* ipc_sender,
-                               int routing_id,
-                               int request_id);
+  static void SendAccessDenied(
+      const ExtensionFunction::ResponseCallback& callback);
 
   Profile* profile_;
 
   Delegate* delegate_;
+
+  // This map doesn't own either the keys or the values. When a RenderViewHost
+  // instance goes away, the corresponding entry in this map (if exists) will be
+  // removed.
+  typedef std::map<content::RenderViewHost*, UIThreadResponseCallbackWrapper*>
+      UIThreadResponseCallbackWrapperMap;
+  UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_;
 };
 
 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_