OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * | 13 * |
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | 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 | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 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 | 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #ifndef Dictionary_h | 26 #ifndef Dictionary_h |
27 #define Dictionary_h | 27 #define Dictionary_h |
28 | 28 |
29 #include "bindings/core/v8/ExceptionMessages.h" | |
30 #include "bindings/core/v8/ExceptionState.h" | 29 #include "bindings/core/v8/ExceptionState.h" |
31 #include "bindings/core/v8/Nullable.h" | 30 #include "bindings/core/v8/PropertyBag.h" |
32 #include "bindings/core/v8/ScriptValue.h" | |
33 #include "bindings/core/v8/V8Binding.h" | 31 #include "bindings/core/v8/V8Binding.h" |
34 #include "bindings/core/v8/V8BindingMacros.h" | 32 #include "bindings/core/v8/V8Element.h" |
| 33 #include "bindings/core/v8/V8Path2D.h" |
35 #include "core/dom/MessagePort.h" | 34 #include "core/dom/MessagePort.h" |
36 #include "core/events/EventListener.h" | 35 #include "core/events/EventListener.h" |
37 #include "wtf/HashMap.h" | 36 #include "wtf/OwnPtr.h" |
38 #include "wtf/HashSet.h" | |
39 #include "wtf/Vector.h" | |
40 #include "wtf/text/AtomicString.h" | |
41 #include "wtf/text/WTFString.h" | 37 #include "wtf/text/WTFString.h" |
42 #include <v8.h> | |
43 | 38 |
44 namespace blink { | 39 namespace blink { |
45 | 40 |
46 class Element; | 41 // NOTE: I don't intend to land this; just to show how we can remove DictionaryH
elper. |
47 class Path2D; | 42 // We should remove Dictionary class at all. |
48 | |
49 class Dictionary { | 43 class Dictionary { |
50 ALLOW_ONLY_INLINE_ALLOCATION(); | 44 ALLOW_ONLY_INLINE_ALLOCATION(); |
51 public: | 45 public: |
52 Dictionary(); | 46 Dictionary() |
53 Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate*); | 47 { |
54 ~Dictionary(); | 48 } |
55 | 49 |
56 Dictionary& operator=(const Dictionary&); | 50 Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate* isolate) |
| 51 : m_options(options) |
| 52 { |
| 53 if (!m_options.IsEmpty() && m_options->IsObject()) { |
| 54 v8::Handle<v8::Object> v8Object = options->ToObject(); |
| 55 m_propertyBag = adoptPtr(new PropertyBag(isolate, v8Object, m_except
ionState)); |
| 56 } |
| 57 } |
| 58 |
| 59 Dictionary(const Dictionary& rhs) |
| 60 { |
| 61 m_options = rhs.m_options; |
| 62 if (rhs.m_propertyBag) |
| 63 m_propertyBag = adoptPtr(new PropertyBag(rhs.m_propertyBag->m_isolat
e, rhs.m_propertyBag->m_object, m_exceptionState)); |
| 64 } |
57 | 65 |
58 // This is different from the default constructor: | 66 // This is different from the default constructor: |
59 // * isObject() is true when using createEmpty(). | 67 // * isObject() is true when using createEmpty(). |
60 // * isUndefinedOrNull() is true when using default constructor. | 68 // * isUndefinedOrNull() is true when using default constructor. |
61 static Dictionary createEmpty(v8::Isolate*); | 69 static Dictionary createEmpty(v8::Isolate* isolate) |
| 70 { |
| 71 return Dictionary(v8::Object::New(isolate), isolate); |
| 72 } |
62 | 73 |
63 bool isObject() const; | 74 ~Dictionary() { } |
64 bool isUndefinedOrNull() const; | |
65 | 75 |
66 bool get(const String&, Dictionary&) const; | 76 Dictionary& operator=(const Dictionary& rhs) |
67 bool get(const String&, v8::Local<v8::Value>&) const; | 77 { |
| 78 m_options = rhs.m_options; |
| 79 if (rhs.m_propertyBag) |
| 80 m_propertyBag = adoptPtr(new PropertyBag(rhs.m_propertyBag->m_isolat
e, rhs.m_propertyBag->m_object, m_exceptionState)); |
| 81 else |
| 82 m_propertyBag.clear(); |
| 83 return *this; |
| 84 } |
| 85 |
| 86 bool isObject() const |
| 87 { |
| 88 return !m_options.IsEmpty() && m_options->IsObject(); |
| 89 } |
| 90 |
| 91 bool isUndefinedOrNull() const |
| 92 { |
| 93 return m_options.IsEmpty() || blink::isUndefinedOrNull(m_options); |
| 94 } |
| 95 |
| 96 template <typename T> |
| 97 bool get(const String& key, T& value) const { return m_propertyBag && m_prop
ertyBag->get(key, value, PropertyBag::IsNotNullable); } |
| 98 |
| 99 template <typename T> |
| 100 bool get(const String& key, T& value, bool& hasValue) const { return m_prope
rtyBag && m_propertyBag->get(key, value, hasValue); } |
| 101 template <typename T> |
| 102 bool getWithUndefinedOrNullCheck(const String& key, T& value) const { return
get(key, value); } |
68 | 103 |
69 // Sets properties using default attributes. | 104 // Sets properties using default attributes. |
70 bool set(const String&, const v8::Handle<v8::Value>&); | 105 template <typename T> |
71 bool set(const String&, const String&); | 106 bool set(const String& key, const T& value) { return m_propertyBag && m_prop
ertyBag->set(key, value); } |
72 bool set(const String&, unsigned); | |
73 bool set(const String&, const Dictionary&); | |
74 | 107 |
75 v8::Handle<v8::Value> v8Value() const { return m_options; } | 108 v8::Handle<v8::Value> v8Value() const |
| 109 { |
| 110 return m_options; |
| 111 } |
76 | 112 |
77 class ConversionContext { | 113 // FIXME: These should be gone. |
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; | 114 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const; |
127 bool getOwnPropertyNames(Vector<String>&) const; | 115 bool getOwnPropertyNames(Vector<String>&) const; |
128 | 116 |
129 bool getWithUndefinedOrNullCheck(const String&, String&) const; | 117 bool hasProperty(const String& key) const { return m_propertyBag && m_proper
tyBag->hasProperty(key); } |
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 | 118 |
137 private: | 119 private: |
138 bool getKey(const String& key, v8::Local<v8::Value>&) const; | 120 v8::Handle<v8::Value> m_options; |
| 121 TrackExceptionState m_exceptionState; |
| 122 OwnPtr<PropertyBag> m_propertyBag; |
| 123 }; |
139 | 124 |
140 v8::Handle<v8::Value> m_options; | 125 template <> |
141 v8::Isolate* m_isolate; | 126 struct V8ValueTraits<Dictionary> { |
| 127 static v8::Handle<v8::Value> toV8Value(const Dictionary& value, v8::Handle<v
8::Object>, v8::Isolate*) |
| 128 { |
| 129 return value.v8Value(); |
| 130 } |
142 }; | 131 }; |
143 | 132 |
144 template<> | 133 template<> |
145 struct NativeValueTraits<Dictionary> { | 134 struct NativeValueTraits<Dictionary> { |
146 static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8:
:Isolate* isolate) | 135 static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8:
:Isolate* isolate) |
147 { | 136 { |
148 return Dictionary(value, isolate); | 137 return Dictionary(value, isolate); |
149 } | 138 } |
150 }; | 139 static inline Dictionary nativeValueMayFail(v8::Isolate* isolate, const v8::
Handle<v8::Value>& value, ExceptionState& exceptionState) |
151 | 140 { |
152 // DictionaryHelper is a collection of static methods for getting or | 141 if (value->IsObject()) { |
153 // converting a value from Dictionary. | 142 return Dictionary(value, isolate); |
154 struct DictionaryHelper { | 143 } |
155 template <typename T> | 144 return Dictionary(); |
156 static bool get(const Dictionary&, const String& key, T& value); | 145 } |
157 template <typename T> | |
158 static bool get(const Dictionary&, const String& key, T& value, bool& hasVal
ue); | |
159 template <template <typename> class PointerType, typename T> | |
160 static bool get(const Dictionary&, const String& key, PointerType<T>& value)
; | |
161 template <typename T> | |
162 static bool convert(const Dictionary&, Dictionary::ConversionContext&, const
String& key, T& value); | |
163 template <typename T> | |
164 static bool convert(const Dictionary&, Dictionary::ConversionContext&, const
String& key, Nullable<T>& value); | |
165 template <template <typename> class PointerType, typename T> | |
166 static bool convert(const Dictionary&, Dictionary::ConversionContext&, const
String& key, PointerType<T>& value); | |
167 }; | 146 }; |
168 | 147 |
169 } | 148 } |
170 | 149 |
171 #endif // Dictionary_h | 150 #endif // Dictionary_h |
OLD | NEW |