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
;
24 namespace extensions
{
29 class IOThreadExtensionMessageFilter
;
31 class WindowController
;
33 // A factory function for creating new ExtensionFunction instances.
34 typedef ExtensionFunction
* (*ExtensionFunctionFactory
)();
36 // ExtensionFunctionDispatcher receives requests to execute functions from
37 // Chrome extensions running in a RenderFrameHost and dispatches them to the
38 // appropriate handler. It lives entirely on the UI thread.
40 // ExtensionFunctionDispatcher should be a member of some class that hosts
41 // RenderFrameHosts and wants them to be able to display extension content.
42 // This class should also implement ExtensionFunctionDispatcher::Delegate.
44 // Note that a single ExtensionFunctionDispatcher does *not* correspond to a
45 // single RVH, a single extension, or a single URL. This is by design so that
46 // we can gracefully handle cases like WebContents, where the RVH, extension,
47 // and URL can all change over the lifetime of the tab. Instead, these items
48 // are all passed into each request.
49 class ExtensionFunctionDispatcher
50 : public base::SupportsWeakPtr
<ExtensionFunctionDispatcher
> {
54 // Returns the WindowController associated with this delegate, or NULL if no
55 // window is associated with the delegate.
56 virtual WindowController
* GetExtensionWindowController() const;
58 // Asks the delegate for any relevant WebContents associated with this
59 // context. For example, the WebContents in which an infobar or
60 // chrome-extension://<id> URL are being shown. Callers must check for a
61 // NULL return value (as in the case of a background page).
62 virtual content::WebContents
* GetAssociatedWebContents() const;
64 // If the associated web contents is not null, returns that. Otherwise,
65 // returns the next most relevant visible web contents. Callers must check
66 // for a NULL return value (as in the case of a background page).
67 virtual content::WebContents
* GetVisibleWebContents() const;
70 virtual ~Delegate() {}
73 // Override a previously registered function. Returns true if successful,
74 // false if no such function was registered.
75 static bool OverrideFunction(const std::string
& name
,
76 ExtensionFunctionFactory factory
);
78 // Dispatches an IO-thread extension function. Only used for specific
79 // functions that must be handled on the IO-thread.
80 static void DispatchOnIOThread(
81 InfoMap
* extension_info_map
,
83 int render_process_id
,
84 base::WeakPtr
<IOThreadExtensionMessageFilter
> ipc_sender
,
86 const ExtensionHostMsg_Request_Params
& params
);
88 // Public constructor. Callers must ensure that:
89 // - This object outlives any RenderFrameHost's passed to created
90 // ExtensionFunctions.
91 explicit ExtensionFunctionDispatcher(
92 content::BrowserContext
* browser_context
);
93 ~ExtensionFunctionDispatcher();
96 // The response is sent to the corresponding render view in an
97 // ExtensionMsg_Response message.
98 void Dispatch(const ExtensionHostMsg_Request_Params
& params
,
99 content::RenderFrameHost
* render_frame_host
);
101 // Called when an ExtensionFunction is done executing, after it has sent
102 // a response (if any) to the extension.
103 void OnExtensionFunctionCompleted(const Extension
* extension
);
105 // See the Delegate class for documentation on these methods.
106 // TODO(devlin): None of these belong here. We should kill
107 // ExtensionFunctionDispatcher::Delegate.
108 WindowController
* GetExtensionWindowController() const;
109 content::WebContents
* GetAssociatedWebContents() const;
110 content::WebContents
* GetVisibleWebContents() const;
112 // The BrowserContext that this dispatcher is associated with.
113 content::BrowserContext
* browser_context() { return browser_context_
; }
115 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
118 // For a given RenderFrameHost 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 RenderFrameHost 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::RenderFrameHost
* render_frame_host
,
155 const ExtensionFunction::ResponseCallback
& callback
);
157 content::BrowserContext
* browser_context_
;
161 // This map doesn't own either the keys or the values. When a RenderFrameHost
162 // instance goes away, the corresponding entry in this map (if exists) will be
164 typedef std::map
<content::RenderFrameHost
*, UIThreadResponseCallbackWrapper
*>
165 UIThreadResponseCallbackWrapperMap
;
166 UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_
;
169 } // namespace extensions
171 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_