#include "config.h"
#include "Dictionary.h"
#include "DOMStringList.h"
#include "V8Binding.h"
#include "V8DOMWindow.h"
#include "V8Storage.h"
#include "V8Uint8Array.h"
#include "V8Utilities.h"
#include <wtf/MathExtras.h>
#if ENABLE(INDEXED_DATABASE)
#include "IDBKeyRange.h"
#include "V8IDBKeyRange.h"
#endif
#if ENABLE(ENCRYPTED_MEDIA)
#include "V8MediaKeyError.h"
#endif
#if ENABLE(VIDEO_TRACK)
#include "TrackBase.h"
#include "V8TextTrack.h"
#endif
#if ENABLE(SCRIPTED_SPEECH)
#include "SpeechRecognitionError.h"
#include "SpeechRecognitionResult.h"
#include "SpeechRecognitionResultList.h"
#include "V8SpeechRecognitionError.h"
#include "V8SpeechRecognitionResult.h"
#include "V8SpeechRecognitionResultList.h"
#endif
namespace WebCore {
Dictionary::Dictionary()
{
}
Dictionary::Dictionary(const v8::Local<v8::Value>& options)
: m_options(options)
{
}
Dictionary::~Dictionary()
{
}
Dictionary& Dictionary::operator=(const Dictionary& optionsObject)
{
m_options = optionsObject.m_options;
return *this;
}
bool Dictionary::isObject() const
{
return !isUndefinedOrNull() && m_options->IsObject();
}
bool Dictionary::isUndefinedOrNull() const
{
if (m_options.IsEmpty())
return true;
return WebCore::isUndefinedOrNull(m_options);
}
bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const
{
if (isUndefinedOrNull())
return false;
v8::Local<v8::Object> options = m_options->ToObject();
ASSERT(!options.IsEmpty());
v8::Handle<v8::String> v8Key = v8String(key);
if (!options->Has(v8Key))
return false;
value = options->Get(v8Key);
if (value.IsEmpty())
return false;
return true;
}
bool Dictionary::get(const String& key, bool& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean();
if (v8Bool.IsEmpty())
return false;
value = v8Bool->Value();
return true;
}
bool Dictionary::get(const String& key, int32_t& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
if (v8Int32.IsEmpty())
return false;
value = v8Int32->Value();
return true;
}
bool Dictionary::get(const String& key, double& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
v8::Local<v8::Number> v8Number = v8Value->ToNumber();
if (v8Number.IsEmpty())
return false;
value = v8Number->Value();
return true;
}
bool Dictionary::get(const String& key, String& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
value = v8ValueToWebCoreString(v8Value);
return true;
}
bool Dictionary::get(const String& key, ScriptValue& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
value = ScriptValue(v8Value);
return true;
}
bool Dictionary::get(const String& key, unsigned short& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
if (v8Int32.IsEmpty())
return false;
value = static_cast<unsigned short>(v8Int32->Value());
return true;
}
bool Dictionary::get(const String& key, short& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
if (v8Int32.IsEmpty())
return false;
value = static_cast<short>(v8Int32->Value());
return true;
}
bool Dictionary::get(const String& key, unsigned& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
if (v8Int32.IsEmpty())
return false;
value = static_cast<unsigned>(v8Int32->Value());
return true;
}
bool Dictionary::get(const String& key, unsigned long long& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
v8::Local<v8::Number> v8Number = v8Value->ToNumber();
if (v8Number.IsEmpty())
return false;
double d = v8Number->Value();
doubleToInteger(d, value);
return true;
}
bool Dictionary::get(const String& key, RefPtr<DOMWindow>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
DOMWindow* source = 0;
if (v8Value->IsObject()) {
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
v8::Handle<v8::Object> window = V8DOMWrapper::lookupDOMWrapper(V8DOMWindow::GetTemplate(), wrapper);
if (!window.IsEmpty())
source = V8DOMWindow::toNative(window);
}
value = source;
return true;
}
bool Dictionary::get(const String& key, RefPtr<Storage>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
Storage* source = 0;
if (v8Value->IsObject()) {
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
v8::Handle<v8::Object> storage = V8DOMWrapper::lookupDOMWrapper(V8Storage::GetTemplate(), wrapper);
if (!storage.IsEmpty())
source = V8Storage::toNative(storage);
}
value = source;
return true;
}
bool Dictionary::get(const String& key, MessagePortArray& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
return getMessagePortArray(v8Value, value);
}
bool Dictionary::get(const String& key, HashSet<AtomicString>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
if (!v8Value->IsArray())
return false;
v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
for (size_t i = 0; i < v8Array->Length(); ++i) {
v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(i));
value.add(v8ValueToWebCoreString(indexedValue));
}
return true;
}
bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value) || v8Value->IsNull() || v8Value->IsUndefined())
return false;
value = WebCore::isUndefinedOrNull(v8Value) ? String() : v8ValueToWebCoreString(v8Value);
return true;
}
bool Dictionary::get(const String& key, RefPtr<Uint8Array>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
Uint8Array* source = 0;
if (v8Value->IsObject()) {
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
v8::Handle<v8::Object> array = V8DOMWrapper::lookupDOMWrapper(V8Uint8Array::GetTemplate(), wrapper);
if (!array.IsEmpty())
source = V8Uint8Array::toNative(array);
}
value = source;
return true;
}
#if ENABLE(ENCRYPTED_MEDIA)
bool Dictionary::get(const String& key, RefPtr<MediaKeyError>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
MediaKeyError* source = 0;
if (v8Value->IsObject()) {
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
v8::Handle<v8::Object> error = V8DOMWrapper::lookupDOMWrapper(V8MediaKeyError::GetTemplate(), wrapper);
if (!error.IsEmpty())
source = V8MediaKeyError::toNative(error);
}
value = source;
return true;
}
#endif
#if ENABLE(VIDEO_TRACK)
bool Dictionary::get(const String& key, RefPtr<TrackBase>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
TrackBase* source = 0;
if (v8Value->IsObject()) {
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
v8::Handle<v8::Object> track = V8DOMWrapper::lookupDOMWrapper(V8TextTrack::GetTemplate(), wrapper);
if (!track.IsEmpty())
source = V8TextTrack::toNative(track);
}
value = source;
return true;
}
#endif
#if ENABLE(SCRIPTED_SPEECH)
bool Dictionary::get(const String& key, RefPtr<SpeechRecognitionError>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
SpeechRecognitionError* source = 0;
if (v8Value->IsObject()) {
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
v8::Handle<v8::Object> speechRecognitionError = V8DOMWrapper::lookupDOMWrapper(V8SpeechRecognitionError::GetTemplate(), wrapper);
if (!speechRecognitionError.IsEmpty())
source = V8SpeechRecognitionError::toNative(speechRecognitionError);
}
value = source;
return true;
}
bool Dictionary::get(const String& key, RefPtr<SpeechRecognitionResult>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
SpeechRecognitionResult* source = 0;
if (v8Value->IsObject()) {
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
v8::Handle<v8::Object> speechRecognitionResult = V8DOMWrapper::lookupDOMWrapper(V8SpeechRecognitionResult::GetTemplate(), wrapper);
if (!speechRecognitionResult.IsEmpty())
source = V8SpeechRecognitionResult::toNative(speechRecognitionResult);
}
value = source;
return true;
}
bool Dictionary::get(const String& key, RefPtr<SpeechRecognitionResultList>& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
SpeechRecognitionResultList* source = 0;
if (v8Value->IsObject()) {
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
v8::Handle<v8::Object> speechRecognitionResultList = V8DOMWrapper::lookupDOMWrapper(V8SpeechRecognitionResultList::GetTemplate(), wrapper);
if (!speechRecognitionResultList.IsEmpty())
source = V8SpeechRecognitionResultList::toNative(speechRecognitionResultList);
}
value = source;
return true;
}
#endif
bool Dictionary::get(const String& key, Dictionary& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
if (v8Value->IsObject())
value = Dictionary(v8Value);
return true;
}
bool Dictionary::getOwnPropertiesAsStringHashMap(WTF::HashMap<String, String>& hashMap) const
{
if (!isObject())
return false;
v8::Handle<v8::Object> options = m_options->ToObject();
if (options.IsEmpty())
return false;
v8::Local<v8::Array> properties = options->GetOwnPropertyNames();
if (properties.IsEmpty())
return true;
for (uint32_t i = 0; i < properties->Length(); ++i) {
v8::Local<v8::String> key = properties->Get(i)->ToString();
if (!options->Has(key))
continue;
v8::Local<v8::Value> value = options->Get(key);
String stringKey = v8ValueToWebCoreString(key);
String stringValue = v8ValueToWebCoreString(value);
if (!stringKey.isEmpty())
hashMap.set(stringKey, stringValue);
}
return true;
}
}