2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "OptionsObject.h"
29 #include "DOMStringList.h"
30 #include "V8Binding.h"
31 #include "V8DOMWindow.h"
32 #include "V8Utilities.h"
33 #include <wtf/MathExtras.h>
35 #if ENABLE(INDEXED_DATABASE)
36 #include "IDBKeyRange.h"
37 #include "V8IDBKeyRange.h"
40 #if ENABLE(VIDEO_TRACK)
41 #include "TrackBase.h"
42 #include "V8TextTrack.h"
47 OptionsObject::OptionsObject()
51 OptionsObject::OptionsObject(const v8::Local<v8::Value>& options)
56 OptionsObject::~OptionsObject()
60 OptionsObject& OptionsObject::operator=(const OptionsObject& optionsObject)
62 m_options = optionsObject.m_options;
66 bool OptionsObject::isUndefinedOrNull() const
68 if (m_options.IsEmpty())
70 return WebCore::isUndefinedOrNull(m_options);
73 bool OptionsObject::getKey(const String& key, v8::Local<v8::Value>& value) const
75 if (isUndefinedOrNull())
77 v8::Local<v8::Object> options = m_options->ToObject();
78 ASSERT(!options.IsEmpty());
80 v8::Handle<v8::String> v8Key = v8String(key);
81 if (!options->Has(v8Key))
83 value = options->Get(v8Key);
89 bool OptionsObject::get(const String& key, bool& value) const
91 v8::Local<v8::Value> v8Value;
92 if (!getKey(key, v8Value))
95 v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean();
98 value = v8Bool->Value();
102 bool OptionsObject::get(const String& key, int32_t& value) const
104 v8::Local<v8::Value> v8Value;
105 if (!getKey(key, v8Value))
108 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
109 if (v8Int32.IsEmpty())
111 value = v8Int32->Value();
115 bool OptionsObject::get(const String& key, double& value) const
117 v8::Local<v8::Value> v8Value;
118 if (!getKey(key, v8Value))
121 v8::Local<v8::Number> v8Number = v8Value->ToNumber();
122 if (v8Number.IsEmpty())
124 value = v8Number->Value();
128 bool OptionsObject::get(const String& key, String& value) const
130 v8::Local<v8::Value> v8Value;
131 if (!getKey(key, v8Value))
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
138 value = v8ValueToWebCoreString(v8Value);
142 bool OptionsObject::get(const String& key, ScriptValue& value) const
144 v8::Local<v8::Value> v8Value;
145 if (!getKey(key, v8Value))
148 value = ScriptValue(v8Value);
152 bool OptionsObject::get(const String& key, unsigned short& value) const
154 v8::Local<v8::Value> v8Value;
155 if (!getKey(key, v8Value))
158 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
159 if (v8Int32.IsEmpty())
161 value = static_cast<unsigned short>(v8Int32->Value());
165 bool OptionsObject::get(const String& key, unsigned& value) const
167 v8::Local<v8::Value> v8Value;
168 if (!getKey(key, v8Value))
171 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
172 if (v8Int32.IsEmpty())
174 value = static_cast<unsigned>(v8Int32->Value());
178 bool OptionsObject::get(const String& key, unsigned long long& value) const
180 v8::Local<v8::Value> v8Value;
181 if (!getKey(key, v8Value))
184 v8::Local<v8::Number> v8Number = v8Value->ToNumber();
185 if (v8Number.IsEmpty())
187 double d = v8Number->Value();
188 doubleToInteger(d, value);
192 bool OptionsObject::get(const String& key, RefPtr<DOMWindow>& value) const
194 v8::Local<v8::Value> v8Value;
195 if (!getKey(key, v8Value))
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);
209 bool OptionsObject::get(const String& key, MessagePortArray& value) const
211 v8::Local<v8::Value> v8Value;
212 if (!getKey(key, v8Value))
215 return getMessagePortArray(v8Value, value);
218 bool OptionsObject::get(const String& key, HashSet<AtomicString>& value) const
220 v8::Local<v8::Value> v8Value;
221 if (!getKey(key, v8Value))
224 // FIXME: Support array-like objects
225 if (!v8Value->IsArray())
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));
237 bool OptionsObject::getWithUndefinedOrNullCheck(const String& key, String& value) const
239 v8::Local<v8::Value> v8Value;
240 if (!getKey(key, v8Value) || v8Value->IsNull() || v8Value->IsUndefined())
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
247 value = WebCore::isUndefinedOrNull(v8Value) ? String() : v8ValueToWebCoreString(v8Value);
251 #if ENABLE(VIDEO_TRACK)
252 bool OptionsObject::get(const String& key, RefPtr<TrackBase>& value) const
254 v8::Local<v8::Value> v8Value;
255 if (!getKey(key, v8Value))
258 TrackBase* source = 0;
259 if (v8Value->IsObject()) {
260 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
262 // FIXME: this will need to be changed so it can also return an AudioTrack or a VideoTrack once
264 v8::Handle<v8::Object> track = V8DOMWrapper::lookupDOMWrapper(V8TextTrack::GetTemplate(), wrapper);
265 if (!track.IsEmpty())
266 source = V8TextTrack::toNative(track);
273 } // namespace WebCore