blob: cda3b22957c3b260cb91fdf507495143cbdc85db [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
avi90e658dd2015-12-21 07:16:195#include <stdint.h>
6
Sebastien Marchand6d0558fd2019-01-25 16:49:377#include "base/bind.h"
[email protected]5c969b82014-03-12 04:59:058#include "base/logging.h"
avi90e658dd2015-12-21 07:16:199#include "base/macros.h"
[email protected]5c969b82014-03-12 04:59:0510#include "gin/arguments.h"
11#include "gin/handle.h"
12#include "gin/interceptor.h"
13#include "gin/object_template_builder.h"
14#include "gin/per_isolate_data.h"
15#include "gin/public/isolate_holder.h"
16#include "gin/test/v8_test.h"
17#include "gin/try_catch.h"
18#include "gin/wrappable.h"
19#include "testing/gtest/include/gtest/gtest.h"
[email protected]dda52e482014-06-27 17:08:1620#include "v8/include/v8-util.h"
[email protected]5c969b82014-03-12 04:59:0521
22namespace gin {
23
24class MyInterceptor : public Wrappable<MyInterceptor>,
25 public NamedPropertyInterceptor,
26 public IndexedPropertyInterceptor {
27 public:
28 static WrapperInfo kWrapperInfo;
29
30 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
31 return CreateHandle(isolate, new MyInterceptor(isolate));
32 }
33
34 int value() const { return value_; }
35 void set_value(int value) { value_ = value; }
36
37 // gin::NamedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2538 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
39 const std::string& property) override {
[email protected]5c969b82014-03-12 04:59:0540 if (property == "value") {
41 return ConvertToV8(isolate, value_);
42 } else if (property == "func") {
Andreas Haasc0715d32018-09-28 08:12:1743 v8::Local<v8::Context> context = isolate->GetCurrentContext();
44 return GetFunctionTemplate(isolate, "func")
45 ->GetFunction(context)
46 .ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:0547 } else {
48 return v8::Local<v8::Value>();
49 }
50 }
dcheng074c0392014-10-23 19:08:2551 bool SetNamedProperty(v8::Isolate* isolate,
52 const std::string& property,
53 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5554 if (property == "value") {
55 ConvertFromV8(isolate, value, &value_);
56 return true;
57 }
58 return false;
[email protected]5c969b82014-03-12 04:59:0559 }
dcheng074c0392014-10-23 19:08:2560 std::vector<std::string> EnumerateNamedProperties(
anujk.sharmae5481912014-10-06 11:24:1961 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0562 std::vector<std::string> result;
63 result.push_back("func");
64 result.push_back("value");
65 return result;
66 }
67
68 // gin::IndexedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2569 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
70 uint32_t index) override {
[email protected]5c969b82014-03-12 04:59:0571 if (index == 0)
72 return ConvertToV8(isolate, value_);
73 return v8::Local<v8::Value>();
74 }
dcheng074c0392014-10-23 19:08:2575 bool SetIndexedProperty(v8::Isolate* isolate,
76 uint32_t index,
77 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5578 if (index == 0) {
79 ConvertFromV8(isolate, value, &value_);
80 return true;
81 }
82 // Don't allow bypassing the interceptor.
83 return true;
[email protected]5c969b82014-03-12 04:59:0584 }
dcheng074c0392014-10-23 19:08:2585 std::vector<uint32_t> EnumerateIndexedProperties(
86 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0587 std::vector<uint32_t> result;
88 result.push_back(0);
89 return result;
90 }
91
92 private:
93 explicit MyInterceptor(v8::Isolate* isolate)
94 : NamedPropertyInterceptor(isolate, this),
95 IndexedPropertyInterceptor(isolate, this),
[email protected]dda52e482014-06-27 17:08:1696 value_(0),
97 template_cache_(isolate) {}
Chris Watkins756035a2017-12-01 03:03:2798 ~MyInterceptor() override = default;
[email protected]5c969b82014-03-12 04:59:0599
100 // gin::Wrappable
dcheng074c0392014-10-23 19:08:25101 ObjectTemplateBuilder GetObjectTemplateBuilder(
102 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:05103 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
104 .AddNamedPropertyInterceptor()
105 .AddIndexedPropertyInterceptor();
106 }
107
108 int Call(int value) {
109 int tmp = value_;
110 value_ = value;
111 return tmp;
112 }
113
[email protected]dda52e482014-06-27 17:08:16114 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
115 const std::string& name) {
116 v8::Local<v8::FunctionTemplate> function_template =
117 template_cache_.Get(name);
118 if (!function_template.IsEmpty())
119 return function_template;
120 function_template = CreateFunctionTemplate(
tzik6934a312018-03-08 01:03:16121 isolate, base::BindRepeating(&MyInterceptor::Call),
Devlin Cronine9db9842018-04-09 17:51:05122 InvokerOptions{true, nullptr});
[email protected]dda52e482014-06-27 17:08:16123 template_cache_.Set(name, function_template);
124 return function_template;
125 }
126
[email protected]5c969b82014-03-12 04:59:05127 int value_;
[email protected]dda52e482014-06-27 17:08:16128
dcarney36f78b642015-04-24 10:22:49129 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
[email protected]dda52e482014-06-27 17:08:16130
131 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
[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