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