ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / extensions / renderer / dispatcher.h
blob44cb751a01e67321327e4affb5f6d52651b6f233
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_RENDERER_DISPATCHER_H_
6 #define EXTENSIONS_RENDERER_DISPATCHER_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/scoped_observer.h"
15 #include "base/timer/timer.h"
16 #include "content/public/renderer/render_process_observer.h"
17 #include "extensions/common/event_filter.h"
18 #include "extensions/common/extension_set.h"
19 #include "extensions/common/extensions_client.h"
20 #include "extensions/common/features/feature.h"
21 #include "extensions/renderer/resource_bundle_source_map.h"
22 #include "extensions/renderer/script_context.h"
23 #include "extensions/renderer/script_context_set.h"
24 #include "extensions/renderer/user_script_set_manager.h"
25 #include "extensions/renderer/v8_schema_registry.h"
26 #include "third_party/WebKit/public/platform/WebString.h"
27 #include "third_party/WebKit/public/platform/WebVector.h"
28 #include "v8/include/v8.h"
30 class ChromeRenderViewTest;
31 class GURL;
32 class ModuleSystem;
33 class URLPattern;
34 struct ExtensionMsg_ExternalConnectionInfo;
35 struct ExtensionMsg_Loaded_Params;
36 struct ExtensionMsg_TabConnectionInfo;
37 struct ExtensionMsg_UpdatePermissions_Params;
39 namespace blink {
40 class WebFrame;
41 class WebSecurityOrigin;
44 namespace base {
45 class ListValue;
48 namespace content {
49 class RenderThread;
52 namespace extensions {
53 class ContentWatcher;
54 class DispatcherDelegate;
55 class Extension;
56 class FilteredEventRouter;
57 class ManifestPermissionSet;
58 class RequestSender;
59 class ScriptContext;
60 class ScriptInjectionManager;
61 struct Message;
63 // Dispatches extension control messages sent to the renderer and stores
64 // renderer extension related state.
65 class Dispatcher : public content::RenderProcessObserver,
66 public UserScriptSetManager::Observer {
67 public:
68 explicit Dispatcher(DispatcherDelegate* delegate);
69 ~Dispatcher() override;
71 const std::set<std::string>& function_names() const {
72 return function_names_;
75 bool is_extension_process() const { return is_extension_process_; }
77 const ExtensionSet* extensions() const { return &extensions_; }
79 const ScriptContextSet& script_context_set() const {
80 return script_context_set_;
83 V8SchemaRegistry* v8_schema_registry() { return v8_schema_registry_.get(); }
85 ContentWatcher* content_watcher() { return content_watcher_.get(); }
87 RequestSender* request_sender() { return request_sender_.get(); }
89 void OnRenderViewCreated(content::RenderView* render_view);
91 bool IsExtensionActive(const std::string& extension_id) const;
93 // Finds the extension for the JavaScript context associated with the
94 // specified |frame| and isolated world. If |world_id| is zero, finds the
95 // extension ID associated with the main world's JavaScript context. If the
96 // JavaScript context isn't from an extension, returns empty string.
97 const Extension* GetExtensionFromFrameAndWorld(const blink::WebFrame* frame,
98 int world_id,
99 bool use_effective_url);
101 void DidCreateScriptContext(blink::WebFrame* frame,
102 const v8::Handle<v8::Context>& context,
103 int extension_group,
104 int world_id);
106 void WillReleaseScriptContext(blink::WebFrame* frame,
107 const v8::Handle<v8::Context>& context,
108 int world_id);
110 void DidCreateDocumentElement(blink::WebFrame* frame);
112 void DidMatchCSS(
113 blink::WebFrame* frame,
114 const blink::WebVector<blink::WebString>& newly_matching_selectors,
115 const blink::WebVector<blink::WebString>& stopped_matching_selectors);
117 void OnExtensionResponse(int request_id,
118 bool success,
119 const base::ListValue& response,
120 const std::string& error);
122 // Checks that the current context contains an extension that has permission
123 // to execute the specified function. If it does not, a v8 exception is thrown
124 // and the method returns false. Otherwise returns true.
125 bool CheckContextAccessToExtensionAPI(const std::string& function_name,
126 ScriptContext* context) const;
128 // Dispatches the event named |event_name| to all render views.
129 void DispatchEvent(const std::string& extension_id,
130 const std::string& event_name) const;
132 // Shared implementation of the various MessageInvoke IPCs.
133 void InvokeModuleSystemMethod(content::RenderView* render_view,
134 const std::string& extension_id,
135 const std::string& module_name,
136 const std::string& function_name,
137 const base::ListValue& args,
138 bool user_gesture);
140 void ClearPortData(int port_id);
142 // Returns a list of (module name, resource id) pairs for the JS modules to
143 // add to the source map.
144 static std::vector<std::pair<std::string, int> > GetJsResources();
145 static void RegisterNativeHandlers(ModuleSystem* module_system,
146 ScriptContext* context,
147 Dispatcher* dispatcher,
148 RequestSender* request_sender,
149 V8SchemaRegistry* v8_schema_registry);
151 bool WasWebRequestUsedBySomeExtensions() const { return webrequest_used_; }
153 private:
154 friend class ::ChromeRenderViewTest;
155 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest,
156 CannotScriptWebstore);
158 // RenderProcessObserver implementation:
159 bool OnControlMessageReceived(const IPC::Message& message) override;
160 void WebKitInitialized() override;
161 void IdleNotification() override;
162 void OnRenderProcessShutdown() override;
164 void OnActivateExtension(const std::string& extension_id);
165 void OnCancelSuspend(const std::string& extension_id);
166 void OnDeliverMessage(int target_port_id, const Message& message);
167 void OnDispatchOnConnect(int target_port_id,
168 const std::string& channel_name,
169 const ExtensionMsg_TabConnectionInfo& source,
170 const ExtensionMsg_ExternalConnectionInfo& info,
171 const std::string& tls_channel_id);
172 void OnDispatchOnDisconnect(int port_id, const std::string& error_message);
173 void OnLoaded(
174 const std::vector<ExtensionMsg_Loaded_Params>& loaded_extensions);
175 void OnLoadedInternal(scoped_refptr<const Extension> extension);
176 void OnMessageInvoke(const std::string& extension_id,
177 const std::string& module_name,
178 const std::string& function_name,
179 const base::ListValue& args,
180 bool user_gesture);
181 void OnSetChannel(int channel);
182 void OnSetFunctionNames(const std::vector<std::string>& names);
183 void OnSetScriptingWhitelist(
184 const ExtensionsClient::ScriptingWhitelist& extension_ids);
185 void OnSetSystemFont(const std::string& font_family,
186 const std::string& font_size);
187 void OnShouldSuspend(const std::string& extension_id, uint64 sequence_id);
188 void OnSuspend(const std::string& extension_id);
189 void OnTransferBlobs(const std::vector<std::string>& blob_uuids);
190 void OnUnloaded(const std::string& id);
191 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params& params);
192 void OnUpdateTabSpecificPermissions(const GURL& visible_url,
193 const std::string& extension_id,
194 const URLPatternSet& new_hosts,
195 bool update_origin_whitelist,
196 int tab_id);
197 void OnClearTabSpecificPermissions(
198 const std::vector<std::string>& extension_ids,
199 bool update_origin_whitelist,
200 int tab_id);
201 void OnUsingWebRequestAPI(bool webrequest_used);
203 // UserScriptSetManager::Observer implementation.
204 void OnUserScriptsUpdated(const std::set<std::string>& changed_extensions,
205 const std::vector<UserScript*>& scripts) override;
207 void UpdateActiveExtensions();
209 // Sets up the host permissions for |extension|.
210 void InitOriginPermissions(const Extension* extension);
212 // Updates the host permissions for the extension url to include only those in
213 // |new_patterns|, and remove from |old_patterns| that are no longer allowed.
214 void UpdateOriginPermissions(const GURL& extension_url,
215 const URLPatternSet& old_patterns,
216 const URLPatternSet& new_patterns);
218 // Enable custom element whitelist in Apps.
219 void EnableCustomElementWhiteList();
221 // Adds or removes bindings for every context belonging to |extension_id|, or
222 // or all contexts if |extension_id| is empty.
223 void UpdateBindings(const std::string& extension_id);
225 void UpdateBindingsForContext(ScriptContext* context);
227 void RegisterBinding(const std::string& api_name, ScriptContext* context);
229 void RegisterNativeHandlers(ModuleSystem* module_system,
230 ScriptContext* context);
232 // Determines if a ScriptContext can connect to any externally_connectable-
233 // enabled extension.
234 bool IsRuntimeAvailableToContext(ScriptContext* context);
236 // Updates a web page context with any content capabilities granted by active
237 // extensions.
238 void UpdateContentCapabilities(ScriptContext* context);
240 // Inserts static source code into |source_map_|.
241 void PopulateSourceMap();
243 // Returns whether the current renderer hosts a platform app.
244 bool IsWithinPlatformApp();
246 bool IsSandboxedPage(const GURL& url) const;
248 // Returns the Feature::Context type of context for a JavaScript context.
249 Feature::Context ClassifyJavaScriptContext(
250 const Extension* extension,
251 int extension_group,
252 const GURL& url,
253 const blink::WebSecurityOrigin& origin);
255 // Gets |field| from |object| or creates it as an empty object if it doesn't
256 // exist.
257 v8::Handle<v8::Object> GetOrCreateObject(const v8::Handle<v8::Object>& object,
258 const std::string& field,
259 v8::Isolate* isolate);
261 v8::Handle<v8::Object> GetOrCreateBindObjectIfAvailable(
262 const std::string& api_name,
263 std::string* bind_name,
264 ScriptContext* context);
266 // The delegate for this dispatcher. Not owned, but must extend beyond the
267 // Dispatcher's own lifetime.
268 DispatcherDelegate* delegate_;
270 // True if this renderer is running extensions.
271 bool is_extension_process_;
273 // Contains all loaded extensions. This is essentially the renderer
274 // counterpart to ExtensionService in the browser. It contains information
275 // about all extensions currently loaded by the browser.
276 ExtensionSet extensions_;
278 // The IDs of extensions that failed to load, mapped to the error message
279 // generated on failure.
280 std::map<std::string, std::string> extension_load_errors_;
282 // All the bindings contexts that are currently loaded for this renderer.
283 // There is zero or one for each v8 context.
284 ScriptContextSet script_context_set_;
286 scoped_ptr<ContentWatcher> content_watcher_;
288 scoped_ptr<UserScriptSetManager> user_script_set_manager_;
290 scoped_ptr<ScriptInjectionManager> script_injection_manager_;
292 // Same as above, but on a longer timer and will run even if the process is
293 // not idle, to ensure that IdleHandle gets called eventually.
294 scoped_ptr<base::RepeatingTimer<content::RenderThread> > forced_idle_timer_;
296 // All declared function names.
297 std::set<std::string> function_names_;
299 // The extensions and apps that are active in this process.
300 std::set<std::string> active_extension_ids_;
302 ResourceBundleSourceMap source_map_;
304 // Cache for the v8 representation of extension API schemas.
305 scoped_ptr<V8SchemaRegistry> v8_schema_registry_;
307 // Sends API requests to the extension host.
308 scoped_ptr<RequestSender> request_sender_;
310 // The platforms system font family and size;
311 std::string system_font_family_;
312 std::string system_font_size_;
314 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
315 std::map<int, int> port_to_tab_id_map_;
317 // True once WebKit has been initialized (and it is therefore safe to poke).
318 bool is_webkit_initialized_;
320 // It is important for this to come after the ScriptInjectionManager, so that
321 // the observer is destroyed before the UserScriptSet.
322 ScopedObserver<UserScriptSetManager, UserScriptSetManager::Observer>
323 user_script_set_manager_observer_;
325 // Status of webrequest usage.
326 bool webrequest_used_;
328 DISALLOW_COPY_AND_ASSIGN(Dispatcher);
331 } // namespace extensions
333 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_