1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef Dictionary_h
28#define Dictionary_h
29
30#include "JSDictionary.h"
31#include "JSEventListener.h"
32#include <bindings/ScriptValue.h>
33#include <wtf/HashMap.h>
34#include <wtf/text/CString.h>
35#include <wtf/text/WTFString.h>
36
37namespace JSC {
38class JSValue;
39}
40
41namespace WebCore {
42class EventListener;
43
44class Dictionary {
45public:
46    Dictionary();
47    Dictionary(JSC::ExecState*, JSC::JSValue);
48
49    // Returns true if a value was found for the provided property.
50    template <typename Result>
51    bool get(const char* propertyName, Result&) const;
52    template <typename Result>
53    bool get(const String& propertyName, Result&) const;
54
55    template <typename T>
56    PassRefPtr<EventListener> getEventListener(const char* propertyName, T* target) const;
57    template <typename T>
58    PassRefPtr<EventListener> getEventListener(const String& propertyName, T* target) const;
59
60    bool isObject() const { return m_dictionary.isValid(); }
61    bool isUndefinedOrNull() const { return !m_dictionary.isValid(); }
62    bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
63    bool getOwnPropertyNames(Vector<String>&) const;
64    bool getWithUndefinedOrNullCheck(const String& propertyName, String& value) const;
65
66private:
67    template <typename T>
68    JSC::JSObject* asJSObject(T*) const;
69
70    JSDictionary m_dictionary;
71};
72
73template <typename Result>
74bool Dictionary::get(const char* propertyName, Result& result) const
75{
76    if (!m_dictionary.isValid())
77        return false;
78
79    return m_dictionary.get(propertyName, result);
80}
81
82template <typename Result>
83bool Dictionary::get(const String& propertyName, Result& result) const
84{
85    return get(propertyName.utf8().data(), result);
86}
87
88template <typename T>
89PassRefPtr<EventListener> Dictionary::getEventListener(const char* propertyName, T* target) const
90{
91    if (!m_dictionary.isValid())
92        return 0;
93
94    Deprecated::ScriptValue eventListener;
95    if (!m_dictionary.tryGetProperty(propertyName, eventListener))
96        return 0;
97    if (eventListener.hasNoValue())
98        return 0;
99    if (!eventListener.isObject())
100        return 0;
101
102    return JSEventListener::create(asObject(eventListener.jsValue()), asJSObject(target), true, currentWorld(m_dictionary.execState()));
103}
104
105template <typename T>
106PassRefPtr<EventListener> Dictionary::getEventListener(const String& propertyName, T* target) const
107{
108    return getEventListener(propertyName.utf8().data(), target);
109}
110
111}
112
113#endif // Dictionary_h
114