summaryrefslogtreecommitdiff
path: root/chromium/third_party/WebKit/Source/bindings/v8/Dictionary.h
blob: 3f29651e8762bb673290c9f58c989e0a2483ecb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef Dictionary_h
#define Dictionary_h

#include "bindings/v8/ExceptionMessages.h"
#include "bindings/v8/ExceptionState.h"
#include "bindings/v8/ScriptValue.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8BindingMacros.h"
#include "core/events/EventListener.h"
#include "core/dom/MessagePort.h"
#include <v8.h>
#include "wtf/HashMap.h"
#include "wtf/HashSet.h"
#include "wtf/Vector.h"
#include "wtf/text/AtomicString.h"
#include "wtf/text/WTFString.h"

namespace WebCore {

class ArrayValue;
class DOMError;
class DOMWindow;
class IDBKeyRange;
class MIDIPort;
class MediaKeyError;
class Notification;
class SpeechRecognitionError;
class SpeechRecognitionResult;
class SpeechRecognitionResultList;
class Storage;
class TrackBase;
class VoidCallback;

class Dictionary {
public:
    Dictionary();
    Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate*);
    ~Dictionary();

    Dictionary& operator=(const Dictionary&);

    bool isObject() const;
    bool isUndefinedOrNull() const;

    bool get(const String&, bool&) const;
    bool get(const String&, int32_t&) const;
    bool get(const String&, double&, bool& hasValue) const;
    bool get(const String&, double&) const;
    bool get(const String&, String&) const;
    bool get(const String&, ScriptValue&) const;
    bool get(const String&, short&) const;
    bool get(const String&, unsigned short&) const;
    bool get(const String&, unsigned&) const;
    bool get(const String&, unsigned long&) const;
    bool get(const String&, unsigned long long&) const;
    bool get(const String&, RefPtr<DOMWindow>&) const;
    bool get(const String&, RefPtr<Storage>&) const;
    bool get(const String&, MessagePortArray&) const;
    bool get(const String&, RefPtr<Uint8Array>&) const;
    bool get(const String&, RefPtr<ArrayBufferView>&) const;
    bool get(const String&, RefPtr<MIDIPort>&) const;
    bool get(const String&, RefPtr<MediaKeyError>&) const;
    bool get(const String&, RefPtr<TrackBase>&) const;
    bool get(const String&, RefPtr<SpeechRecognitionError>&) const;
    bool get(const String&, RefPtr<SpeechRecognitionResult>&) const;
    bool get(const String&, RefPtr<SpeechRecognitionResultList>&) const;
    bool get(const String&, RefPtr<MediaStream>&) const;
    bool get(const String&, RefPtr<EventTarget>&) const;
    bool get(const String&, HashSet<AtomicString>&) const;
    bool get(const String&, Dictionary&) const;
    bool get(const String&, Vector<String>&) const;
    bool get(const String&, ArrayValue&) const;
    bool get(const String&, RefPtr<DOMError>&) const;
    bool get(const String&, OwnPtr<VoidCallback>&) const;
    bool get(const String&, v8::Local<v8::Value>&) const;

    class ConversionContext {
    public:
        ConversionContext(const String& interfaceName, const String& methodName, ExceptionState& exceptionState)
            : m_interfaceName(interfaceName)
            , m_methodName(methodName)
            , m_exceptionState(exceptionState)
            , m_dirty(true)
        {
            resetPerPropertyContext();
        }

        const String& interfaceName() const { return m_interfaceName; }
        const String& methodName() const { return m_methodName; }
        bool forConstructor() const { return m_methodName.isEmpty(); }
        ExceptionState& exceptionState() const { return m_exceptionState; }

        bool isNullable() const { return m_isNullable; }
        String typeName() const { return m_propertyTypeName; }

        ConversionContext& setConversionType(const String&, bool);

        void throwTypeError(const String& detail);

        void resetPerPropertyContext();

    private:
        const String m_interfaceName;
        const String m_methodName;
        ExceptionState& m_exceptionState;
        bool m_dirty;

        bool m_isNullable;
        String m_propertyTypeName;
    };

    class ConversionContextScope {
    public:
        ConversionContextScope(ConversionContext& context)
            : m_context(context) { }
        ~ConversionContextScope()
        {
            m_context.resetPerPropertyContext();
        }
    private:
        ConversionContext& m_context;
    };

