blob: 03b0505b65c862f5b96603e7ca99e4869b77cc57 [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"
[email protected]dda52e482014-06-27 17:08:1619#include "v8/include/v8-util.h"
[email protected]5c969b82014-03-12 04:59:0520
21namespace gin {
22
23class MyInterceptor : public Wrappable<MyInterceptor>,
24 public NamedPropertyInterceptor,
25 public IndexedPropertyInterceptor {
26 public:
Daniel Hosseinian68c0798d2021-04-16 08:16:0727 MyInterceptor(const MyInterceptor&) = delete;
28 MyInterceptor& operator=(const MyInterceptor&) = delete;
29
[email protected]5c969b82014-03-12 04:59:0530 static WrapperInfo kWrapperInfo;
31
32 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
33 return CreateHandle(isolate, new MyInterceptor(isolate));
34 }
35
36 int value() const { return value_; }
37 void set_value(int value) { value_ = value; }
38
39 // gin::NamedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2540 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
41 const std::string& property) override {
[email protected]5c969b82014-03-12 04:59:0542 if (property == "value") {
43 return ConvertToV8(isolate, value_);
44 } else if (property == "func") {
Andreas Haasc0715d32018-09-28 08:12:1745 v8::Local<v8::Context> context = isolate->GetCurrentContext();
46 return GetFunctionTemplate(isolate, "func")
47 ->GetFunction(context)
48 .ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:0549 } else {
50 return v8::Local<v8::Value>();
51 }
52 }
dcheng074c0392014-10-23 19:08:2553 bool SetNamedProperty(v8::Isolate* isolate,
54 const std::string& property,
55 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5556 if (property == "value") {
57 ConvertFromV8(isolate, value, &value_);
58 return true;
59 }
60 return false;
[email protected]5c969b82014-03-12 04:59:0561 }
dcheng074c0392014-10-23 19:08:2562 std::vector<std::string> EnumerateNamedProperties(
anujk.sharmae5481912014-10-06 11:24:1963 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0564 std::vector<std::string> result;
65 result.push_back("func");
66 result.push_back("value");
67 return result;
68 }
69
70 // gin::IndexedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2571 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
72 uint32_t index) override {
[email protected]5c969b82014-03-12 04:59:0573 if (index == 0)
74 return ConvertToV8(isolate, value_);
75 return v8::Local<v8::Value>();
76 }
dcheng074c0392014-10-23 19:08:2577 bool SetIndexedProperty(v8::Isolate* isolate,
78 uint32_t index,
79 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5580 if (index == 0) {
81 ConvertFromV8(isolate, value, &value_);
82 return true;
83 }
84 // Don't allow bypassing the interceptor.
85 return true;
[email protected]5c969b82014-03-12 04:59:0586 }
dcheng074c0392014-10-23 19:08:2587 std::vector<uint32_t> EnumerateIndexedProperties(
88 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0589 std::vector<uint32_t> result;
90 result.push_back(0);
91 return result;
92 }
93
94 private:
95 explicit MyInterceptor(v8::Isolate* isolate)
96 : NamedPropertyInterceptor(isolate, this),
97 IndexedPropertyInterceptor(isolate, this),
[email protected]dda52e482014-06-27 17:08:1698 value_(0),
99 template_cache_(isolate) {}
Chris Watkins756035a2017-12-01 03:03:27100 ~MyInterceptor() override = default;
[email protected]5c969b82014-03-12 04:59:05101
102 // gin::Wrappable
dcheng074c0392014-10-23 19:08:25103 ObjectTemplateBuilder GetObjectTemplateBuilder(
104 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:05105 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
106 .AddNamedPropertyInterceptor()
107 .AddIndexedPropertyInterceptor();
108 }
109
110 int Call(int value) {
111 int tmp = value_;
112 value_ = value;
113 return tmp;
114 }
115
[email protected]dda52e482014-06-27 17:08:16116 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
117 const std::string& name) {
118 v8::Local<v8::FunctionTemplate> function_template =
119 template_cache_.Get(name);
120 if (!function_template.IsEmpty())
121 return function_template;
122 function_template = CreateFunctionTemplate(
tzik6934a312018-03-08 01:03:16123 isolate, base::BindRepeating(&MyInterceptor::Call),
Devlin Cronine9db9842018-04-09 17:51:05124 InvokerOptions{true, nullptr});
[email protected]dda52e482014-06-27 17:08:16125 template_cache_.Set(name, function_template);
126 return function_template;
127 }
128
[email protected]5c969b82014-03-12 04:59:05129 int value_;
[email protected]dda52e482014-06-27 17:08:16130
dcarney36f78b642015-04-24 10:22:49131 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
[email protected]5c969b82014-03-12 04:59:05132};
133
134WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
135
136class InterceptorTest : public V8Test {
137 public:
138 void RunInterceptorTest(const std::string& script_source) {
139 v8::Isolate* isolate = instance_->isolate();
140 v8::HandleScope handle_scope(isolate);
141
142 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
143
144 obj->set_value(42);
145 EXPECT_EQ(42, obj->value());
146
deepak.sfaaa1b62015-04-30 07:30:48147 v8::Local<v8::String> source = StringToV8(isolate, script_source);
[email protected]5c969b82014-03-12 04:59:05148 EXPECT_FALSE(source.IsEmpty());
149
bashidbd2ef9bb2015-06-02 01:39:32150 gin::TryCatch try_catch(isolate);
Adam Klein8a35b5b2018-01-17 00:51:00151 v8::Local<v8::Script> script =
152 v8::Script::Compile(context_.Get(isolate), source).ToLocalChecked();
153 v8::Local<v8::Value> val =
154 script->Run(context_.Get(isolate)).ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:05155 EXPECT_FALSE(val.IsEmpty());
deepak.sfaaa1b62015-04-30 07:30:48156 v8::Local<v8::Function> func;
[email protected]5c969b82014-03-12 04:59:05157 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
Ken Rockot2b0f07652017-04-12 19:10:49158 v8::Local<v8::Value> argv[] = {
Jeremy Romaneb8a2f172018-01-29 17:33:40159 ConvertToV8(isolate, obj.get()).ToLocalChecked(),
Ken Rockot2b0f07652017-04-12 19:10:49160 };
Ross McIlroyd298cced2018-11-23 16:14:13161 func->Call(context_.Get(isolate), v8::Undefined(isolate), 1, argv)
162 .ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:05163 EXPECT_FALSE(try_catch.HasCaught());
164 EXPECT_EQ("", try_catch.GetStackTrace());
165
166 EXPECT_EQ(191, obj->value());
167 }
168};
169
170TEST_F(InterceptorTest, NamedInterceptor) {
171 RunInterceptorTest(
172 "(function (obj) {"
173 " if (obj.value !== 42) throw 'FAIL';"
174 " else obj.value = 191; })");
175}
176
177TEST_F(InterceptorTest, NamedInterceptorCall) {
178 RunInterceptorTest(
179 "(function (obj) {"
180 " if (obj.func(191) !== 42) throw 'FAIL';"
181 " })");
182}
183
184TEST_F(InterceptorTest, IndexedInterceptor) {
185 RunInterceptorTest(
186 "(function (obj) {"
187 " if (obj[0] !== 42) throw 'FAIL';"
188 " else obj[0] = 191; })");
189}
190
[email protected]d656f872014-08-13 17:12:55191TEST_F(InterceptorTest, BypassInterceptorAllowed) {
192 RunInterceptorTest(
193 "(function (obj) {"
194 " obj.value = 191 /* make test happy */;"
195 " obj.foo = 23;"
196 " if (obj.foo !== 23) throw 'FAIL'; })");
197}
198
199TEST_F(InterceptorTest, BypassInterceptorForbidden) {
200 RunInterceptorTest(
201 "(function (obj) {"
202 " obj.value = 191 /* make test happy */;"
203 " obj[1] = 23;"
204 " if (obj[1] === 23) throw 'FAIL'; })");
205}
206
[email protected]5c969b82014-03-12 04:59:05207} // namespace gin