blob: 089920071ec7ff06bc01686372753400d1861489 [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(
117 isolate, base::Bind(&MyInterceptor::Call), HolderIsFirstArgument);
118 template_cache_.Set(name, function_template);
119 return function_template;
120 }
121
[email protected]5c969b82014-03-12 04:59:05122 int value_;
[email protected]dda52e482014-06-27 17:08:16123
dcarney36f78b642015-04-24 10:22:49124 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
[email protected]dda52e482014-06-27 17:08:16125
126 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
[email protected]5c969b82014-03-12 04:59:05127};
128
129WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
130
131class InterceptorTest : public V8Test {
132 public:
133 void RunInterceptorTest(const std::string& script_source) {
134 v8::Isolate* isolate = instance_->isolate();
135 v8::HandleScope handle_scope(isolate);
136
137 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
138
139 obj->set_value(42);
140 EXPECT_EQ(42, obj->value());
141
deepak.sfaaa1b62015-04-30 07:30:48142 v8::Local<v8::String> source = StringToV8(isolate, script_source);
[email protected]5c969b82014-03-12 04:59:05143 EXPECT_FALSE(source.IsEmpty());
144
bashidbd2ef9bb2015-06-02 01:39:32145 gin::TryCatch try_catch(isolate);
deepak.sfaaa1b62015-04-30 07:30:48146 v8::Local<v8::Script> script = v8::Script::Compile(source);
[email protected]5c969b82014-03-12 04:59:05147 EXPECT_FALSE(script.IsEmpty());
deepak.sfaaa1b62015-04-30 07:30:48148 v8::Local<v8::Value> val = script->Run();
[email protected]5c969b82014-03-12 04:59:05149 EXPECT_FALSE(val.IsEmpty());
deepak.sfaaa1b62015-04-30 07:30:48150 v8::Local<v8::Function> func;
[email protected]5c969b82014-03-12 04:59:05151 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
Ken Rockot2b0f07652017-04-12 19:10:49152 v8::Local<v8::Value> argv[] = {
153 ConvertToV8(isolate->GetCurrentContext(), obj.get()).ToLocalChecked(),
154 };
[email protected]5c969b82014-03-12 04:59:05155 func->Call(v8::Undefined(isolate), 1, argv);
156 EXPECT_FALSE(try_catch.HasCaught());
157 EXPECT_EQ("", try_catch.GetStackTrace());
158
159 EXPECT_EQ(191, obj->value());
160 }
161};
162
163TEST_F(InterceptorTest, NamedInterceptor) {
164 RunInterceptorTest(
165 "(function (obj) {"
166 " if (obj.value !== 42) throw 'FAIL';"
167 " else obj.value = 191; })");
168}
169
170TEST_F(InterceptorTest, NamedInterceptorCall) {
171 RunInterceptorTest(
172 "(function (obj) {"
173 " if (obj.func(191) !== 42) throw 'FAIL';"
174 " })");
175}
176
177TEST_F(InterceptorTest, IndexedInterceptor) {
178 RunInterceptorTest(
179 "(function (obj) {"
180 " if (obj[0] !== 42) throw 'FAIL';"
181 " else obj[0] = 191; })");
182}
183
[email protected]d656f872014-08-13 17:12:55184TEST_F(InterceptorTest, BypassInterceptorAllowed) {
185 RunInterceptorTest(
186 "(function (obj) {"
187 " obj.value = 191 /* make test happy */;"
188 " obj.foo = 23;"
189 " if (obj.foo !== 23) throw 'FAIL'; })");
190}
191
192TEST_F(InterceptorTest, BypassInterceptorForbidden) {
193 RunInterceptorTest(
194 "(function (obj) {"
195 " obj.value = 191 /* make test happy */;"
196 " obj[1] = 23;"
197 " if (obj[1] === 23) throw 'FAIL'; })");
198}
199
[email protected]5c969b82014-03-12 04:59:05200} // namespace gin