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 virtual v8::Local
<v8::Value
> GetNamedProperty(v8::Isolate
* isolate
,
35 const std::string
& property
)
37 if (property
== "value") {
38 return ConvertToV8(isolate
, value_
);
39 } else if (property
== "func") {
40 return GetFunctionTemplate(isolate
, "func")->GetFunction();
42 return v8::Local
<v8::Value
>();
45 virtual bool SetNamedProperty(v8::Isolate
* isolate
,
46 const std::string
& property
,
47 v8::Local
<v8::Value
> value
) OVERRIDE
{
48 if (property
== "value") {
49 ConvertFromV8(isolate
, value
, &value_
);
54 virtual std::vector
<std::string
> EnumerateNamedProperties(
55 v8::Isolate
* isolate
) OVERRIDE
{
56 std::vector
<std::string
> result
;
57 result
.push_back("func");
58 result
.push_back("value");
62 // gin::IndexedPropertyInterceptor
63 virtual v8::Local
<v8::Value
> GetIndexedProperty(v8::Isolate
* isolate
,
64 uint32_t index
) OVERRIDE
{
66 return ConvertToV8(isolate
, value_
);
67 return v8::Local
<v8::Value
>();
69 virtual bool SetIndexedProperty(v8::Isolate
* isolate
,
71 v8::Local
<v8::Value
> value
) OVERRIDE
{
73 ConvertFromV8(isolate
, value
, &value_
);
76 // Don't allow bypassing the interceptor.
79 virtual std::vector
<uint32_t> EnumerateIndexedProperties(v8::Isolate
* isolate
)
81 std::vector
<uint32_t> result
;
87 explicit MyInterceptor(v8::Isolate
* isolate
)
88 : NamedPropertyInterceptor(isolate
, this),
89 IndexedPropertyInterceptor(isolate
, this),
91 template_cache_(isolate
) {}
92 virtual ~MyInterceptor() {}
95 virtual ObjectTemplateBuilder
GetObjectTemplateBuilder(v8::Isolate
* isolate
)
97 return Wrappable
<MyInterceptor
>::GetObjectTemplateBuilder(isolate
)
98 .AddNamedPropertyInterceptor()
99 .AddIndexedPropertyInterceptor();
102 int Call(int value
) {
108 v8::Local
<v8::FunctionTemplate
> GetFunctionTemplate(v8::Isolate
* isolate
,
109 const std::string
& name
) {
110 v8::Local
<v8::FunctionTemplate
> function_template
=
111 template_cache_
.Get(name
);
112 if (!function_template
.IsEmpty())
113 return function_template
;
114 function_template
= CreateFunctionTemplate(
115 isolate
, base::Bind(&MyInterceptor::Call
), HolderIsFirstArgument
);
116 template_cache_
.Set(name
, function_template
);
117 return function_template
;
122 v8::StdPersistentValueMap
<std::string
, v8::FunctionTemplate
> template_cache_
;
124 DISALLOW_COPY_AND_ASSIGN(MyInterceptor
);
127 WrapperInfo
MyInterceptor::kWrapperInfo
= {kEmbedderNativeGin
};
129 class InterceptorTest
: public V8Test
{
131 void RunInterceptorTest(const std::string
& script_source
) {
132 v8::Isolate
* isolate
= instance_
->isolate();
133 v8::HandleScope
handle_scope(isolate
);
135 gin::Handle
<MyInterceptor
> obj
= MyInterceptor::Create(isolate
);
138 EXPECT_EQ(42, obj
->value());
140 v8::Handle
<v8::String
> source
= StringToV8(isolate
, script_source
);
141 EXPECT_FALSE(source
.IsEmpty());
143 gin::TryCatch try_catch
;
144 v8::Handle
<v8::Script
> script
= v8::Script::Compile(source
);
145 EXPECT_FALSE(script
.IsEmpty());
146 v8::Handle
<v8::Value
> val
= script
->Run();
147 EXPECT_FALSE(val
.IsEmpty());
148 v8::Handle
<v8::Function
> func
;
149 EXPECT_TRUE(ConvertFromV8(isolate
, val
, &func
));
150 v8::Handle
<v8::Value
> argv
[] = {ConvertToV8(isolate
, obj
.get()), };
151 func
->Call(v8::Undefined(isolate
), 1, argv
);
152 EXPECT_FALSE(try_catch
.HasCaught());
153 EXPECT_EQ("", try_catch
.GetStackTrace());
155 EXPECT_EQ(191, obj
->value());
159 TEST_F(InterceptorTest
, NamedInterceptor
) {
162 " if (obj.value !== 42) throw 'FAIL';"
163 " else obj.value = 191; })");
166 TEST_F(InterceptorTest
, NamedInterceptorCall
) {
169 " if (obj.func(191) !== 42) throw 'FAIL';"
173 TEST_F(InterceptorTest
, IndexedInterceptor
) {
176 " if (obj[0] !== 42) throw 'FAIL';"
177 " else obj[0] = 191; })");
180 TEST_F(InterceptorTest
, BypassInterceptorAllowed
) {
183 " obj.value = 191 /* make test happy */;"
185 " if (obj.foo !== 23) throw 'FAIL'; })");
188 TEST_F(InterceptorTest
, BypassInterceptorForbidden
) {
191 " obj.value = 191 /* make test happy */;"
193 " if (obj[1] === 23) throw 'FAIL'; })");