Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / extension_function_dispatcher.h
blob53ec8be4b997c4e1eb373c840793fe642ee25118
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_
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 struct ExtensionHostMsg_Request_Params;
18 namespace content {
19 class BrowserContext;
20 class RenderFrameHost;
21 class WebContents;
24 namespace extensions {
26 class Extension;
27 class ExtensionAPI;
28 class InfoMap;
29 class IOThreadExtensionMessageFilter;
30 class ProcessMap;
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> {
51 public:
52 class Delegate {
53 public:
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;
69 protected:
70 virtual ~Delegate() {}
73 // Gets a list of all known extension function names.
74 static void GetAllFunctionNames(std::vector<std::string>* names);
76 // Override a previously registered function. Returns true if successful,
77 // false if no such function was registered.
78 static bool OverrideFunction(const std::string& name,
79 ExtensionFunctionFactory factory);
81 // Dispatches an IO-thread extension function. Only used for specific
82 // functions that must be handled on the IO-thread.
83 static void DispatchOnIOThread(
84 InfoMap* extension_info_map,
85 void* profile_id,
86 int render_process_id,
87 base::WeakPtr<IOThreadExtensionMessageFilter> ipc_sender,
88 int routing_id,
89 const ExtensionHostMsg_Request_Params& params);
91 // Public constructor. Callers must ensure that:
92 // - This object outlives any RenderFrameHost's passed to created
93 // ExtensionFunctions.
94 explicit ExtensionFunctionDispatcher(
95 content::BrowserContext* browser_context);
96 ~ExtensionFunctionDispatcher();
98 // Message handlers.
99 // The response is sent to the corresponding render view in an
100 // ExtensionMsg_Response message.
101 void Dispatch(const ExtensionHostMsg_Request_Params& params,
102 content::RenderFrameHost* render_frame_host);
104 // Called when an ExtensionFunction is done executing, after it has sent
105 // a response (if any) to the extension.
106 void OnExtensionFunctionCompleted(const Extension* extension);
108 // See the Delegate class for documentation on these methods.
109 // TODO(devlin): None of these belong here. We should kill
110 // ExtensionFunctionDispatcher::Delegate.
111 WindowController* GetExtensionWindowController() const;
112 content::WebContents* GetAssociatedWebContents() const;
113 content::WebContents* GetVisibleWebContents() const;
115 // The BrowserContext that this dispatcher is associated with.
116 content::BrowserContext* browser_context() { return browser_context_; }
118 void set_delegate(Delegate* delegate) { delegate_ = delegate; }
120 private:
121 // For a given RenderFrameHost instance, UIThreadResponseCallbackWrapper
122 // creates ExtensionFunction::ResponseCallback instances which send responses
123 // to the corresponding render view in ExtensionMsg_Response messages.
124 // This class tracks the lifespan of the RenderFrameHost instance, and will be
125 // destroyed automatically when it goes away.
126 class UIThreadResponseCallbackWrapper;
128 // Helper to check whether an ExtensionFunction has the required permissions.
129 // This should be called after the function is fully initialized.
130 // If the check fails, |callback| is run with an access-denied error and false
131 // is returned. |function| must not be run in that case.
132 static bool CheckPermissions(
133 ExtensionFunction* function,
134 const ExtensionHostMsg_Request_Params& params,
135 const ExtensionFunction::ResponseCallback& callback);
137 // Helper to create an ExtensionFunction to handle the function given by
138 // |params|. Can be called on any thread.
139 // Does not set subclass properties, or include_incognito.
140 static ExtensionFunction* CreateExtensionFunction(
141 const ExtensionHostMsg_Request_Params& params,
142 const Extension* extension,
143 int requesting_process_id,
144 const ProcessMap& process_map,
145 ExtensionAPI* api,
146 void* profile_id,
147 const ExtensionFunction::ResponseCallback& callback);
149 // Helper to run the response callback with an access denied error. Can be
150 // called on any thread.
151 static void SendAccessDenied(
152 const ExtensionFunction::ResponseCallback& callback,
153 functions::HistogramValue histogram_value);
155 void DispatchWithCallbackInternal(
156 const ExtensionHostMsg_Request_Params& params,
157 content::RenderFrameHost* render_frame_host,
158 const ExtensionFunction::ResponseCallback& callback);
160 content::BrowserContext* browser_context_;
162 Delegate* delegate_;
164 // This map doesn't own either the keys or the values. When a RenderFrameHost
165 // instance goes away, the corresponding entry in this map (if exists) will be
166 // removed.
167 typedef std::map<content::RenderFrameHost*, UIThreadResponseCallbackWrapper*>
168 UIThreadResponseCallbackWrapperMap;
169 UIThreadResponseCallbackWrapperMap ui_thread_response_callback_wrappers_;
172 } // namespace extensions
174 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_DISPATCHER_H_