| 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/api_binding_bridge.h" | 5 #include "extensions/renderer/api_binding_bridge.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "extensions/renderer/api_binding_hooks.h" | 8 #include "extensions/renderer/api_binding_hooks.h" |
| 9 #include "extensions/renderer/api_event_handler.h" |
| 9 #include "extensions/renderer/api_request_handler.h" | 10 #include "extensions/renderer/api_request_handler.h" |
| 10 #include "extensions/renderer/api_signature.h" | 11 #include "extensions/renderer/api_signature.h" |
| 11 #include "extensions/renderer/api_type_reference_map.h" | 12 #include "extensions/renderer/api_type_reference_map.h" |
| 12 #include "gin/converter.h" | 13 #include "gin/converter.h" |
| 13 #include "gin/object_template_builder.h" | 14 #include "gin/object_template_builder.h" |
| 14 | 15 |
| 15 namespace extensions { | 16 namespace extensions { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char kApiObjectKey[] = "extensions::bridge::api_object"; | 20 const char kApiObjectKey[] = "extensions::bridge::api_object"; |
| 20 const char kHookInterfaceKey[] = "extensions::bridge::hook_object"; | 21 const char kHookInterfaceKey[] = "extensions::bridge::hook_object"; |
| 21 | 22 |
| 22 v8::Local<v8::Private> GetPrivatePropertyName(v8::Isolate* isolate, | 23 v8::Local<v8::Private> GetPrivatePropertyName(v8::Isolate* isolate, |
| 23 const char* key) { | 24 const char* key) { |
| 24 return v8::Private::ForApi(isolate, gin::StringToSymbol(isolate, key)); | 25 return v8::Private::ForApi(isolate, gin::StringToSymbol(isolate, key)); |
| 25 } | 26 } |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 gin::WrapperInfo APIBindingBridge::kWrapperInfo = {gin::kEmbedderNativeGin}; | 30 gin::WrapperInfo APIBindingBridge::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 30 | 31 |
| 31 APIBindingBridge::APIBindingBridge(const APITypeReferenceMap* type_refs, | 32 APIBindingBridge::APIBindingBridge(const APITypeReferenceMap* type_refs, |
| 32 APIRequestHandler* request_handler, | 33 APIRequestHandler* request_handler, |
| 34 APIEventHandler* event_handler, |
| 33 APIBindingHooks* hooks, | 35 APIBindingHooks* hooks, |
| 34 v8::Local<v8::Context> context, | 36 v8::Local<v8::Context> context, |
| 35 v8::Local<v8::Value> api_object, | 37 v8::Local<v8::Value> api_object, |
| 36 const std::string& extension_id, | 38 const std::string& extension_id, |
| 37 const std::string& context_type, | 39 const std::string& context_type, |
| 38 const binding::RunJSFunction& run_js) | 40 const binding::RunJSFunction& run_js) |
| 39 : type_refs_(type_refs), | 41 : type_refs_(type_refs), |
| 40 request_handler_(request_handler), | 42 request_handler_(request_handler), |
| 43 event_handler_(event_handler), |
| 41 hooks_(hooks), | 44 hooks_(hooks), |
| 42 extension_id_(extension_id), | 45 extension_id_(extension_id), |
| 43 context_type_(context_type), | 46 context_type_(context_type), |
| 44 run_js_(run_js) { | 47 run_js_(run_js) { |
| 45 v8::Isolate* isolate = context->GetIsolate(); | 48 v8::Isolate* isolate = context->GetIsolate(); |
| 46 v8::Local<v8::Object> wrapper = GetWrapper(isolate); | 49 v8::Local<v8::Object> wrapper = GetWrapper(isolate); |
| 47 v8::Maybe<bool> result = wrapper->SetPrivate( | 50 v8::Maybe<bool> result = wrapper->SetPrivate( |
| 48 context, GetPrivatePropertyName(isolate, kApiObjectKey), api_object); | 51 context, GetPrivatePropertyName(isolate, kApiObjectKey), api_object); |
| 49 if (!result.IsJust() || !result.FromJust()) { | 52 if (!result.IsJust() || !result.FromJust()) { |
| 50 NOTREACHED(); | 53 NOTREACHED(); |
| 51 return; | 54 return; |
| 52 } | 55 } |
| 53 v8::Local<v8::Object> js_hook_interface = hooks_->GetJSHookInterface(context); | 56 v8::Local<v8::Object> js_hook_interface = hooks_->GetJSHookInterface(context); |
| 54 result = wrapper->SetPrivate(context, | 57 result = wrapper->SetPrivate(context, |
| 55 GetPrivatePropertyName(isolate, | 58 GetPrivatePropertyName(isolate, |
| 56 kHookInterfaceKey), | 59 kHookInterfaceKey), |
| 57 js_hook_interface); | 60 js_hook_interface); |
| 58 DCHECK(result.IsJust() && result.FromJust()); | 61 DCHECK(result.IsJust() && result.FromJust()); |
| 59 } | 62 } |
| 60 | 63 |
| 61 APIBindingBridge::~APIBindingBridge() {} | 64 APIBindingBridge::~APIBindingBridge() {} |
| 62 | 65 |
| 63 gin::ObjectTemplateBuilder APIBindingBridge::GetObjectTemplateBuilder( | 66 gin::ObjectTemplateBuilder APIBindingBridge::GetObjectTemplateBuilder( |
| 64 v8::Isolate* isolate) { | 67 v8::Isolate* isolate) { |
| 65 return Wrappable<APIBindingBridge>::GetObjectTemplateBuilder(isolate) | 68 return Wrappable<APIBindingBridge>::GetObjectTemplateBuilder(isolate) |
| 66 .SetMethod("registerCustomHook", &APIBindingBridge::RegisterCustomHook) | 69 .SetMethod("registerCustomHook", &APIBindingBridge::RegisterCustomHook) |
| 67 .SetMethod("sendRequest", &APIBindingBridge::SendRequest); | 70 .SetMethod("sendRequest", &APIBindingBridge::SendRequest) |
| 71 .SetMethod("registerEventArgumentMassager", |
| 72 &APIBindingBridge::RegisterEventArgumentMassager); |
| 68 } | 73 } |
| 69 | 74 |
| 70 void APIBindingBridge::RegisterCustomHook(v8::Isolate* isolate, | 75 void APIBindingBridge::RegisterCustomHook(v8::Isolate* isolate, |
| 71 v8::Local<v8::Function> function) { | 76 v8::Local<v8::Function> function) { |
| 72 // The object and arguments here are meant to match those passed to the hook | 77 // The object and arguments here are meant to match those passed to the hook |
| 73 // functions in binding.js. | 78 // functions in binding.js. |
| 74 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 79 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 75 v8::Local<v8::Object> hook_object = v8::Object::New(isolate); | 80 v8::Local<v8::Object> hook_object = v8::Object::New(isolate); |
| 76 v8::Local<v8::Object> wrapper = GetWrapper(isolate); | 81 v8::Local<v8::Object> wrapper = GetWrapper(isolate); |
| 77 v8::Local<v8::Value> hook_interface = | 82 v8::Local<v8::Value> hook_interface = |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 std::string error; | 131 std::string error; |
| 127 CHECK(signature->ParseArgumentsToJSON(context, request_args, *type_refs_, | 132 CHECK(signature->ParseArgumentsToJSON(context, request_args, *type_refs_, |
| 128 &converted_arguments, &callback, | 133 &converted_arguments, &callback, |
| 129 &error)); | 134 &error)); |
| 130 | 135 |
| 131 request_handler_->StartRequest(context, name, std::move(converted_arguments), | 136 request_handler_->StartRequest(context, name, std::move(converted_arguments), |
| 132 callback, | 137 callback, |
| 133 hooks_->GetCustomJSCallback(name, context)); | 138 hooks_->GetCustomJSCallback(name, context)); |
| 134 } | 139 } |
| 135 | 140 |
| 141 void APIBindingBridge::RegisterEventArgumentMassager( |
| 142 gin::Arguments* arguments, |
| 143 const std::string& event_name, |
| 144 v8::Local<v8::Function> massager) { |
| 145 v8::Isolate* isolate = arguments->isolate(); |
| 146 v8::HandleScope handle_scope(isolate); |
| 147 v8::Local<v8::Object> holder; |
| 148 CHECK(arguments->GetHolder(&holder)); |
| 149 v8::Local<v8::Context> context = holder->CreationContext(); |
| 150 |
| 151 event_handler_->RegisterArgumentMassager(context, event_name, massager); |
| 152 } |
| 153 |
| 136 } // namespace extensions | 154 } // namespace extensions |
| OLD | NEW |