1 // 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.
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"
16 #include "v8/include/v8-util.h"
20 class MyInterceptor
: public Wrappable
<MyInterceptor
>,
21 public NamedPropertyInterceptor
,
22 public IndexedPropertyInterceptor
{
24 static WrapperInfo kWrapperInfo
;
26 static gin::Handle
<MyInterceptor
> Create(v8::Isolate
* isolate
) {
27 return CreateHandle(isolate
, new MyInterceptor(isolate
));
30 int value() const { return value_
; }
31 void set_value(int value
) { value_
= value
; }
33 // gin::NamedPropertyInterceptor
34 v8::Local
<v8::Value
> GetNamedProperty(v8::Isolate
* isolate
,
35 const std::string
& property
) override
{
36 if (property
== "value") {
37 return ConvertToV8(isolate
, value_
);
38 } else if (property
== "func") {
39 return GetFunctionTemplate(isolate
, "func")->GetFunction();
41 return v8::Local
<v8::Value
>();
44 bool SetNamedProperty(v8::Isolate
* isolate
,
45 const std::string
& property
,
46 v8::Local
<v8::Value
> value
) override
{
47 if (property
== "value") {
48 ConvertFromV8(isolate
, value
, &value_
);
53 std::vector
<std::string
> EnumerateNamedProperties(
54 v8::Isolate
* isolate
) override
{
55 std::vector
<std::string
> result
;
56 result
.push_back("func");
57 result
.push_back("value");
61 // gin::IndexedPropertyInterceptor
62 v8::Local
<v8::Value
> GetIndexedProperty(v8::Isolate
* isolate
,
63 uint32_t index
) override
{
65 return ConvertToV8(isolate
, value_
);
66 return v8::Local
<v8::Value
>();
68 bool SetIndexedProperty(v8::Isolate
* isolate
,
70 v8::Local
<v8::Value
> value
) override
{
72 ConvertFromV8(isolate
, value
, &value_
);
75 // Don't allow bypassing the interceptor.
78 std::vector
<uint32_t> EnumerateIndexedProperties(
79 v8::Isolate
* isolate
) override
{
80 std::vector
<uint32_t> result
;
86 explicit MyInterceptor(v8::Isolate
* isolate
)
87 : NamedPropertyInterceptor(isolate
, this),
88 IndexedPropertyInterceptor(isolate
, this),
90 template_cache_(isolate
) {}
91 ~MyInterceptor() override
{}
94 ObjectTemplateBuilder
GetObjectTemplateBuilder(
95 v8::Isolate
* isolate
) override
{
96 return Wrappable
<MyInterceptor
>::GetObjectTemplateBuilder(isolate
)
97 .AddNamedPropertyInterceptor()
98 .AddIndexedPropertyInterceptor();
101 int Call(int value
) {
107 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
;
121 v8::StdPersistentValueMap
<std::string
, v8::FunctionTemplate
> template_cache_
;
123 DISALLOW_COPY_AND_ASSIGN(MyInterceptor
);
126 WrapperInfo
MyInterceptor::kWrapperInfo
= {kEmbedderNativeGin
};
128 class InterceptorTest
: public V8Test
{
130 void RunInterceptorTest(const std::string
& script_source
) {
131 v8::Isolate
* isolate
= instance_
->isolate();
132 v8::HandleScope
handle_scope(isolate
);
134 gin::Handle
<MyInterceptor
> obj
= MyInterceptor::Create(isolate
);
137 EXPECT_EQ(42, obj
->value());
139 v8::Handle
<v8::String
> source
= StringToV8(isolate
, script_source
);
140 EXPECT_FALSE(source
.IsEmpty());
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());
154 EXPECT_EQ(191, obj
->value());
158 TEST_F(InterceptorTest
, NamedInterceptor
) {
161 " if (obj.value !== 42) throw 'FAIL';"
162 " else obj.value = 191; })");
165 TEST_F(InterceptorTest
, NamedInterceptorCall
) {
168 " if (obj.func(191) !== 42) throw 'FAIL';"
172 TEST_F(InterceptorTest
, IndexedInterceptor
) {
175 " if (obj[0] !== 42) throw 'FAIL';"
176 " else obj[0] = 191; })");
179 TEST_F(InterceptorTest
, BypassInterceptorAllowed
) {
182 " obj.value = 191 /* make test happy */;"
184 " if (obj.foo !== 23) throw 'FAIL'; })");
187 TEST_F(InterceptorTest
, BypassInterceptorForbidden
) {
190 " obj.value = 191 /* make test happy */;"
192 " if (obj[1] === 23) throw 'FAIL'; })");