[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 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 7 | #include "base/logging.h" |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 8 | #include "base/macros.h" |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 9 | #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] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 19 | #include "v8/include/v8-util.h" |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 20 | |
| 21 | namespace gin { |
| 22 | |
| 23 | class 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 |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 37 | v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate, |
| 38 | const std::string& property) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 39 | if (property == "value") { |
| 40 | return ConvertToV8(isolate, value_); |
| 41 | } else if (property == "func") { |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 42 | return GetFunctionTemplate(isolate, "func")->GetFunction(); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 43 | } else { |
| 44 | return v8::Local<v8::Value>(); |
| 45 | } |
| 46 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 47 | bool SetNamedProperty(v8::Isolate* isolate, |
| 48 | const std::string& property, |
| 49 | v8::Local<v8::Value> value) override { |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 50 | if (property == "value") { |
| 51 | ConvertFromV8(isolate, value, &value_); |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 55 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 56 | std::vector<std::string> EnumerateNamedProperties( |
anujk.sharma | e548191 | 2014-10-06 11:24:19 | [diff] [blame] | 57 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 58 | std::vector<std::string> result; |
| 59 | result.push_back("func"); |
| 60 | result.push_back("value"); |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | // gin::IndexedPropertyInterceptor |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 65 | v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, |
| 66 | uint32_t index) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 67 | if (index == 0) |
| 68 | return ConvertToV8(isolate, value_); |
| 69 | return v8::Local<v8::Value>(); |
| 70 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 71 | bool SetIndexedProperty(v8::Isolate* isolate, |
| 72 | uint32_t index, |
| 73 | v8::Local<v8::Value> value) override { |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 74 | if (index == 0) { |
| 75 | ConvertFromV8(isolate, value, &value_); |
| 76 | return true; |
| 77 | } |
| 78 | // Don't allow bypassing the interceptor. |
| 79 | return true; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 80 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 81 | std::vector<uint32_t> EnumerateIndexedProperties( |
| 82 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 83 | 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] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 92 | value_(0), |
| 93 | template_cache_(isolate) {} |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 94 | ~MyInterceptor() override {} |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 95 | |
| 96 | // gin::Wrappable |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 97 | ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 98 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 99 | 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] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 110 | 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] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 122 | int value_; |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 123 | |
dcarney | 36f78b64 | 2015-04-24 10:22:49 | [diff] [blame] | 124 | v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_; |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 125 | |
| 126 | DISALLOW_COPY_AND_ASSIGN(MyInterceptor); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin}; |
| 130 | |
| 131 | class 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.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 142 | v8::Local<v8::String> source = StringToV8(isolate, script_source); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 143 | EXPECT_FALSE(source.IsEmpty()); |
| 144 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 145 | gin::TryCatch try_catch(isolate); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 146 | v8::Local<v8::Script> script = v8::Script::Compile(source); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 147 | EXPECT_FALSE(script.IsEmpty()); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 148 | v8::Local<v8::Value> val = script->Run(); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 149 | EXPECT_FALSE(val.IsEmpty()); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 150 | v8::Local<v8::Function> func; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 151 | EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 152 | v8::Local<v8::Value> argv[] = {ConvertToV8(isolate, obj.get()), }; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 153 | func->Call(v8::Undefined(isolate), 1, argv); |
| 154 | EXPECT_FALSE(try_catch.HasCaught()); |
| 155 | EXPECT_EQ("", try_catch.GetStackTrace()); |
| 156 | |
| 157 | EXPECT_EQ(191, obj->value()); |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | TEST_F(InterceptorTest, NamedInterceptor) { |
| 162 | RunInterceptorTest( |
| 163 | "(function (obj) {" |
| 164 | " if (obj.value !== 42) throw 'FAIL';" |
| 165 | " else obj.value = 191; })"); |
| 166 | } |
| 167 | |
| 168 | TEST_F(InterceptorTest, NamedInterceptorCall) { |
| 169 | RunInterceptorTest( |
| 170 | "(function (obj) {" |
| 171 | " if (obj.func(191) !== 42) throw 'FAIL';" |
| 172 | " })"); |
| 173 | } |
| 174 | |
| 175 | TEST_F(InterceptorTest, IndexedInterceptor) { |
| 176 | RunInterceptorTest( |
| 177 | "(function (obj) {" |
| 178 | " if (obj[0] !== 42) throw 'FAIL';" |
| 179 | " else obj[0] = 191; })"); |
| 180 | } |
| 181 | |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 182 | TEST_F(InterceptorTest, BypassInterceptorAllowed) { |
| 183 | RunInterceptorTest( |
| 184 | "(function (obj) {" |
| 185 | " obj.value = 191 /* make test happy */;" |
| 186 | " obj.foo = 23;" |
| 187 | " if (obj.foo !== 23) throw 'FAIL'; })"); |
| 188 | } |
| 189 | |
| 190 | TEST_F(InterceptorTest, BypassInterceptorForbidden) { |
| 191 | RunInterceptorTest( |
| 192 | "(function (obj) {" |
| 193 | " obj.value = 191 /* make test happy */;" |
| 194 | " obj[1] = 23;" |
| 195 | " if (obj[1] === 23) throw 'FAIL'; })"); |
| 196 | } |
| 197 | |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 198 | } // namespace gin |