[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") { |
Andreas Haas | c0715d3 | 2018-09-28 08:12:17 | [diff] [blame] | 42 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 43 | return GetFunctionTemplate(isolate, "func") |
| 44 | ->GetFunction(context) |
| 45 | .ToLocalChecked(); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 46 | } else { |
| 47 | return v8::Local<v8::Value>(); |
| 48 | } |
| 49 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 50 | bool SetNamedProperty(v8::Isolate* isolate, |
| 51 | const std::string& property, |
| 52 | v8::Local<v8::Value> value) override { |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 53 | if (property == "value") { |
| 54 | ConvertFromV8(isolate, value, &value_); |
| 55 | return true; |
| 56 | } |
| 57 | return false; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 58 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 59 | std::vector<std::string> EnumerateNamedProperties( |
anujk.sharma | e548191 | 2014-10-06 11:24:19 | [diff] [blame] | 60 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 61 | std::vector<std::string> result; |
| 62 | result.push_back("func"); |
| 63 | result.push_back("value"); |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | // gin::IndexedPropertyInterceptor |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 68 | v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, |
| 69 | uint32_t index) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 70 | if (index == 0) |
| 71 | return ConvertToV8(isolate, value_); |
| 72 | return v8::Local<v8::Value>(); |
| 73 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 74 | bool SetIndexedProperty(v8::Isolate* isolate, |
| 75 | uint32_t index, |
| 76 | v8::Local<v8::Value> value) override { |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 77 | if (index == 0) { |
| 78 | ConvertFromV8(isolate, value, &value_); |
| 79 | return true; |
| 80 | } |
| 81 | // Don't allow bypassing the interceptor. |
| 82 | return true; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 83 | } |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 84 | std::vector<uint32_t> EnumerateIndexedProperties( |
| 85 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 86 | std::vector<uint32_t> result; |
| 87 | result.push_back(0); |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | explicit MyInterceptor(v8::Isolate* isolate) |
| 93 | : NamedPropertyInterceptor(isolate, this), |
| 94 | IndexedPropertyInterceptor(isolate, this), |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 95 | value_(0), |
| 96 | template_cache_(isolate) {} |
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 97 | ~MyInterceptor() override = default; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 98 | |
| 99 | // gin::Wrappable |
dcheng | 074c039 | 2014-10-23 19:08:25 | [diff] [blame] | 100 | ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 101 | v8::Isolate* isolate) override { |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 102 | return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate) |
| 103 | .AddNamedPropertyInterceptor() |
| 104 | .AddIndexedPropertyInterceptor(); |
| 105 | } |
| 106 | |
| 107 | int Call(int value) { |
| 108 | int tmp = value_; |
| 109 | value_ = value; |
| 110 | return tmp; |
| 111 | } |
| 112 | |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 113 | v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate, |
| 114 | const std::string& name) { |
| 115 | v8::Local<v8::FunctionTemplate> function_template = |
| 116 | template_cache_.Get(name); |
| 117 | if (!function_template.IsEmpty()) |
| 118 | return function_template; |
| 119 | function_template = CreateFunctionTemplate( |
tzik | 6934a31 | 2018-03-08 01:03:16 | [diff] [blame] | 120 | isolate, base::BindRepeating(&MyInterceptor::Call), |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 121 | InvokerOptions{true, nullptr}); |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 122 | template_cache_.Set(name, function_template); |
| 123 | return function_template; |
| 124 | } |
| 125 | |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 126 | int value_; |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 127 | |
dcarney | 36f78b64 | 2015-04-24 10:22:49 | [diff] [blame] | 128 | v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_; |
[email protected] | dda52e48 | 2014-06-27 17:08:16 | [diff] [blame] | 129 | |
| 130 | DISALLOW_COPY_AND_ASSIGN(MyInterceptor); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin}; |
| 134 | |
| 135 | class InterceptorTest : public V8Test { |
| 136 | public: |
| 137 | void RunInterceptorTest(const std::string& script_source) { |
| 138 | v8::Isolate* isolate = instance_->isolate(); |
| 139 | v8::HandleScope handle_scope(isolate); |
| 140 | |
| 141 | gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate); |
| 142 | |
| 143 | obj->set_value(42); |
| 144 | EXPECT_EQ(42, obj->value()); |
| 145 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 146 | v8::Local<v8::String> source = StringToV8(isolate, script_source); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 147 | EXPECT_FALSE(source.IsEmpty()); |
| 148 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 149 | gin::TryCatch try_catch(isolate); |
Adam Klein | 8a35b5b | 2018-01-17 00:51:00 | [diff] [blame] | 150 | v8::Local<v8::Script> script = |
| 151 | v8::Script::Compile(context_.Get(isolate), source).ToLocalChecked(); |
| 152 | v8::Local<v8::Value> val = |
| 153 | script->Run(context_.Get(isolate)).ToLocalChecked(); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 154 | EXPECT_FALSE(val.IsEmpty()); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 155 | v8::Local<v8::Function> func; |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 156 | EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); |
Ken Rockot | 2b0f0765 | 2017-04-12 19:10:49 | [diff] [blame] | 157 | v8::Local<v8::Value> argv[] = { |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 158 | ConvertToV8(isolate, obj.get()).ToLocalChecked(), |
Ken Rockot | 2b0f0765 | 2017-04-12 19:10:49 | [diff] [blame] | 159 | }; |
Ross McIlroy | d298cced | 2018-11-23 16:14:13 | [diff] [blame] | 160 | func->Call(context_.Get(isolate), v8::Undefined(isolate), 1, argv) |
| 161 | .ToLocalChecked(); |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 162 | EXPECT_FALSE(try_catch.HasCaught()); |
| 163 | EXPECT_EQ("", try_catch.GetStackTrace()); |
| 164 | |
| 165 | EXPECT_EQ(191, obj->value()); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | TEST_F(InterceptorTest, NamedInterceptor) { |
| 170 | RunInterceptorTest( |
| 171 | "(function (obj) {" |
| 172 | " if (obj.value !== 42) throw 'FAIL';" |
| 173 | " else obj.value = 191; })"); |
| 174 | } |
| 175 | |
| 176 | TEST_F(InterceptorTest, NamedInterceptorCall) { |
| 177 | RunInterceptorTest( |
| 178 | "(function (obj) {" |
| 179 | " if (obj.func(191) !== 42) throw 'FAIL';" |
| 180 | " })"); |
| 181 | } |
| 182 | |
| 183 | TEST_F(InterceptorTest, IndexedInterceptor) { |
| 184 | RunInterceptorTest( |
| 185 | "(function (obj) {" |
| 186 | " if (obj[0] !== 42) throw 'FAIL';" |
| 187 | " else obj[0] = 191; })"); |
| 188 | } |
| 189 | |
[email protected] | d656f87 | 2014-08-13 17:12:55 | [diff] [blame] | 190 | TEST_F(InterceptorTest, BypassInterceptorAllowed) { |
| 191 | RunInterceptorTest( |
| 192 | "(function (obj) {" |
| 193 | " obj.value = 191 /* make test happy */;" |
| 194 | " obj.foo = 23;" |
| 195 | " if (obj.foo !== 23) throw 'FAIL'; })"); |
| 196 | } |
| 197 | |
| 198 | TEST_F(InterceptorTest, BypassInterceptorForbidden) { |
| 199 | RunInterceptorTest( |
| 200 | "(function (obj) {" |
| 201 | " obj.value = 191 /* make test happy */;" |
| 202 | " obj[1] = 23;" |
| 203 | " if (obj[1] === 23) throw 'FAIL'; })"); |
| 204 | } |
| 205 | |
[email protected] | 5c969b8 | 2014-03-12 04:59:05 | [diff] [blame] | 206 | } // namespace gin |