blob: af830d4d0314fa2b5d22d9f0558aa9fce995a6e8 [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
5#include "base/logging.h"
6#include "gin/arguments.h"
7#include "gin/handle.h"
8#include "gin/interceptor.h"
9#include "gin/object_template_builder.h"
10#include "gin/per_isolate_data.h"
11#include "gin/public/isolate_holder.h"
12#include "gin/test/v8_test.h"
13#include "gin/try_catch.h"
14#include "gin/wrappable.h"
15#include "testing/gtest/include/gtest/gtest.h"
[email protected]dda52e482014-06-27 17:08:1616#include "v8/include/v8-util.h"
[email protected]5c969b82014-03-12 04:59:0517
18namespace gin {
19
20class MyInterceptor : public Wrappable<MyInterceptor>,
21 public NamedPropertyInterceptor,
22 public IndexedPropertyInterceptor {
23 public:
24 static WrapperInfo kWrapperInfo;
25
26 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
27 return CreateHandle(isolate, new MyInterceptor(isolate));
28 }
29
30 int value() const { return value_; }
31 void set_value(int value) { value_ = value; }
32
33 // gin::NamedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2534 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
35 const std::string& property) override {
[email protected]5c969b82014-03-12 04:59:0536 if (property == "value") {
37 return ConvertToV8(isolate, value_);
38 } else if (property == "func") {
[email protected]dda52e482014-06-27 17:08:1639 return GetFunctionTemplate(isolate, "func")->GetFunction();
[email protected]5c969b82014-03-12 04:59:0540 } else {
41 return v8::Local<v8::Value>();
42 }
43 }
dcheng074c0392014-10-23 19:08:2544 bool SetNamedProperty(v8::Isolate* isolate,
45 const std::string& property,
46 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5547 if (property == "value") {
48 ConvertFromV8(isolate, value, &value_);
49 return true;
50 }
51 return false;
[email protected]5c969b82014-03-12 04:59:0552 }
dcheng074c0392014-10-23 19:08:2553 std::vector<std::string> EnumerateNamedProperties(
anujk.sharmae5481912014-10-06 11:24:1954 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0555 std::vector<std::string> result;
56 result.push_back("func");
57 result.push_back("value");
58 return result;
59 }
60
61 // gin::IndexedPropertyInterceptor
dcheng074c0392014-10-23 19:08:2562 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
63 uint32_t index) override {
[email protected]5c969b82014-03-12 04:59:0564 if (index == 0)
65 return ConvertToV8(isolate, value_);
66 return v8::Local<v8::Value>();
67 }
dcheng074c0392014-10-23 19:08:2568 bool SetIndexedProperty(v8::Isolate* isolate,
69 uint32_t index,
70 v8::Local<v8::Value> value) override {
[email protected]d656f872014-08-13 17:12:5571 if (index == 0) {
72 ConvertFromV8(isolate, value, &value_);
73 return true;
74 }
75 // Don't allow bypassing the interceptor.
76 return true;
[email protected]5c969b82014-03-12 04:59:0577 }
dcheng074c0392014-10-23 19:08:2578 std::vector<uint32_t> EnumerateIndexedProperties(
79 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0580 std::vector<uint32_t> result;
81 result.push_back(0);
82 return result;
83 }
84
85 private:
86 explicit MyInterceptor(v8::Isolate* isolate)
87 : NamedPropertyInterceptor(isolate, this),
88 IndexedPropertyInterceptor(isolate, this),
[email protected]dda52e482014-06-27 17:08:1689 value_(0),
90 template_cache_(isolate) {}
dcheng074c0392014-10-23 19:08:2591 ~MyInterceptor() override {}
[email protected]5c969b82014-03-12 04:59:0592
93 // gin::Wrappable
dcheng074c0392014-10-23 19:08:2594 ObjectTemplateBuilder GetObjectTemplateBuilder(
95 v8::Isolate* isolate) override {
[email protected]5c969b82014-03-12 04:59:0596 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
97 .AddNamedPropertyInterceptor()
98 .AddIndexedPropertyInterceptor();
99 }
100
101 int Call(int value) {
102 int tmp = value_;
103 value_ = value;
104 return tmp;
105 }
106
[email protected]dda52e482014-06-27 17:08:16107 v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
108 const std::string& name) {
109 v8::Local<v8::FunctionTemplate> function_template =
110 template_cache_.Get(name);
111 if (!function_template.IsEmpty())
112 return function_template;
113 function_template = CreateFunctionTemplate(
114 isolate, base::Bind(&MyInterceptor::Call), HolderIsFirstArgument);
115 template_cache_.Set(name, function_template);
116 return function_template;
117 }
118
[email protected]5c969b82014-03-12 04:59:05119 int value_;
[email protected]dda52e482014-06-27 17:08:16120
dcarney36f78b642015-04-24 10:22:49121 v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
[email protected]dda52e482014-06-27 17:08:16122
123 DISALLOW_COPY_AND_ASSIGN(MyInterceptor);
[email protected]5c969b82014-03-12 04:59:05124};
125
126WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
127
128class InterceptorTest : public V8Test {
129 public:
130 void RunInterceptorTest(const std::string& script_source) {
131 v8::Isolate* isolate = instance_->isolate();
132 v8::HandleScope handle_scope(isolate);
133
134 gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
135
136 obj->set_value(42);
137 EXPECT_EQ(42, obj->value());
138
139 v8::Handle<v8::String> source = StringToV8(isolate, script_source);
140 EXPECT_FALSE(source.IsEmpty());
141
142 gin::TryCatch try_catch;
143 v8::Handle<v8::Script> script = v8::Script::Compile(source);
144 EXPECT_FALSE(script.IsEmpty());
145 v8::Handle<v8::Value> val = script->Run();
146 EXPECT_FALSE(val.IsEmpty());
147 v8::Handle<v8::Function> func;
148 EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
149 v8::Handle<v8::Value> argv[] = {ConvertToV8(isolate, obj.get()), };
150 func->Call(v8::Undefined(isolate), 1, argv);
151 EXPECT_FALSE(try_catch.HasCaught());
152 EXPECT_EQ("", try_catch.GetStackTrace());
153
154 EXPECT_EQ(191, obj->value());
155 }
156};
157
158TEST_F(InterceptorTest, NamedInterceptor) {
159 RunInterceptorTest(
160 "(function (obj) {"
161 " if (obj.value !== 42) throw 'FAIL';"
162 " else obj.value = 191; })");
163}
164
165TEST_F(InterceptorTest, NamedInterceptorCall) {
166 RunInterceptorTest(
167 "(function (obj) {"
168 " if (obj.func(191) !== 42) throw 'FAIL';"
169 " })");
170}
171
172TEST_F(InterceptorTest, IndexedInterceptor) {
173 RunInterceptorTest(
174 "(function (obj) {"
175 " if (obj[0] !== 42) throw 'FAIL';"
176 " else obj[0] = 191; })");
177}
178
[email protected]d656f872014-08-13 17:12:55179TEST_F(InterceptorTest, BypassInterceptorAllowed) {
180 RunInterceptorTest(
181 "(function (obj) {"
182 " obj.value = 191 /* make test happy */;"
183 " obj.foo = 23;"
184 " if (obj.foo !== 23) throw 'FAIL'; })");
185}
186
187TEST_F(InterceptorTest, BypassInterceptorForbidden) {
188 RunInterceptorTest(
189 "(function (obj) {"
190 " obj.value = 191 /* make test happy */;"
191 " obj[1] = 23;"
192 " if (obj[1] === 23) throw 'FAIL'; })");
193}
194
[email protected]5c969b82014-03-12 04:59:05195} // namespace gin