OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_ | 5 #ifndef EXTENSIONS_RENDERER_V8_HELPERS_H_ |
6 #define EXTENSIONS_RENDERER_V8_HELPERS_H_ | 6 #define EXTENSIONS_RENDERER_V8_HELPERS_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 return maybe.IsJust() && maybe.FromJust(); | 53 return maybe.IsJust() && maybe.FromJust(); |
54 } | 54 } |
55 | 55 |
56 // Returns true if |value| is empty or undefined. | 56 // Returns true if |value| is empty or undefined. |
57 inline bool IsEmptyOrUndefied(v8::Local<v8::Value> value) { | 57 inline bool IsEmptyOrUndefied(v8::Local<v8::Value> value) { |
58 return value.IsEmpty() || value->IsUndefined(); | 58 return value.IsEmpty() || value->IsUndefined(); |
59 } | 59 } |
60 | 60 |
61 // SetProperty() family wraps V8::Object::DefineOwnProperty(). | 61 // SetProperty() family wraps V8::Object::DefineOwnProperty(). |
62 // Returns true on success. | 62 // Returns true on success. |
| 63 // NOTE: Think about whether you want this or SetPrivateProperty() below. |
| 64 // TODO(devlin): Sort through more of the callers of this and see if we can |
| 65 // convert more to be private. |
63 inline bool SetProperty(v8::Local<v8::Context> context, | 66 inline bool SetProperty(v8::Local<v8::Context> context, |
64 v8::Local<v8::Object> object, | 67 v8::Local<v8::Object> object, |
65 v8::Local<v8::String> key, | 68 v8::Local<v8::String> key, |
66 v8::Local<v8::Value> value) { | 69 v8::Local<v8::Value> value) { |
67 return IsTrue(object->DefineOwnProperty(context, key, value)); | 70 return IsTrue(object->DefineOwnProperty(context, key, value)); |
68 } | 71 } |
69 | 72 |
70 inline bool SetProperty(v8::Local<v8::Context> context, | 73 inline bool SetProperty(v8::Local<v8::Context> context, |
71 v8::Local<v8::Object> object, | 74 v8::Local<v8::Object> object, |
72 const char* key, | 75 const char* key, |
73 v8::Local<v8::Value> value) { | 76 v8::Local<v8::Value> value) { |
74 v8::Local<v8::String> v8_key; | 77 v8::Local<v8::String> v8_key; |
75 if (!ToV8String(context->GetIsolate(), key, &v8_key)) | 78 if (!ToV8String(context->GetIsolate(), key, &v8_key)) |
76 return false; | 79 return false; |
77 return SetProperty(context, object, v8_key, value); | 80 return SetProperty(context, object, v8_key, value); |
78 } | 81 } |
79 | 82 |
80 inline bool SetProperty(v8::Local<v8::Context> context, | 83 inline bool SetProperty(v8::Local<v8::Context> context, |
81 v8::Local<v8::Object> object, | 84 v8::Local<v8::Object> object, |
82 uint32_t index, | 85 uint32_t index, |
83 v8::Local<v8::Value> value) { | 86 v8::Local<v8::Value> value) { |
84 return SetProperty(context, object, base::UintToString(index).c_str(), value); | 87 return SetProperty(context, object, base::UintToString(index).c_str(), value); |
85 } | 88 } |
86 | 89 |
| 90 // Wraps v8::Object::SetPrivate(). When possible, prefer this to SetProperty(). |
| 91 inline bool SetPrivateProperty(v8::Local<v8::Context> context, |
| 92 v8::Local<v8::Object> object, |
| 93 v8::Local<v8::String> key, |
| 94 v8::Local<v8::Value> value) { |
| 95 return IsTrue(object->SetPrivate( |
| 96 context, v8::Private::ForApi(context->GetIsolate(), key), value)); |
| 97 } |
| 98 |
| 99 inline bool SetPrivateProperty(v8::Local<v8::Context> context, |
| 100 v8::Local<v8::Object> object, |
| 101 const char* key, |
| 102 v8::Local<v8::Value> value) { |
| 103 v8::Local<v8::String> v8_key; |
| 104 return ToV8String(context->GetIsolate(), key, &v8_key) && |
| 105 IsTrue(object->SetPrivate( |
| 106 context, v8::Private::ForApi(context->GetIsolate(), v8_key), |
| 107 value)); |
| 108 } |
| 109 |
87 // GetProperty() family calls V8::Object::Get() and extracts a value from | 110 // GetProperty() family calls V8::Object::Get() and extracts a value from |
88 // returned MaybeLocal. Returns true on success. | 111 // returned MaybeLocal. Returns true on success. |
| 112 // NOTE: Think about whether you want this or GetPrivateProperty() below. |
89 template <typename Key> | 113 template <typename Key> |
90 inline bool GetProperty(v8::Local<v8::Context> context, | 114 inline bool GetProperty(v8::Local<v8::Context> context, |
91 v8::Local<v8::Object> object, | 115 v8::Local<v8::Object> object, |
92 Key key, | 116 Key key, |
93 v8::Local<v8::Value>* out) { | 117 v8::Local<v8::Value>* out) { |
94 return object->Get(context, key).ToLocal(out); | 118 return object->Get(context, key).ToLocal(out); |
95 } | 119 } |
96 | 120 |
97 inline bool GetProperty(v8::Local<v8::Context> context, | 121 inline bool GetProperty(v8::Local<v8::Context> context, |
98 v8::Local<v8::Object> object, | 122 v8::Local<v8::Object> object, |
99 const char* key, | 123 const char* key, |
100 v8::Local<v8::Value>* out) { | 124 v8::Local<v8::Value>* out) { |
101 v8::Local<v8::String> v8_key; | 125 v8::Local<v8::String> v8_key; |
102 if (!ToV8String(context->GetIsolate(), key, &v8_key)) | 126 if (!ToV8String(context->GetIsolate(), key, &v8_key)) |
103 return false; | 127 return false; |
104 return GetProperty(context, object, v8_key, out); | 128 return GetProperty(context, object, v8_key, out); |
105 } | 129 } |
106 | 130 |
| 131 // Wraps v8::Object::GetPrivate(). When possible, prefer this to GetProperty(). |
| 132 inline bool GetPrivateProperty(v8::Local<v8::Context> context, |
| 133 v8::Local<v8::Object> object, |
| 134 v8::Local<v8::String> key, |
| 135 v8::Local<v8::Value>* out) { |
| 136 return object |
| 137 ->GetPrivate(context, v8::Private::ForApi(context->GetIsolate(), key)) |
| 138 .ToLocal(out); |
| 139 } |
| 140 |
| 141 inline bool GetPrivateProperty(v8::Local<v8::Context> context, |
| 142 v8::Local<v8::Object> object, |
| 143 const char* key, |
| 144 v8::Local<v8::Value>* out) { |
| 145 v8::Local<v8::String> v8_key; |
| 146 return ToV8String(context->GetIsolate(), key, &v8_key) && |
| 147 GetPrivateProperty(context, object, v8_key, out); |
| 148 } |
| 149 |
107 // GetPropertyUnsafe() family wraps v8::Object::Get(). They crash when an | 150 // GetPropertyUnsafe() family wraps v8::Object::Get(). They crash when an |
108 // exception is thrown. | 151 // exception is thrown. |
109 inline v8::Local<v8::Value> GetPropertyUnsafe(v8::Local<v8::Context> context, | 152 inline v8::Local<v8::Value> GetPropertyUnsafe(v8::Local<v8::Context> context, |
110 v8::Local<v8::Object> object, | 153 v8::Local<v8::Object> object, |
111 v8::Local<v8::Value> key) { | 154 v8::Local<v8::Value> key) { |
112 return object->Get(context, key).ToLocalChecked(); | 155 return object->Get(context, key).ToLocalChecked(); |
113 } | 156 } |
114 | 157 |
115 inline v8::Local<v8::Value> GetPropertyUnsafe( | 158 inline v8::Local<v8::Value> GetPropertyUnsafe( |
116 v8::Local<v8::Context> context, | 159 v8::Local<v8::Context> context, |
(...skipping 12 matching lines...) Expand all Loading... |
129 int argc, | 172 int argc, |
130 v8::Local<v8::Value> argv[], | 173 v8::Local<v8::Value> argv[], |
131 v8::Local<v8::Value>* out) { | 174 v8::Local<v8::Value>* out) { |
132 return function->Call(context, recv, argc, argv).ToLocal(out); | 175 return function->Call(context, recv, argc, argv).ToLocal(out); |
133 } | 176 } |
134 | 177 |
135 } // namespace v8_helpers | 178 } // namespace v8_helpers |
136 } // namespace extensions | 179 } // namespace extensions |
137 | 180 |
138 #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ | 181 #endif // EXTENSIONS_RENDERER_V8_HELPERS_H_ |
OLD | NEW |