2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "bindings/v8/ExceptionMessages.h"
30 #include "bindings/v8/ExceptionState.h"
31 #include "bindings/v8/ScriptValue.h"
32 #include "bindings/v8/V8Binding.h"
33 #include "bindings/v8/V8BindingMacros.h"
34 #include "core/events/EventListener.h"
35 #include "core/dom/MessagePort.h"
37 #include "wtf/HashMap.h"
38 #include "wtf/HashSet.h"
39 #include "wtf/Vector.h"
40 #include "wtf/text/AtomicString.h"
41 #include "wtf/text/WTFString.h"
52 class SpeechRecognitionError;
53 class SpeechRecognitionResult;
54 class SpeechRecognitionResultList;
62 Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate*);
65 Dictionary& operator=(const Dictionary&);
67 bool isObject() const;
68 bool isUndefinedOrNull() const;
70 bool get(const String&, bool&) const;
71 bool get(const String&, int32_t&) const;
72 bool get(const String&, double&, bool& hasValue) const;
73 bool get(const String&, double&) const;
74 bool get(const String&, String&) const;
75 bool get(const String&, AtomicString&) const;
76 bool get(const String&, ScriptValue&) const;
77 bool get(const String&, short&) const;
78 bool get(const String&, unsigned short&) const;
79 bool get(const String&, unsigned&) const;
80 bool get(const String&, unsigned long&) const;
81 bool get(const String&, unsigned long long&) const;
82 bool get(const String&, RefPtr<DOMWindow>&) const;
83 bool get(const String&, RefPtrWillBeRawPtr<Storage>&) const;
84 bool get(const String&, MessagePortArray&) const;
85 bool get(const String&, RefPtr<Uint8Array>&) const;
86 bool get(const String&, RefPtr<ArrayBufferView>&) const;
87 bool get(const String&, RefPtr<MIDIPort>&) const;
88 bool get(const String&, RefPtr<MediaKeyError>&) const;
89 bool get(const String&, RefPtr<TrackBase>&) const;
90 bool get(const String&, RefPtr<SpeechRecognitionError>&) const;
91 bool get(const String&, RefPtrWillBeRawPtr<SpeechRecognitionResult>&) const;
92 bool get(const String&, RefPtrWillBeRawPtr<SpeechRecognitionResultList>&) const;
93 bool get(const String&, RefPtr<MediaStream>&) const;
94 bool get(const String&, RefPtr<EventTarget>&) const;
95 bool get(const String&, HashSet<AtomicString>&) const;
96 bool get(const String&, Dictionary&) const;
97 bool get(const String&, Vector<String>&) const;
98 bool get(const String&, ArrayValue&) const;
99 bool get(const String&, RefPtr<DOMError>&) const;
100 bool get(const String&, OwnPtr<VoidCallback>&) const;
101 bool get(const String&, v8::Local<v8::Value>&) const;
103 class ConversionContext {
105 ConversionContext(const String& interfaceName, const String& methodName, ExceptionState& exceptionState)
106 : m_interfaceName(interfaceName)
107 , m_methodName(methodName)
108 , m_exceptionState(exceptionState)
111 resetPerPropertyContext();
114 const String& interfaceName() const { return m_interfaceName; }
115 const String& methodName() const { return m_methodName; }
116 bool forConstructor() const { return m_methodName.isEmpty(); }
117 ExceptionState& exceptionState() const { return m_exceptionState; }
119 bool isNullable() const { return m_isNullable; }
120 String typeName() const { return m_propertyTypeName; }
122 ConversionContext& setConversionType(const String&, bool);
124 void throwTypeError(const String& detail);
126 void resetPerPropertyContext();
129 const String m_interfaceName;
130 const String m_methodName;
131 ExceptionState& m_exceptionState;
135 String m_propertyTypeName;
138 class ConversionContextScope {
140 ConversionContextScope(ConversionContext& context)
141 : m_context(context) { }
142 ~ConversionContextScope()
144 m_context.resetPerPropertyContext();
147 ConversionContext& m_context;
150 bool convert(ConversionContext&, const String&, bool&) const;
151 bool convert(ConversionContext&, const String&, double&) const;
152 bool convert(ConversionContext&, const String&, String&) const;
153 bool convert(ConversionContext&, const String&, ScriptValue&) const;
155 template<typename IntegralType>
156 bool convert(ConversionContext&, const String&, IntegralType&) const;
157 bool convert(ConversionContext&, const String&, MessagePortArray&) const;
158 bool convert(ConversionContext&, const String&, HashSet<AtomicString>&) const;
159 bool convert(ConversionContext&, const String&, Dictionary&) const;
160 bool convert(ConversionContext&, const String&, Vector<String>&) const;
161 bool convert(ConversionContext&, const String&, ArrayValue&) const;
162 template<template <typename> class PointerType, typename T>
163 bool convert(ConversionContext&, const String&, PointerType<T>&) const;
165 template<typename StringType>
166 bool getStringType(const String&, StringType&) const;
168 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
169 bool getOwnPropertyNames(Vector<String>&) const;
171 bool getWithUndefinedOrNullCheck(const String&, String&) const;
173 bool hasProperty(const String&) const;
175 // Only allow inline allocation.
176 void* operator new(size_t, NotNullTag, void* location) { return location; }
179 // Disallow new allocation.
180 void* operator new(size_t);
182 bool getKey(const String& key, v8::Local<v8::Value>&) const;
184 v8::Handle<v8::Value> m_options;
185 v8::Isolate* m_isolate;
189 struct NativeValueTraits<Dictionary> {
190 static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
192 return Dictionary(value, isolate);
196 template <typename T>
197 struct IntegralTypeTraits {
201 struct IntegralTypeTraits<uint8_t> {
202 static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
204 return toUInt8(value, configuration, exceptionState);
206 static const String typeName() { return "UInt8"; }
210 struct IntegralTypeTraits<int8_t> {
211 static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
213 return toInt8(value, configuration, exceptionState);
215 static const String typeName() { return "Int8"; }
219 struct IntegralTypeTraits<unsigned short> {
220 static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
222 return toUInt16(value, configuration, exceptionState);
224 static const String typeName() { return "UInt16"; }
228 struct IntegralTypeTraits<short> {
229 static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
231 return toInt16(value, configuration, exceptionState);
233 static const String typeName() { return "Int16"; }
237 struct IntegralTypeTraits<unsigned> {
238 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
240 return toUInt32(value, configuration, exceptionState);
242 static const String typeName() { return "UInt32"; }
246 struct IntegralTypeTraits<unsigned long> {
247 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
249 return toUInt32(value, configuration, exceptionState);
251 static const String typeName() { return "UInt32"; }
255 struct IntegralTypeTraits<int> {
256 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
258 return toInt32(value, configuration, exceptionState);
260 static const String typeName() { return "Int32"; }
264 struct IntegralTypeTraits<long> {
265 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
267 return toInt32(value, configuration, exceptionState);
269 static const String typeName() { return "Int32"; }
273 struct IntegralTypeTraits<unsigned long long> {
274 static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
276 return toUInt64(value, configuration, exceptionState);
278 static const String typeName() { return "UInt64"; }
282 struct IntegralTypeTraits<long long> {
283 static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
285 return toInt64(value, configuration, exceptionState);
287 static const String typeName() { return "Int64"; }
290 template<typename T> bool Dictionary::convert(ConversionContext& context, const String& key, T& value) const
292 ConversionContextScope scope(context);
294 v8::Local<v8::Value> v8Value;
295 if (!getKey(key, v8Value))
298 value = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, context.exceptionState());
299 if (context.exceptionState().throwIfNeeded())
305 template<template <typename> class PointerType, typename T> bool Dictionary::convert(ConversionContext& context, const String& key, PointerType<T>& value) const
307 ConversionContextScope scope(context);
309 if (!get(key, value))
315 v8::Local<v8::Value> v8Value;
316 getKey(key, v8Value);
317 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value))
320 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does not have a " + context.typeName() + " type."));
326 #endif // Dictionary_h