1 // Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_
12 #include "base/memory/weak_ptr.h"
13 #include "extensions/browser/extension_function.h"
14 #include "ipc/ipc_sender.h"
16 struct ExtensionHostMsg_Request_Params
;
20 class RenderFrameHost
;
25 namespace extensions
{
30 class IOThreadExtensionMessageFilter
;
32 class WindowController
;
34 // A factory function for creating new ExtensionFunction instances.
35 typedef ExtensionFunction
* (*ExtensionFunctionFactory
)();
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.
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.
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
> {
55 // Returns the WindowController associated with this delegate, or NULL if no
56 // window is associated with the delegate.
57 virtual WindowController
* GetExtensionWindowController() const;
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;
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;
71 virtual ~Delegate() {}
74 // Gets a list of all known extension function names.
75 static void GetAllFunctionNames(std::vector
<std::string
>* names
);
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
);
82 // Dispatches an IO-thread extension function. Only used for specific
83 // functions that must be handled on the IO-thread.
84 static void DispatchOnIOThread(
85 InfoMap
* extension_info_map
,
87 int render_process_id
,
88 base::WeakPtr
<IOThreadExtensionMessageFilter
> ipc_sender
,
90 const ExtensionHostMsg_Request_Params
& params
);
92 // Public constructor. Callers must ensure that:
93 // - |delegate| outlives this object.
94 // - This object outlives any RenderViewHost's passed to created
95 // ExtensionFunctions.
96 ExtensionFunctionDispatcher(content::BrowserContext
* browser_context
,
99 ~ExtensionFunctionDispatcher();
101 Delegate
* delegate() { return delegate_
; }
104 // The response is sent to the corresponding render view in an
105 // ExtensionMsg_Response message.
106 // TODO (jam): convert all callers to use RenderFrameHost.
107 void Dispatch(const ExtensionHostMsg_Request_Params
& params
,
108 content::RenderViewHost
* render_view_host
);
110 // Called when an ExtensionFunction is done executing, after it has sent
111 // a response (if any) to the extension.
112 void OnExtensionFunctionCompleted(const Extension
* extension
);
114 // The BrowserContext that this dispatcher is associated with.
115 content::BrowserContext
* browser_context() { return browser_context_
; }
118 // For a given RenderViewHost instance, UIThreadResponseCallbackWrapper
119 // creates ExtensionFunction::ResponseCallback instances which send responses
120 // to the corresponding render view in ExtensionMsg_Response messages.
121 // This class tracks the lifespan of the RenderViewHost instance, and will be
122 // destroyed automatically when it goes away.
123 class UIThreadResponseCallbackWrapper
;
125 // Helper to check whether an ExtensionFunction has the required permissions.
126 // This should be called after the function is fully initialized.
127 // If the check fails, |callback| is run with an access-denied error and false
128 // is returned. |function| must not be run in that case.
129 static bool CheckPermissions(
130 ExtensionFunction
* function
,
131 const ExtensionHostMsg_Request_Params
& params
,
132 const ExtensionFunction::ResponseCallback
& callback
);
134 // Helper to create an ExtensionFunction to handle the function given by
135 // |params|. Can be called on any thread.
136 // Does not set subclass properties, or include_incognito.
137 static ExtensionFunction
* CreateExtensionFunction(
138 const ExtensionHostMsg_Request_Params
& params
,
139 const Extension
* extension
,
140 int requesting_process_id
,
141 const ProcessMap
& process_map
,
144 const ExtensionFunction::ResponseCallback
& callback
);
146 // Helper to run the response callback with an access denied error. Can be
147 // called on any thread.
148 static void SendAccessDenied(
149 const ExtensionFunction::ResponseCallback
& callback
,
150 functions::HistogramValue histogram_value
);
152 void DispatchWithCallbackInternal(
153 const ExtensionHostMsg_Request_Params
& params
,
154 content::RenderViewHost
* render_view_host
,
155 content::RenderFrameHost
* render_frame_host
,
156 const ExtensionFunction::ResponseCallback
& callback
);
158 content::BrowserContext
* browser_context_
;
162 // This map doesn't own either the keys or the values. When a RenderViewHost
163 // instance goes away, the corresponding entry in this map (if exists) will be
165 typedef std::map
<content::RenderViewHost
*, UIThreadResponseCallbackWrapper
*>
166 UIThreadResponseCallbackWrapperMap
;
167 UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_
;
170 } // namespace extensions
172 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_