| 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/native_extension_bindings_system.h" | 5 #include "extensions/renderer/native_extension_bindings_system.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "content/public/common/content_switches.h" | 8 #include "content/public/common/content_switches.h" |
| 9 #include "extensions/common/constants.h" | 9 #include "extensions/common/constants.h" |
| 10 #include "extensions/common/extension_api.h" | 10 #include "extensions/common/extension_api.h" |
| 11 #include "extensions/common/extension_messages.h" | 11 #include "extensions/common/extension_messages.h" |
| 12 #include "extensions/common/features/feature_provider.h" | 12 #include "extensions/common/features/feature_provider.h" |
| 13 #include "extensions/renderer/api_binding_bridge.h" |
| 14 #include "extensions/renderer/api_binding_hooks.h" |
| 15 #include "extensions/renderer/module_system.h" |
| 13 #include "extensions/renderer/script_context.h" | 16 #include "extensions/renderer/script_context.h" |
| 14 #include "extensions/renderer/script_context_set.h" | 17 #include "extensions/renderer/script_context_set.h" |
| 15 #include "gin/converter.h" | 18 #include "gin/converter.h" |
| 19 #include "gin/handle.h" |
| 16 #include "gin/per_context_data.h" | 20 #include "gin/per_context_data.h" |
| 17 #include "third_party/WebKit/public/web/WebDocument.h" | 21 #include "third_party/WebKit/public/web/WebDocument.h" |
| 18 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 22 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 19 | 23 |
| 20 namespace extensions { | 24 namespace extensions { |
| 21 | 25 |
| 22 namespace { | 26 namespace { |
| 23 | 27 |
| 24 const char kBindingsSystemPerContextKey[] = "extension_bindings_system"; | 28 const char kBindingsSystemPerContextKey[] = "extension_bindings_system"; |
| 25 | 29 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 NativeExtensionBindingsSystem::~NativeExtensionBindingsSystem() {} | 110 NativeExtensionBindingsSystem::~NativeExtensionBindingsSystem() {} |
| 107 | 111 |
| 108 void NativeExtensionBindingsSystem::DidCreateScriptContext( | 112 void NativeExtensionBindingsSystem::DidCreateScriptContext( |
| 109 ScriptContext* context) {} | 113 ScriptContext* context) {} |
| 110 | 114 |
| 111 void NativeExtensionBindingsSystem::WillReleaseScriptContext( | 115 void NativeExtensionBindingsSystem::WillReleaseScriptContext( |
| 112 ScriptContext* context) {} | 116 ScriptContext* context) {} |
| 113 | 117 |
| 114 void NativeExtensionBindingsSystem::UpdateBindingsForContext( | 118 void NativeExtensionBindingsSystem::UpdateBindingsForContext( |
| 115 ScriptContext* context) { | 119 ScriptContext* context) { |
| 120 v8::HandleScope handle_scope(context->isolate()); |
| 116 v8::Local<v8::Context> v8_context = context->v8_context(); | 121 v8::Local<v8::Context> v8_context = context->v8_context(); |
| 117 v8::Local<v8::Object> chrome = GetOrCreateChrome(v8_context); | 122 v8::Local<v8::Object> chrome = GetOrCreateChrome(v8_context); |
| 118 if (chrome.IsEmpty()) | 123 if (chrome.IsEmpty()) |
| 119 return; | 124 return; |
| 120 | 125 |
| 121 gin::PerContextData* per_context_data = gin::PerContextData::From(v8_context); | 126 gin::PerContextData* per_context_data = gin::PerContextData::From(v8_context); |
| 122 DCHECK(per_context_data); | 127 DCHECK(per_context_data); |
| 123 BindingsSystemPerContextData* data = | 128 BindingsSystemPerContextData* data = |
| 124 static_cast<BindingsSystemPerContextData*>( | 129 static_cast<BindingsSystemPerContextData*>( |
| 125 per_context_data->GetUserData(kBindingsSystemPerContextKey)); | 130 per_context_data->GetUserData(kBindingsSystemPerContextKey)); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 // one to handle any weirdness from the caller (non-existent strings, etc). | 230 // one to handle any weirdness from the caller (non-existent strings, etc). |
| 226 v8::Local<v8::String> api_name = info.Data().As<v8::String>(); | 231 v8::Local<v8::String> api_name = info.Data().As<v8::String>(); |
| 227 v8::Local<v8::Value> result; | 232 v8::Local<v8::Value> result; |
| 228 v8::Maybe<bool> has_property = apis->HasRealNamedProperty(context, api_name); | 233 v8::Maybe<bool> has_property = apis->HasRealNamedProperty(context, api_name); |
| 229 if (!has_property.IsJust()) | 234 if (!has_property.IsJust()) |
| 230 return; | 235 return; |
| 231 | 236 |
| 232 if (has_property.FromJust()) { | 237 if (has_property.FromJust()) { |
| 233 result = apis->GetRealNamedProperty(context, api_name).ToLocalChecked(); | 238 result = apis->GetRealNamedProperty(context, api_name).ToLocalChecked(); |
| 234 } else { | 239 } else { |
| 240 ScriptContext* script_context = |
| 241 ScriptContextSet::GetContextByV8Context(context); |
| 235 std::string api_name_string; | 242 std::string api_name_string; |
| 236 CHECK(gin::Converter<std::string>::FromV8(isolate, api_name, | 243 CHECK(gin::Converter<std::string>::FromV8(isolate, api_name, |
| 237 &api_name_string)); | 244 &api_name_string)); |
| 238 result = data->bindings_system->api_system_.CreateAPIInstance( | 245 v8::Local<v8::Object> hooks_interface; |
| 246 APIBindingsSystem& api_system = data->bindings_system->api_system_; |
| 247 result = api_system.CreateAPIInstance( |
| 239 api_name_string, context, isolate, | 248 api_name_string, context, isolate, |
| 240 base::Bind(&IsAPIMethodAvailable, | 249 base::Bind(&IsAPIMethodAvailable, script_context), &hooks_interface); |
| 241 ScriptContextSet::GetContextByV8Context(context))); | 250 |
| 251 gin::Handle<APIBindingBridge> bridge_handle = gin::CreateHandle( |
| 252 isolate, |
| 253 new APIBindingBridge(v8::Global<v8::Value>(isolate, result), |
| 254 v8::Global<v8::Value>(isolate, hooks_interface), |
| 255 script_context->GetExtensionID(), |
| 256 script_context->GetContextTypeDescription(), |
| 257 base::Bind(&CallJsFunction))); |
| 258 v8::Local<v8::Value> native_api_bridge = bridge_handle.ToV8(); |
| 259 |
| 260 script_context->module_system()->OnNativeBindingCreated(api_name_string, |
| 261 native_api_bridge); |
| 262 |
| 242 v8::Maybe<bool> success = | 263 v8::Maybe<bool> success = |
| 243 apis->CreateDataProperty(context, api_name, result); | 264 apis->CreateDataProperty(context, api_name, result); |
| 244 if (!success.IsJust() || !success.FromJust()) | 265 if (!success.IsJust() || !success.FromJust()) |
| 245 return; | 266 return; |
| 246 } | 267 } |
| 247 info.GetReturnValue().Set(result); | 268 info.GetReturnValue().Set(result); |
| 248 } | 269 } |
| 249 | 270 |
| 250 void NativeExtensionBindingsSystem::SendRequest( | 271 void NativeExtensionBindingsSystem::SendRequest( |
| 251 std::unique_ptr<APIBindingsSystem::Request> request, | 272 std::unique_ptr<APIBindingsSystem::Request> request, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 269 params.has_callback = request->has_callback; | 290 params.has_callback = request->has_callback; |
| 270 params.user_gesture = request->has_user_gesture; | 291 params.user_gesture = request->has_user_gesture; |
| 271 // TODO(devlin): Make this work in ServiceWorkers. | 292 // TODO(devlin): Make this work in ServiceWorkers. |
| 272 params.worker_thread_id = -1; | 293 params.worker_thread_id = -1; |
| 273 params.service_worker_version_id = kInvalidServiceWorkerVersionId; | 294 params.service_worker_version_id = kInvalidServiceWorkerVersionId; |
| 274 | 295 |
| 275 send_ipc_.Run(script_context, params); | 296 send_ipc_.Run(script_context, params); |
| 276 } | 297 } |
| 277 | 298 |
| 278 } // namespace extensions | 299 } // namespace extensions |
| OLD | NEW |