OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/renderer/api_binding_bridge.h" |
| 6 |
| 7 #include "gin/converter.h" |
| 8 #include "gin/object_template_builder.h" |
| 9 |
| 10 namespace extensions { |
| 11 |
| 12 namespace { |
| 13 |
| 14 const char kApiObjectKey[] = "extensions::bridge::api_object"; |
| 15 const char kHookInterfaceKey[] = "extensions::bridge::hook_object"; |
| 16 |
| 17 v8::Local<v8::Private> GetPrivatePropertyName(v8::Isolate* isolate, |
| 18 const char* key) { |
| 19 return v8::Private::ForApi(isolate, gin::StringToSymbol(isolate, key)); |
| 20 } |
| 21 |
| 22 } // namespace |
| 23 |
| 24 gin::WrapperInfo APIBindingBridge::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 25 |
| 26 APIBindingBridge::APIBindingBridge(v8::Local<v8::Context> context, |
| 27 v8::Local<v8::Value> api_object, |
| 28 v8::Local<v8::Value> js_hook_interface, |
| 29 const std::string& extension_id, |
| 30 const std::string& context_type, |
| 31 const binding::RunJSFunction& run_js) |
| 32 : extension_id_(extension_id), |
| 33 context_type_(context_type), |
| 34 run_js_(run_js) { |
| 35 v8::Isolate* isolate = context->GetIsolate(); |
| 36 v8::Local<v8::Object> wrapper = GetWrapper(isolate); |
| 37 v8::Maybe<bool> result = wrapper->SetPrivate( |
| 38 context, GetPrivatePropertyName(isolate, kApiObjectKey), api_object); |
| 39 if (!result.IsJust() || !result.FromJust()) { |
| 40 NOTREACHED(); |
| 41 return; |
| 42 } |
| 43 result = wrapper->SetPrivate(context, |
| 44 GetPrivatePropertyName(isolate, |
| 45 kHookInterfaceKey), |
| 46 js_hook_interface); |
| 47 DCHECK(result.IsJust() && result.FromJust()); |
| 48 } |
| 49 |
| 50 APIBindingBridge::~APIBindingBridge() {} |
| 51 |
| 52 gin::ObjectTemplateBuilder APIBindingBridge::GetObjectTemplateBuilder( |
| 53 v8::Isolate* isolate) { |
| 54 return Wrappable<APIBindingBridge>::GetObjectTemplateBuilder(isolate) |
| 55 .SetMethod("registerCustomHook", &APIBindingBridge::RegisterCustomHook); |
| 56 } |
| 57 |
| 58 void APIBindingBridge::RegisterCustomHook(v8::Isolate* isolate, |
| 59 v8::Local<v8::Function> function) { |
| 60 // The object and arguments here are meant to match those passed to the hook |
| 61 // functions in binding.js. |
| 62 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 63 v8::Local<v8::Object> hook_object = v8::Object::New(isolate); |
| 64 v8::Local<v8::Object> wrapper = GetWrapper(isolate); |
| 65 v8::Local<v8::Value> hook_interface = |
| 66 wrapper->GetPrivate( |
| 67 context, GetPrivatePropertyName(isolate, kHookInterfaceKey)) |
| 68 .ToLocalChecked(); |
| 69 v8::Maybe<bool> result = hook_object->CreateDataProperty( |
| 70 context, gin::StringToSymbol(isolate, "apiFunctions"), hook_interface); |
| 71 if (!result.IsJust() || !result.FromJust()) |
| 72 return; |
| 73 |
| 74 v8::Local<v8::Value> api_object = |
| 75 wrapper |
| 76 ->GetPrivate(context, GetPrivatePropertyName(isolate, kApiObjectKey)) |
| 77 .ToLocalChecked(); |
| 78 result = hook_object->CreateDataProperty( |
| 79 context, gin::StringToSymbol(isolate, "compiledApi"), api_object); |
| 80 if (!result.IsJust() || !result.FromJust()) |
| 81 return; |
| 82 |
| 83 // TODO(devlin): The binding.js version of these hooks also has a 'schema' |
| 84 // property. I wonder if we can factor that out? If not, we'll need to add it |
| 85 // here. |
| 86 |
| 87 result = hook_object->SetPrototype(context, v8::Null(isolate)); |
| 88 if (!result.IsJust() || !result.FromJust()) |
| 89 return; |
| 90 |
| 91 v8::Local<v8::String> extension_id = |
| 92 gin::StringToSymbol(isolate, extension_id_); |
| 93 v8::Local<v8::String> context_type = |
| 94 gin::StringToSymbol(isolate, context_type_); |
| 95 v8::Local<v8::Value> args[] = {hook_object, extension_id, context_type}; |
| 96 run_js_.Run(function, context, arraysize(args), args); |
| 97 } |
| 98 |
| 99 } // namespace extensions |
OLD | NEW |