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 15 matching lines...) Expand all Loading... |
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 virtual v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate, |
35 const std::string& property) | 35 const std::string& property) |
36 OVERRIDE { | 36 override { |
37 if (property == "value") { | 37 if (property == "value") { |
38 return ConvertToV8(isolate, value_); | 38 return ConvertToV8(isolate, value_); |
39 } else if (property == "func") { | 39 } else if (property == "func") { |
40 return GetFunctionTemplate(isolate, "func")->GetFunction(); | 40 return GetFunctionTemplate(isolate, "func")->GetFunction(); |
41 } else { | 41 } else { |
42 return v8::Local<v8::Value>(); | 42 return v8::Local<v8::Value>(); |
43 } | 43 } |
44 } | 44 } |
45 virtual bool SetNamedProperty(v8::Isolate* isolate, | 45 virtual bool SetNamedProperty(v8::Isolate* isolate, |
46 const std::string& property, | 46 const std::string& property, |
47 v8::Local<v8::Value> value) OVERRIDE { | 47 v8::Local<v8::Value> value) override { |
48 if (property == "value") { | 48 if (property == "value") { |
49 ConvertFromV8(isolate, value, &value_); | 49 ConvertFromV8(isolate, value, &value_); |
50 return true; | 50 return true; |
51 } | 51 } |
52 return false; | 52 return false; |
53 } | 53 } |
54 virtual std::vector<std::string> EnumerateNamedProperties( | 54 virtual std::vector<std::string> EnumerateNamedProperties( |
55 v8::Isolate* isolate) OVERRIDE { | 55 v8::Isolate* isolate) override { |
56 std::vector<std::string> result; | 56 std::vector<std::string> result; |
57 result.push_back("func"); | 57 result.push_back("func"); |
58 result.push_back("value"); | 58 result.push_back("value"); |
59 return result; | 59 return result; |
60 } | 60 } |
61 | 61 |
62 // gin::IndexedPropertyInterceptor | 62 // gin::IndexedPropertyInterceptor |
63 virtual v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, | 63 virtual v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, |
64 uint32_t index) OVERRIDE { | 64 uint32_t index) override { |
65 if (index == 0) | 65 if (index == 0) |
66 return ConvertToV8(isolate, value_); | 66 return ConvertToV8(isolate, value_); |
67 return v8::Local<v8::Value>(); | 67 return v8::Local<v8::Value>(); |
68 } | 68 } |
69 virtual bool SetIndexedProperty(v8::Isolate* isolate, | 69 virtual bool SetIndexedProperty(v8::Isolate* isolate, |
70 uint32_t index, | 70 uint32_t index, |
71 v8::Local<v8::Value> value) OVERRIDE { | 71 v8::Local<v8::Value> value) override { |
72 if (index == 0) { | 72 if (index == 0) { |
73 ConvertFromV8(isolate, value, &value_); | 73 ConvertFromV8(isolate, value, &value_); |
74 return true; | 74 return true; |
75 } | 75 } |
76 // Don't allow bypassing the interceptor. | 76 // Don't allow bypassing the interceptor. |
77 return true; | 77 return true; |
78 } | 78 } |
79 virtual std::vector<uint32_t> EnumerateIndexedProperties(v8::Isolate* isolate) | 79 virtual std::vector<uint32_t> EnumerateIndexedProperties(v8::Isolate* isolate) |
80 OVERRIDE { | 80 override { |
81 std::vector<uint32_t> result; | 81 std::vector<uint32_t> result; |
82 result.push_back(0); | 82 result.push_back(0); |
83 return result; | 83 return result; |
84 } | 84 } |
85 | 85 |
86 private: | 86 private: |
87 explicit MyInterceptor(v8::Isolate* isolate) | 87 explicit MyInterceptor(v8::Isolate* isolate) |
88 : NamedPropertyInterceptor(isolate, this), | 88 : NamedPropertyInterceptor(isolate, this), |
89 IndexedPropertyInterceptor(isolate, this), | 89 IndexedPropertyInterceptor(isolate, this), |
90 value_(0), | 90 value_(0), |
91 template_cache_(isolate) {} | 91 template_cache_(isolate) {} |
92 virtual ~MyInterceptor() {} | 92 virtual ~MyInterceptor() {} |
93 | 93 |
94 // gin::Wrappable | 94 // gin::Wrappable |
95 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) | 95 virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) |
96 OVERRIDE { | 96 override { |
97 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate) | 97 return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate) |
98 .AddNamedPropertyInterceptor() | 98 .AddNamedPropertyInterceptor() |
99 .AddIndexedPropertyInterceptor(); | 99 .AddIndexedPropertyInterceptor(); |
100 } | 100 } |
101 | 101 |
102 int Call(int value) { | 102 int Call(int value) { |
103 int tmp = value_; | 103 int tmp = value_; |
104 value_ = value; | 104 value_ = value; |
105 return tmp; | 105 return tmp; |
106 } | 106 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 187 |
188 TEST_F(InterceptorTest, BypassInterceptorForbidden) { | 188 TEST_F(InterceptorTest, BypassInterceptorForbidden) { |
189 RunInterceptorTest( | 189 RunInterceptorTest( |
190 "(function (obj) {" | 190 "(function (obj) {" |
191 " obj.value = 191 /* make test happy */;" | 191 " obj.value = 191 /* make test happy */;" |
192 " obj[1] = 23;" | 192 " obj[1] = 23;" |
193 " if (obj[1] === 23) throw 'FAIL'; })"); | 193 " if (obj[1] === 23) throw 'FAIL'; })"); |
194 } | 194 } |
195 | 195 |
196 } // namespace gin | 196 } // namespace gin |
OLD | NEW |