    bool convert(ConversionContext&, const String&, bool&) const;
    bool convert(ConversionContext&, const String&, double&) const;
    bool convert(ConversionContext&, const String&, String&) const;
    bool convert(ConversionContext&, const String&, ScriptValue&) const;

    template<typename IntegralType>
    bool convert(ConversionContext &, const String&, IntegralType&) const;
    bool convert(ConversionContext &, const String&, MessagePortArray&) const;
    bool convert(ConversionContext &, const String&, HashSet<AtomicString>&) const;
    bool convert(ConversionContext &, const String&, Dictionary&) const;
    bool convert(ConversionContext &, const String&, Vector<String>&) const;
    bool convert(ConversionContext &, const String&, ArrayValue&) const;
    template<typename T>
    bool convert(ConversionContext &, const String&, RefPtr<T>&) const;

    bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
    bool getOwnPropertyNames(Vector<String>&) const;

    bool getWithUndefinedOrNullCheck(const String&, String&) const;

    bool hasProperty(const String&) const;

    // Only allow inline allocation.
    void* operator new(size_t, NotNullTag, void* location) { return location; }

private:
    // Disallow new allocation.
    void* operator new(size_t);

    bool getKey(const String& key, v8::Local<v8::Value>&) const;

    v8::Handle<v8::Value> m_options;
    v8::Isolate* m_isolate;
};

template<>
struct NativeValueTraits<Dictionary> {
    static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
    {
        return Dictionary(value, isolate);
    }
};

template <typename T>
struct IntegralTypeTraits {
};

template <>
struct IntegralTypeTraits<uint8_t> {
    static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toUInt8(value, configuration, ok);
    }
    static const String typeName() { return "UInt8"; }
};

template <>
struct IntegralTypeTraits<int8_t> {
    static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toInt8(value, configuration, ok);
    }
    static const String typeName() { return "Int8"; }
};

template <>
struct IntegralTypeTraits<unsigned short> {
    static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toUInt16(value, configuration, ok);
    }
    static const String typeName() { return "UInt16"; }
};

template <>
struct IntegralTypeTraits<short> {
    static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toInt16(value, configuration, ok);
    }
    static const String typeName() { return "Int16"; }
};

template <>
struct IntegralTypeTraits<unsigned> {
    static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toUInt32(value, configuration, ok);
    }
    static const String typeName() { return "UInt32"; }
};

template <>
struct IntegralTypeTraits<unsigned long> {
    static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toUInt32(value, configuration, ok);
    }
    static const String typeName() { return "UInt32"; }
};

template <>
struct IntegralTypeTraits<int> {
    static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toInt32(value, configuration, ok);
    }
    static const String typeName() { return "Int32"; }
};

template <>
struct IntegralTypeTraits<long> {
    static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toInt32(value, configuration, ok);
    }
    static const String typeName() { return "Int32"; }
};

template <>
struct IntegralTypeTraits<unsigned long long> {
    static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toUInt64(value, configuration, ok);
    }
    static const String typeName() { return "UInt64"; }
};

template <>
struct IntegralTypeTraits<long long> {
    static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
    {
        return toInt64(value, configuration, ok);
    }
    static const String typeName() { return "Int64"; }
};

template<typename T> bool Dictionary::convert(ConversionContext& context, const String& key, T& value) const
{
    ConversionContextScope scope(context);

    v8::Local<v8::Value> v8Value;
    if (!getKey(key, v8Value))
        return true;

    bool ok = false;
    value = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, ok);
    if (ok)
        return true;

    V8TRYCATCH_RETURN(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false);
    ASSERT(v8Number.IsEmpty());
    context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does not have type " + IntegralTypeTraits<T>::typeName() + "."));
    return false;
}

template<typename T> bool Dictionary::convert(ConversionContext& context, const String& key, RefPtr<T>& value) const
{
    ConversionContextScope scope(context);

    if (!get(key, value))
        return true;

    if (value)
        return true;

    v8::Local<v8::Value> v8Value;
    getKey(key, v8Value);
    if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value))
        return true;

    context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does not have a " + context.typeName() + " type."));
    return false;
}

}

#endif // Dictionary_h