[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 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. |
| 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] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 16 | #include "v8/include/v8-util.h" |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 17 | |
| 18 | namespace gin { |
| 19 | |
| 20 | class 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 |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 34 | v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate, |
| 35 | const std::string& property) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 36 | if (property == "value") { |
| 37 | return ConvertToV8(isolate, value_); |
| 38 | } else if (property == "func") { |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 39 | return GetFunctionTemplate(isolate, "func")->GetFunction(); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 40 | } else { |
| 41 | return v8::Local<v8::Value>(); |
| 42 | } |
| 43 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 44 | bool SetNamedProperty(v8::Isolate* isolate, |
| 45 | const std::string& property, |
| 46 | v8::Local<v8::Value> value) override { |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 47 | if (property == "value") { |
| 48 | ConvertFromV8(isolate, value, &value_); |
| 49 | return true; |
| 50 | } |
| 51 | return false; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 52 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 53 | std::vector<std::string> EnumerateNamedProperties( |
anujk.sharma | e548191 | 2014-10-06 11:24:19 | [diff] [blame] | 54 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 55 | std::vector<std::string> result; |
| 56 | result.push_back("func"); |
| 57 | result.push_back("value"); |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | // gin::IndexedPropertyInterceptor |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 62 | v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, |
| 63 | uint32_t index) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 64 | if (index == 0) |
| 65 | return ConvertToV8(isolate, value_); |
| 66 | return v8::Local<v8::Value>(); |
| 67 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 68 | bool SetIndexedProperty(v8::Isolate* isolate, |
| 69 | uint32_t index, |
| 70 | v8::Local<v8::Value> value) override { |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 71 | if (index == 0) { |
| 72 | ConvertFromV8(isolate, value, &value_); |
| 73 | return true; |
| 74 | } |
| 75 | // Don't allow bypassing the interceptor. |
| 76 | return true; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 77 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 78 | std::vector<uint32_t> EnumerateIndexedProperties( |
| 79 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 80 | 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] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 89 | value_(0), |
| 90 | template_cache_(isolate) {} |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 91 | ~MyInterceptor() override {} |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 92 | |
| 93 | // gin::Wrappable |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 94 | ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 95 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 96 | 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] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 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; |
| 117 | } |
| 118 | |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 119 | int value_; |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 120 | |
dcarney | 36f78b64 | 2015-04-24 10:22:49 | [diff] [blame^] | 121 | v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_; |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 122 | |
| 123 | DISALLOW_COPY_AND_ASSIGN(MyInterceptor); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin}; |
| 127 | |
| 128 | class 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 | |
| 158 | TEST_F(InterceptorTest, NamedInterceptor) { |
| 159 | RunInterceptorTest( |
| 160 | "(function (obj) {" |
| 161 | " if (obj.value !== 42) throw 'FAIL';" |
| 162 | " else obj.value = 191; })"); |
| 163 | } |
| 164 | |
| 165 | TEST_F(InterceptorTest, NamedInterceptorCall) { |
| 166 | RunInterceptorTest( |
| 167 | "(function (obj) {" |
| 168 | " if (obj.func(191) !== 42) throw 'FAIL';" |
| 169 | " })"); |
| 170 | } |
| 171 | |
| 172 | TEST_F(InterceptorTest, IndexedInterceptor) { |
| 173 | RunInterceptorTest( |
| 174 | "(function (obj) {" |
| 175 | " if (obj[0] !== 42) throw 'FAIL';" |
| 176 | " else obj[0] = 191; })"); |
| 177 | } |
| 178 | |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 179 | TEST_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 | |
| 187 | TEST_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] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 195 | } // namespace gin |