blob: c72f190be60c7c4a8bb579b22de13d0b861ccae7 [file] [log] [blame]
[email protected]5c969b82014-03-12 04:59:051// Copyright 2014 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
Daniel Hosseinian68c0798d2021-04-16 08:16:075#include "gin/interceptor.h"
6
avi90e658dd2015-12-21 07:16:197#include <stdint.h>
8
Sebastien Marchand6d0558fd2019-01-25 16:49:379#include "base/bind.h"
[email protected]5c969b82014-03-12 04:59:0510#include "gin/arguments.h"
11#include "gin/handle.h"
[email protected]5c969b82014-03-12 04:59:0512#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"
Dan Elphick05acd602021-08-30 15:22:0719#include "v8/include/v8-function.h"
20#include "v8/include/v8-script.h"
[email protected]dda52e482014-06-27 17:08:1621#include "v8/include/v8-util.h"
[email protected]5c969b82014-03-12 04:59:0522
23namespace gin {
24
25class MyInterceptor : public Wrappable<MyInterceptor>,
26 public NamedPropertyInterceptor,
27 public IndexedPropertyInterceptor {
28 public:
Daniel Hosseinian68c0798d2021-04-16 08:16:0729 MyInterceptor(const MyInterceptor&) = delete;
30 MyInterceptor& operator=(const MyInterceptor&) = delete;
31
[email protected]5c969b82014-03-12 04:59:0532 static WrapperInfo kWrapperInfo;
33
34 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
35 return CreateHandle(isolate, new MyInterceptor(isolate));
36 }
37
38 int value() const { return value_; }
39 void set_value(int value) { value_ = value; }
40
41 // gin::NamedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2542 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
43 const std::string& property) override {
[email protected]5c969b82014-03-12 04:59:0544 if (property == "value") {
45 return ConvertToV8(isolate, value_);
46 } else if (property == "func") {
Andreas Haasc0715d32018-09-28 08:12:1747 v8::Local<v8::Context> context = isolate->GetCurrentContext();
48 return GetFunctionTemplate(isolate, "func")
49 ->GetFunction(context)
50 .ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:0551 } else {
52 return v8::Local<v8::Value>();
53 }
54 }
dcheng074c0392014-10-23 19:08:2555 bool SetNamedProperty(v8::Isolate* isolate,
56 const std::string& property,
57 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5558 if (property == "value") {
59 ConvertFromV8(isolate, value, &value_);
60 return true;
61 }
62 return false;
[email protected]5c969b82014-03-12 04:59:0563 }
dcheng074c0392014-10-23 19:08:2564 std::vector<std::string> EnumerateNamedProperties(
anujk.sharmae5481912014-10-06 11:24:1965 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0566 std::vector<std::string> result;
67 result.push_back("func");
68 result.push_back("value");
69 return result;
70 }
71
72 // gin::IndexedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2573 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
74 uint32_t index) override {
[email protected]5c969b82014-03-12 04:59:0575 if (index == 0)
76 return ConvertToV8(isolate, value_);
77 return v8::Local<v8::Value>();
78 }
dcheng074c0392014-10-23 19:08:2579 bool SetIndexedProperty(v8::Isolate* isolate,
80 uint32_t index,
81 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5582 if (index == 0) {
83 ConvertFromV8(isolate, value, &value_);
84 return true;
85 }
86 // Don't allow bypassing the interceptor.
87 return true;
[email protected]5c969b82014-03-12 04:59:0588 }
dcheng074c0392014-10-23 19:08:2589 std::vector<uint32_t> EnumerateIndexedProperties(
90 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0591 std::vector<uint32_t> result;
92 result.push_back(0);
93 return result;
94 }
95
96 private:
97 explicit MyInterceptor(v8::Isolate* isolate)
98 : NamedPropertyInterceptor(isolate, this),
99 IndexedPropertyInterceptor(isolate, this),
[email protected]dda52e482014-06-27 17:08:16100 value_(0),
101 template_cache_(isolate) {}
Chris Watkins756035a2017-12-01 03:03:27102 ~MyInterceptor() override = default;
[email protected]5c969b82014-03-12 04:59:05103
104 // gin::Wrappable
dcheng074c0392014-10-23 19:08:25105 ObjectTemplateBuilder GetObjectTemplateBuilder(
106 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:05107 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
108 .AddNamedPropertyInterceptor()
109 .AddIndexedPropertyInterceptor();
110 }
111
112 int Call(int value) {
113 int tmp = value_;
114 value_ = value;
115 return tmp;
116 }
117
[email protected]dda52e482014-06-27 17:08:16118 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(
tzik6934a312018-03-08 01:03:16125 isolate, base::BindRepeating(&MyInterceptor::Call),
Devlin Cronine9db9842018-04-09 17:51:05126 InvokerOptions{true, nullptr});
[email protected]dda52e482014-06-27 17:08:16127 template_cache_.Set(name, function_template);
128 return function_template;
129 }
130
[email protected]5c969b82014-03-12 04:59:05131 int value_;
[email protected]dda52e482014-06-27 17:08:16132
dcarney36f78b642015-04-24 10:22:49133 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
[email protected]5c969b82014-03-12 04:59:05134};
135
136WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
137
138class InterceptorTest : public V8Test {
139 public:
140 void RunInterceptorTest(const std::string& script_source) {
141 v8::Isolate* isolate = instance_->isolate();
142 v8::HandleScope handle_scope(isolate);
143
144 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
145
146 obj->set_value(42);
147 EXPECT_EQ(42, obj->value());
148
deepak.sfaaa1b62015-04-30 07:30:48149 v8::Local<v8::String> source = StringToV8(isolate, script_source);
[email protected]5c969b82014-03-12 04:59:05150 EXPECT_FALSE(source.IsEmpty());
151
bashidbd2ef9bb2015-06-02 01:39:32152 gin::TryCatch try_catch(isolate);
Adam Klein8a35b5b2018-01-17 00:51:00153 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();
[email protected]5c969b82014-03-12 04:59:05157 EXPECT_FALSE(val.IsEmpty());
deepak.sfaaa1b62015-04-30 07:30:48158 v8::Local<v8::Function> func;
[email protected]5c969b82014-03-12 04:59:05159 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
Ken Rockot2b0f07652017-04-12 19:10:49160 v8::Local<v8::Value> argv[] = {
Jeremy Romaneb8a2f172018-01-29 17:33:40161 ConvertToV8(isolate, obj.get()).ToLocalChecked(),
Ken Rockot2b0f07652017-04-12 19:10:49162 };
Ross McIlroyd298cced2018-11-23 16:14:13163 func->Call(context_.Get(isolate), v8::Undefined(isolate), 1, argv)
164 .ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:05165 EXPECT_FALSE(try_catch.HasCaught());
166 EXPECT_EQ("", try_catch.GetStackTrace());
167
168 EXPECT_EQ(191, obj->value());
169 }
170};
171
172TEST_F(InterceptorTest, NamedInterceptor) {
173 RunInterceptorTest(
174 "(function (obj) {"
175 " if (obj.value !== 42) throw 'FAIL';"
176 " else obj.value = 191; })");
177}
178
179TEST_F(InterceptorTest, NamedInterceptorCall) {
180 RunInterceptorTest(
181 "(function (obj) {"
182 " if (obj.func(191) !== 42) throw 'FAIL';"
183 " })");
184}
185
186TEST_F(InterceptorTest, IndexedInterceptor) {
187 RunInterceptorTest(
188 "(function (obj) {"
189 " if (obj[0] !== 42) throw 'FAIL';"
190 " else obj[0] = 191; })");
191}
192
[email protected]d656f872014-08-13 17:12:55193TEST_F(InterceptorTest, BypassInterceptorAllowed) {
194 RunInterceptorTest(
195 "(function (obj) {"
196 " obj.value = 191 /* make test happy */;"
197 " obj.foo = 23;"
198 " if (obj.foo !== 23) throw 'FAIL'; })");
199}
200
201TEST_F(InterceptorTest, BypassInterceptorForbidden) {
202 RunInterceptorTest(
203 "(function (obj) {"
204 " obj.value = 191 /* make test happy */;"
205 " obj[1] = 23;"
206 " if (obj[1] === 23) throw 'FAIL'; })");
207}
208
[email protected]5c969b82014-03-12 04:59:05209} // namespace gin