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

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

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

Powered by Google App Engine
This is Rietveld 408576698