1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/interceptor.h"
9 #include "base/functional/bind.h"
10 #include "gin/arguments.h"
11 #include "gin/handle.h"
12 #include "gin/object_template_builder.h"
13 #include "gin/per_isolate_data.h"
14 #include "gin/public/isolate_holder.h"
15 #include "gin/test/v8_test.h"
16 #include "gin/try_catch.h"
17 #include "gin/wrappable.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "v8/include/v8-function.h"
20 #include "v8/include/v8-script.h"
21 #include "v8/include/v8-util.h"
25 class MyInterceptor : public Wrappable<MyInterceptor>,
26 public NamedPropertyInterceptor,
27 public IndexedPropertyInterceptor {
29 MyInterceptor(const MyInterceptor&) = delete;
30 MyInterceptor& operator=(const MyInterceptor&) = delete;
32 static WrapperInfo kWrapperInfo;
34 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
35 return CreateHandle(isolate, new MyInterceptor(isolate));
38 int value() const { return value_; }
39 void set_value(int value) { value_ = value; }
41 // gin::NamedPropertyInterceptor
42 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
43 const std::string& property) override {
44 if (property == "value") {
45 return ConvertToV8(isolate, value_);
46 } else if (property == "func") {
47 v8::Local<v8::Context> context = isolate->GetCurrentContext();
48 return GetFunctionTemplate(isolate, "func")
49 ->GetFunction(context)
52 return v8::Local<v8::Value>();
55 bool SetNamedProperty(v8::Isolate* isolate,
56 const std::string& property,
57 v8::Local<v8::Value> value) override {
58 if (property == "value") {
59 ConvertFromV8(isolate, value, &value_);
64 std::vector<std::string> EnumerateNamedProperties(
65 v8::Isolate* isolate) override {
66 std::vector<std::string> result;
67 result.push_back("func");
68 result.push_back("value");
72 // gin::IndexedPropertyInterceptor
73 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
74 uint32_t index) override {
76 return ConvertToV8(isolate, value_);
77 return v8::Local<v8::Value>();
79 bool SetIndexedProperty(v8::Isolate* isolate,
81 v8::Local<v8::Value> value) override {
83 ConvertFromV8(isolate, value, &value_);
86 // Don't allow bypassing the interceptor.
89 std::vector<uint32_t> EnumerateIndexedProperties(
90 v8::Isolate* isolate) override {
91 std::vector<uint32_t> result;
97 explicit MyInterceptor(v8::Isolate* isolate)
98 : NamedPropertyInterceptor(isolate, this),
99 IndexedPropertyInterceptor(isolate, this),
101 template_cache_(isolate) {}
102 ~MyInterceptor() override = default;
105 ObjectTemplateBuilder GetObjectTemplateBuilder(
106 v8::Isolate* isolate) override {
107 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
108 .AddNamedPropertyInterceptor()
109 .AddIndexedPropertyInterceptor();
112 int Call(int value) {
118 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
119 const std::string& name) {
120 v8::Local<v8::FunctionTemplate> function_template =
121 template_cache_.Get(name);
122 if (!function_template.IsEmpty())
123 return function_template;
124 function_template = CreateFunctionTemplate(
125 isolate, base::BindRepeating(&MyInterceptor::Call),
126 InvokerOptions{true, nullptr});
127 template_cache_.Set(name, function_template);
128 return function_template;
133 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
136 WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
138 class InterceptorTest : public V8Test {
140 void RunInterceptorTest(const std::string& script_source) {
141 v8::Isolate* isolate = instance_->isolate();
142 v8::HandleScope handle_scope(isolate);
144 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
147 EXPECT_EQ(42, obj->value());
149 v8::Local<v8::String> source = StringToV8(isolate, script_source);
150 EXPECT_FALSE(source.IsEmpty());
152 gin::TryCatch try_catch(isolate);
153 v8::Local<v8::Script> script =
154 v8::Script::Compile(context_.Get(isolate), source).ToLocalChecked();
155 v8::Local<v8::Value> val =
156 script->Run(context_.Get(isolate)).ToLocalChecked();
157 EXPECT_FALSE(val.IsEmpty());
158 v8::Local<v8::Function> func;
159 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
160 v8::Local<v8::Value> argv[] = {
161 ConvertToV8(isolate, obj.get()).ToLocalChecked(),
163 func->Call(context_.Get(isolate), v8::Undefined(isolate), 1, argv)
165 EXPECT_FALSE(try_catch.HasCaught());
166 EXPECT_EQ("", try_catch.GetStackTrace());
168 EXPECT_EQ(191, obj->value());
172 TEST_F(InterceptorTest, NamedInterceptor) {
175 " if (obj.value !== 42) throw 'FAIL';"
176 " else obj.value = 191; })");
179 TEST_F(InterceptorTest, NamedInterceptorCall) {
182 " if (obj.func(191) !== 42) throw 'FAIL';"
186 TEST_F(InterceptorTest, IndexedInterceptor) {
189 " if (obj[0] !== 42) throw 'FAIL';"
190 " else obj[0] = 191; })");
193 TEST_F(InterceptorTest, BypassInterceptorAllowed) {
196 " obj.value = 191 /* make test happy */;"
198 " if (obj.foo !== 23) throw 'FAIL'; })");
201 TEST_F(InterceptorTest, BypassInterceptorForbidden) {
204 " obj.value = 191 /* make test happy */;"
206 " if (obj[1] === 23) throw 'FAIL'; })");