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_
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
;
36 struct ExtensionMsg_ExternalConnectionInfo
;
37 struct ExtensionMsg_Loaded_Params
;
38 struct ExtensionMsg_TabConnectionInfo
;
39 struct ExtensionMsg_UpdatePermissions_Params
;
44 class WebSecurityOrigin
;
55 namespace extensions
{
57 class DispatcherDelegate
;
58 class FilteredEventRouter
;
59 class ManifestPermissionSet
;
62 class ScriptInjectionManager
;
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
{
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 OnRenderFrameCreated(content::RenderFrame
* render_frame
);
91 bool IsExtensionActive(const std::string
& extension_id
) const;
93 void DidCreateScriptContext(blink::WebLocalFrame
* frame
,
94 const v8::Local
<v8::Context
>& context
,
98 // Runs on a different thread and should not use any member variables.
99 static void DidInitializeServiceWorkerContextOnWorkerThread(
100 v8::Local
<v8::Context
> context
,
103 void WillReleaseScriptContext(blink::WebLocalFrame
* frame
,
104 const v8::Local
<v8::Context
>& context
,
107 void DidCreateDocumentElement(blink::WebLocalFrame
* frame
);
109 void OnExtensionResponse(int request_id
,
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
,
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_
; }
140 // The RendererPermissionsPolicyDelegateTest.CannotScriptWebstore test needs
141 // to call LoadExtensionForTest and the OnActivateExtension IPCs.
142 friend class ::ChromeRenderViewTest
;
143 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest
,
144 CannotScriptWebstore
);
146 // Inserts an Extension into |extensions_|. Normally the only way to do this
147 // would be through the ExtensionMsg_Loaded IPC (OnLoaded) but this can't be
148 // triggered for tests, because in the process of serializing then
149 // deserializing the IPC, Extension IDs manually set for testing are lost.
150 void LoadExtensionForTest(const Extension
* extension
);
152 // RenderProcessObserver implementation:
153 bool OnControlMessageReceived(const IPC::Message
& message
) override
;
154 void WebKitInitialized() override
;
155 void IdleNotification() override
;
156 void OnRenderProcessShutdown() override
;
158 void OnActivateExtension(const std::string
& extension_id
);
159 void OnCancelSuspend(const std::string
& extension_id
);
160 void OnDeliverMessage(int target_port_id
, const Message
& message
);
161 void OnDispatchOnConnect(int target_port_id
,
162 const std::string
& channel_name
,
163 const ExtensionMsg_TabConnectionInfo
& source
,
164 const ExtensionMsg_ExternalConnectionInfo
& info
,
165 const std::string
& tls_channel_id
);
166 void OnDispatchOnDisconnect(int port_id
, const std::string
& error_message
);
168 const std::vector
<ExtensionMsg_Loaded_Params
>& loaded_extensions
);
169 void OnMessageInvoke(const std::string
& extension_id
,
170 const std::string
& module_name
,
171 const std::string
& function_name
,
172 const base::ListValue
& args
,
174 void OnSetChannel(int channel
);
175 void OnSetFunctionNames(const std::vector
<std::string
>& names
);
176 void OnSetScriptingWhitelist(
177 const ExtensionsClient::ScriptingWhitelist
& extension_ids
);
178 void OnSetSystemFont(const std::string
& font_family
,
179 const std::string
& font_size
);
180 void OnShouldSuspend(const std::string
& extension_id
, uint64 sequence_id
);
181 void OnSuspend(const std::string
& extension_id
);
182 void OnTransferBlobs(const std::vector
<std::string
>& blob_uuids
);
183 void OnUnloaded(const std::string
& id
);
184 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params
& params
);
185 void OnUpdateTabSpecificPermissions(const GURL
& visible_url
,
186 const std::string
& extension_id
,
187 const URLPatternSet
& new_hosts
,
188 bool update_origin_whitelist
,
190 void OnClearTabSpecificPermissions(
191 const std::vector
<std::string
>& extension_ids
,
192 bool update_origin_whitelist
,
194 void OnUsingWebRequestAPI(bool webrequest_used
);
196 // UserScriptSetManager::Observer implementation.
197 void OnUserScriptsUpdated(const std::set
<HostID
>& changed_hosts
,
198 const std::vector
<UserScript
*>& scripts
) override
;
200 void UpdateActiveExtensions();
202 // Sets up the host permissions for |extension|.
203 void InitOriginPermissions(const Extension
* extension
);
205 // Updates the host permissions for the extension url to include only those in
206 // |new_patterns|, and remove from |old_patterns| that are no longer allowed.
207 void UpdateOriginPermissions(const GURL
& extension_url
,
208 const URLPatternSet
& old_patterns
,
209 const URLPatternSet
& new_patterns
);
211 // Enable custom element whitelist in Apps.
212 void EnableCustomElementWhiteList();
214 // Adds or removes bindings for every context belonging to |extension_id|, or
215 // or all contexts if |extension_id| is empty.
216 void UpdateBindings(const std::string
& extension_id
);
218 void UpdateBindingsForContext(ScriptContext
* context
);
220 void RegisterBinding(const std::string
& api_name
, ScriptContext
* context
);
222 void RegisterNativeHandlers(ModuleSystem
* module_system
,
223 ScriptContext
* context
);
225 // Determines if a ScriptContext can connect to any externally_connectable-
226 // enabled extension.
227 bool IsRuntimeAvailableToContext(ScriptContext
* context
);
229 // Updates a web page context with any content capabilities granted by active
231 void UpdateContentCapabilities(ScriptContext
* context
);
233 // Inserts static source code into |source_map_|.
234 void PopulateSourceMap();
236 // Returns whether the current renderer hosts a platform app.
237 bool IsWithinPlatformApp();
239 // Gets |field| from |object| or creates it as an empty object if it doesn't
241 v8::Local
<v8::Object
> GetOrCreateObject(const v8::Local
<v8::Object
>& object
,
242 const std::string
& field
,
243 v8::Isolate
* isolate
);
245 v8::Local
<v8::Object
> GetOrCreateBindObjectIfAvailable(
246 const std::string
& api_name
,
247 std::string
* bind_name
,
248 ScriptContext
* context
);
250 // Requires the GuestView modules in the module system of the ScriptContext
252 void RequireGuestViewModules(ScriptContext
* context
);
254 // Adds features that are specific to the current channel.
255 void AddChannelSpecificFeatures();
257 // The delegate for this dispatcher. Not owned, but must extend beyond the
258 // Dispatcher's own lifetime.
259 DispatcherDelegate
* delegate_
;
261 // True if the IdleNotification timer should be set.
262 bool set_idle_notifications_
;
264 // Contains all loaded extensions. This is essentially the renderer
265 // counterpart to ExtensionService in the browser. It contains information
266 // about all extensions currently loaded by the browser.
267 ExtensionSet extensions_
;
269 // The IDs of extensions that failed to load, mapped to the error message
270 // generated on failure.
271 std::map
<std::string
, std::string
> extension_load_errors_
;
273 // All the bindings contexts that are currently loaded for this renderer.
274 // There is zero or one for each v8 context.
275 scoped_ptr
<ScriptContextSet
> script_context_set_
;
277 scoped_ptr
<ContentWatcher
> content_watcher_
;
279 scoped_ptr
<UserScriptSetManager
> user_script_set_manager_
;
281 scoped_ptr
<ScriptInjectionManager
> script_injection_manager_
;
283 // Same as above, but on a longer timer and will run even if the process is
284 // not idle, to ensure that IdleHandle gets called eventually.
285 scoped_ptr
<base::RepeatingTimer
<content::RenderThread
> > forced_idle_timer_
;
287 // All declared function names.
288 std::set
<std::string
> function_names_
;
290 // The extensions and apps that are active in this process.
291 ExtensionIdSet active_extension_ids_
;
293 ResourceBundleSourceMap source_map_
;
295 // Cache for the v8 representation of extension API schemas.
296 scoped_ptr
<V8SchemaRegistry
> v8_schema_registry_
;
298 // Sends API requests to the extension host.
299 scoped_ptr
<RequestSender
> request_sender_
;
301 // The platforms system font family and size;
302 std::string system_font_family_
;
303 std::string system_font_size_
;
305 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
306 std::map
<int, int> port_to_tab_id_map_
;
308 // True once WebKit has been initialized (and it is therefore safe to poke).
309 bool is_webkit_initialized_
;
311 // It is important for this to come after the ScriptInjectionManager, so that
312 // the observer is destroyed before the UserScriptSet.
313 ScopedObserver
<UserScriptSetManager
, UserScriptSetManager::Observer
>
314 user_script_set_manager_observer_
;
316 // Status of webrequest usage.
317 bool webrequest_used_
;
319 DISALLOW_COPY_AND_ASSIGN(Dispatcher
);
322 } // namespace extensions
324 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_