Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: extensions/renderer/api_binding_bridge.cc

Issue 2575173002: [Extensions Bindings] Add a bridge to use current custom bindings (Closed)
Patch Set: update test Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 kHookObjectKey[] = "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, kHookObjectKey),
45 js_hook_interface);
46 DCHECK(result.IsJust() && result.FromJust());
47 }
48
49 APIBindingBridge::~APIBindingBridge() {}
50
51 gin::ObjectTemplateBuilder APIBindingBridge::GetObjectTemplateBuilder(
52 v8::Isolate* isolate) {
53 return Wrappable<APIBindingBridge>::GetObjectTemplateBuilder(isolate)
54 .SetMethod("registerCustomHook", &APIBindingBridge::RegisterCustomHook);
55 }
56
57 void APIBindingBridge::RegisterCustomHook(v8::Isolate* isolate,
58 v8::Local<v8::Function> function) {
59 // The object and arguments here are meant to match those passed to the hook
60 // functions in binding.js.
61 v8::Local<v8::Context> context = isolate->GetCurrentContext();
62 v8::Local<v8::Object> hook_object = v8::Object::New(isolate);
63 v8::Local<v8::Object> wrapper = GetWrapper(isolate);
64 v8::Local<v8::Value> hook_interface;
jbroman 2016/12/20 20:51:34 The naming is a little funny here, with kHookObjec
Devlin 2016/12/20 22:20:19 s/kHookObjectKey/kHookInterfaceKey
65 if (!wrapper
66 ->GetPrivate(context,
67 GetPrivatePropertyName(isolate, kHookObjectKey))
68 .ToLocal(&hook_interface)) {
69 return;
jbroman 2016/12/20 20:51:34 nit: It'd be pretty surprising for these private p
Devlin 2016/12/20 22:20:19 ToLocalChecked()'ing.
70 }
71 v8::Maybe<bool> result = hook_object->CreateDataProperty(
72 context, gin::StringToSymbol(isolate, "apiFunctions"), hook_interface);
73 if (!result.IsJust() || !result.FromJust())
74 return;
75
76 v8::Local<v8::Value> api_object;
77 if (!wrapper
78 ->GetPrivate(context, GetPrivatePropertyName(isolate, kApiObjectKey))
79 .ToLocal(&api_object)) {
80 return;
81 }
82 result = hook_object->CreateDataProperty(
83 context, gin::StringToSymbol(isolate, "compiledApi"), api_object);
84 if (!result.IsJust() || !result.FromJust())
85 return;
86
87 // TODO(devlin): The binding.js version of these hooks also has a 'schema'
88 // property. I wonder if we can factor that out? If not, we'll need to add it
89 // here.
90
91 result = hook_object->SetPrototype(context, v8::Null(isolate));
92 if (!result.IsJust() || !result.FromJust())
93 return;
94
95 v8::Local<v8::String> extension_id =
96 gin::StringToSymbol(isolate, extension_id_);
97 v8::Local<v8::String> context_type =
98 gin::StringToSymbol(isolate, context_type_);
99 v8::Local<v8::Value> args[] = {hook_object, extension_id, context_type};
100 run_js_.Run(function, context, arraysize(args), args);
101 }
102
103 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698