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/core/v8/ExceptionMessages.h"
30#include "bindings/core/v8/ExceptionState.h"
31#include "bindings/core/v8/Nullable.h"
32#include "bindings/core/v8/ScriptValue.h"
33#include "bindings/core/v8/V8Binding.h"
34#include "bindings/core/v8/V8BindingMacros.h"
35#include "core/dom/MessagePort.h"
36#include "core/events/EventListener.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#include <v8.h>
43
44namespace blink {
45
46class Element;
47class Path2D;
48
49class Dictionary {
50    ALLOW_ONLY_INLINE_ALLOCATION();
51public:
52    Dictionary();
53    Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate*);
54    ~Dictionary();
55
56    Dictionary& operator=(const Dictionary&);
57
58    // This is different from the default constructor:
59    //   * isObject() is true when using createEmpty().
60    //   * isUndefinedOrNull() is true when using default constructor.
61    static Dictionary createEmpty(v8::Isolate*);
62
63    bool isObject() const;
64    bool isUndefinedOrNull() const;
65
66    bool get(const String&, Dictionary&) const;
67    bool get(const String&, v8::Local<v8::Value>&) const;
68
69    // Sets properties using default attributes.
70    bool set(const String&, const v8::Handle<v8::Value>&);
71    bool set(const String&, const String&);
72    bool set(const String&, unsigned);
73    bool set(const String&, const Dictionary&);
74
75    v8::Handle<v8::Value> v8Value() const { return m_options; }
76
77    class ConversionContext {
78    public:
79        ConversionContext(const String& interfaceName, const String& methodName, ExceptionState& exceptionState)
80            : m_interfaceName(interfaceName)
81            , m_methodName(methodName)
82            , m_exceptionState(exceptionState)
83            , m_dirty(true)
84        {
85            resetPerPropertyContext();
86        }
87
88        const String& interfaceName() const { return m_interfaceName; }
89        const String& methodName() const { return m_methodName; }
90        bool forConstructor() const { return m_methodName.isEmpty(); }
91        ExceptionState& exceptionState() const { return m_exceptionState; }
92
93        bool isNullable() const { return m_isNullable; }
94        String typeName() const { return m_propertyTypeName; }
95
96        ConversionContext& setConversionType(const String&, bool);
97
98        void throwTypeError(const String& detail);
99
100        void resetPerPropertyContext();
101
102    private:
103        const String m_interfaceName;
104        const String m_methodName;
105        ExceptionState& m_exceptionState;
106        bool m_dirty;
107
108        bool m_isNullable;
109        String m_propertyTypeName;
110    };
111
112    class ConversionContextScope {
113    public:
114        ConversionContextScope(ConversionContext& context)
115            : m_context(context) { }
116        ~ConversionContextScope()
117        {
118            m_context.resetPerPropertyContext();
119        }
120    private:
121        ConversionContext& m_context;
122    };
123
124    bool convert(ConversionContext&, const String&, Dictionary&) const;
125
126    bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
127    bool getOwnPropertyNames(Vector<String>&) const;
128
129    bool getWithUndefinedOrNullCheck(const String&, String&) const;
130    bool getWithUndefinedOrNullCheck(const String&, RefPtrWillBeMember<Element>&) const;
131    bool getWithUndefinedOrNullCheck(const String&, RefPtrWillBeMember<Path2D>&) const;
132
133    bool hasProperty(const String&) const;
134
135    v8::Isolate* isolate() const { return m_isolate; }
136
137    bool getKey(const String& key, v8::Local<v8::Value>&) const;
138
139private:
140    v8::Handle<v8::Value> m_options;
141    v8::Isolate* m_isolate;
142};
143
144template<>
145struct NativeValueTraits<Dictionary> {
146    static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate, ExceptionState&)
147    {
148        return Dictionary(value, isolate);
149    }
150};
151
152// DictionaryHelper is a collection of static methods for getting or
153// converting a value from Dictionary.
154struct DictionaryHelper {
155    template <typename T>
156    static bool get(const Dictionary&, const String& key, T& value);
157    template <typename T>
158    static bool get(const Dictionary&, const String& key, T& value, bool& hasValue);
159    template <typename T>
160    static bool getWithUndefinedOrNullCheck(const Dictionary& dictionary, const String& key, T& value)
161    {
162        v8::Local<v8::Value> v8Value;
163        if (!dictionary.getKey(key, v8Value) || isUndefinedOrNull(v8Value))
164            return false;
165        return DictionaryHelper::get(dictionary, key, value);
166    }
167    template <template <typename> class PointerType, typename T>
168    static bool get(const Dictionary&, const String& key, PointerType<T>& value);
169    template <typename T>
170    static bool convert(const Dictionary&, Dictionary::ConversionContext&, const String& key, T& value);
171    template <typename T>
172    static bool convert(const Dictionary&, Dictionary::ConversionContext&, const String& key, Nullable<T>& value);
173    template <template <typename> class PointerType, typename T>
174    static bool convert(const Dictionary&, Dictionary::ConversionContext&, const String& key, PointerType<T>& value);
175};
176
177}
178
179#endif // Dictionary_h
180