Chromium Code Reviews| 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 gin::WrapperInfo APIBindingBridge::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
| 13 | |
| 14 APIBindingBridge::APIBindingBridge(v8::Global<v8::Value> api_object, | |
| 15 v8::Global<v8::Value> js_hooks_interface, | |
| 16 const std::string& extension_id, | |
| 17 const std::string& context_type, | |
| 18 const binding::RunJSFunction& run_js) | |
| 19 : api_object_(std::move(api_object)), | |
| 20 js_hooks_interface_(std::move(js_hooks_interface)), | |
| 21 extension_id_(extension_id), | |
| 22 context_type_(context_type), | |
| 23 run_js_(run_js) {} | |
| 24 | |
| 25 APIBindingBridge::~APIBindingBridge() {} | |
| 26 | |
| 27 gin::ObjectTemplateBuilder APIBindingBridge::GetObjectTemplateBuilder( | |
| 28 v8::Isolate* isolate) { | |
| 29 return Wrappable<APIBindingBridge>::GetObjectTemplateBuilder(isolate) | |
| 30 .SetMethod("registerCustomHook", &APIBindingBridge::RegisterCustomHook); | |
| 31 } | |
| 32 | |
| 33 void APIBindingBridge::RegisterCustomHook(v8::Isolate* isolate, | |
| 34 v8::Local<v8::Function> function) { | |
| 35 // The object and arguments here are meant to match those passed to the hook | |
| 36 // functions in binding.js. | |
| 37 v8::Local<v8::Context> context = isolate->GetCallingContext(); | |
|
jbroman
2016/12/16 19:00:49
GetCallingContext is deprecated; did you want GetC
Devlin
2016/12/16 20:31:17
Done.
| |
| 38 v8::Local<v8::Object> hook_object = v8::Object::New(isolate); | |
| 39 v8::Maybe<bool> result = hook_object->CreateDataProperty( | |
| 40 context, gin::StringToSymbol(isolate, "apiFunctions"), | |
| 41 js_hooks_interface_.Get(isolate)); | |
| 42 if (!result.IsJust() || !result.FromJust()) | |
| 43 return; | |
| 44 | |
| 45 result = hook_object->CreateDataProperty( | |
| 46 context, gin::StringToSymbol(isolate, "compiledApi"), | |
| 47 api_object_.Get(isolate)); | |
| 48 if (!result.IsJust() || !result.FromJust()) | |
| 49 return; | |
| 50 | |
| 51 // TODO(devlin): The binding.js version of these hooks also has a 'schema' | |
| 52 // property. I wonder if we can factor that out? If not, we'll need to add it | |
| 53 // here. | |
| 54 | |
| 55 result = hook_object->SetPrototype(context, v8::Null(isolate)); | |
| 56 if (!result.IsJust() || !result.FromJust()) | |
| 57 return; | |
| 58 | |
| 59 v8::Local<v8::String> extension_id = | |
| 60 gin::StringToSymbol(isolate, extension_id_); | |
| 61 v8::Local<v8::String> context_type = | |
| 62 gin::StringToSymbol(isolate, context_type_); | |
| 63 v8::Local<v8::Value> args[] = {hook_object, extension_id, context_type}; | |
| 64 run_js_.Run(function, context, arraysize(args), args); | |
| 65 } | |
| 66 | |
| 67 } // namespace extensions | |
| OLD | NEW |