OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
6 #include "gin/arguments.h" | 6 #include "gin/arguments.h" |
7 #include "gin/handle.h" | 7 #include "gin/handle.h" |
8 #include "gin/interceptor.h" | 8 #include "gin/interceptor.h" |
9 #include "gin/object_template_builder.h" | 9 #include "gin/object_template_builder.h" |
10 #include "gin/per_isolate_data.h" | 10 #include "gin/per_isolate_data.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 static WrapperInfo kWrapperInfo; | 24 static WrapperInfo kWrapperInfo; |
25 | 25 |
26 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) { | 26 static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) { |
27 return CreateHandle(isolate, new MyInterceptor(isolate)); | 27 return CreateHandle(isolate, new MyInterceptor(isolate)); |
28 } | 28 } |
29 | 29 |
30 int value() const { return value_; } | 30 int value() const { return value_; } |
31 void set_value(int value) { value_ = value; } | 31 void set_value(int value) { value_ = value; } |
32 | 32 |
33 // gin::NamedPropertyInterceptor | 33 // gin::NamedPropertyInterceptor |
34 virtual v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate, | 34 v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate, |
35 const std::string& property) | 35 const std::string& property) override { |
36 override { | |
37 if (property == "value") { | 36 if (property == "value") { |
38 return ConvertToV8(isolate, value_); | 37 return ConvertToV8(isolate, value_); |
39 } else if (property == "func") { | 38 } else if (property == "func") { |
40 return GetFunctionTemplate(isolate, "func")->GetFunction(); | 39 return GetFunctionTemplate(isolate, "func")->GetFunction(); |
41 } else { | 40 } else { |
42 return v8::Local<v8::Value>(); | 41 return v8::Local<v8::Value>(); |
43 } | 42 } |
44 } | 43 } |
45 virtual bool SetNamedProperty(v8::Isolate* isolate, | 44 bool SetNamedProperty(v8::Isolate* isolate, |
46 const std::string& property, | 45 const std::string& property, |
47 v8::Local<v8::Value> value) override { | 46 v8::Local<v8::Value> value) override { |
48 if (property == "value") { | 47 if (property == "value") { |
49 ConvertFromV8(isolate, value, &value_); | 48 ConvertFromV8(isolate, value, &value_); |
50 return true; | 49 return true; |
51 } | 50 } |
52 return false; | 51 return false; |
53 } | 52 } |
54 virtual std::vector<std::string> EnumerateNamedProperties( | 53 std::vector<std::string> EnumerateNamedProperties( |
55 v8::Isolate* isolate) override { | 54 v8::Isolate* isolate) override { |
56 std::vector<std::string> result; | 55 std::vector<std::string> result; |
57 result.push_back("func"); | 56 result.push_back("func"); |
58 result.push_back("value"); | 57 result.push_back("value"); |
59 return result; | 58 return result; |
60 } | 59 } |
61 | 60 |
62 // gin::IndexedPropertyInterceptor | 61 // gin::IndexedPropertyInterceptor |
63 virtual v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, | 62 v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, |
64 uint32_t index) override { | 63 uint32_t index) override { |
65 if (index == 0) | 64 if (index == 0) |
66 return ConvertToV8(isolate, value_); | 65 return ConvertToV8(isolate, value_); |
67 return v8::Local<v8::Value>(); | 66 return v8::Local<v8::Value>(); |
68 } | 67 } |
69 virtual bool SetIndexedProperty(v8::Isolate* isolate, | 68 bool SetIndexedProperty(v8::Isolate* isolate, |
70 uint32_t index, | 69 uint32_t index, |
71 v8::Local<v8::Value> value) override { | 70 v8::Local<v8::Value> value) override { |
72 if (index == 0) { | 71 if (index == 0) { |
73 ConvertFromV8(isolate, value, &value_); | 72 ConvertFromV8(isolate, value, &value_); |
74 return true; | 73 return true; |
75 } | 74 } |
76 // Don't allow bypassing the interceptor. | 75 // Don't allow bypassing the interceptor. |
77 return true; | 76 return true; |
78 } | 77 } |
79 virtual std::vector<uint32_t> EnumerateIndexedProperties(v8::Isolate* isolate) | 78 std::vector<uint32_t> EnumerateIndexedProperties( |
80 override { | 79 v8::Isolate* isolate) override { |
81 std::vector<uint32_t> result; | 80 std::vector<uint32_t> result; |
82 result.push_back(0); | 81 result.push_back(0); |
83 return result; | 82 return result; |
84 } | 83 } |
85 | 84 |
86 private: | 85 private: |
87 explicit MyInterceptor(v8::Isolate* isolate) | 86 explicit MyInterceptor(v8::Isolate* isolate) |
88 : NamedPropertyInterceptor(isolate, this), | 87 : NamedPropertyInterceptor(isolate, this), |
89 IndexedPropertyInterceptor(isolate, this), | 88 IndexedPropertyInterceptor(isolate, this), |
90 value_(0), | 89 value_(0), |
91 template_cache_(isolate) {} | 90 template_cache_(isolate) {} |
92 virtual ~MyInterceptor() {} | 91 ~MyInterceptor() override {} |
93 | 92 |
94 // gin::Wrappable | 93 // gin::Wrappable |
95 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) | 94 ObjectTemplateBuilder GetObjectTemplateBuilder( |
96 override { | 95 v8::Isolate* isolate) override { |
97 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate) | 96 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate) |
98 .AddNamedPropertyInterceptor() | 97 .AddNamedPropertyInterceptor() |
99 .AddIndexedPropertyInterceptor(); | 98 .AddIndexedPropertyInterceptor(); |
100 } | 99 } |
101 | 100 |
102 int Call(int value) { | 101 int Call(int value) { |
103 int tmp = value_; | 102 int tmp = value_; |
104 value_ = value; | 103 value_ = value; |
105 return tmp; | 104 return tmp; |
106 } | 105 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 186 |
188 TEST_F(InterceptorTest, BypassInterceptorForbidden) { | 187 TEST_F(InterceptorTest, BypassInterceptorForbidden) { |
189 RunInterceptorTest( | 188 RunInterceptorTest( |
190 "(function (obj) {" | 189 "(function (obj) {" |
191 " obj.value = 191 /* make test happy */;" | 190 " obj.value = 191 /* make test happy */;" |
192 " obj[1] = 23;" | 191 " obj[1] = 23;" |
193 " if (obj[1] === 23) throw 'FAIL'; })"); | 192 " if (obj[1] === 23) throw 'FAIL'; })"); |
194 } | 193 } |
195 | 194 |
196 } // namespace gin | 195 } // namespace gin |
OLD | NEW |