SupervisedUserSyncService cleanup: Implement SigninManagerBase::Observer
[chromium-blink-merge.git] / extensions / renderer / dispatcher.h
blobc71cb64757bd3b6c12b13358ac0cd834f195b665
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
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;
33 class GURL;
34 class ModuleSystem;
35 class URLPattern;
36 struct ExtensionMsg_ExternalConnectionInfo;
37 struct ExtensionMsg_Loaded_Params;
38 struct ExtensionMsg_TabConnectionInfo;
39 struct ExtensionMsg_UpdatePermissions_Params;
41 namespace blink {
42 class WebFrame;
43 class WebLocalFrame;
44 class WebSecurityOrigin;
47 namespace base {
48 class ListValue;
51 namespace content {
52 class RenderThread;
55 namespace extensions {
56 class ContentWatcher;
57 class DispatcherDelegate;
58 class FilteredEventRouter;
59 class ManifestPermissionSet;
60 class RequestSender;
61 class ScriptContext;
62 class ScriptInjectionManager;
63 struct Message;
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 {
69 public:
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 OnRenderViewCreated(content::RenderView* render_view);
91 bool IsExtensionActive(const std::string& extension_id) const;
93 void DidCreateScriptContext(blink::WebLocalFrame* frame,
94 const v8::Handle<v8::Context>& context,
95 int extension_group,
96 int world_id);
98 void WillReleaseScriptContext(blink::WebLocalFrame* frame,
99 const v8::Handle<v8::Context>& context,
100 int world_id);
102 void DidCreateDocumentElement(blink::WebFrame* frame);
104 void DidMatchCSS(
105 blink::WebFrame* frame,
106 const blink::WebVector<blink::WebString>& newly_matching_selectors,
107 const blink::WebVector<blink::WebString>& stopped_matching_selectors);
109 void OnExtensionResponse(int request_id,
110 bool success,
111 const base::ListValue& response,
112 const std::string& error);
114 // Checks that the current context contains an extension that has permission
115 // to execute the specified function. If it does not, a v8 exception is thrown
116 // and the method returns false. Otherwise returns true.
117 bool CheckContextAccessToExtensionAPI(const std::string& function_name,
118 ScriptContext* context) const;
120 // Dispatches the event named |event_name| to all render views.
121 void DispatchEvent(const std::string& extension_id,
122 const std::string& event_name) const;
124 // Shared implementation of the various MessageInvoke IPCs.
125 void InvokeModuleSystemMethod(content::RenderView* render_view,
126 const std::string& extension_id,
127 const std::string& module_name,
128 const std::string& function_name,
129 const base::ListValue& args,
130 bool user_gesture);
132 void ClearPortData(int port_id);
134 // Returns a list of (module name, resource id) pairs for the JS modules to
135 // add to the source map.
136 static std::vector<std::pair<std::string, int> > GetJsResources();
137 static void RegisterNativeHandlers(ModuleSystem* module_system,
138 ScriptContext* context,
139 Dispatcher* dispatcher,
140 RequestSender* request_sender,
141 V8SchemaRegistry* v8_schema_registry);
143 bool WasWebRequestUsedBySomeExtensions() const { return webrequest_used_; }
145 private:
146 friend class ::ChromeRenderViewTest;
147 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest,
148 CannotScriptWebstore);
150 // RenderProcessObserver implementation:
151 bool OnControlMessageReceived(const IPC::Message& message) override;
152 void WebKitInitialized() override;
153 void IdleNotification() override;
154 void OnRenderProcessShutdown() override;
156 void OnActivateExtension(const std::string& extension_id);
157 void OnCancelSuspend(const std::string& extension_id);
158 void OnDeliverMessage(int target_port_id, const Message& message);
159 void OnDispatchOnConnect(int target_port_id,
160 const std::string& channel_name,
161 const ExtensionMsg_TabConnectionInfo& source,
162 const ExtensionMsg_ExternalConnectionInfo& info,
163 const std::string& tls_channel_id);
164 void OnDispatchOnDisconnect(int port_id, const std::string& error_message);
165 void OnLoaded(
166 const std::vector<ExtensionMsg_Loaded_Params>& loaded_extensions);
167 void OnLoadedInternal(scoped_refptr<const Extension> extension);
168 void OnMessageInvoke(const std::string& extension_id,
169 const std::string& module_name,
170 const std::string& function_name,
171 const base::ListValue& args,
172 bool user_gesture);
173 void OnSetChannel(int channel);
174 void OnSetFunctionNames(const std::vector<std::string>& names);
175 void OnSetScriptingWhitelist(
176 const ExtensionsClient::ScriptingWhitelist& extension_ids);
177 void OnSetSystemFont(const std::string& font_family,
178 const std::string& font_size);
179 void OnShouldSuspend(const std::string& extension_id, uint64 sequence_id);
180 void OnSuspend(const std::string& extension_id);
181 void OnTransferBlobs(const std::vector<std::string>& blob_uuids);
182 void OnUnloaded(const std::string& id);
183 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params& params);
184 void OnUpdateTabSpecificPermissions(const GURL& visible_url,
185 const std::string& extension_id,
186 const URLPatternSet& new_hosts,
187 bool update_origin_whitelist,
188 int tab_id);
189 void OnClearTabSpecificPermissions(
190 const std::vector<std::string>& extension_ids,
191 bool update_origin_whitelist,
192 int tab_id);
193 void OnUsingWebRequestAPI(bool webrequest_used);
195 // UserScriptSetManager::Observer implementation.
196 void OnUserScriptsUpdated(const std::set<HostID>& changed_hosts,
197 const std::vector<UserScript*>& scripts) override;
199 void UpdateActiveExtensions();
201 // Sets up the host permissions for |extension|.
202 void InitOriginPermissions(const Extension* extension);
204 // Updates the host permissions for the extension url to include only those in
205 // |new_patterns|, and remove from |old_patterns| that are no longer allowed.
206 void UpdateOriginPermissions(const GURL& extension_url,
207 const URLPatternSet& old_patterns,
208 const URLPatternSet& new_patterns);
210 // Enable custom element whitelist in Apps.
211 void EnableCustomElementWhiteList();
213 // Adds or removes bindings for every context belonging to |extension_id|, or
214 // or all contexts if |extension_id| is empty.
215 void UpdateBindings(const std::string& extension_id);
217 void UpdateBindingsForContext(ScriptContext* context);
219 void RegisterBinding(const std::string& api_name, ScriptContext* context);
221 void RegisterNativeHandlers(ModuleSystem* module_system,
222 ScriptContext* context);
224 // Determines if a ScriptContext can connect to any externally_connectable-
225 // enabled extension.
226 bool IsRuntimeAvailableToContext(ScriptContext* context);
228 // Updates a web page context with any content capabilities granted by active
229 // extensions.
230 void UpdateContentCapabilities(ScriptContext* context);
232 // Inserts static source code into |source_map_|.
233 void PopulateSourceMap();
235 // Returns whether the current renderer hosts a platform app.
236 bool IsWithinPlatformApp();
238 // Gets |field| from |object| or creates it as an empty object if it doesn't
239 // exist.
240 v8::Handle<v8::Object> GetOrCreateObject(const v8::Handle<v8::Object>& object,
241 const std::string& field,
242 v8::Isolate* isolate);
244 v8::Handle<v8::Object> GetOrCreateBindObjectIfAvailable(
245 const std::string& api_name,
246 std::string* bind_name,
247 ScriptContext* context);
249 // Requires the GuestView modules in the module system of the ScriptContext
250 // |context|.
251 void RequireGuestViewModules(ScriptContext* context);
253 // The delegate for this dispatcher. Not owned, but must extend beyond the
254 // Dispatcher's own lifetime.
255 DispatcherDelegate* delegate_;
257 // True if the IdleNotification timer should be set.
258 bool set_idle_notifications_;
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 scoped_ptr<ScriptContextSet> script_context_set_;
273 scoped_ptr<ContentWatcher> content_watcher_;
275 scoped_ptr<UserScriptSetManager> user_script_set_manager_;
277 scoped_ptr<ScriptInjectionManager> script_injection_manager_;
279 // Same as above, but on a longer timer and will run even if the process is
280 // not idle, to ensure that IdleHandle gets called eventually.
281 scoped_ptr<base::RepeatingTimer<content::RenderThread> > forced_idle_timer_;
283 // All declared function names.
284 std::set<std::string> function_names_;
286 // The extensions and apps that are active in this process.
287 ExtensionIdSet active_extension_ids_;
289 ResourceBundleSourceMap source_map_;
291 // Cache for the v8 representation of extension API schemas.
292 scoped_ptr<V8SchemaRegistry> v8_schema_registry_;
294 // Sends API requests to the extension host.
295 scoped_ptr<RequestSender> request_sender_;
297 // The platforms system font family and size;
298 std::string system_font_family_;
299 std::string system_font_size_;
301 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
302 std::map<int, int> port_to_tab_id_map_;
304 // True once WebKit has been initialized (and it is therefore safe to poke).
305 bool is_webkit_initialized_;
307 // It is important for this to come after the ScriptInjectionManager, so that
308 // the observer is destroyed before the UserScriptSet.
309 ScopedObserver<UserScriptSetManager, UserScriptSetManager::Observer>
310 user_script_set_manager_observer_;
312 // Status of webrequest usage.
313 bool webrequest_used_;
315 DISALLOW_COPY_AND_ASSIGN(Dispatcher);
318 } // namespace extensions
320 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_