Adjust the way GN resolves command line args into files or labels.
[chromium-blink-merge.git] / extensions / renderer / dispatcher.h
blob5ec0c6cb473aeb1d6170098479f1d7126b1a5b1a
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/memory/scoped_ptr.h"
15 #include "base/scoped_observer.h"
16 #include "base/timer/timer.h"
17 #include "content/public/renderer/render_process_observer.h"
18 #include "extensions/common/event_filter.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_set.h"
21 #include "extensions/common/extensions_client.h"
22 #include "extensions/common/features/feature.h"
23 #include "extensions/renderer/resource_bundle_source_map.h"
24 #include "extensions/renderer/script_context.h"
25 #include "extensions/renderer/script_context_set.h"
26 #include "extensions/renderer/user_script_set_manager.h"
27 #include "extensions/renderer/v8_schema_registry.h"
28 #include "third_party/WebKit/public/platform/WebString.h"
29 #include "third_party/WebKit/public/platform/WebVector.h"
30 #include "v8/include/v8.h"
32 class ChromeRenderViewTest;
33 class GURL;
34 class ModuleSystem;
35 class URLPattern;
36 struct ExtensionMsg_ExternalConnectionInfo;
37 struct ExtensionMsg_Loaded_Params;
38 struct ExtensionMsg_TabConnectionInfo;
39 struct ExtensionMsg_UpdatePermissions_Params;
41 namespace blink {
42 class WebFrame;
43 class WebLocalFrame;
44 class WebSecurityOrigin;
47 namespace base {
48 class ListValue;
51 namespace content {
52 class RenderThread;
55 namespace extensions {
56 class ContentWatcher;
57 class DispatcherDelegate;
58 class FilteredEventRouter;
59 class ManifestPermissionSet;
60 class RequestSender;
61 class ScriptContext;
62 class ScriptInjectionManager;
63 struct Message;
65 // Dispatches extension control messages sent to the renderer and stores
66 // renderer extension related state.
67 class Dispatcher : public content::RenderProcessObserver,
68 public UserScriptSetManager::Observer {
69 public:
70 explicit Dispatcher(DispatcherDelegate* delegate);
71 ~Dispatcher() override;
73 const std::set<std::string>& function_names() const {
74 return function_names_;
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 void DidCreateScriptContext(blink::WebLocalFrame* frame,
94 const v8::Local<v8::Context>& context,
95 int extension_group,
96 int world_id);
98 void WillReleaseScriptContext(blink::WebLocalFrame* frame,
99 const v8::Local<v8::Context>& context,
100 int world_id);
102 void DidCreateDocumentElement(blink::WebFrame* frame);
104 void DidMatchCSS(
105 blink::WebFrame* frame,
106 const blink::WebVector<blink::WebString>& newly_matching_selectors,
107 const blink::WebVector<blink::WebString>& stopped_matching_selectors);
109 void OnExtensionResponse(int request_id,
110 bool success,
111 const base::ListValue& response,
112 const std::string& error);
114 // Dispatches the event named |event_name| to all render views.
115 void DispatchEvent(const std::string& extension_id,
116 const std::string& event_name) const;
118 // Shared implementation of the various MessageInvoke IPCs.
119 void InvokeModuleSystemMethod(content::RenderView* render_view,
120 const std::string& extension_id,
121 const std::string& module_name,
122 const std::string& function_name,
123 const base::ListValue& args,
124 bool user_gesture);
126 void ClearPortData(int port_id);
128 // Returns a list of (module name, resource id) pairs for the JS modules to
129 // add to the source map.
130 static std::vector<std::pair<std::string, int> > GetJsResources();
131 static void RegisterNativeHandlers(ModuleSystem* module_system,
132 ScriptContext* context,
133 Dispatcher* dispatcher,
134 RequestSender* request_sender,
135 V8SchemaRegistry* v8_schema_registry);
137 bool WasWebRequestUsedBySomeExtensions() const { return webrequest_used_; }
139 private:
140 friend class ::ChromeRenderViewTest;
141 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest,
142 CannotScriptWebstore);
144 // RenderProcessObserver implementation:
145 bool OnControlMessageReceived(const IPC::Message& message) override;
146 void WebKitInitialized() override;
147 void IdleNotification() override;
148 void OnRenderProcessShutdown() override;
150 void OnActivateExtension(const std::string& extension_id);
151 void OnCancelSuspend(const std::string& extension_id);
152 void OnDeliverMessage(int target_port_id, const Message& message);
153 void OnDispatchOnConnect(int target_port_id,
154 const std::string& channel_name,
155 const ExtensionMsg_TabConnectionInfo& source,
156 const ExtensionMsg_ExternalConnectionInfo& info,
157 const std::string& tls_channel_id);
158 void OnDispatchOnDisconnect(int port_id, const std::string& error_message);
159 void OnLoaded(
160 const std::vector<ExtensionMsg_Loaded_Params>& loaded_extensions);
161 void OnLoadedInternal(scoped_refptr<const Extension> extension);
162 void OnMessageInvoke(const std::string& extension_id,
163 const std::string& module_name,
164 const std::string& function_name,
165 const base::ListValue& args,
166 bool user_gesture);
167 void OnSetChannel(int channel);
168 void OnSetFunctionNames(const std::vector<std::string>& names);
169 void OnSetScriptingWhitelist(
170 const ExtensionsClient::ScriptingWhitelist& extension_ids);
171 void OnSetSystemFont(const std::string& font_family,
172 const std::string& font_size);
173 void OnShouldSuspend(const std::string& extension_id, uint64 sequence_id);
174 void OnSuspend(const std::string& extension_id);
175 void OnTransferBlobs(const std::vector<std::string>& blob_uuids);
176 void OnUnloaded(const std::string& id);
177 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params& params);
178 void OnUpdateTabSpecificPermissions(const GURL& visible_url,
179 const std::string& extension_id,
180 const URLPatternSet& new_hosts,
181 bool update_origin_whitelist,
182 int tab_id);
183 void OnClearTabSpecificPermissions(
184 const std::vector<std::string>& extension_ids,
185 bool update_origin_whitelist,
186 int tab_id);
187 void OnUsingWebRequestAPI(bool webrequest_used);
189 // UserScriptSetManager::Observer implementation.
190 void OnUserScriptsUpdated(const std::set<HostID>& changed_hosts,
191 const std::vector<UserScript*>& scripts) override;
193 void UpdateActiveExtensions();
195 // Sets up the host permissions for |extension|.
196 void InitOriginPermissions(const Extension* extension);
198 // Updates the host permissions for the extension url to include only those in
199 // |new_patterns|, and remove from |old_patterns| that are no longer allowed.
200 void UpdateOriginPermissions(const GURL& extension_url,
201 const URLPatternSet& old_patterns,
202 const URLPatternSet& new_patterns);
204 // Enable custom element whitelist in Apps.
205 void EnableCustomElementWhiteList();
207 // Adds or removes bindings for every context belonging to |extension_id|, or
208 // or all contexts if |extension_id| is empty.
209 void UpdateBindings(const std::string& extension_id);
211 void UpdateBindingsForContext(ScriptContext* context);
213 void RegisterBinding(const std::string& api_name, ScriptContext* context);
215 void RegisterNativeHandlers(ModuleSystem* module_system,
216 ScriptContext* context);
218 // Determines if a ScriptContext can connect to any externally_connectable-
219 // enabled extension.
220 bool IsRuntimeAvailableToContext(ScriptContext* context);
222 // Updates a web page context with any content capabilities granted by active
223 // extensions.
224 void UpdateContentCapabilities(ScriptContext* context);
226 // Inserts static source code into |source_map_|.
227 void PopulateSourceMap();
229 // Returns whether the current renderer hosts a platform app.
230 bool IsWithinPlatformApp();
232 // Gets |field| from |object| or creates it as an empty object if it doesn't
233 // exist.
234 v8::Local<v8::Object> GetOrCreateObject(const v8::Local<v8::Object>& object,
235 const std::string& field,
236 v8::Isolate* isolate);
238 v8::Local<v8::Object> GetOrCreateBindObjectIfAvailable(
239 const std::string& api_name,
240 std::string* bind_name,
241 ScriptContext* context);
243 // Requires the GuestView modules in the module system of the ScriptContext
244 // |context|.
245 void RequireGuestViewModules(ScriptContext* context);
247 // The delegate for this dispatcher. Not owned, but must extend beyond the
248 // Dispatcher's own lifetime.
249 DispatcherDelegate* delegate_;
251 // True if the IdleNotification timer should be set.
252 bool set_idle_notifications_;
254 // Contains all loaded extensions. This is essentially the renderer
255 // counterpart to ExtensionService in the browser. It contains information
256 // about all extensions currently loaded by the browser.
257 ExtensionSet extensions_;
259 // The IDs of extensions that failed to load, mapped to the error message
260 // generated on failure.
261 std::map<std::string, std::string> extension_load_errors_;
263 // All the bindings contexts that are currently loaded for this renderer.
264 // There is zero or one for each v8 context.
265 scoped_ptr<ScriptContextSet> script_context_set_;
267 scoped_ptr<ContentWatcher> content_watcher_;
269 scoped_ptr<UserScriptSetManager> user_script_set_manager_;
271 scoped_ptr<ScriptInjectionManager> script_injection_manager_;
273 // Same as above, but on a longer timer and will run even if the process is
274 // not idle, to ensure that IdleHandle gets called eventually.
275 scoped_ptr<base::RepeatingTimer<content::RenderThread> > forced_idle_timer_;
277 // All declared function names.
278 std::set<std::string> function_names_;
280 // The extensions and apps that are active in this process.
281 ExtensionIdSet active_extension_ids_;
283 ResourceBundleSourceMap source_map_;
285 // Cache for the v8 representation of extension API schemas.
286 scoped_ptr<V8SchemaRegistry> v8_schema_registry_;
288 // Sends API requests to the extension host.
289 scoped_ptr<RequestSender> request_sender_;
291 // The platforms system font family and size;
292 std::string system_font_family_;
293 std::string system_font_size_;
295 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
296 std::map<int, int> port_to_tab_id_map_;
298 // True once WebKit has been initialized (and it is therefore safe to poke).
299 bool is_webkit_initialized_;
301 // It is important for this to come after the ScriptInjectionManager, so that
302 // the observer is destroyed before the UserScriptSet.
303 ScopedObserver<UserScriptSetManager, UserScriptSetManager::Observer>
304 user_script_set_manager_observer_;
306 // Status of webrequest usage.
307 bool webrequest_used_;
309 DISALLOW_COPY_AND_ASSIGN(Dispatcher);
312 } // namespace extensions
314 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_