Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / extensions / renderer / dispatcher.h
blob27adbaec192769ce8165773527d487f6915444f7
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/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;
32 class GURL;
33 class ModuleSystem;
34 class URLPattern;
35 struct ExtensionMsg_ExternalConnectionInfo;
36 struct ExtensionMsg_Loaded_Params;
37 struct ExtensionMsg_TabConnectionInfo;
38 struct ExtensionMsg_UpdatePermissions_Params;
40 namespace blink {
41 class WebFrame;
42 class WebLocalFrame;
43 class WebSecurityOrigin;
46 namespace base {
47 class ListValue;
50 namespace content {
51 class RenderThread;
54 namespace extensions {
55 class ContentWatcher;
56 class DispatcherDelegate;
57 class FilteredEventRouter;
58 class ManifestPermissionSet;
59 class RequestSender;
60 class ScriptContext;
61 class ScriptInjectionManager;
62 struct Message;
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 {
68 public:
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 const std::string& webview_partition_id() { return webview_partition_id_; }
84 void OnRenderFrameCreated(content::RenderFrame* render_frame);
86 bool IsExtensionActive(const std::string& extension_id) const;
88 void DidCreateScriptContext(blink::WebLocalFrame* frame,
89 const v8::Local<v8::Context>& context,
90 int extension_group,
91 int world_id);
93 // Runs on a different thread and should not use any member variables.
94 static void DidInitializeServiceWorkerContextOnWorkerThread(
95 v8::Local<v8::Context> v8_context,
96 const GURL& url);
98 void WillReleaseScriptContext(blink::WebLocalFrame* frame,
99 const v8::Local<v8::Context>& context,
100 int world_id);
102 // Runs on a different thread and should not use any member variables.
103 static void WillDestroyServiceWorkerContextOnWorkerThread(const GURL& url);
105 void DidCreateDocumentElement(blink::WebLocalFrame* frame);
107 void OnExtensionResponse(int request_id,
108 bool success,
109 const base::ListValue& response,
110 const std::string& error);
112 // Dispatches the event named |event_name| to all render views.
113 void DispatchEvent(const std::string& extension_id,
114 const std::string& event_name) const;
116 // Shared implementation of the various MessageInvoke IPCs.
117 void InvokeModuleSystemMethod(content::RenderFrame* render_frame,
118 const std::string& extension_id,
119 const std::string& module_name,
120 const std::string& function_name,
121 const base::ListValue& args,
122 bool user_gesture);
124 void ClearPortData(int port_id);
126 // Returns a list of (module name, resource id) pairs for the JS modules to
127 // add to the source map.
128 static std::vector<std::pair<std::string, int> > GetJsResources();
129 static void RegisterNativeHandlers(ModuleSystem* module_system,
130 ScriptContext* context,
131 Dispatcher* dispatcher,
132 RequestSender* request_sender,
133 V8SchemaRegistry* v8_schema_registry);
135 bool WasWebRequestUsedBySomeExtensions() const { return webrequest_used_; }
137 private:
138 // The RendererPermissionsPolicyDelegateTest.CannotScriptWebstore test needs
139 // to call the OnActivateExtension IPCs.
140 friend class ::ChromeRenderViewTest;
141 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest,
142 CannotScriptWebstore);
144 // RenderProcessObserver implementation:
145 bool OnControlMessageReceived(const IPC::Message& message) override;
146 void WebKitInitialized() override;
147 void IdleNotification() override;
148 void OnRenderProcessShutdown() override;
150 void OnActivateExtension(const std::string& extension_id);
151 void OnCancelSuspend(const std::string& extension_id);
152 void OnDeliverMessage(int target_port_id, const Message& message);
153 void OnDispatchOnConnect(int target_port_id,
154 const std::string& channel_name,
155 const ExtensionMsg_TabConnectionInfo& source,
156 const ExtensionMsg_ExternalConnectionInfo& info,
157 const std::string& tls_channel_id);
158 void OnDispatchOnDisconnect(int port_id, const std::string& error_message);
159 void OnLoaded(
160 const std::vector<ExtensionMsg_Loaded_Params>& loaded_extensions);
161 void OnMessageInvoke(const std::string& extension_id,
162 const std::string& module_name,
163 const std::string& function_name,
164 const base::ListValue& args,
165 bool user_gesture);
166 void OnSetChannel(int channel);
167 void OnSetScriptingWhitelist(
168 const ExtensionsClient::ScriptingWhitelist& extension_ids);
169 void OnSetSystemFont(const std::string& font_family,
170 const std::string& font_size);
171 void OnSetWebViewPartitionID(const std::string& partition_id);
172 void OnShouldSuspend(const std::string& extension_id, uint64 sequence_id);
173 void OnSuspend(const std::string& extension_id);
174 void OnTransferBlobs(const std::vector<std::string>& blob_uuids);
175 void OnUnloaded(const std::string& id);
176 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params& params);
177 void OnUpdateTabSpecificPermissions(const GURL& visible_url,
178 const std::string& extension_id,
179 const URLPatternSet& new_hosts,
180 bool update_origin_whitelist,
181 int tab_id);
182 void OnClearTabSpecificPermissions(
183 const std::vector<std::string>& extension_ids,
184 bool update_origin_whitelist,
185 int tab_id);
186 void OnUsingWebRequestAPI(bool webrequest_used);
188 // UserScriptSetManager::Observer implementation.
189 void OnUserScriptsUpdated(const std::set<HostID>& changed_hosts,
190 const std::vector<UserScript*>& scripts) override;
192 void UpdateActiveExtensions();
194 // Sets up the host permissions for |extension|.
195 void InitOriginPermissions(const Extension* extension);
197 // Updates the host permissions for the extension url to include only those in
198 // |new_patterns|, and remove from |old_patterns| that are no longer allowed.
199 void UpdateOriginPermissions(const GURL& extension_url,
200 const URLPatternSet& old_patterns,
201 const URLPatternSet& new_patterns);
203 // Enable custom element whitelist in Apps.
204 void EnableCustomElementWhiteList();
206 // Adds or removes bindings for every context belonging to |extension_id|, or
207 // or all contexts if |extension_id| is empty.
208 void UpdateBindings(const std::string& extension_id);
210 void UpdateBindingsForContext(ScriptContext* context);
212 void RegisterBinding(const std::string& api_name, ScriptContext* context);
214 void RegisterNativeHandlers(ModuleSystem* module_system,
215 ScriptContext* context);
217 // Determines if a ScriptContext can connect to any externally_connectable-
218 // enabled extension.
219 bool IsRuntimeAvailableToContext(ScriptContext* context);
221 // Updates a web page context with any content capabilities granted by active
222 // extensions.
223 void UpdateContentCapabilities(ScriptContext* context);
225 // Inserts static source code into |source_map_|.
226 void PopulateSourceMap();
228 // Returns whether the current renderer hosts a platform app.
229 bool IsWithinPlatformApp();
231 // Gets |field| from |object| or creates it as an empty object if it doesn't
232 // exist.
233 v8::Local<v8::Object> GetOrCreateObject(const v8::Local<v8::Object>& object,
234 const std::string& field,
235 v8::Isolate* isolate);
237 v8::Local<v8::Object> GetOrCreateBindObjectIfAvailable(
238 const std::string& api_name,
239 std::string* bind_name,
240 ScriptContext* context);
242 // Requires the GuestView modules in the module system of the ScriptContext
243 // |context|.
244 void RequireGuestViewModules(ScriptContext* context);
246 // Adds features that are specific to the current channel.
247 void AddChannelSpecificFeatures();
249 // The delegate for this dispatcher. Not owned, but must extend beyond the
250 // Dispatcher's own lifetime.
251 DispatcherDelegate* delegate_;
253 // True if the IdleNotification timer should be set.
254 bool set_idle_notifications_;
256 // The IDs of extensions that failed to load, mapped to the error message
257 // generated on failure.
258 std::map<std::string, std::string> extension_load_errors_;
260 // All the bindings contexts that are currently loaded for this renderer.
261 // There is zero or one for each v8 context.
262 scoped_ptr<ScriptContextSet> script_context_set_;
264 scoped_ptr<ContentWatcher> content_watcher_;
266 scoped_ptr<UserScriptSetManager> user_script_set_manager_;
268 scoped_ptr<ScriptInjectionManager> script_injection_manager_;
270 // Same as above, but on a longer timer and will run even if the process is
271 // not idle, to ensure that IdleHandle gets called eventually.
272 scoped_ptr<base::RepeatingTimer<content::RenderThread> > forced_idle_timer_;
274 // The extensions and apps that are active in this process.
275 ExtensionIdSet active_extension_ids_;
277 ResourceBundleSourceMap source_map_;
279 // Cache for the v8 representation of extension API schemas.
280 scoped_ptr<V8SchemaRegistry> v8_schema_registry_;
282 // Sends API requests to the extension host.
283 scoped_ptr<RequestSender> request_sender_;
285 // The platforms system font family and size;
286 std::string system_font_family_;
287 std::string system_font_size_;
289 // Mapping of port IDs to tabs. If there is no tab, the value would be -1.
290 std::map<int, int> port_to_tab_id_map_;
292 // True once WebKit has been initialized (and it is therefore safe to poke).
293 bool is_webkit_initialized_;
295 // It is important for this to come after the ScriptInjectionManager, so that
296 // the observer is destroyed before the UserScriptSet.
297 ScopedObserver<UserScriptSetManager, UserScriptSetManager::Observer>
298 user_script_set_manager_observer_;
300 // Status of webrequest usage.
301 bool webrequest_used_;
303 // The WebView partition ID associated with this process's storage partition,
304 // if this renderer is a WebView guest render process. Otherwise, this will be
305 // empty.
306 std::string webview_partition_id_;
308 DISALLOW_COPY_AND_ASSIGN(Dispatcher);
311 } // namespace extensions
313 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_