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_
13 #include "base/memory/shared_memory.h"
14 #include "base/timer/timer.h"
15 #include "content/public/renderer/render_process_observer.h"
16 #include "extensions/common/event_filter.h"
17 #include "extensions/common/extension_set.h"
18 #include "extensions/common/extensions_client.h"
19 #include "extensions/common/features/feature.h"
20 #include "extensions/renderer/resource_bundle_source_map.h"
21 #include "extensions/renderer/script_context.h"
22 #include "extensions/renderer/script_context_set.h"
23 #include "extensions/renderer/v8_schema_registry.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
{
52 class DispatcherDelegate
;
54 class FilteredEventRouter
;
55 class ManifestPermissionSet
;
58 class UserScriptSlave
;
61 // Dispatches extension control messages sent to the renderer and stores
62 // renderer extension related state.
63 class Dispatcher
: public content::RenderProcessObserver
{
65 explicit Dispatcher(DispatcherDelegate
* delegate
);
66 virtual ~Dispatcher();
68 const std::set
<std::string
>& function_names() const {
69 return function_names_
;
72 bool is_extension_process() const { return is_extension_process_
; }
74 const ExtensionSet
* extensions() const { return &extensions_
; }
76 const ScriptContextSet
& script_context_set() const {
77 return script_context_set_
;
80 V8SchemaRegistry
* v8_schema_registry() { return v8_schema_registry_
.get(); }
82 ContentWatcher
* content_watcher() { return content_watcher_
.get(); }
84 UserScriptSlave
* user_script_slave() { return user_script_slave_
.get(); }
86 RequestSender
* request_sender() { 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 const v8::Handle
<v8::Context
>& context
,
101 void WillReleaseScriptContext(blink::WebFrame
* frame
,
102 const v8::Handle
<v8::Context
>& context
,
105 void DidCreateDocumentElement(blink::WebFrame
* frame
);
108 blink::WebFrame
* frame
,
109 const blink::WebVector
<blink::WebString
>& newly_matching_selectors
,
110 const blink::WebVector
<blink::WebString
>& stopped_matching_selectors
);
112 void OnExtensionResponse(int request_id
,
114 const base::ListValue
& response
,
115 const std::string
& error
);
117 // Checks that the current context contains an extension that has permission
118 // to execute the specified function. If it does not, a v8 exception is thrown
119 // and the method returns false. Otherwise returns true.
120 bool CheckContextAccessToExtensionAPI(const std::string
& function_name
,
121 ScriptContext
* context
) const;
123 // Dispatches the event named |event_name| to all render views.
124 void DispatchEvent(const std::string
& extension_id
,
125 const std::string
& event_name
) const;
127 // Shared implementation of the various MessageInvoke IPCs.
128 void InvokeModuleSystemMethod(content::RenderView
* render_view
,
129 const std::string
& extension_id
,
130 const std::string
& module_name
,
131 const std::string
& function_name
,
132 const base::ListValue
& args
,
135 void ClearPortData(int port_id
);
138 friend class ::ChromeRenderViewTest
;
139 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest
,
140 CannotScriptWebstore
);
142 // RenderProcessObserver implementation:
143 virtual bool OnControlMessageReceived(const IPC::Message
& message
) OVERRIDE
;
144 virtual void WebKitInitialized() OVERRIDE
;
145 virtual void IdleNotification() OVERRIDE
;
146 virtual void OnRenderProcessShutdown() OVERRIDE
;
148 void OnActivateExtension(const std::string
& extension_id
);
149 void OnCancelSuspend(const std::string
& extension_id
);
150 void OnClearTabSpecificPermissions(
152 const std::vector
<std::string
>& extension_ids
);
153 void OnDeliverMessage(int target_port_id
, const Message
& message
);
154 void OnDispatchOnConnect(int target_port_id
,
155 const std::string
& channel_name
,
156 const base::DictionaryValue
& source_tab
,
157 const ExtensionMsg_ExternalConnectionInfo
& info
,
158 const std::string
& tls_channel_id
);
159 void OnDispatchOnDisconnect(int port_id
, const std::string
& error_message
);
161 const std::vector
<ExtensionMsg_Loaded_Params
>& loaded_extensions
);
162 void OnLoadedInternal(scoped_refptr
<const Extension
> extension
);
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
,
168 void OnSetChannel(int channel
);
169 void OnSetFunctionNames(const std::vector
<std::string
>& names
);
170 void OnSetScriptingWhitelist(
171 const ExtensionsClient::ScriptingWhitelist
& extension_ids
);
172 void OnSetSystemFont(const std::string
& font_family
,
173 const std::string
& font_size
);
174 void OnShouldSuspend(const std::string
& extension_id
, int sequence_id
);
175 void OnSuspend(const std::string
& extension_id
);
176 void OnUnloaded(const std::string
& id
);
177 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params
& params
);
178 void OnUpdateTabSpecificPermissions(int page_id
,
180 const std::string
& extension_id
,
181 const URLPatternSet
& origin_set
);
182 void OnUpdateUserScripts(base::SharedMemoryHandle scripts
);
183 void OnUsingWebRequestAPI(bool adblock
,
185 bool other_webrequest
);
187 void UpdateActiveExtensions();
189 // Sets up the host permissions for |extension|.
190 void InitOriginPermissions(const Extension
* extension
,
191 Feature::Context context_type
);
192 void UpdateOriginPermissions(UpdatedExtensionPermissionsInfo::Reason reason
,
193 const Extension
* extension
,
194 const URLPatternSet
& origins
);
196 // Enable custom element whitelist in Apps.
197 void EnableCustomElementWhiteList();
199 // Adds or removes bindings for every context belonging to |extension_id|, or
200 // or all contexts if |extension_id| is empty.
201 void UpdateBindings(const std::string
& extension_id
);
203 void UpdateBindingsForContext(ScriptContext
* context
);
205 void RegisterBinding(const std::string
& api_name
, ScriptContext
* context
);
207 void RegisterNativeHandlers(ModuleSystem
* module_system
,
208 ScriptContext
* context
);
210 // Inserts static source code into |source_map_|.
211 void PopulateSourceMap();
213 // Returns whether the current renderer hosts a platform app.
214 bool IsWithinPlatformApp();
216 bool IsSandboxedPage(const GURL
& url
) const;
218 // Returns the Feature::Context type of context for a JavaScript context.
219 Feature::Context
ClassifyJavaScriptContext(
220 const Extension
* extension
,
223 const blink::WebSecurityOrigin
& origin
);
225 // Gets |field| from |object| or creates it as an empty object if it doesn't
227 v8::Handle
<v8::Object
> GetOrCreateObject(const v8::Handle
<v8::Object
>& object
,
228 const std::string
& field
,
229 v8::Isolate
* isolate
);
231 v8::Handle
<v8::Object
> GetOrCreateBindObjectIfAvailable(
232 const std::string
& api_name
,
233 std::string
* bind_name
,
234 ScriptContext
* context
);
236 // The delegate for this dispatcher. Not owned, but must extend beyond the
237 // Dispatcher's own lifetime.
238 DispatcherDelegate
* delegate_
;
240 // True if this renderer is running extensions.
241 bool is_extension_process_
;
243 // Contains all loaded extensions. This is essentially the renderer
244 // counterpart to ExtensionService in the browser. It contains information
245 // about all extensions currently loaded by the browser.
246 ExtensionSet extensions_
;
248 // The IDs of extensions that failed to load, mapped to the error message
249 // generated on failure.
250 std::map
<std::string
, std::string
> extension_load_errors_
;
252 // All the bindings contexts that are currently loaded for this renderer.
253 // There is zero or one for each v8 context.
254 ScriptContextSet script_context_set_
;
256 scoped_ptr
<ContentWatcher
> content_watcher_
;
258 scoped_ptr
<UserScriptSlave
> user_script_slave_
;
260 // Same as above, but on a longer timer and will run even if the process is
261 // not idle, to ensure that IdleHandle gets called eventually.
262 scoped_ptr
<base::RepeatingTimer
<content::RenderThread
> > forced_idle_timer_
;
264 // All declared function names.
265 std::set
<std::string
> function_names_
;
267 // The extensions and apps that are active in this process.
268 std::set
<std::string
> active_extension_ids_
;
270 ResourceBundleSourceMap source_map_
;
272 // Cache for the v8 representation of extension API schemas.
273 scoped_ptr
<V8SchemaRegistry
> v8_schema_registry_
;
275 // Sends API requests to the extension host.
276 scoped_ptr
<RequestSender
> request_sender_
;
278 // The platforms system font family and size;
279 std::string system_font_family_
;
280 std::string system_font_size_
;
282 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
283 std::map
<int, int> port_to_tab_id_map_
;
285 // True once WebKit has been initialized (and it is therefore safe to poke).
286 bool is_webkit_initialized_
;
288 DISALLOW_COPY_AND_ASSIGN(Dispatcher
);
291 } // namespace extensions
293 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_