1 // Copyright (c) 2012 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 CHROME_RENDERER_EXTENSIONS_DISPATCHER_H_
6 #define CHROME_RENDERER_EXTENSIONS_DISPATCHER_H_
13 #include "base/memory/shared_memory.h"
14 #include "base/timer/timer.h"
15 #include "chrome/renderer/extensions/chrome_v8_context.h"
16 #include "chrome/renderer/extensions/chrome_v8_context_set.h"
17 #include "chrome/renderer/extensions/v8_schema_registry.h"
18 #include "chrome/renderer/resource_bundle_source_map.h"
19 #include "content/public/renderer/render_process_observer.h"
20 #include "extensions/common/event_filter.h"
21 #include "extensions/common/extension_set.h"
22 #include "extensions/common/extensions_client.h"
23 #include "extensions/common/features/feature.h"
24 #include "third_party/WebKit/public/platform/WebString.h"
25 #include "third_party/WebKit/public/platform/WebVector.h"
26 #include "v8/include/v8.h"
28 class ChromeRenderViewTest
;
32 struct ExtensionMsg_ExternalConnectionInfo
;
33 struct ExtensionMsg_Loaded_Params
;
34 struct ExtensionMsg_UpdatePermissions_Params
;
38 class WebSecurityOrigin
;
42 class DictionaryValue
;
50 namespace extensions
{
53 class FilteredEventRouter
;
54 class ManifestPermissionSet
;
56 class UserScriptSlave
;
59 // Dispatches extension control messages sent to the renderer and stores
60 // renderer extension related state.
61 class Dispatcher
: public content::RenderProcessObserver
{
64 virtual ~Dispatcher();
66 const std::set
<std::string
>& function_names() const {
67 return function_names_
;
70 bool is_extension_process() const { return is_extension_process_
; }
71 const ExtensionSet
* extensions() const { return &extensions_
; }
72 const ChromeV8ContextSet
& v8_context_set() const {
73 return v8_context_set_
;
75 UserScriptSlave
* user_script_slave() {
76 return user_script_slave_
.get();
78 V8SchemaRegistry
* v8_schema_registry() {
79 return v8_schema_registry_
.get();
81 ContentWatcher
* content_watcher() {
82 return content_watcher_
.get();
84 RequestSender
* request_sender() {
85 return request_sender_
.get();
88 bool IsExtensionActive(const std::string
& extension_id
) const;
90 // Finds the extension ID for the JavaScript context associated with the
91 // specified |frame| and isolated world. If |world_id| is zero, finds the
92 // extension ID associated with the main world's JavaScript context. If the
93 // JavaScript context isn't from an extension, returns empty string.
94 std::string
GetExtensionID(const blink::WebFrame
* frame
, int world_id
);
96 void DidCreateScriptContext(blink::WebFrame
* frame
,
97 v8::Handle
<v8::Context
> context
,
100 void WillReleaseScriptContext(blink::WebFrame
* frame
,
101 v8::Handle
<v8::Context
> context
,
104 void DidCreateDocumentElement(blink::WebFrame
* frame
);
107 blink::WebFrame
* frame
,
108 const blink::WebVector
<blink::WebString
>& newly_matching_selectors
,
109 const blink::WebVector
<blink::WebString
>& stopped_matching_selectors
);
111 // TODO(mpcomplete): remove. http://crbug.com/100411
112 bool IsAdblockWithWebRequestInstalled() const {
113 return webrequest_adblock_
;
115 bool IsAdblockPlusWithWebRequestInstalled() const {
116 return webrequest_adblock_plus_
;
118 bool IsOtherExtensionWithWebRequestInstalled() const {
119 return webrequest_other_
;
122 void OnExtensionResponse(int request_id
,
124 const base::ListValue
& response
,
125 const std::string
& error
);
127 // Checks that the current context contains an extension that has permission
128 // to execute the specified function. If it does not, a v8 exception is thrown
129 // and the method returns false. Otherwise returns true.
130 bool CheckContextAccessToExtensionAPI(
131 const std::string
& function_name
, ChromeV8Context
* context
) const;
133 // Dispatches the event named |event_name| to all render views.
134 void DispatchEvent(const std::string
& extension_id
,
135 const std::string
& event_name
) const;
137 // Shared implementation of the various MessageInvoke IPCs.
138 void InvokeModuleSystemMethod(
139 content::RenderView
* render_view
,
140 const std::string
& extension_id
,
141 const std::string
& module_name
,
142 const std::string
& function_name
,
143 const base::ListValue
& args
,
147 friend class ::ChromeRenderViewTest
;
148 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest
,
149 CannotScriptWebstore
);
150 typedef void (*BindingInstaller
)(ModuleSystem
* module_system
,
151 v8::Handle
<v8::Object
> chrome
);
153 // RenderProcessObserver implementation:
154 virtual bool OnControlMessageReceived(const IPC::Message
& message
) OVERRIDE
;
155 virtual void WebKitInitialized() OVERRIDE
;
156 virtual void IdleNotification() OVERRIDE
;
157 virtual void OnRenderProcessShutdown() OVERRIDE
;
159 void OnSetChannel(int channel
);
160 void OnMessageInvoke(const std::string
& extension_id
,
161 const std::string
& module_name
,
162 const std::string
& function_name
,
163 const base::ListValue
& args
,
165 void OnDispatchOnConnect(int target_port_id
,
166 const std::string
& channel_name
,
167 const base::DictionaryValue
& source_tab
,
168 const ExtensionMsg_ExternalConnectionInfo
& info
,
169 const std::string
& tls_channel_id
);
170 void OnDeliverMessage(int target_port_id
, const Message
& message
);
171 void OnDispatchOnDisconnect(int port_id
, const std::string
& error_message
);
172 void OnSetFunctionNames(const std::vector
<std::string
>& names
);
173 void OnSetSystemFont(const std::string
& font_family
,
174 const std::string
& font_size
);
176 const std::vector
<ExtensionMsg_Loaded_Params
>& loaded_extensions
);
177 void OnLoadedInternal(scoped_refptr
<const Extension
> extension
);
178 void OnUnloaded(const std::string
& id
);
179 void OnSetScriptingWhitelist(
180 const ExtensionsClient::ScriptingWhitelist
& extension_ids
);
181 void OnPageActionsUpdated(const std::string
& extension_id
,
182 const std::vector
<std::string
>& page_actions
);
183 void OnActivateExtension(const std::string
& extension_id
);
184 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params
& params
);
185 void OnUpdateTabSpecificPermissions(int page_id
,
187 const std::string
& extension_id
,
188 const URLPatternSet
& origin_set
);
189 void OnClearTabSpecificPermissions(
191 const std::vector
<std::string
>& extension_ids
);
192 void OnUpdateUserScripts(base::SharedMemoryHandle table
);
193 void OnUsingWebRequestAPI(
196 bool other_webrequest
);
197 void OnShouldSuspend(const std::string
& extension_id
, int sequence_id
);
198 void OnSuspend(const std::string
& extension_id
);
199 void OnCancelSuspend(const std::string
& extension_id
);
201 // Update the list of active extensions that will be reported when we crash.
202 void UpdateActiveExtensions();
204 // Sets up the host permissions for |extension|.
205 void InitOriginPermissions(const Extension
* extension
);
206 void AddOrRemoveOriginPermissions(
207 UpdatedExtensionPermissionsInfo::Reason reason
,
208 const Extension
* extension
,
209 const URLPatternSet
& origins
);
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 AddOrRemoveBindings(const std::string
& extension_id
);
218 void RegisterNativeHandlers(ModuleSystem
* module_system
,
219 ChromeV8Context
* context
);
220 void AddOrRemoveBindingsForContext(ChromeV8Context
* context
);
221 void RegisterBinding(const std::string
& api_name
,
222 ChromeV8Context
* context
);
223 v8::Handle
<v8::Object
> GetOrCreateBindObjectIfAvailable(
224 const std::string
& api_name
,
225 std::string
* bind_name
,
226 ChromeV8Context
* context
);
228 // Inserts static source code into |source_map_|.
229 void PopulateSourceMap();
231 // Inserts BindingInstallers into |lazy_bindings_map_|.
232 void PopulateLazyBindingsMap();
234 // Sets up the bindings for the given api.
235 void InstallBindings(ModuleSystem
* module_system
,
236 v8::Handle
<v8::Context
> v8_context
,
237 const std::string
& api
);
239 // Returns whether the current renderer hosts a platform app.
240 bool IsWithinPlatformApp();
242 bool IsSandboxedPage(const GURL
& url
) const;
244 // Returns the Feature::Context type of context for a JavaScript context.
245 Feature::Context
ClassifyJavaScriptContext(
246 const Extension
* extension
,
249 const blink::WebSecurityOrigin
& origin
);
251 // Gets |field| from |object| or creates it as an empty object if it doesn't
253 v8::Handle
<v8::Object
> GetOrCreateObject(v8::Handle
<v8::Object
> object
,
254 const std::string
& field
,
255 v8::Isolate
* isolate
);
257 // True if this renderer is running extensions.
258 bool is_extension_process_
;
260 // Contains all loaded extensions. This is essentially the renderer
261 // counterpart to ExtensionService in the browser. It contains information
262 // about all extensions currently loaded by the browser.
263 ExtensionSet extensions_
;
265 // The IDs of extensions that failed to load, mapped to the error message
266 // generated on failure.
267 std::map
<std::string
, std::string
> extension_load_errors_
;
269 // All the bindings contexts that are currently loaded for this renderer.
270 // There is zero or one for each v8 context.
271 ChromeV8ContextSet v8_context_set_
;
273 scoped_ptr
<UserScriptSlave
> user_script_slave_
;
275 scoped_ptr
<ContentWatcher
> content_watcher_
;
277 // Same as above, but on a longer timer and will run even if the process is
278 // not idle, to ensure that IdleHandle gets called eventually.
279 base::RepeatingTimer
<content::RenderThread
> forced_idle_timer_
;
281 // All declared function names.
282 std::set
<std::string
> function_names_
;
284 // The extensions and apps that are active in this process.
285 std::set
<std::string
> active_extension_ids_
;
287 // True once WebKit has been initialized (and it is therefore safe to poke).
288 bool is_webkit_initialized_
;
290 // Status of webrequest usage for known extensions.
291 // TODO(mpcomplete): remove. http://crbug.com/100411
292 bool webrequest_adblock_
;
293 bool webrequest_adblock_plus_
;
294 bool webrequest_other_
;
296 ResourceBundleSourceMap source_map_
;
298 // Cache for the v8 representation of extension API schemas.
299 scoped_ptr
<V8SchemaRegistry
> v8_schema_registry_
;
301 // Bindings that are defined lazily and have BindingInstallers to install
303 std::map
<std::string
, BindingInstaller
> lazy_bindings_map_
;
305 // Sends API requests to the extension host.
306 scoped_ptr
<RequestSender
> request_sender_
;
308 // The platforms system font family and size;
309 std::string system_font_family_
;
310 std::string system_font_size_
;
312 DISALLOW_COPY_AND_ASSIGN(Dispatcher
);
315 } // namespace extensions
317 #endif // CHROME_RENDERER_EXTENSIONS_DISPATCHER_H_