blob: ffffaae48081e8fcd7430cf393899167b2031f09 [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") {
Andreas Haasc0715d32018-09-28 08:12:1742 v8::Local<v8::Context> context = isolate->GetCurrentContext();
43 return GetFunctionTemplate(isolate, "func")
44 ->GetFunction(context)
45 .ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:0546 } else {
47 return v8::Local<v8::Value>();
48 }
49 }
dcheng074c0392014-10-23 19:08:2550 bool SetNamedProperty(v8::Isolate* isolate,
51 const std::string& property,
52 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5553 if (property == "value") {
54 ConvertFromV8(isolate, value, &value_);
55 return true;
56 }
57 return false;
[email protected]5c969b82014-03-12 04:59:0558 }
dcheng074c0392014-10-23 19:08:2559 std::vector<std::string> EnumerateNamedProperties(
anujk.sharmae5481912014-10-06 11:24:1960 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0561 std::vector<std::string> result;
62 result.push_back("func");
63 result.push_back("value");
64 return result;
65 }
66
67 // gin::IndexedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2568 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
69 uint32_t index) override {
[email protected]5c969b82014-03-12 04:59:0570 if (index == 0)
71 return ConvertToV8(isolate, value_);
72 return v8::Local<v8::Value>();
73 }
dcheng074c0392014-10-23 19:08:2574 bool SetIndexedProperty(v8::Isolate* isolate,
75 uint32_t index,
76 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5577 if (index == 0) {
78 ConvertFromV8(isolate, value, &value_);
79 return true;
80 }
81 // Don't allow bypassing the interceptor.
82 return true;
[email protected]5c969b82014-03-12 04:59:0583 }
dcheng074c0392014-10-23 19:08:2584 std::vector<uint32_t> EnumerateIndexedProperties(
85 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0586 std::vector<uint32_t> result;
87 result.push_back(0);
88 return result;
89 }
90
91 private:
92 explicit MyInterceptor(v8::Isolate* isolate)
93 : NamedPropertyInterceptor(isolate, this),
94 IndexedPropertyInterceptor(isolate, this),
[email protected]dda52e482014-06-27 17:08:1695 value_(0),
96 template_cache_(isolate) {}
Chris Watkins756035a2017-12-01 03:03:2797 ~MyInterceptor() override = default;
[email protected]5c969b82014-03-12 04:59:0598
99 // gin::Wrappable
dcheng074c0392014-10-23 19:08:25100 ObjectTemplateBuilder GetObjectTemplateBuilder(
101 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:05102 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
103 .AddNamedPropertyInterceptor()
104 .AddIndexedPropertyInterceptor();
105 }
106
107 int Call(int value) {
108 int tmp = value_;
109 value_ = value;
110 return tmp;
111 }
112
[email protected]dda52e482014-06-27 17:08:16113 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
114 const std::string& name) {
115 v8::Local<v8::FunctionTemplate> function_template =
116 template_cache_.Get(name);
117 if (!function_template.IsEmpty())
118 return function_template;
119 function_template = CreateFunctionTemplate(
tzik6934a312018-03-08 01:03:16120 isolate, base::BindRepeating(&MyInterceptor::Call),
Devlin Cronine9db9842018-04-09 17:51:05121 InvokerOptions{true, nullptr});
[email protected]dda52e482014-06-27 17:08:16122 template_cache_.Set(name, function_template);
123 return function_template;
124 }
125
[email protected]5c969b82014-03-12 04:59:05126 int value_;
[email protected]dda52e482014-06-27 17:08:16127
dcarney36f78b642015-04-24 10:22:49128 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
[email protected]dda52e482014-06-27 17:08:16129
130 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
[email protected]5c969b82014-03-12 04:59:05131};
132
133WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
134
135class InterceptorTest : public V8Test {
136 public:
137 void RunInterceptorTest(const std::string& script_source) {
138 v8::Isolate* isolate = instance_->isolate();
139 v8::HandleScope handle_scope(isolate);
140
141 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
142
143 obj->set_value(42);
144 EXPECT_EQ(42, obj->value());
145
deepak.sfaaa1b62015-04-30 07:30:48146 v8::Local<v8::String> source = StringToV8(isolate, script_source);
[email protected]5c969b82014-03-12 04:59:05147 EXPECT_FALSE(source.IsEmpty());
148
bashidbd2ef9bb2015-06-02 01:39:32149 gin::TryCatch try_catch(isolate);
Adam Klein8a35b5b2018-01-17 00:51:00150 v8::Local<v8::Script> script =
151 v8::Script::Compile(context_.Get(isolate), source).ToLocalChecked();
152 v8::Local<v8::Value> val =
153 script->Run(context_.Get(isolate)).ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:05154 EXPECT_FALSE(val.IsEmpty());
deepak.sfaaa1b62015-04-30 07:30:48155 v8::Local<v8::Function> func;
[email protected]5c969b82014-03-12 04:59:05156 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
Ken Rockot2b0f07652017-04-12 19:10:49157 v8::Local<v8::Value> argv[] = {
Jeremy Romaneb8a2f172018-01-29 17:33:40158 ConvertToV8(isolate, obj.get()).ToLocalChecked(),
Ken Rockot2b0f07652017-04-12 19:10:49159 };
Ross McIlroyd298cced2018-11-23 16:14:13160 func->Call(context_.Get(isolate), v8::Undefined(isolate), 1, argv)
161 .ToLocalChecked();
[email protected]5c969b82014-03-12 04:59:05162 EXPECT_FALSE(try_catch.HasCaught());
163 EXPECT_EQ("", try_catch.GetStackTrace());
164
165 EXPECT_EQ(191, obj->value());
166 }
167};
168
169TEST_F(InterceptorTest, NamedInterceptor) {
170 RunInterceptorTest(
171 "(function (obj) {"
172 " if (obj.value !== 42) throw 'FAIL';"
173 " else obj.value = 191; })");
174}
175
176TEST_F(InterceptorTest, NamedInterceptorCall) {
177 RunInterceptorTest(
178 "(function (obj) {"
179 " if (obj.func(191) !== 42) throw 'FAIL';"
180 " })");
181}
182
183TEST_F(InterceptorTest, IndexedInterceptor) {
184 RunInterceptorTest(
185 "(function (obj) {"
186 " if (obj[0] !== 42) throw 'FAIL';"
187 " else obj[0] = 191; })");
188}
189
[email protected]d656f872014-08-13 17:12:55190TEST_F(InterceptorTest, BypassInterceptorAllowed) {
191 RunInterceptorTest(
192 "(function (obj) {"
193 " obj.value = 191 /* make test happy */;"
194 " obj.foo = 23;"
195 " if (obj.foo !== 23) throw 'FAIL'; })");
196}
197
198TEST_F(InterceptorTest, BypassInterceptorForbidden) {
199 RunInterceptorTest(
200 "(function (obj) {"
201 " obj.value = 191 /* make test happy */;"
202 " obj[1] = 23;"
203 " if (obj[1] === 23) throw 'FAIL'; })");
204}
205
[email protected]5c969b82014-03-12 04:59:05206} // namespace gin