blob: e5fb1f22173eee3494ac7444e1167b01b89f31f6 [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
[email protected]5c969b82014-03-12 04:59:057#include "base/logging.h"
avi90e658dd2015-12-21 07:16:198#include "base/macros.h"
[email protected]5c969b82014-03-12 04:59:059#include "gin/arguments.h"
10#include "gin/handle.h"
11#include "gin/interceptor.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"
[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:
27 static WrapperInfo kWrapperInfo;
28
29 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
30 return CreateHandle(isolate, new MyInterceptor(isolate));
31 }
32
33 int value() const { return value_; }
34 void set_value(int value) { value_ = value; }
35
36 // gin::NamedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2537 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
38 const std::string& property) override {
[email protected]5c969b82014-03-12 04:59:0539 if (property == "value") {
40 return ConvertToV8(isolate, value_);
41 } else if (property == "func") {
[email protected]dda52e482014-06-27 17:08:1642 return GetFunctionTemplate(isolate, "func")->GetFunction();
[email protected]5c969b82014-03-12 04:59:0543 } else {
44 return v8::Local<v8::Value>();
45 }
46 }
dcheng074c0392014-10-23 19:08:2547 bool SetNamedProperty(v8::Isolate* isolate,
48 const std::string& property,
49 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5550 if (property == "value") {
51 ConvertFromV8(isolate, value, &value_);
52 return true;
53 }
54 return false;
[email protected]5c969b82014-03-12 04:59:0555 }
dcheng074c0392014-10-23 19:08:2556 std::vector<std::string> EnumerateNamedProperties(
anujk.sharmae5481912014-10-06 11:24:1957 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0558 std::vector<std::string> result;
59 result.push_back("func");
60 result.push_back("value");
61 return result;
62 }
63
64 // gin::IndexedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2565 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
66 uint32_t index) override {
[email protected]5c969b82014-03-12 04:59:0567 if (index == 0)
68 return ConvertToV8(isolate, value_);
69 return v8::Local<v8::Value>();
70 }
dcheng074c0392014-10-23 19:08:2571 bool SetIndexedProperty(v8::Isolate* isolate,
72 uint32_t index,
73 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5574 if (index == 0) {
75 ConvertFromV8(isolate, value, &value_);
76 return true;
77 }
78 // Don't allow bypassing the interceptor.
79 return true;
[email protected]5c969b82014-03-12 04:59:0580 }
dcheng074c0392014-10-23 19:08:2581 std::vector<uint32_t> EnumerateIndexedProperties(
82 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0583 std::vector<uint32_t> result;
84 result.push_back(0);
85 return result;
86 }
87
88 private:
89 explicit MyInterceptor(v8::Isolate* isolate)
90 : NamedPropertyInterceptor(isolate, this),
91 IndexedPropertyInterceptor(isolate, this),
[email protected]dda52e482014-06-27 17:08:1692 value_(0),
93 template_cache_(isolate) {}
Chris Watkins756035a2017-12-01 03:03:2794 ~MyInterceptor() override = default;
[email protected]5c969b82014-03-12 04:59:0595
96 // gin::Wrappable
dcheng074c0392014-10-23 19:08:2597 ObjectTemplateBuilder GetObjectTemplateBuilder(
98 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0599 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
100 .AddNamedPropertyInterceptor()
101 .AddIndexedPropertyInterceptor();
102 }
103
104 int Call(int value) {
105 int tmp = value_;
106 value_ = value;
107 return tmp;
108 }
109
[email protected]dda52e482014-06-27 17:08:16110 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
111 const std::string& name) {
112 v8::Local<v8::FunctionTemplate> function_template =
113 template_cache_.Get(name);
114 if (!function_template.IsEmpty())
115 return function_template;
116 function_template = CreateFunctionTemplate(
tzik6934a312018-03-08 01:03:16117 isolate, base::BindRepeating(&MyInterceptor::Call),
118 HolderIsFirstArgument);
[email protected]dda52e482014-06-27 17:08:16119 template_cache_.Set(name, function_template);
120 return function_template;
121 }
122
[email protected]5c969b82014-03-12 04:59:05123 int value_;
[email protected]dda52e482014-06-27 17:08:16124
dcarney36f78b642015-04-24 10:22:49125 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
[email protected]dda52e482014-06-27 17:08:16126
127 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
[email protected]5c969b82014-03-12 04:59:05128};
129
130WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
131
132class InterceptorTest : public V8Test {
133 public:
134 void RunInterceptorTest(const std::string& script_source) {
135 v8::Isolate* isolate = instance_->isolate();
136 v8::HandleScope handle_scope(isolate);
137
138 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
139
140 obj->set_value(42);
141 EXPECT_EQ(42, obj->value());
142
deepak.sfaaa1b62015-04-30 07:30:48143 v8::Local<v8::String> source = StringToV8(isolate, script_source);
[email protected]5c969b82014-03-12 04:59:05144 EXPECT_FALSE(source.IsEmpty());
145
bashidbd2ef9bb2015-06-02 01:39:32146 gin::TryCatch try_catch(isolate);
Adam Klein8a35b5b2018-01-17 00:51:00147 v8::Local<v8::Script> script =
148 v8::Script::Compile(context_.Get(isolate), source).ToLocalChecked();
149 v8::Local<v8::Value> val =
150 script->Run(context_.Get(isolate)).ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:05151 EXPECT_FALSE(val.IsEmpty());
deepak.sfaaa1b62015-04-30 07:30:48152 v8::Local<v8::Function> func;
[email protected]5c969b82014-03-12 04:59:05153 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
Ken Rockot2b0f07652017-04-12 19:10:49154 v8::Local<v8::Value> argv[] = {
Jeremy Romaneb8a2f172018-01-29 17:33:40155 ConvertToV8(isolate, obj.get()).ToLocalChecked(),
Ken Rockot2b0f07652017-04-12 19:10:49156 };
[email protected]5c969b82014-03-12 04:59:05157 func->Call(v8::Undefined(isolate), 1, argv);
158 EXPECT_FALSE(try_catch.HasCaught());
159 EXPECT_EQ("", try_catch.GetStackTrace());
160
161 EXPECT_EQ(191, obj->value());
162 }
163};
164
165TEST_F(InterceptorTest, NamedInterceptor) {
166 RunInterceptorTest(
167 "(function (obj) {"
168 " if (obj.value !== 42) throw 'FAIL';"
169 " else obj.value = 191; })");
170}
171
172TEST_F(InterceptorTest, NamedInterceptorCall) {
173 RunInterceptorTest(
174 "(function (obj) {"
175 " if (obj.func(191) !== 42) throw 'FAIL';"
176 " })");
177}
178
179TEST_F(InterceptorTest, IndexedInterceptor) {
180 RunInterceptorTest(
181 "(function (obj) {"
182 " if (obj[0] !== 42) throw 'FAIL';"
183 " else obj[0] = 191; })");
184}
185
[email protected]d656f872014-08-13 17:12:55186TEST_F(InterceptorTest, BypassInterceptorAllowed) {
187 RunInterceptorTest(
188 "(function (obj) {"
189 " obj.value = 191 /* make test happy */;"
190 " obj.foo = 23;"
191 " if (obj.foo !== 23) throw 'FAIL'; })");
192}
193
194TEST_F(InterceptorTest, BypassInterceptorForbidden) {
195 RunInterceptorTest(
196 "(function (obj) {"
197 " obj.value = 191 /* make test happy */;"
198 " obj[1] = 23;"
199 " if (obj[1] === 23) throw 'FAIL'; })");
200}
201
[email protected]5c969b82014-03-12 04:59:05202} // namespace gin