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

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: . 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 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698