| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/renderer/js_extension_bindings_system.h" | 5 #include "extensions/renderer/js_extension_bindings_system.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "content/public/child/v8_value_converter.h" | 9 #include "content/public/child/v8_value_converter.h" |
| 10 #include "content/public/common/content_switches.h" | 10 #include "content/public/common/content_switches.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "extensions/renderer/renderer_extension_registry.h" | 21 #include "extensions/renderer/renderer_extension_registry.h" |
| 22 #include "extensions/renderer/resource_bundle_source_map.h" | 22 #include "extensions/renderer/resource_bundle_source_map.h" |
| 23 #include "extensions/renderer/script_context.h" | 23 #include "extensions/renderer/script_context.h" |
| 24 #include "gin/converter.h" | 24 #include "gin/converter.h" |
| 25 #include "v8/include/v8.h" | 25 #include "v8/include/v8.h" |
| 26 | 26 |
| 27 namespace extensions { | 27 namespace extensions { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 static const char kEventDispatchFunction[] = "dispatchEvent"; | |
| 32 | |
| 33 // Gets |field| from |object| or creates it as an empty object if it doesn't | 31 // Gets |field| from |object| or creates it as an empty object if it doesn't |
| 34 // exist. | 32 // exist. |
| 35 v8::Local<v8::Object> GetOrCreateObject(const v8::Local<v8::Object>& object, | 33 v8::Local<v8::Object> GetOrCreateObject(const v8::Local<v8::Object>& object, |
| 36 const std::string& field, | 34 const std::string& field, |
| 37 v8::Isolate* isolate) { | 35 v8::Isolate* isolate) { |
| 38 v8::Local<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str()); | 36 v8::Local<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str()); |
| 39 // If the object has a callback property, it is assumed it is an unavailable | 37 // If the object has a callback property, it is assumed it is an unavailable |
| 40 // API, so it is safe to delete. This is checked before GetOrCreateObject is | 38 // API, so it is safe to delete. This is checked before GetOrCreateObject is |
| 41 // called. | 39 // called. |
| 42 if (object->HasRealNamedCallbackProperty(key)) { | 40 if (object->HasRealNamedCallbackProperty(key)) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 | 243 |
| 246 RequestSender* JsExtensionBindingsSystem::GetRequestSender() { | 244 RequestSender* JsExtensionBindingsSystem::GetRequestSender() { |
| 247 return request_sender_.get(); | 245 return request_sender_.get(); |
| 248 } | 246 } |
| 249 | 247 |
| 250 void JsExtensionBindingsSystem::DispatchEventInContext( | 248 void JsExtensionBindingsSystem::DispatchEventInContext( |
| 251 const std::string& event_name, | 249 const std::string& event_name, |
| 252 const base::ListValue* event_args, | 250 const base::ListValue* event_args, |
| 253 const base::DictionaryValue* filtering_info, | 251 const base::DictionaryValue* filtering_info, |
| 254 ScriptContext* context) { | 252 ScriptContext* context) { |
| 255 v8::HandleScope handle_scope(context->isolate()); | 253 EventBindings::DispatchEventInContext(event_name, event_args, filtering_info, |
| 256 v8::Context::Scope context_scope(context->v8_context()); | 254 context); |
| 257 | |
| 258 std::vector<v8::Local<v8::Value>> arguments; | |
| 259 arguments.push_back(gin::StringToSymbol(context->isolate(), event_name)); | |
| 260 | |
| 261 { | |
| 262 std::unique_ptr<content::V8ValueConverter> converter( | |
| 263 content::V8ValueConverter::create()); | |
| 264 arguments.push_back( | |
| 265 converter->ToV8Value(event_args, context->v8_context())); | |
| 266 if (filtering_info && !filtering_info->empty()) { | |
| 267 arguments.push_back( | |
| 268 converter->ToV8Value(filtering_info, context->v8_context())); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 context->module_system()->CallModuleMethodSafe( | |
| 273 kEventBindings, kEventDispatchFunction, arguments.size(), | |
| 274 arguments.data()); | |
| 275 } | 255 } |
| 276 | 256 |
| 277 bool JsExtensionBindingsSystem::HasEventListenerInContext( | 257 bool JsExtensionBindingsSystem::HasEventListenerInContext( |
| 278 const std::string& event_name, | 258 const std::string& event_name, |
| 279 ScriptContext* context) { | 259 ScriptContext* context) { |
| 280 return EventBindings::HasListener(context, event_name); | 260 return EventBindings::HasListener(context, event_name); |
| 281 } | 261 } |
| 282 | 262 |
| 283 void JsExtensionBindingsSystem::RegisterBinding( | 263 void JsExtensionBindingsSystem::RegisterBinding( |
| 284 const std::string& api_name, | 264 const std::string& api_name, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 std::unique_ptr<NativeHandler>( | 298 std::unique_ptr<NativeHandler>( |
| 319 new BindingGeneratingNativeHandler(context, api_name, "binding"))); | 299 new BindingGeneratingNativeHandler(context, api_name, "binding"))); |
| 320 module_system->SetNativeLazyField(bind_object, bind_name, api_bind_name, | 300 module_system->SetNativeLazyField(bind_object, bind_name, api_bind_name, |
| 321 "binding"); | 301 "binding"); |
| 322 } else { | 302 } else { |
| 323 module_system->SetLazyField(bind_object, bind_name, api_name, "binding"); | 303 module_system->SetLazyField(bind_object, bind_name, api_name, "binding"); |
| 324 } | 304 } |
| 325 } | 305 } |
| 326 | 306 |
| 327 } // namespace extensions | 307 } // namespace extensions |
| OLD | NEW |