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 void WillReleaseScriptContext(blink::WebLocalFrame
* frame
,
99 const v8::Local
<v8::Context
>& context
,
102 void DidCreateDocumentElement(blink::WebLocalFrame
* frame
);
104 void OnExtensionResponse(int request_id
,
106 const base::ListValue
& response
,
107 const std::string
& error
);
109 // Dispatches the event named |event_name| to all render views.
110 void DispatchEvent(const std::string
& extension_id
,
111 const std::string
& event_name
) const;
113 // Shared implementation of the various MessageInvoke IPCs.
114 void InvokeModuleSystemMethod(content::RenderFrame
* render_frame
,
115 const std::string
& extension_id
,
116 const std::string
& module_name
,
117 const std::string
& function_name
,
118 const base::ListValue
& args
,
121 void ClearPortData(int port_id
);
123 // Returns a list of (module name, resource id) pairs for the JS modules to
124 // add to the source map.
125 static std::vector
<std::pair
<std::string
, int> > GetJsResources();
126 static void RegisterNativeHandlers(ModuleSystem
* module_system
,
127 ScriptContext
* context
,
128 Dispatcher
* dispatcher
,
129 RequestSender
* request_sender
,
130 V8SchemaRegistry
* v8_schema_registry
);
132 bool WasWebRequestUsedBySomeExtensions() const { return webrequest_used_
; }
135 // The RendererPermissionsPolicyDelegateTest.CannotScriptWebstore test needs
136 // to call LoadExtensionForTest and the OnActivateExtension IPCs.
137 friend class ::ChromeRenderViewTest
;
138 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest
,
139 CannotScriptWebstore
);
141 // Inserts an Extension into |extensions_|. Normally the only way to do this
142 // would be through the ExtensionMsg_Loaded IPC (OnLoaded) but this can't be
143 // triggered for tests, because in the process of serializing then
144 // deserializing the IPC, Extension IDs manually set for testing are lost.
145 void LoadExtensionForTest(const Extension
* extension
);
147 // RenderProcessObserver implementation:
148 bool OnControlMessageReceived(const IPC::Message
& message
) override
;
149 void WebKitInitialized() override
;
150 void IdleNotification() override
;
151 void OnRenderProcessShutdown() override
;
153 void OnActivateExtension(const std::string
& extension_id
);
154 void OnCancelSuspend(const std::string
& extension_id
);
155 void OnDeliverMessage(int target_port_id
, const Message
& message
);
156 void OnDispatchOnConnect(int target_port_id
,
157 const std::string
& channel_name
,
158 const ExtensionMsg_TabConnectionInfo
& source
,
159 const ExtensionMsg_ExternalConnectionInfo
& info
,
160 const std::string
& tls_channel_id
);
161 void OnDispatchOnDisconnect(int port_id
, const std::string
& error_message
);
163 const std::vector
<ExtensionMsg_Loaded_Params
>& loaded_extensions
);
164 void OnMessageInvoke(const std::string
& extension_id
,
165 const std::string
& module_name
,
166 const std::string
& function_name
,
167 const base::ListValue
& args
,
169 void OnSetChannel(int channel
);
170 void OnSetFunctionNames(const std::vector
<std::string
>& names
);
171 void OnSetScriptingWhitelist(
172 const ExtensionsClient::ScriptingWhitelist
& extension_ids
);
173 void OnSetSystemFont(const std::string
& font_family
,
174 const std::string
& font_size
);
175 void OnShouldSuspend(const std::string
& extension_id
, uint64 sequence_id
);
176 void OnSuspend(const std::string
& extension_id
);
177 void OnTransferBlobs(const std::vector
<std::string
>& blob_uuids
);
178 void OnUnloaded(const std::string
& id
);
179 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params
& params
);
180 void OnUpdateTabSpecificPermissions(const GURL
& visible_url
,
181 const std::string
& extension_id
,
182 const URLPatternSet
& new_hosts
,
183 bool update_origin_whitelist
,
185 void OnClearTabSpecificPermissions(
186 const std::vector
<std::string
>& extension_ids
,
187 bool update_origin_whitelist
,
189 void OnUsingWebRequestAPI(bool webrequest_used
);
191 // UserScriptSetManager::Observer implementation.
192 void OnUserScriptsUpdated(const std::set
<HostID
>& changed_hosts
,
193 const std::vector
<UserScript
*>& scripts
) override
;
195 void UpdateActiveExtensions();
197 // Sets up the host permissions for |extension|.
198 void InitOriginPermissions(const Extension
* extension
);
200 // Updates the host permissions for the extension url to include only those in
201 // |new_patterns|, and remove from |old_patterns| that are no longer allowed.
202 void UpdateOriginPermissions(const GURL
& extension_url
,
203 const URLPatternSet
& old_patterns
,
204 const URLPatternSet
& new_patterns
);
206 // Enable custom element whitelist in Apps.
207 void EnableCustomElementWhiteList();
209 // Adds or removes bindings for every context belonging to |extension_id|, or
210 // or all contexts if |extension_id| is empty.
211 void UpdateBindings(const std::string
& extension_id
);
213 void UpdateBindingsForContext(ScriptContext
* context
);
215 void RegisterBinding(const std::string
& api_name
, ScriptContext
* context
);
217 void RegisterNativeHandlers(ModuleSystem
* module_system
,
218 ScriptContext
* context
);
220 // Determines if a ScriptContext can connect to any externally_connectable-
221 // enabled extension.
222 bool IsRuntimeAvailableToContext(ScriptContext
* context
);
224 // Updates a web page context with any content capabilities granted by active
226 void UpdateContentCapabilities(ScriptContext
* context
);
228 // Inserts static source code into |source_map_|.
229 void PopulateSourceMap();
231 // Returns whether the current renderer hosts a platform app.
232 bool IsWithinPlatformApp();
234 // Gets |field| from |object| or creates it as an empty object if it doesn't
236 v8::Local
<v8::Object
> GetOrCreateObject(const v8::Local
<v8::Object
>& object
,
237 const std::string
& field
,
238 v8::Isolate
* isolate
);
240 v8::Local
<v8::Object
> GetOrCreateBindObjectIfAvailable(
241 const std::string
& api_name
,
242 std::string
* bind_name
,
243 ScriptContext
* context
);
245 // Requires the GuestView modules in the module system of the ScriptContext
247 void RequireGuestViewModules(ScriptContext
* context
);
249 // Adds features that are specific to the current channel.
250 void AddChannelSpecificFeatures();
252 // The delegate for this dispatcher. Not owned, but must extend beyond the
253 // Dispatcher's own lifetime.
254 DispatcherDelegate
* delegate_
;
256 // True if the IdleNotification timer should be set.
257 bool set_idle_notifications_
;
259 // Contains all loaded extensions. This is essentially the renderer
260 // counterpart to ExtensionService in the browser. It contains information
261 // about all extensions currently loaded by the browser.
262 ExtensionSet extensions_
;
264 // The IDs of extensions that failed to load, mapped to the error message
265 // generated on failure.
266 std::map
<std::string
, std::string
> extension_load_errors_
;
268 // All the bindings contexts that are currently loaded for this renderer.
269 // There is zero or one for each v8 context.
270 scoped_ptr
<ScriptContextSet
> script_context_set_
;
272 scoped_ptr
<ContentWatcher
> content_watcher_
;
274 scoped_ptr
<UserScriptSetManager
> user_script_set_manager_
;
276 scoped_ptr
<ScriptInjectionManager
> script_injection_manager_
;
278 // Same as above, but on a longer timer and will run even if the process is
279 // not idle, to ensure that IdleHandle gets called eventually.
280 scoped_ptr
<base::RepeatingTimer
<content::RenderThread
> > forced_idle_timer_
;
282 // All declared function names.
283 std::set
<std::string
> function_names_
;
285 // The extensions and apps that are active in this process.
286 ExtensionIdSet active_extension_ids_
;
288 ResourceBundleSourceMap source_map_
;
290 // Cache for the v8 representation of extension API schemas.
291 scoped_ptr
<V8SchemaRegistry
> v8_schema_registry_
;
293 // Sends API requests to the extension host.
294 scoped_ptr
<RequestSender
> request_sender_
;
296 // The platforms system font family and size;
297 std::string system_font_family_
;
298 std::string system_font_size_
;
300 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
301 std::map
<int, int> port_to_tab_id_map_
;
303 // True once WebKit has been initialized (and it is therefore safe to poke).
304 bool is_webkit_initialized_
;
306 // It is important for this to come after the ScriptInjectionManager, so that
307 // the observer is destroyed before the UserScriptSet.
308 ScopedObserver
<UserScriptSetManager
, UserScriptSetManager::Observer
>
309 user_script_set_manager_observer_
;
311 // Status of webrequest usage.
312 bool webrequest_used_
;
314 DISALLOW_COPY_AND_ASSIGN(Dispatcher
);
317 } // namespace extensions
319 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_