OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #ifndef Dictionary_h | |
27 #define Dictionary_h | |
28 | |
29 #include "bindings/v8/ExceptionMessages.h" | |
30 #include "bindings/v8/ExceptionState.h" | |
31 #include "bindings/v8/Nullable.h" | |
32 #include "bindings/v8/ScriptValue.h" | |
33 #include "bindings/v8/V8Binding.h" | |
34 #include "bindings/v8/V8BindingMacros.h" | |
35 #include "core/events/EventListener.h" | |
36 #include "core/dom/MessagePort.h" | |
37 #include <v8.h> | |
38 #include "wtf/HashMap.h" | |
39 #include "wtf/HashSet.h" | |
40 #include "wtf/Vector.h" | |
41 #include "wtf/text/AtomicString.h" | |
42 #include "wtf/text/WTFString.h" | |
43 | |
44 namespace WebCore { | |
45 | |
46 class ArrayValue; | |
47 class DOMError; | |
48 class Element; | |
49 class LocalDOMWindow; | |
50 class Gamepad; | |
51 class MediaStream; | |
52 class HeaderMap; | |
53 class Headers; | |
54 class IDBKeyRange; | |
55 class MIDIPort; | |
56 class MediaKeyError; | |
57 class Notification; | |
58 class SpeechRecognitionResult; | |
59 class SpeechRecognitionResultList; | |
60 class Storage; | |
61 class TrackBase; | |
62 class VoidCallback; | |
63 | |
64 class Dictionary { | |
65 ALLOW_ONLY_INLINE_ALLOCATION(); | |
66 public: | |
67 Dictionary(); | |
68 Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate*); | |
69 ~Dictionary(); | |
70 | |
71 Dictionary& operator=(const Dictionary&); | |
72 | |
73 bool isObject() const; | |
74 bool isUndefinedOrNull() const; | |
75 | |
76 bool get(const String&, bool&) const; | |
77 bool get(const String&, int32_t&) const; | |
78 bool get(const String&, double&, bool& hasValue) const; | |
79 bool get(const String&, double&) const; | |
80 bool get(const String&, String&) const; | |
81 bool get(const String&, AtomicString&) const; | |
82 bool get(const String&, ScriptValue&) const; | |
83 bool get(const String&, short&) const; | |
84 bool get(const String&, unsigned short&) const; | |
85 bool get(const String&, unsigned&) const; | |
86 bool get(const String&, unsigned long&) const; | |
87 bool get(const String&, unsigned long long&) const; | |
88 bool get(const String&, RefPtrWillBeMember<LocalDOMWindow>&) const; | |
89 bool get(const String&, RefPtrWillBeMember<Storage>&) const; | |
90 bool get(const String&, MessagePortArray&) const; | |
91 bool get(const String&, RefPtr<Uint8Array>&) const; | |
92 bool get(const String&, RefPtr<ArrayBufferView>&) const; | |
93 bool get(const String&, Member<MIDIPort>&) const; | |
94 bool get(const String&, RefPtrWillBeMember<MediaKeyError>&) const; | |
95 bool get(const String&, RefPtrWillBeMember<TrackBase>&) const; | |
96 bool get(const String&, Member<SpeechRecognitionResult>&) const; | |
97 bool get(const String&, Member<SpeechRecognitionResultList>&) const; | |
98 bool get(const String&, Member<Gamepad>&) const; | |
99 bool get(const String&, Member<MediaStream>&) const; | |
100 bool get(const String&, RefPtrWillBeMember<EventTarget>&) const; | |
101 bool get(const String&, HashSet<AtomicString>&) const; | |
102 bool get(const String&, Dictionary&) const; | |
103 bool get(const String&, Vector<String>&) const; | |
104 bool get(const String&, ArrayValue&) const; | |
105 bool get(const String&, RefPtrWillBeMember<DOMError>&) const; | |
106 bool get(const String&, v8::Local<v8::Value>&) const; | |
107 bool get(const String&, RefPtr<HeaderMap>&) const; | |
108 bool get(const String&, RefPtr<Headers>&) const; | |
109 | |
110 class ConversionContext { | |
111 public: | |
112 ConversionContext(const String& interfaceName, const String& methodName,
ExceptionState& exceptionState) | |
113 : m_interfaceName(interfaceName) | |
114 , m_methodName(methodName) | |
115 , m_exceptionState(exceptionState) | |
116 , m_dirty(true) | |
117 { | |
118 resetPerPropertyContext(); | |
119 } | |
120 | |
121 const String& interfaceName() const { return m_interfaceName; } | |
122 const String& methodName() const { return m_methodName; } | |
123 bool forConstructor() const { return m_methodName.isEmpty(); } | |
124 ExceptionState& exceptionState() const { return m_exceptionState; } | |
125 | |
126 bool isNullable() const { return m_isNullable; } | |
127 String typeName() const { return m_propertyTypeName; } | |
128 | |
129 ConversionContext& setConversionType(const String&, bool); | |
130 | |
131 void throwTypeError(const String& detail); | |
132 | |
133 void resetPerPropertyContext(); | |
134 | |
135 private: | |
136 const String m_interfaceName; | |
137 const String m_methodName; | |
138 ExceptionState& m_exceptionState; | |
139 bool m_dirty; | |
140 | |
141 bool m_isNullable; | |
142 String m_propertyTypeName; | |
143 }; | |
144 | |
145 class ConversionContextScope { | |
146 public: | |
147 ConversionContextScope(ConversionContext& context) | |
148 : m_context(context) { } | |
149 ~ConversionContextScope() | |
150 { | |
151 m_context.resetPerPropertyContext(); | |
152 } | |
153 private: | |
154 ConversionContext& m_context; | |
155 }; | |
156 | |
157 bool convert(ConversionContext&, const String&, bool&) const; | |
158 bool convert(ConversionContext&, const String&, double&) const; | |
159 bool convert(ConversionContext&, const String&, String&) const; | |
160 bool convert(ConversionContext&, const String&, ScriptValue&) const; | |
161 | |
162 template<typename IntegralType> | |
163 bool convert(ConversionContext&, const String&, IntegralType&) const; | |
164 template<typename IntegralType> | |
165 bool convert(ConversionContext&, const String&, Nullable<IntegralType>&) con
st; | |
166 | |
167 bool convert(ConversionContext&, const String&, MessagePortArray&) const; | |
168 bool convert(ConversionContext&, const String&, HashSet<AtomicString>&) cons
t; | |
169 bool convert(ConversionContext&, const String&, Dictionary&) const; | |
170 bool convert(ConversionContext&, const String&, Vector<String>&) const; | |
171 bool convert(ConversionContext&, const String&, ArrayValue&) const; | |
172 template<template <typename> class PointerType, typename T> | |
173 bool convert(ConversionContext&, const String&, PointerType<T>&) const; | |
174 | |
175 template<typename StringType> | |
176 bool getStringType(const String&, StringType&) const; | |
177 | |
178 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const; | |
179 bool getOwnPropertyNames(Vector<String>&) const; | |
180 | |
181 bool getWithUndefinedOrNullCheck(const String&, String&) const; | |
182 bool getWithUndefinedOrNullCheck(const String&, RefPtrWillBeMember<Element>&
) const; | |
183 | |
184 bool hasProperty(const String&) const; | |
185 | |
186 private: | |
187 bool getKey(const String& key, v8::Local<v8::Value>&) const; | |
188 | |
189 v8::Handle<v8::Value> m_options; | |
190 v8::Isolate* m_isolate; | |
191 }; | |
192 | |
193 template<> | |
194 struct NativeValueTraits<Dictionary> { | |
195 static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8:
:Isolate* isolate) | |
196 { | |
197 return Dictionary(value, isolate); | |
198 } | |
199 }; | |
200 | |
201 template <typename T> | |
202 struct IntegralTypeTraits { | |
203 }; | |
204 | |
205 template <> | |
206 struct IntegralTypeTraits<uint8_t> { | |
207 static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers
ionConfiguration configuration, ExceptionState& exceptionState) | |
208 { | |
209 return toUInt8(value, configuration, exceptionState); | |
210 } | |
211 static const String typeName() { return "UInt8"; } | |
212 }; | |
213 | |
214 template <> | |
215 struct IntegralTypeTraits<int8_t> { | |
216 static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversi
onConfiguration configuration, ExceptionState& exceptionState) | |
217 { | |
218 return toInt8(value, configuration, exceptionState); | |
219 } | |
220 static const String typeName() { return "Int8"; } | |
221 }; | |
222 | |
223 template <> | |
224 struct IntegralTypeTraits<unsigned short> { | |
225 static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConver
sionConfiguration configuration, ExceptionState& exceptionState) | |
226 { | |
227 return toUInt16(value, configuration, exceptionState); | |
228 } | |
229 static const String typeName() { return "UInt16"; } | |
230 }; | |
231 | |
232 template <> | |
233 struct IntegralTypeTraits<short> { | |
234 static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers
ionConfiguration configuration, ExceptionState& exceptionState) | |
235 { | |
236 return toInt16(value, configuration, exceptionState); | |
237 } | |
238 static const String typeName() { return "Int16"; } | |
239 }; | |
240 | |
241 template <> | |
242 struct IntegralTypeTraits<unsigned> { | |
243 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver
sionConfiguration configuration, ExceptionState& exceptionState) | |
244 { | |
245 return toUInt32(value, configuration, exceptionState); | |
246 } | |
247 static const String typeName() { return "UInt32"; } | |
248 }; | |
249 | |
250 template <> | |
251 struct IntegralTypeTraits<unsigned long> { | |
252 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver
sionConfiguration configuration, ExceptionState& exceptionState) | |
253 { | |
254 return toUInt32(value, configuration, exceptionState); | |
255 } | |
256 static const String typeName() { return "UInt32"; } | |
257 }; | |
258 | |
259 template <> | |
260 struct IntegralTypeTraits<int> { | |
261 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers
ionConfiguration configuration, ExceptionState& exceptionState) | |
262 { | |
263 return toInt32(value, configuration, exceptionState); | |
264 } | |
265 static const String typeName() { return "Int32"; } | |
266 }; | |
267 | |
268 template <> | |
269 struct IntegralTypeTraits<long> { | |
270 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers
ionConfiguration configuration, ExceptionState& exceptionState) | |
271 { | |
272 return toInt32(value, configuration, exceptionState); | |
273 } | |
274 static const String typeName() { return "Int32"; } | |
275 }; | |
276 | |
277 template <> | |
278 struct IntegralTypeTraits<unsigned long long> { | |
279 static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, Int
egerConversionConfiguration configuration, ExceptionState& exceptionState) | |
280 { | |
281 return toUInt64(value, configuration, exceptionState); | |
282 } | |
283 static const String typeName() { return "UInt64"; } | |
284 }; | |
285 | |
286 template <> | |
287 struct IntegralTypeTraits<long long> { | |
288 static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConve
rsionConfiguration configuration, ExceptionState& exceptionState) | |
289 { | |
290 return toInt64(value, configuration, exceptionState); | |
291 } | |
292 static const String typeName() { return "Int64"; } | |
293 }; | |
294 | |
295 template<typename T> bool Dictionary::convert(ConversionContext& context, const
String& key, T& value) const | |
296 { | |
297 ConversionContextScope scope(context); | |
298 | |
299 v8::Local<v8::Value> v8Value; | |
300 if (!getKey(key, v8Value)) | |
301 return true; | |
302 | |
303 value = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, context
.exceptionState()); | |
304 if (context.exceptionState().throwIfNeeded()) | |
305 return false; | |
306 | |
307 return true; | |
308 } | |
309 | |
310 template<typename T> bool Dictionary::convert(ConversionContext& context, const
String& key, Nullable<T>& value) const | |
311 { | |
312 ConversionContextScope scope(context); | |
313 | |
314 v8::Local<v8::Value> v8Value; | |
315 if (!getKey(key, v8Value)) | |
316 return true; | |
317 | |
318 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) { | |
319 value = Nullable<T>(); | |
320 return true; | |
321 } | |
322 | |
323 T converted = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, c
ontext.exceptionState()); | |
324 | |
325 if (context.exceptionState().throwIfNeeded()) | |
326 return false; | |
327 | |
328 value = Nullable<T>(converted); | |
329 return true; | |
330 } | |
331 | |
332 template<template <typename> class PointerType, typename T> bool Dictionary::con
vert(ConversionContext& context, const String& key, PointerType<T>& value) const | |
333 { | |
334 ConversionContextScope scope(context); | |
335 | |
336 if (!get(key, value)) | |
337 return true; | |
338 | |
339 if (value) | |
340 return true; | |
341 | |
342 v8::Local<v8::Value> v8Value; | |
343 getKey(key, v8Value); | |
344 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
345 return true; | |
346 | |
347 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n
ot have a " + context.typeName() + " type.")); | |
348 return false; | |
349 } | |
350 | |
351 } | |
352 | |
353 #endif // Dictionary_h | |
OLD | NEW |