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/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
;
35 struct ExtensionMsg_ExternalConnectionInfo
;
36 struct ExtensionMsg_Loaded_Params
;
37 struct ExtensionMsg_TabConnectionInfo
;
38 struct ExtensionMsg_UpdatePermissions_Params
;
43 class WebSecurityOrigin
;
54 namespace extensions
{
56 class DispatcherDelegate
;
57 class FilteredEventRouter
;
58 class ManifestPermissionSet
;
61 class ScriptInjectionManager
;
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
{
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 void OnRenderFrameCreated(content::RenderFrame
* render_frame
);
84 bool IsExtensionActive(const std::string
& extension_id
) const;
86 void DidCreateScriptContext(blink::WebLocalFrame
* frame
,
87 const v8::Local
<v8::Context
>& context
,
91 // Runs on a different thread and should not use any member variables.
92 static void DidInitializeServiceWorkerContextOnWorkerThread(
93 v8::Local
<v8::Context
> v8_context
,
96 void WillReleaseScriptContext(blink::WebLocalFrame
* frame
,
97 const v8::Local
<v8::Context
>& context
,
100 // Runs on a different thread and should not use any member variables.
101 static void WillDestroyServiceWorkerContextOnWorkerThread(const GURL
& url
);
103 void DidCreateDocumentElement(blink::WebLocalFrame
* frame
);
105 void OnExtensionResponse(int request_id
,
107 const base::ListValue
& response
,
108 const std::string
& error
);
110 // Dispatches the event named |event_name| to all render views.
111 void DispatchEvent(const std::string
& extension_id
,
112 const std::string
& event_name
) const;
114 // Shared implementation of the various MessageInvoke IPCs.
115 void InvokeModuleSystemMethod(content::RenderFrame
* render_frame
,
116 const std::string
& extension_id
,
117 const std::string
& module_name
,
118 const std::string
& function_name
,
119 const base::ListValue
& args
,
122 void ClearPortData(int port_id
);
124 // Returns a list of (module name, resource id) pairs for the JS modules to
125 // add to the source map.
126 static std::vector
<std::pair
<std::string
, int> > GetJsResources();
127 static void RegisterNativeHandlers(ModuleSystem
* module_system
,
128 ScriptContext
* context
,
129 Dispatcher
* dispatcher
,
130 RequestSender
* request_sender
,
131 V8SchemaRegistry
* v8_schema_registry
);
133 bool WasWebRequestUsedBySomeExtensions() const { return webrequest_used_
; }
136 // The RendererPermissionsPolicyDelegateTest.CannotScriptWebstore test needs
137 // to call the OnActivateExtension IPCs.
138 friend class ::ChromeRenderViewTest
;
139 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest
,
140 CannotScriptWebstore
);
142 // RenderProcessObserver implementation:
143 bool OnControlMessageReceived(const IPC::Message
& message
) override
;
144 void WebKitInitialized() override
;
145 void IdleNotification() override
;
146 void OnRenderProcessShutdown() override
;
148 void OnActivateExtension(const std::string
& extension_id
);
149 void OnCancelSuspend(const std::string
& extension_id
);
150 void OnDeliverMessage(int target_port_id
, const Message
& message
);
151 void OnDispatchOnConnect(int target_port_id
,
152 const std::string
& channel_name
,
153 const ExtensionMsg_TabConnectionInfo
& source
,
154 const ExtensionMsg_ExternalConnectionInfo
& info
,
155 const std::string
& tls_channel_id
);
156 void OnDispatchOnDisconnect(int port_id
, const std::string
& error_message
);
158 const std::vector
<ExtensionMsg_Loaded_Params
>& loaded_extensions
);
159 void OnMessageInvoke(const std::string
& extension_id
,
160 const std::string
& module_name
,
161 const std::string
& function_name
,
162 const base::ListValue
& args
,
164 void OnSetChannel(int channel
);
165 void OnSetScriptingWhitelist(
166 const ExtensionsClient::ScriptingWhitelist
& extension_ids
);
167 void OnSetSystemFont(const std::string
& font_family
,
168 const std::string
& font_size
);
169 void OnShouldSuspend(const std::string
& extension_id
, uint64 sequence_id
);
170 void OnSuspend(const std::string
& extension_id
);
171 void OnTransferBlobs(const std::vector
<std::string
>& blob_uuids
);
172 void OnUnloaded(const std::string
& id
);
173 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params
& params
);
174 void OnUpdateTabSpecificPermissions(const GURL
& visible_url
,
175 const std::string
& extension_id
,
176 const URLPatternSet
& new_hosts
,
177 bool update_origin_whitelist
,
179 void OnClearTabSpecificPermissions(
180 const std::vector
<std::string
>& extension_ids
,
181 bool update_origin_whitelist
,
183 void OnUsingWebRequestAPI(bool webrequest_used
);
185 // UserScriptSetManager::Observer implementation.
186 void OnUserScriptsUpdated(const std::set
<HostID
>& changed_hosts
,
187 const std::vector
<UserScript
*>& scripts
) override
;
189 void UpdateActiveExtensions();
191 // Sets up the host permissions for |extension|.
192 void InitOriginPermissions(const Extension
* extension
);
194 // Updates the host permissions for the extension url to include only those in
195 // |new_patterns|, and remove from |old_patterns| that are no longer allowed.
196 void UpdateOriginPermissions(const GURL
& extension_url
,
197 const URLPatternSet
& old_patterns
,
198 const URLPatternSet
& new_patterns
);
200 // Enable custom element whitelist in Apps.
201 void EnableCustomElementWhiteList();
203 // Adds or removes bindings for every context belonging to |extension_id|, or
204 // or all contexts if |extension_id| is empty.
205 void UpdateBindings(const std::string
& extension_id
);
207 void UpdateBindingsForContext(ScriptContext
* context
);
209 void RegisterBinding(const std::string
& api_name
, ScriptContext
* context
);
211 void RegisterNativeHandlers(ModuleSystem
* module_system
,
212 ScriptContext
* context
);
214 // Determines if a ScriptContext can connect to any externally_connectable-
215 // enabled extension.
216 bool IsRuntimeAvailableToContext(ScriptContext
* context
);
218 // Updates a web page context with any content capabilities granted by active
220 void UpdateContentCapabilities(ScriptContext
* context
);
222 // Inserts static source code into |source_map_|.
223 void PopulateSourceMap();
225 // Returns whether the current renderer hosts a platform app.
226 bool IsWithinPlatformApp();
228 // Gets |field| from |object| or creates it as an empty object if it doesn't
230 v8::Local
<v8::Object
> GetOrCreateObject(const v8::Local
<v8::Object
>& object
,
231 const std::string
& field
,
232 v8::Isolate
* isolate
);
234 v8::Local
<v8::Object
> GetOrCreateBindObjectIfAvailable(
235 const std::string
& api_name
,
236 std::string
* bind_name
,
237 ScriptContext
* context
);
239 // Requires the GuestView modules in the module system of the ScriptContext
241 void RequireGuestViewModules(ScriptContext
* context
);
243 // Adds features that are specific to the current channel.
244 void AddChannelSpecificFeatures();
246 // The delegate for this dispatcher. Not owned, but must extend beyond the
247 // Dispatcher's own lifetime.
248 DispatcherDelegate
* delegate_
;
250 // True if the IdleNotification timer should be set.
251 bool set_idle_notifications_
;
253 // The IDs of extensions that failed to load, mapped to the error message
254 // generated on failure.
255 std::map
<std::string
, std::string
> extension_load_errors_
;
257 // All the bindings contexts that are currently loaded for this renderer.
258 // There is zero or one for each v8 context.
259 scoped_ptr
<ScriptContextSet
> script_context_set_
;
261 scoped_ptr
<ContentWatcher
> content_watcher_
;
263 scoped_ptr
<UserScriptSetManager
> user_script_set_manager_
;
265 scoped_ptr
<ScriptInjectionManager
> script_injection_manager_
;
267 // Same as above, but on a longer timer and will run even if the process is
268 // not idle, to ensure that IdleHandle gets called eventually.
269 scoped_ptr
<base::RepeatingTimer
<content::RenderThread
> > forced_idle_timer_
;
271 // The extensions and apps that are active in this process.
272 ExtensionIdSet active_extension_ids_
;
274 ResourceBundleSourceMap source_map_
;
276 // Cache for the v8 representation of extension API schemas.
277 scoped_ptr
<V8SchemaRegistry
> v8_schema_registry_
;
279 // Sends API requests to the extension host.
280 scoped_ptr
<RequestSender
> request_sender_
;
282 // The platforms system font family and size;
283 std::string system_font_family_
;
284 std::string system_font_size_
;
286 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
287 std::map
<int, int> port_to_tab_id_map_
;
289 // True once WebKit has been initialized (and it is therefore safe to poke).
290 bool is_webkit_initialized_
;
292 // It is important for this to come after the ScriptInjectionManager, so that
293 // the observer is destroyed before the UserScriptSet.
294 ScopedObserver
<UserScriptSetManager
, UserScriptSetManager::Observer
>
295 user_script_set_manager_observer_
;
297 // Status of webrequest usage.
298 bool webrequest_used_
;
300 DISALLOW_COPY_AND_ASSIGN(Dispatcher
);
303 } // namespace extensions
305 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_