Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / Dictionary.h
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/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"
36 #include <v8.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"
42
43 namespace WebCore {
44
45 class ArrayValue;
46 class DOMError;
47 class DOMWindow;
48 class IDBKeyRange;
49 class MIDIPort;
50 class MediaKeyError;
51 class Notification;
52 class SpeechRecognitionError;
53 class SpeechRecognitionResult;
54 class SpeechRecognitionResultList;
55 class Storage;
56 class TrackBase;
57 class VoidCallback;
58
59 class Dictionary {
60 public:
61     Dictionary();
62     Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate*);
63     ~Dictionary();
64
65     Dictionary& operator=(const Dictionary&);
66
67     bool isObject() const;
68     bool isUndefinedOrNull() const;
69
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;
102
103     class ConversionContext {
104     public:
105         ConversionContext(const String& interfaceName, const String& methodName, ExceptionState& exceptionState)
106             : m_interfaceName(interfaceName)
107             , m_methodName(methodName)
108             , m_exceptionState(exceptionState)
109             , m_dirty(true)
110         {
111             resetPerPropertyContext();
112         }
113
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; }
118
119         bool isNullable() const { return m_isNullable; }
120         String typeName() const { return m_propertyTypeName; }
121
122         ConversionContext& setConversionType(const String&, bool);
123
124         void throwTypeError(const String& detail);
125
126         void resetPerPropertyContext();
127
128     private:
129         const String m_interfaceName;
130         const String m_methodName;
131         ExceptionState& m_exceptionState;
132         bool m_dirty;
133
134         bool m_isNullable;
135         String m_propertyTypeName;
136     };
137
138     class ConversionContextScope {
139     public:
140         ConversionContextScope(ConversionContext& context)
141             : m_context(context) { }
142         ~ConversionContextScope()
143         {
144             m_context.resetPerPropertyContext();
145         }
146     private:
147         ConversionContext& m_context;
148     };
149
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;
154
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;
164
165     template<typename StringType>
166     bool getStringType(const String&, StringType&) const;
167
168     bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
169     bool getOwnPropertyNames(Vector<String>&) const;
170
171     bool getWithUndefinedOrNullCheck(const String&, String&) const;
172
173     bool hasProperty(const String&) const;
174
175     // Only allow inline allocation.
176     void* operator new(size_t, NotNullTag, void* location) { return location; }
177
178 private:
179     // Disallow new allocation.
180     void* operator new(size_t);
181
182     bool getKey(const String& key, v8::Local<v8::Value>&) const;
183
184     v8::Handle<v8::Value> m_options;
185     v8::Isolate* m_isolate;
186 };
187
188 template<>
189 struct NativeValueTraits<Dictionary> {
190     static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
191     {
192         return Dictionary(value, isolate);
193     }
194 };
195
196 template <typename T>
197 struct IntegralTypeTraits {
198 };
199
200 template <>
201 struct IntegralTypeTraits<uint8_t> {
202     static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
203     {
204         return toUInt8(value, configuration, exceptionState);
205     }
206     static const String typeName() { return "UInt8"; }
207 };
208
209 template <>
210 struct IntegralTypeTraits<int8_t> {
211     static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
212     {
213         return toInt8(value, configuration, exceptionState);
214     }
215     static const String typeName() { return "Int8"; }
216 };
217
218 template <>
219 struct IntegralTypeTraits<unsigned short> {
220     static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
221     {
222         return toUInt16(value, configuration, exceptionState);
223     }
224     static const String typeName() { return "UInt16"; }
225 };
226
227 template <>
228 struct IntegralTypeTraits<short> {
229     static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
230     {
231         return toInt16(value, configuration, exceptionState);
232     }
233     static const String typeName() { return "Int16"; }
234 };
235
236 template <>
237 struct IntegralTypeTraits<unsigned> {
238     static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
239     {
240         return toUInt32(value, configuration, exceptionState);
241     }
242     static const String typeName() { return "UInt32"; }
243 };
244
245 template <>
246 struct IntegralTypeTraits<unsigned long> {
247     static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
248     {
249         return toUInt32(value, configuration, exceptionState);
250     }
251     static const String typeName() { return "UInt32"; }
252 };
253
254 template <>
255 struct IntegralTypeTraits<int> {
256     static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
257     {
258         return toInt32(value, configuration, exceptionState);
259     }
260     static const String typeName() { return "Int32"; }
261 };
262
263 template <>
264 struct IntegralTypeTraits<long> {
265     static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
266     {
267         return toInt32(value, configuration, exceptionState);
268     }
269     static const String typeName() { return "Int32"; }
270 };
271
272 template <>
273 struct IntegralTypeTraits<unsigned long long> {
274     static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
275     {
276         return toUInt64(value, configuration, exceptionState);
277     }
278     static const String typeName() { return "UInt64"; }
279 };
280
281 template <>
282 struct IntegralTypeTraits<long long> {
283     static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, ExceptionState& exceptionState)
284     {
285         return toInt64(value, configuration, exceptionState);
286     }
287     static const String typeName() { return "Int64"; }
288 };
289
290 template<typename T> bool Dictionary::convert(ConversionContext& context, const String& key, T& value) const
291 {
292     ConversionContextScope scope(context);
293
294     v8::Local<v8::Value> v8Value;
295     if (!getKey(key, v8Value))
296         return true;
297
298     value = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, context.exceptionState());
299     if (context.exceptionState().throwIfNeeded())
300         return false;
301
302     return true;
303 }
304
305 template<template <typename> class PointerType, typename T> bool Dictionary::convert(ConversionContext& context, const String& key, PointerType<T>& value) const
306 {
307     ConversionContextScope scope(context);
308
309     if (!get(key, value))
310         return true;
311
312     if (value)
313         return true;
314
315     v8::Local<v8::Value> v8Value;
316     getKey(key, v8Value);
317     if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value))
318         return true;
319
320     context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does not have a " + context.typeName() + " type."));
321     return false;
322 }
323
324 }
325
326 #endif // Dictionary_h