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. 4 5 #ifndef EXTENSIONS_RENDERER_DISPATCHER_H_ 6 #define EXTENSIONS_RENDERER_DISPATCHER_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 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" 27 28 class ChromeRenderViewTest; 29 class GURL; 30 class ModuleSystem; 31 class URLPattern; 32 struct ExtensionMsg_ExternalConnectionInfo; 33 struct ExtensionMsg_Loaded_Params; 34 struct ExtensionMsg_UpdatePermissions_Params; 35 36 namespace blink { 37 class WebFrame; 38 class WebSecurityOrigin; 39 } 40 41 namespace base { 42 class DictionaryValue; 43 class ListValue; 44 } 45 46 namespace content { 47 class RenderThread; 48 } 49 50 namespace extensions { 51 class ContentWatcher; 52 class DispatcherDelegate; 53 class Extension; 54 class FilteredEventRouter; 55 class ManifestPermissionSet; 56 class RequestSender; 57 class ScriptContext; 58 class UserScriptSlave; 59 struct Message; 60 61 // Dispatches extension control messages sent to the renderer and stores 62 // renderer extension related state. 63 class Dispatcher : public content::RenderProcessObserver { 64 public: 65 explicit Dispatcher(DispatcherDelegate* delegate); 66 virtual ~Dispatcher(); 67 function_names()68 const std::set<std::string>& function_names() const { 69 return function_names_; 70 } 71 is_extension_process()72 bool is_extension_process() const { return is_extension_process_; } 73 extensions()74 const ExtensionSet* extensions() const { return &extensions_; } 75 script_context_set()76 const ScriptContextSet& script_context_set() const { 77 return script_context_set_; 78 } 79 v8_schema_registry()80 V8SchemaRegistry* v8_schema_registry() { return v8_schema_registry_.get(); } 81 content_watcher()82 ContentWatcher* content_watcher() { return content_watcher_.get(); } 83 user_script_slave()84 UserScriptSlave* user_script_slave() { return user_script_slave_.get(); } 85 request_sender()86 RequestSender* request_sender() { return request_sender_.get(); } 87 88 bool IsExtensionActive(const std::string& extension_id) const; 89 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); 95 96 void DidCreateScriptContext(blink::WebFrame* frame, 97 const v8::Handle<v8::Context>& context, 98 int extension_group, 99 int world_id); 100 101 void WillReleaseScriptContext(blink::WebFrame* frame, 102 const v8::Handle<v8::Context>& context, 103 int world_id); 104 105 void DidCreateDocumentElement(blink::WebFrame* frame); 106 107 void DidMatchCSS( 108 blink::WebFrame* frame, 109 const blink::WebVector<blink::WebString>& newly_matching_selectors, 110 const blink::WebVector<blink::WebString>& stopped_matching_selectors); 111 112 void OnExtensionResponse(int request_id, 113 bool success, 114 const base::ListValue& response, 115 const std::string& error); 116 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; 122 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; 126 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, 133 bool user_gesture); 134 135 void ClearPortData(int port_id); 136 137 private: 138 friend class ::ChromeRenderViewTest; 139 FRIEND_TEST_ALL_PREFIXES(RendererPermissionsPolicyDelegateTest, 140 CannotScriptWebstore); 141 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; 147 148 void OnActivateExtension(const std::string& extension_id); 149 void OnCancelSuspend(const std::string& extension_id); 150 void OnClearTabSpecificPermissions( 151 int tab_id, 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); 160 void OnLoaded( 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, 167 bool user_gesture); 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 OnTransferBlobs(const std::vector<std::string>& blob_uuids); 177 void OnUnloaded(const std::string& id); 178 void OnUpdatePermissions(const ExtensionMsg_UpdatePermissions_Params& params); 179 void OnUpdateTabSpecificPermissions(int page_id, 180 int tab_id, 181 const std::string& extension_id, 182 const URLPatternSet& origin_set); 183 void OnUpdateUserScripts(base::SharedMemoryHandle scripts, 184 const std::set<std::string>& extension_ids); 185 void OnUsingWebRequestAPI(bool webrequest_used); 186 187 void UpdateActiveExtensions(); 188 189 // Sets up the host permissions for |extension|. 190 void InitOriginPermissions(const Extension* extension); 191 void UpdateOriginPermissions(UpdatedExtensionPermissionsInfo::Reason reason, 192 const Extension* extension, 193 const URLPatternSet& origins); 194 195 // Enable custom element whitelist in Apps. 196 void EnableCustomElementWhiteList(); 197 198 // Adds or removes bindings for every context belonging to |extension_id|, or 199 // or all contexts if |extension_id| is empty. 200 void UpdateBindings(const std::string& extension_id); 201 202 void UpdateBindingsForContext(ScriptContext* context); 203 204 void RegisterBinding(const std::string& api_name, ScriptContext* context); 205 206 void RegisterNativeHandlers(ModuleSystem* module_system, 207 ScriptContext* context); 208 209 // Inserts static source code into |source_map_|. 210 void PopulateSourceMap(); 211 212 // Returns whether the current renderer hosts a platform app. 213 bool IsWithinPlatformApp(); 214 215 bool IsSandboxedPage(const GURL& url) const; 216 217 // Returns the Feature::Context type of context for a JavaScript context. 218 Feature::Context ClassifyJavaScriptContext( 219 const Extension* extension, 220 int extension_group, 221 const GURL& url, 222 const blink::WebSecurityOrigin& origin); 223 224 // Gets |field| from |object| or creates it as an empty object if it doesn't 225 // exist. 226 v8::Handle<v8::Object> GetOrCreateObject(const v8::Handle<v8::Object>& object, 227 const std::string& field, 228 v8::Isolate* isolate); 229 230 v8::Handle<v8::Object> GetOrCreateBindObjectIfAvailable( 231 const std::string& api_name, 232 std::string* bind_name, 233 ScriptContext* context); 234 235 // The delegate for this dispatcher. Not owned, but must extend beyond the 236 // Dispatcher's own lifetime. 237 DispatcherDelegate* delegate_; 238 239 // True if this renderer is running extensions. 240 bool is_extension_process_; 241 242 // Contains all loaded extensions. This is essentially the renderer 243 // counterpart to ExtensionService in the browser. It contains information 244 // about all extensions currently loaded by the browser. 245 ExtensionSet extensions_; 246 247 // The IDs of extensions that failed to load, mapped to the error message 248 // generated on failure. 249 std::map<std::string, std::string> extension_load_errors_; 250 251 // All the bindings contexts that are currently loaded for this renderer. 252 // There is zero or one for each v8 context. 253 ScriptContextSet script_context_set_; 254 255 scoped_ptr<ContentWatcher> content_watcher_; 256 257 scoped_ptr<UserScriptSlave> user_script_slave_; 258 259 // Same as above, but on a longer timer and will run even if the process is 260 // not idle, to ensure that IdleHandle gets called eventually. 261 scoped_ptr<base::RepeatingTimer<content::RenderThread> > forced_idle_timer_; 262 263 // All declared function names. 264 std::set<std::string> function_names_; 265 266 // The extensions and apps that are active in this process. 267 std::set<std::string> active_extension_ids_; 268 269 ResourceBundleSourceMap source_map_; 270 271 // Cache for the v8 representation of extension API schemas. 272 scoped_ptr<V8SchemaRegistry> v8_schema_registry_; 273 274 // Sends API requests to the extension host. 275 scoped_ptr<RequestSender> request_sender_; 276 277 // The platforms system font family and size; 278 std::string system_font_family_; 279 std::string system_font_size_; 280 281 // Mapping of port IDs to tabs. If there is no tab, the value would be -1. 282 std::map<int, int> port_to_tab_id_map_; 283 284 // True once WebKit has been initialized (and it is therefore safe to poke). 285 bool is_webkit_initialized_; 286 287 DISALLOW_COPY_AND_ASSIGN(Dispatcher); 288 }; 289 290 } // namespace extensions 291 292 #endif // EXTENSIONS_RENDERER_DISPATCHER_H_ 293