NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / extension_function_dispatcher.h
blob64e6ead4cd873c2284fe540f8b35f9cd0173d183
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_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/memory/weak_ptr.h"
13 #include "extensions/browser/extension_function.h"
14 #include "ipc/ipc_sender.h"
16 class ChromeRenderMessageFilter;
17 struct ExtensionHostMsg_Request_Params;
19 namespace content {
20 class BrowserContext;
21 class RenderFrameHost;
22 class RenderViewHost;
23 class WebContents;
26 namespace extensions {
27 class Extension;
28 class ExtensionAPI;
29 class InfoMap;
30 class ProcessMap;
31 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> {
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;
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;
70 protected:
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 // Resets all functions to their initial implementation.
83 static void ResetFunctions();
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);
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);
102 ~ExtensionFunctionDispatcher();
104 Delegate* delegate() { return delegate_; }
106 // Message handlers.
107 // The response is sent to the corresponding render view in an
108 // ExtensionMsg_Response message.
109 // TODO (jam): convert all callers to use RenderFrameHost.
110 void Dispatch(const ExtensionHostMsg_Request_Params& params,
111 content::RenderViewHost* render_view_host);
112 // Dispatch an extension function and calls |callback| when the execution
113 // completes.
114 void DispatchWithCallback(
115 const ExtensionHostMsg_Request_Params& params,
116 content::RenderFrameHost* render_frame_host,
117 const ExtensionFunction::ResponseCallback& callback);
119 // Called when an ExtensionFunction is done executing, after it has sent
120 // a response (if any) to the extension.
121 void OnExtensionFunctionCompleted(const extensions::Extension* extension);
123 // The BrowserContext that this dispatcher is associated with.
124 content::BrowserContext* browser_context() { return browser_context_; }
126 private:
127 // For a given RenderViewHost instance, UIThreadResponseCallbackWrapper
128 // creates ExtensionFunction::ResponseCallback instances which send responses
129 // to the corresponding render view in ExtensionMsg_Response messages.
130 // This class tracks the lifespan of the RenderViewHost instance, and will be
131 // destroyed automatically when it goes away.
132 class UIThreadResponseCallbackWrapper;
134 // Helper to check whether an ExtensionFunction has the required permissions.
135 // This should be called after the function is fully initialized.
136 // If the check fails, |callback| is run with an access-denied error and false
137 // is returned. |function| must not be run in that case.
138 static bool CheckPermissions(
139 ExtensionFunction* function,
140 const extensions::Extension* extension,
141 const ExtensionHostMsg_Request_Params& params,
142 const ExtensionFunction::ResponseCallback& callback);
144 // Helper to create an ExtensionFunction to handle the function given by
145 // |params|. Can be called on any thread.
146 // Does not set subclass properties, or include_incognito.
147 static ExtensionFunction* CreateExtensionFunction(
148 const ExtensionHostMsg_Request_Params& params,
149 const extensions::Extension* extension,
150 int requesting_process_id,
151 const extensions::ProcessMap& process_map,
152 extensions::ExtensionAPI* api,
153 void* profile,
154 const ExtensionFunction::ResponseCallback& callback);
156 // Helper to run the response callback with an access denied error. Can be
157 // called on any thread.
158 static void SendAccessDenied(
159 const ExtensionFunction::ResponseCallback& callback);
161 void DispatchWithCallbackInternal(
162 const ExtensionHostMsg_Request_Params& params,
163 content::RenderViewHost* render_view_host,
164 content::RenderFrameHost* render_frame_host,
165 const ExtensionFunction::ResponseCallback& callback);
167 content::BrowserContext* browser_context_;
169 Delegate* delegate_;
171 // This map doesn't own either the keys or the values. When a RenderViewHost
172 // instance goes away, the corresponding entry in this map (if exists) will be
173 // removed.
174 typedef std::map<content::RenderViewHost*, UIThreadResponseCallbackWrapper*>
175 UIThreadResponseCallbackWrapperMap;
176 UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_;
179 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_