#include "config.h"
#include "OptionsObject.h"
#include "DOMStringList.h"
#include "V8Binding.h"
#include <limits>
#if ENABLE(INDEXED_DATABASE)
#include "IDBKeyRange.h"
#include "V8IDBKeyRange.h"
#endif
namespace WebCore {
OptionsObject::OptionsObject()
{
}
OptionsObject::OptionsObject(const v8::Local<v8::Value>& options)
: m_options(options)
{
}
OptionsObject::~OptionsObject()
{
}
OptionsObject& OptionsObject::operator=(const OptionsObject& optionsObject)
{
m_options = optionsObject.m_options;
return *this;
}
bool OptionsObject::isUndefinedOrNull() const
{
if (m_options.IsEmpty())
return true;
return WebCore::isUndefinedOrNull(m_options);
}
bool OptionsObject::getKeyBool(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 OptionsObject::getKeyInt32(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 OptionsObject::getKeyString(const String& key, String& value) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return false;
value = v8ValueToWebCoreString(v8Value);
return true;
}
PassRefPtr<DOMStringList> OptionsObject::getKeyDOMStringList(const String& key) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return 0;
if (!v8Value->IsArray())
return 0;
RefPtr<DOMStringList> ret = DOMStringList::create();
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));
ret->append(v8ValueToWebCoreString(indexedValue));
}
return ret.release();
}
#if ENABLE(INDEXED_DATABASE)
PassRefPtr<IDBKeyRange> OptionsObject::getKeyKeyRange(const String& key) const
{
v8::Local<v8::Value> v8Value;
if (!getKey(key, v8Value))
return 0;
if (!V8IDBKeyRange::HasInstance(v8Value))
return 0;
return V8IDBKeyRange::toNative(v8::Handle<v8::Object>::Cast(v8Value));
}
#endif
bool OptionsObject::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 !value->IsUndefined(); }
}