tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / bindings / v8 / OptionsObject.cpp
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 #include "config.h"
27 #include "OptionsObject.h"
28
29 #include "DOMStringList.h"
30 #include "V8Binding.h"
31 #include "V8DOMWindow.h"
32 #include "V8Utilities.h"
33 #include <wtf/MathExtras.h>
34
35 #if ENABLE(INDEXED_DATABASE)
36 #include "IDBKeyRange.h"
37 #include "V8IDBKeyRange.h"
38 #endif
39
40 #if ENABLE(VIDEO_TRACK)
41 #include "TrackBase.h"
42 #include "V8TextTrack.h"
43 #endif
44
45 namespace WebCore {
46
47 OptionsObject::OptionsObject()
48 {
49 }
50
51 OptionsObject::OptionsObject(const v8::Local<v8::Value>& options)
52     : m_options(options)
53 {
54 }
55
56 OptionsObject::~OptionsObject()
57 {
58 }
59
60 OptionsObject& OptionsObject::operator=(const OptionsObject& optionsObject)
61 {
62     m_options = optionsObject.m_options;
63     return *this;
64 }
65
66 bool OptionsObject::isUndefinedOrNull() const
67 {
68     if (m_options.IsEmpty())
69         return true;
70     return WebCore::isUndefinedOrNull(m_options);
71 }
72
73 bool OptionsObject::getKey(const String& key, v8::Local<v8::Value>& value) const
74 {
75     if (isUndefinedOrNull())
76         return false;
77     v8::Local<v8::Object> options = m_options->ToObject();
78     ASSERT(!options.IsEmpty());
79
80     v8::Handle<v8::String> v8Key = v8String(key);
81     if (!options->Has(v8Key))
82         return false;
83     value = options->Get(v8Key);
84     if (value.IsEmpty())
85         return false;
86     return true;
87 }
88
89 bool OptionsObject::get(const String& key, bool& value) const
90 {
91     v8::Local<v8::Value> v8Value;
92     if (!getKey(key, v8Value))
93         return false;
94
95     v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean();
96     if (v8Bool.IsEmpty())
97         return false;
98     value = v8Bool->Value();
99     return true;
100 }
101
102 bool OptionsObject::get(const String& key, int32_t& value) const
103 {
104     v8::Local<v8::Value> v8Value;
105     if (!getKey(key, v8Value))
106         return false;
107
108     v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
109     if (v8Int32.IsEmpty())
110         return false;
111     value = v8Int32->Value();
112     return true;
113 }
114
115 bool OptionsObject::get(const String& key, double& value) const
116 {
117     v8::Local<v8::Value> v8Value;
118     if (!getKey(key, v8Value))
119         return false;
120
121     v8::Local<v8::Number> v8Number = v8Value->ToNumber();
122     if (v8Number.IsEmpty())
123         return false;
124     value = v8Number->Value();
125     return true;
126 }
127
128 bool OptionsObject::get(const String& key, String& value) const
129 {
130     v8::Local<v8::Value> v8Value;
131     if (!getKey(key, v8Value))
132         return false;
133
134     // FIXME: It is possible for this to throw in which case we'd be getting back
135     //        an empty string and returning true when we should be returning false.
136     //        See fast/dom/Geolocation/script-tests/argument-types.js for a similar
137     //        example.
138     value = v8ValueToWebCoreString(v8Value);
139     return true;
140 }
141
142 bool OptionsObject::get(const String& key, ScriptValue& value) const
143 {
144     v8::Local<v8::Value> v8Value;
145     if (!getKey(key, v8Value))
146         return false;
147
148     value = ScriptValue(v8Value);
149     return true;
150 }
151
152 bool OptionsObject::get(const String& key, unsigned short& value) const
153 {
154     v8::Local<v8::Value> v8Value;
155     if (!getKey(key, v8Value))
156         return false;
157
158     v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
159     if (v8Int32.IsEmpty())
160         return false;
161     value = static_cast<unsigned short>(v8Int32->Value());
162     return true;
163 }
164
165 bool OptionsObject::get(const String& key, unsigned& value) const
166 {
167     v8::Local<v8::Value> v8Value;
168     if (!getKey(key, v8Value))
169         return false;
170
171     v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
172     if (v8Int32.IsEmpty())
173         return false;
174     value = static_cast<unsigned>(v8Int32->Value());
175     return true;
176 }
177
178 bool OptionsObject::get(const String& key, unsigned long long& value) const
179 {
180     v8::Local<v8::Value> v8Value;
181     if (!getKey(key, v8Value))
182         return false;
183
184     v8::Local<v8::Number> v8Number = v8Value->ToNumber();
185     if (v8Number.IsEmpty())
186         return false;
187     double d = v8Number->Value();
188     doubleToInteger(d, value);
189     return true;
190 }
191
192 bool OptionsObject::get(const String& key, RefPtr<DOMWindow>& value) const
193 {
194     v8::Local<v8::Value> v8Value;
195     if (!getKey(key, v8Value))
196         return false;
197
198     DOMWindow* source = 0;
199     if (v8Value->IsObject()) {
200         v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
201         v8::Handle<v8::Object> window = V8DOMWrapper::lookupDOMWrapper(V8DOMWindow::GetTemplate(), wrapper);
202         if (!window.IsEmpty())
203             source = V8DOMWindow::toNative(window);
204     }
205     value = source;
206     return true;
207 }
208
209 bool OptionsObject::get(const String& key, MessagePortArray& value) const
210 {
211     v8::Local<v8::Value> v8Value;
212     if (!getKey(key, v8Value))
213         return false;
214
215     return getMessagePortArray(v8Value, value);
216 }
217
218 bool OptionsObject::get(const String& key, HashSet<AtomicString>& value) const
219 {
220     v8::Local<v8::Value> v8Value;
221     if (!getKey(key, v8Value))
222         return false;
223
224     // FIXME: Support array-like objects
225     if (!v8Value->IsArray())
226         return false;
227
228     v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
229     for (size_t i = 0; i < v8Array->Length(); ++i) {
230         v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(i));
231         value.add(v8ValueToWebCoreString(indexedValue));
232     }
233
234     return true;
235 }
236
237 bool OptionsObject::getWithUndefinedOrNullCheck(const String& key, String& value) const
238 {
239     v8::Local<v8::Value> v8Value;
240     if (!getKey(key, v8Value) || v8Value->IsNull() || v8Value->IsUndefined())
241         return false;
242
243     // FIXME: It is possible for this to throw in which case we'd be getting back
244     //        an empty string and returning true when we should be returning false.
245     //        See fast/dom/Geolocation/script-tests/argument-types.js for a similar
246     //        example.
247     value = WebCore::isUndefinedOrNull(v8Value) ? String() : v8ValueToWebCoreString(v8Value);
248     return true;
249 }
250
251 #if ENABLE(VIDEO_TRACK)
252 bool OptionsObject::get(const String& key, RefPtr<TrackBase>& value) const
253 {
254     v8::Local<v8::Value> v8Value;
255     if (!getKey(key, v8Value))
256         return false;
257
258     TrackBase* source = 0;
259     if (v8Value->IsObject()) {
260         v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
261
262         // FIXME: this will need to be changed so it can also return an AudioTrack or a VideoTrack once
263         // we add them.
264         v8::Handle<v8::Object> track = V8DOMWrapper::lookupDOMWrapper(V8TextTrack::GetTemplate(), wrapper);
265         if (!track.IsEmpty())
266             source = V8TextTrack::toNative(track);
267     }
268     value = source;
269     return true;
270 }
271 #endif
272
273 } // namespace WebCore