1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/memory/weak_ptr.h" 13 #include "extensions/browser/extension_function.h" 14 #include "ipc/ipc_sender.h" 15 #include "url/gurl.h" 16 17 class ChromeRenderMessageFilter; 18 struct ExtensionHostMsg_Request_Params; 19 20 namespace content { 21 class BrowserContext; 22 class RenderViewHost; 23 class WebContents; 24 } 25 26 namespace extensions { 27 class Extension; 28 class ExtensionAPI; 29 class InfoMap; 30 class ProcessMap; 31 class WindowController; 32 } 33 34 // A factory function for creating new ExtensionFunction instances. 35 typedef ExtensionFunction* (*ExtensionFunctionFactory)(); 36 37 // ExtensionFunctionDispatcher receives requests to execute functions from 38 // Chrome extensions running in a RenderViewHost and dispatches them to the 39 // appropriate handler. It lives entirely on the UI thread. 40 // 41 // ExtensionFunctionDispatcher should be a member of some class that hosts 42 // RenderViewHosts and wants them to be able to display extension content. 43 // This class should also implement ExtensionFunctionDispatcher::Delegate. 44 // 45 // Note that a single ExtensionFunctionDispatcher does *not* correspond to a 46 // single RVH, a single extension, or a single URL. This is by design so that 47 // we can gracefully handle cases like WebContents, where the RVH, extension, 48 // and URL can all change over the lifetime of the tab. Instead, these items 49 // are all passed into each request. 50 class ExtensionFunctionDispatcher 51 : public base::SupportsWeakPtr<ExtensionFunctionDispatcher> { 52 public: 53 class Delegate { 54 public: 55 // Returns the extensions::WindowController associated with this delegate, 56 // or NULL if no window is associated with the delegate. 57 virtual extensions::WindowController* GetExtensionWindowController() const; 58 59 // Asks the delegate for any relevant WebContents associated with this 60 // context. For example, the WebContents in which an infobar or 61 // chrome-extension://<id> URL are being shown. Callers must check for a 62 // NULL return value (as in the case of a background page). 63 virtual content::WebContents* GetAssociatedWebContents() const; 64 65 // If the associated web contents is not null, returns that. Otherwise, 66 // returns the next most relevant visible web contents. Callers must check 67 // for a NULL return value (as in the case of a background page). 68 virtual content::WebContents* GetVisibleWebContents() const; 69 70 protected: 71 virtual ~Delegate() {} 72 }; 73 74 // Gets a list of all known extension function names. 75 static void GetAllFunctionNames(std::vector<std::string>* names); 76 77 // Override a previously registered function. Returns true if successful, 78 // false if no such function was registered. 79 static bool OverrideFunction(const std::string& name, 80 ExtensionFunctionFactory factory); 81 82 // Resets all functions to their initial implementation. 83 static void ResetFunctions(); 84 85 // Dispatches an IO-thread extension function. Only used for specific 86 // functions that must be handled on the IO-thread. 87 static void DispatchOnIOThread( 88 extensions::InfoMap* extension_info_map, 89 void* profile, 90 int render_process_id, 91 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender, 92 int routing_id, 93 const ExtensionHostMsg_Request_Params& params); 94 95 // Public constructor. Callers must ensure that: 96 // - |delegate| outlives this object. 97 // - This object outlives any RenderViewHost's passed to created 98 // ExtensionFunctions. 99 ExtensionFunctionDispatcher(content::BrowserContext* browser_context, 100 Delegate* delegate); 101 102 ~ExtensionFunctionDispatcher(); 103 104 Delegate* delegate() { return delegate_; } 105 106 // Message handlers. 107 // The response is sent to the corresponding render view in an 108 // ExtensionMsg_Response message. 109 void Dispatch(const ExtensionHostMsg_Request_Params& params, 110 content::RenderViewHost* render_view_host); 111 // |callback| is called when the function execution completes. 112 void DispatchWithCallback( 113 const ExtensionHostMsg_Request_Params& params, 114 content::RenderViewHost* render_view_host, 115 const ExtensionFunction::ResponseCallback& callback); 116 117 // Called when an ExtensionFunction is done executing, after it has sent 118 // a response (if any) to the extension. 119 void OnExtensionFunctionCompleted(const extensions::Extension* extension); 120 121 // The BrowserContext that this dispatcher is associated with. 122 content::BrowserContext* browser_context() { return browser_context_; } 123 124 private: 125 // For a given RenderViewHost instance, UIThreadResponseCallbackWrapper 126 // creates ExtensionFunction::ResponseCallback instances which send responses 127 // to the corresponding render view in ExtensionMsg_Response messages. 128 // This class tracks the lifespan of the RenderViewHost instance, and will be 129 // destroyed automatically when it goes away. 130 class UIThreadResponseCallbackWrapper; 131 132 // Helper to check whether an ExtensionFunction has the required permissions. 133 // This should be called after the function is fully initialized. 134 // If the check fails, |callback| is run with an access-denied error and false 135 // is returned. |function| must not be run in that case. 136 static bool CheckPermissions( 137 ExtensionFunction* function, 138 const extensions::Extension* extension, 139 const ExtensionHostMsg_Request_Params& params, 140 const ExtensionFunction::ResponseCallback& callback); 141 142 // Helper to create an ExtensionFunction to handle the function given by 143 // |params|. Can be called on any thread. 144 // Does not set subclass properties, or include_incognito. 145 static ExtensionFunction* CreateExtensionFunction( 146 const ExtensionHostMsg_Request_Params& params, 147 const extensions::Extension* extension, 148 int requesting_process_id, 149 const extensions::ProcessMap& process_map, 150 extensions::ExtensionAPI* api, 151 void* profile, 152 const ExtensionFunction::ResponseCallback& callback); 153 154 // Helper to run the response callback with an access denied error. Can be 155 // called on any thread. 156 static void SendAccessDenied( 157 const ExtensionFunction::ResponseCallback& callback); 158 159 content::BrowserContext* browser_context_; 160 161 Delegate* delegate_; 162 163 // This map doesn't own either the keys or the values. When a RenderViewHost 164 // instance goes away, the corresponding entry in this map (if exists) will be 165 // removed. 166 typedef std::map<content::RenderViewHost*, UIThreadResponseCallbackWrapper*> 167 UIThreadResponseCallbackWrapperMap; 168 UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_; 169 }; 170 171 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ 172