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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_
12 #include "base/memory/weak_ptr.h"
13 #include "extensions/browser/extension_function.h"
14 #include "ipc/ipc_sender.h"
17 class ChromeRenderMessageFilter
;
18 struct ExtensionHostMsg_Request_Params
;
22 class RenderFrameHost
;
27 namespace extensions
{
32 class WindowController
;
35 // A factory function for creating new ExtensionFunction instances.
36 typedef ExtensionFunction
* (*ExtensionFunctionFactory
)();
38 // ExtensionFunctionDispatcher receives requests to execute functions from
39 // Chrome extensions running in a RenderViewHost and dispatches them to the
40 // appropriate handler. It lives entirely on the UI thread.
42 // ExtensionFunctionDispatcher should be a member of some class that hosts
43 // RenderViewHosts and wants them to be able to display extension content.
44 // This class should also implement ExtensionFunctionDispatcher::Delegate.
46 // Note that a single ExtensionFunctionDispatcher does *not* correspond to a
47 // single RVH, a single extension, or a single URL. This is by design so that
48 // we can gracefully handle cases like WebContents, where the RVH, extension,
49 // and URL can all change over the lifetime of the tab. Instead, these items
50 // are all passed into each request.
51 class ExtensionFunctionDispatcher
52 : public base::SupportsWeakPtr
<ExtensionFunctionDispatcher
> {
56 // Returns the extensions::WindowController associated with this delegate,
57 // or NULL if no window is associated with the delegate.
58 virtual extensions::WindowController
* GetExtensionWindowController() const;
60 // Asks the delegate for any relevant WebContents associated with this
61 // context. For example, the WebContents in which an infobar or
62 // chrome-extension://<id> URL are being shown. Callers must check for a
63 // NULL return value (as in the case of a background page).
64 virtual content::WebContents
* GetAssociatedWebContents() const;
66 // If the associated web contents is not null, returns that. Otherwise,
67 // returns the next most relevant visible web contents. Callers must check
68 // for a NULL return value (as in the case of a background page).
69 virtual content::WebContents
* GetVisibleWebContents() const;
72 virtual ~Delegate() {}
75 // Gets a list of all known extension function names.
76 static void GetAllFunctionNames(std::vector
<std::string
>* names
);
78 // Override a previously registered function. Returns true if successful,
79 // false if no such function was registered.
80 static bool OverrideFunction(const std::string
& name
,
81 ExtensionFunctionFactory factory
);
83 // Resets all functions to their initial implementation.
84 static void ResetFunctions();
86 // Dispatches an IO-thread extension function. Only used for specific
87 // functions that must be handled on the IO-thread.
88 static void DispatchOnIOThread(
89 extensions::InfoMap
* extension_info_map
,
91 int render_process_id
,
92 base::WeakPtr
<ChromeRenderMessageFilter
> ipc_sender
,
94 const ExtensionHostMsg_Request_Params
& params
);
96 // Public constructor. Callers must ensure that:
97 // - |delegate| outlives this object.
98 // - This object outlives any RenderViewHost's passed to created
99 // ExtensionFunctions.
100 ExtensionFunctionDispatcher(content::BrowserContext
* browser_context
,
103 ~ExtensionFunctionDispatcher();
105 Delegate
* delegate() { return delegate_
; }
108 // The response is sent to the corresponding render view in an
109 // ExtensionMsg_Response message.
110 // TODO (jam): convert all callers to use RenderFrameHost.
111 void Dispatch(const ExtensionHostMsg_Request_Params
& params
,
112 content::RenderViewHost
* render_view_host
);
113 // Dispatch an extension function and calls |callback| when the execution
115 void DispatchWithCallback(
116 const ExtensionHostMsg_Request_Params
& params
,
117 content::RenderFrameHost
* render_frame_host
,
118 const ExtensionFunction::ResponseCallback
& callback
);
120 // Called when an ExtensionFunction is done executing, after it has sent
121 // a response (if any) to the extension.
122 void OnExtensionFunctionCompleted(const extensions::Extension
* extension
);
124 // The BrowserContext that this dispatcher is associated with.
125 content::BrowserContext
* browser_context() { return browser_context_
; }
128 // For a given RenderViewHost instance, UIThreadResponseCallbackWrapper
129 // creates ExtensionFunction::ResponseCallback instances which send responses
130 // to the corresponding render view in ExtensionMsg_Response messages.
131 // This class tracks the lifespan of the RenderViewHost instance, and will be
132 // destroyed automatically when it goes away.
133 class UIThreadResponseCallbackWrapper
;
135 // Helper to check whether an ExtensionFunction has the required permissions.
136 // This should be called after the function is fully initialized.
137 // If the check fails, |callback| is run with an access-denied error and false
138 // is returned. |function| must not be run in that case.
139 static bool CheckPermissions(
140 ExtensionFunction
* function
,
141 const extensions::Extension
* extension
,
142 const ExtensionHostMsg_Request_Params
& params
,
143 const ExtensionFunction::ResponseCallback
& callback
);
145 // Helper to create an ExtensionFunction to handle the function given by
146 // |params|. Can be called on any thread.
147 // Does not set subclass properties, or include_incognito.
148 static ExtensionFunction
* CreateExtensionFunction(
149 const ExtensionHostMsg_Request_Params
& params
,
150 const extensions::Extension
* extension
,
151 int requesting_process_id
,
152 const extensions::ProcessMap
& process_map
,
153 extensions::ExtensionAPI
* api
,
155 const ExtensionFunction::ResponseCallback
& callback
);
157 // Helper to run the response callback with an access denied error. Can be
158 // called on any thread.
159 static void SendAccessDenied(
160 const ExtensionFunction::ResponseCallback
& callback
);
162 void DispatchWithCallbackInternal(
163 const ExtensionHostMsg_Request_Params
& params
,
164 content::RenderViewHost
* render_view_host
,
165 content::RenderFrameHost
* render_frame_host
,
166 const ExtensionFunction::ResponseCallback
& callback
);
168 content::BrowserContext
* browser_context_
;
172 // This map doesn't own either the keys or the values. When a RenderViewHost
173 // instance goes away, the corresponding entry in this map (if exists) will be
175 typedef std::map
<content::RenderViewHost
*, UIThreadResponseCallbackWrapper
*>
176 UIThreadResponseCallbackWrapperMap
;
177 UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_
;
180 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_