Roll src/third_party/skia 2440fcd:4de8c3a
[chromium-blink-merge.git] / extensions / renderer / dispatcher.h
blob5d6bb6ad099e352ad412204462899e955e87443c
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/extensions_client.h"
21 #include "extensions/common/features/feature.h"
22 #include "extensions/renderer/resource_bundle_source_map.h"
23 #include "extensions/renderer/script_context.h"
24 #include "extensions/renderer/script_context_set.h"
25 #include "extensions/renderer/user_script_set_manager.h"
26 #include "extensions/renderer/v8_schema_registry.h"
27 #include "third_party/WebKit/public/platform/WebString.h"
28 #include "third_party/WebKit/public/platform/WebVector.h"
29 #include "v8/include/v8.h"
31 class ChromeRenderViewTest;
32 class GURL;
33 class ModuleSystem;
34 class URLPattern;
35 struct ExtensionMsg_ExternalConnectionInfo;
36 struct ExtensionMsg_Loaded_Params;
37 struct ExtensionMsg_TabConnectionInfo;
38 struct ExtensionMsg_UpdatePermissions_Params;
40 namespace blink {
41 class WebFrame;
42 class WebLocalFrame;
43 class WebSecurityOrigin;
46 namespace base {
47 class ListValue;
50 namespace content {
51 class RenderThread;
54 namespace extensions {
55 class ContentWatcher;
56 class DispatcherDelegate;
57 class FilteredEventRouter;
58 class ManifestPermissionSet;
59 class RequestSender;
60 class ScriptContext;
61 class ScriptInjectionManager;
62 struct Message;
64 // Dispatches extension control messages sent to the renderer and stores
65 // renderer extension related state.
66 class Dispatcher : public content::RenderProcessObserver,
67 public UserScriptSetManager::Observer {
68 public:
69 explicit Dispatcher(DispatcherDelegate* delegate);
70 ~Dispatcher() override;
72 const ScriptContextSet& script_context_set() const {
73 return *script_context_set_;
76 V8SchemaRegistry* v8_schema_registry() { return v8_schema_registry_.get(); }
78 ContentWatcher* content_watcher() { return content_watcher_.get(); }
80 RequestSender* request_sender() { return request_sender_.get(); }
82 const std::string& webview_partition_id() { return webview_partition_id_; }
84 void OnRenderFrameCreated(content::RenderFrame* render_frame);
86 bool IsExtensionActive(const std::string& extension_id) const;
88 void DidCreateScriptContext(blink::WebLocalFrame* frame,
89 const v8::Local<v8::Context>& context,
90 int extension_group,
91 int world_id);
93 // Runs on a different thread and should not use any member variables.
94 static void DidInitializeServiceWorkerContextOnWorkerThread(
95 v8::Local<v8::Context> v8_context,
96 const GURL& url);
98 void WillReleaseScriptContext(blink::WebLocalFrame* frame,
99 const v8::Local<v8::Context>& context,
100 int world_id);
102 // Runs on a different thread and should not use any member variables.
103 static void WillDestroyServiceWorkerContextOnWorkerThread(
104 v8::Local<v8::Context> v8_context,
105 const GURL& url);
107 void DidCreateDocumentElement(blink::WebLocalFrame* frame);
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::RenderFrame* render_frame,
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 // The RendererPermissionsPolicyDelegateTest.CannotScriptWebstore test needs
141 // to call the OnActivateExtension IPCs.
142 friend class ::ChromeRenderViewTest;
143 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest,
144 CannotScriptWebstore);
146 // RenderProcessObserver implementation:
147 bool OnControlMessageReceived(const IPC::Message& message) override;
148 void WebKitInitialized() override;
149 void IdleNotification() override;
150 void OnRenderProcessShutdown() override;
152 void OnActivateExtension(const std::string& extension_id);
153 void OnCancelSuspend(const std::string& extension_id);
154 void OnDeliverMessage(int target_port_id, const Message& message);
155 void OnDispatchOnConnect(int target_port_id,
156 const std::string& channel_name,
157 const ExtensionMsg_TabConnectionInfo& source,
158 const ExtensionMsg_ExternalConnectionInfo& info,
159 const std::string& tls_channel_id);
160 void OnDispatchOnDisconnect(int port_id, const std::string& error_message);
161 void OnLoaded(
162 const std::vector<ExtensionMsg_Loaded_Params>& loaded_extensions);
163 void OnMessageInvoke(const std::string& extension_id,
164 const std::string& module_name,
165 const std::string& function_name,
166 const base::ListValue& args,
167 bool user_gesture);
168 void OnSetChannel(int channel);
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 OnSetWebViewPartitionID(const std::string& partition_id);
174 void OnShouldSuspend(const std::string& extension_id, uint64 sequence_id);
175 void OnSuspend(const std::string& extension_id);
176 void OnTransferBlobs(const std::vector<std::string>& blob_uuids);
177 void OnUnloaded(const std::string& id);
178 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params& params);
179 void OnUpdateTabSpecificPermissions(const GURL& visible_url,
180 const std::string& extension_id,
181 const URLPatternSet& new_hosts,
182 bool update_origin_whitelist,
183 int tab_id);
184 void OnClearTabSpecificPermissions(
185 const std::vector<std::string>& extension_ids,
186 bool update_origin_whitelist,
187 int tab_id);
188 void OnUsingWebRequestAPI(bool webrequest_used);
190 // UserScriptSetManager::Observer implementation.
191 void OnUserScriptsUpdated(const std::set<HostID>& changed_hosts,
192 const std::vector<UserScript*>& scripts) override;
194 void UpdateActiveExtensions();
196 // Sets up the host permissions for |extension|.
197 void InitOriginPermissions(const Extension* extension);
199 // Updates the host permissions for the extension url to include only those in
200 // |new_patterns|, and remove from |old_patterns| that are no longer allowed.
201 void UpdateOriginPermissions(const GURL& extension_url,
202 const URLPatternSet& old_patterns,
203 const URLPatternSet& new_patterns);
205 // Enable custom element whitelist in Apps.
206 void EnableCustomElementWhiteList();
208 // Adds or removes bindings for every context belonging to |extension_id|, or
209 // or all contexts if |extension_id| is empty.
210 void UpdateBindings(const std::string& extension_id);
212 void UpdateBindingsForContext(ScriptContext* context);
214 void RegisterBinding(const std::string& api_name, ScriptContext* context);
216 void RegisterNativeHandlers(ModuleSystem* module_system,
217 ScriptContext* context);
219 // Determines if a ScriptContext can connect to any externally_connectable-
220 // enabled extension.
221 bool IsRuntimeAvailableToContext(ScriptContext* context);
223 // Updates a web page context with any content capabilities granted by active
224 // extensions.
225 void UpdateContentCapabilities(ScriptContext* context);
227 // Inserts static source code into |source_map_|.
228 void PopulateSourceMap();
230 // Returns whether the current renderer hosts a platform app.
231 bool IsWithinPlatformApp();
233 // Gets |field| from |object| or creates it as an empty object if it doesn't
234 // exist.
235 v8::Local<v8::Object> GetOrCreateObject(const v8::Local<v8::Object>& object,
236 const std::string& field,
237 v8::Isolate* isolate);
239 v8::Local<v8::Object> GetOrCreateBindObjectIfAvailable(
240 const std::string& api_name,
241 std::string* bind_name,
242 ScriptContext* context);
244 // Requires the GuestView modules in the module system of the ScriptContext
245 // |context|.
246 void RequireGuestViewModules(ScriptContext* context);
248 // Adds features that are specific to the current channel.
249 void AddChannelSpecificFeatures();
251 // The delegate for this dispatcher. Not owned, but must extend beyond the
252 // Dispatcher's own lifetime.
253 DispatcherDelegate* delegate_;
255 // True if the IdleNotification timer should be set.
256 bool set_idle_notifications_;
258 // The IDs of extensions that failed to load, mapped to the error message
259 // generated on failure.
260 std::map<std::string, std::string> extension_load_errors_;
262 // All the bindings contexts that are currently loaded for this renderer.
263 // There is zero or one for each v8 context.
264 scoped_ptr<ScriptContextSet> script_context_set_;
266 scoped_ptr<ContentWatcher> content_watcher_;
268 scoped_ptr<UserScriptSetManager> user_script_set_manager_;
270 scoped_ptr<ScriptInjectionManager> script_injection_manager_;
272 // Same as above, but on a longer timer and will run even if the process is
273 // not idle, to ensure that IdleHandle gets called eventually.
274 scoped_ptr<base::RepeatingTimer<content::RenderThread> > forced_idle_timer_;
276 // The extensions and apps that are active in this process.
277 ExtensionIdSet active_extension_ids_;
279 ResourceBundleSourceMap source_map_;
281 // Cache for the v8 representation of extension API schemas.
282 scoped_ptr<V8SchemaRegistry> v8_schema_registry_;
284 // Sends API requests to the extension host.
285 scoped_ptr<RequestSender> request_sender_;
287 // The platforms system font family and size;
288 std::string system_font_family_;
289 std::string system_font_size_;
291 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
292 std::map<int, int> port_to_tab_id_map_;
294 // True once WebKit has been initialized (and it is therefore safe to poke).
295 bool is_webkit_initialized_;
297 // It is important for this to come after the ScriptInjectionManager, so that
298 // the observer is destroyed before the UserScriptSet.
299 ScopedObserver<UserScriptSetManager, UserScriptSetManager::Observer>
300 user_script_set_manager_observer_;
302 // Status of webrequest usage.
303 bool webrequest_used_;
305 // The WebView partition ID associated with this process's storage partition,
306 // if this renderer is a WebView guest render process. Otherwise, this will be
307 // empty.
308 std::string webview_partition_id_;
310 DISALLOW_COPY_AND_ASSIGN(Dispatcher);
313 } // namespace extensions
315 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_