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 "Dictionary.h"
29
#include "ArrayValue.h"
30
#include "DOMStringList.h"
31
#include "V8Binding.h"
32
#include "V8DOMWindow.h"
33
#include "V8Storage.h"
34
#include "V8Uint8Array.h"
35
#include "V8Utilities.h"
36
#include <wtf/MathExtras.h>
38
#if ENABLE(INDEXED_DATABASE)
39
#include "IDBKeyRange.h"
40
#include "V8IDBKeyRange.h"
43
#if ENABLE(ENCRYPTED_MEDIA)
44
#include "V8MediaKeyError.h"
47
#if ENABLE(VIDEO_TRACK)
48
#include "TrackBase.h"
49
#include "V8TextTrack.h"
52
#if ENABLE(SCRIPTED_SPEECH)
53
#include "SpeechRecognitionError.h"
54
#include "SpeechRecognitionResult.h"
55
#include "SpeechRecognitionResultList.h"
56
#include "V8SpeechRecognitionError.h"
57
#include "V8SpeechRecognitionResult.h"
58
#include "V8SpeechRecognitionResultList.h"
63
Dictionary::Dictionary()
68
Dictionary::Dictionary(const v8::Local<v8::Value>& options, v8::Isolate* isolate)
75
Dictionary::~Dictionary()
79
Dictionary& Dictionary::operator=(const Dictionary& optionsObject)
81
m_options = optionsObject.m_options;
82
m_isolate = optionsObject.m_isolate;
86
bool Dictionary::isObject() const
88
return !isUndefinedOrNull() && m_options->IsObject();
91
bool Dictionary::isUndefinedOrNull() const
93
if (m_options.IsEmpty())
95
return WebCore::isUndefinedOrNull(m_options);
98
bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const
100
if (isUndefinedOrNull())
102
v8::Local<v8::Object> options = m_options->ToObject();
103
ASSERT(!options.IsEmpty());
105
v8::Handle<v8::String> v8Key = v8String(key);
106
if (!options->Has(v8Key))
108
value = options->Get(v8Key);
114
bool Dictionary::get(const String& key, bool& value) const
116
v8::Local<v8::Value> v8Value;
117
if (!getKey(key, v8Value))
120
v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean();
121
if (v8Bool.IsEmpty())
123
value = v8Bool->Value();
127
bool Dictionary::get(const String& key, int32_t& value) const
129
v8::Local<v8::Value> v8Value;
130
if (!getKey(key, v8Value))
133
v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
134
if (v8Int32.IsEmpty())
136
value = v8Int32->Value();
140
bool Dictionary::get(const String& key, double& value) const
142
v8::Local<v8::Value> v8Value;
143
if (!getKey(key, v8Value))
146
v8::Local<v8::Number> v8Number = v8Value->ToNumber();
147
if (v8Number.IsEmpty())
149
value = v8Number->Value();
153
bool Dictionary::get(const String& key, String& value) const
155
v8::Local<v8::Value> v8Value;
156
if (!getKey(key, v8Value))
159
// FIXME: It is possible for this to throw in which case we'd be getting back
160
// an empty string and returning true when we should be returning false.
161
// See fast/dom/Geolocation/script-tests/argument-types.js for a similar
163
value = toWebCoreString(v8Value);
167
bool Dictionary::get(const String& key, ScriptValue& value) const
169
v8::Local<v8::Value> v8Value;
170
if (!getKey(key, v8Value))
173
value = ScriptValue(v8Value);
177
bool Dictionary::get(const String& key, unsigned short& value) const
179
v8::Local<v8::Value> v8Value;
180
if (!getKey(key, v8Value))
183
v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
184
if (v8Int32.IsEmpty())
186
value = static_cast<unsigned short>(v8Int32->Value());
190
bool Dictionary::get(const String& key, short& value) const
192
v8::Local<v8::Value> v8Value;
193
if (!getKey(key, v8Value))
196
v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
197
if (v8Int32.IsEmpty())
199
value = static_cast<short>(v8Int32->Value());
203
bool Dictionary::get(const String& key, unsigned& value) const
205
v8::Local<v8::Value> v8Value;
206
if (!getKey(key, v8Value))
209
v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
210
if (v8Int32.IsEmpty())
212
value = static_cast<unsigned>(v8Int32->Value());
216
bool Dictionary::get(const String& key, unsigned long long& value) const
218
v8::Local<v8::Value> v8Value;
219
if (!getKey(key, v8Value))
222
v8::Local<v8::Number> v8Number = v8Value->ToNumber();
223
if (v8Number.IsEmpty())
225
double d = v8Number->Value();
226
doubleToInteger(d, value);
230
bool Dictionary::get(const String& key, RefPtr<DOMWindow>& value) const
232
v8::Local<v8::Value> v8Value;
233
if (!getKey(key, v8Value))
236
DOMWindow* source = 0;
237
if (v8Value->IsObject()) {
238
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
239
v8::Handle<v8::Object> window = V8DOMWrapper::lookupDOMWrapper(V8DOMWindow::GetTemplate(), wrapper);
240
if (!window.IsEmpty())
241
source = V8DOMWindow::toNative(window);
247
bool Dictionary::get(const String& key, RefPtr<Storage>& value) const
249
v8::Local<v8::Value> v8Value;
250
if (!getKey(key, v8Value))
254
if (v8Value->IsObject()) {
255
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
256
v8::Handle<v8::Object> storage = V8DOMWrapper::lookupDOMWrapper(V8Storage::GetTemplate(), wrapper);
257
if (!storage.IsEmpty())
258
source = V8Storage::toNative(storage);
264
bool Dictionary::get(const String& key, MessagePortArray& value) const
266
v8::Local<v8::Value> v8Value;
267
if (!getKey(key, v8Value))
271
ASSERT(m_isolate == v8::Isolate::GetCurrent());
272
return getMessagePortArray(v8Value, value, m_isolate);
275
bool Dictionary::get(const String& key, HashSet<AtomicString>& value) const
277
v8::Local<v8::Value> v8Value;
278
if (!getKey(key, v8Value))
281
// FIXME: Support array-like objects
282
if (!v8Value->IsArray())
285
v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
286
for (size_t i = 0; i < v8Array->Length(); ++i) {
287
v8::Local<v8::Value> indexedValue = v8Array->Get(v8Integer(i));
288
value.add(toWebCoreString(indexedValue));
294
bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) const
296
v8::Local<v8::Value> v8Value;
297
if (!getKey(key, v8Value) || v8Value->IsNull() || v8Value->IsUndefined())
300
// FIXME: It is possible for this to throw in which case we'd be getting back
301
// an empty string and returning true when we should be returning false.
302
// See fast/dom/Geolocation/script-tests/argument-types.js for a similar
304
value = WebCore::isUndefinedOrNull(v8Value) ? String() : toWebCoreString(v8Value);
308
bool Dictionary::get(const String& key, RefPtr<Uint8Array>& value) const
310
v8::Local<v8::Value> v8Value;
311
if (!getKey(key, v8Value))
314
Uint8Array* source = 0;
315
if (v8Value->IsObject()) {
316
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
318
v8::Handle<v8::Object> array = V8DOMWrapper::lookupDOMWrapper(V8Uint8Array::GetTemplate(), wrapper);
319
if (!array.IsEmpty())
320
source = V8Uint8Array::toNative(array);
326
#if ENABLE(ENCRYPTED_MEDIA)
327
bool Dictionary::get(const String& key, RefPtr<MediaKeyError>& value) const
329
v8::Local<v8::Value> v8Value;
330
if (!getKey(key, v8Value))
333
MediaKeyError* source = 0;
334
if (v8Value->IsObject()) {
335
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
337
v8::Handle<v8::Object> error = V8DOMWrapper::lookupDOMWrapper(V8MediaKeyError::GetTemplate(), wrapper);
338
if (!error.IsEmpty())
339
source = V8MediaKeyError::toNative(error);
346
#if ENABLE(VIDEO_TRACK)
347
bool Dictionary::get(const String& key, RefPtr<TrackBase>& value) const
349
v8::Local<v8::Value> v8Value;
350
if (!getKey(key, v8Value))
353
TrackBase* source = 0;
354
if (v8Value->IsObject()) {
355
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
357
// FIXME: this will need to be changed so it can also return an AudioTrack or a VideoTrack once
359
v8::Handle<v8::Object> track = V8DOMWrapper::lookupDOMWrapper(V8TextTrack::GetTemplate(), wrapper);
360
if (!track.IsEmpty())
361
source = V8TextTrack::toNative(track);
368
#if ENABLE(SCRIPTED_SPEECH)
369
bool Dictionary::get(const String& key, RefPtr<SpeechRecognitionError>& value) const
371
v8::Local<v8::Value> v8Value;
372
if (!getKey(key, v8Value))
375
SpeechRecognitionError* source = 0;
376
if (v8Value->IsObject()) {
377
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
378
v8::Handle<v8::Object> speechRecognitionError = V8DOMWrapper::lookupDOMWrapper(V8SpeechRecognitionError::GetTemplate(), wrapper);
379
if (!speechRecognitionError.IsEmpty())
380
source = V8SpeechRecognitionError::toNative(speechRecognitionError);
386
bool Dictionary::get(const String& key, RefPtr<SpeechRecognitionResult>& value) const
388
v8::Local<v8::Value> v8Value;
389
if (!getKey(key, v8Value))
392
SpeechRecognitionResult* source = 0;
393
if (v8Value->IsObject()) {
394
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
395
v8::Handle<v8::Object> speechRecognitionResult = V8DOMWrapper::lookupDOMWrapper(V8SpeechRecognitionResult::GetTemplate(), wrapper);
396
if (!speechRecognitionResult.IsEmpty())
397
source = V8SpeechRecognitionResult::toNative(speechRecognitionResult);
403
bool Dictionary::get(const String& key, RefPtr<SpeechRecognitionResultList>& value) const
405
v8::Local<v8::Value> v8Value;
406
if (!getKey(key, v8Value))
409
SpeechRecognitionResultList* source = 0;
410
if (v8Value->IsObject()) {
411
v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
412
v8::Handle<v8::Object> speechRecognitionResultList = V8DOMWrapper::lookupDOMWrapper(V8SpeechRecognitionResultList::GetTemplate(), wrapper);
413
if (!speechRecognitionResultList.IsEmpty())
414
source = V8SpeechRecognitionResultList::toNative(speechRecognitionResultList);
422
bool Dictionary::get(const String& key, Dictionary& value) const
424
v8::Local<v8::Value> v8Value;
425
if (!getKey(key, v8Value))
428
if (v8Value->IsObject()) {
430
ASSERT(m_isolate == v8::Isolate::GetCurrent());
431
value = Dictionary(v8Value, m_isolate);
437
bool Dictionary::get(const String& key, Vector<String>& value) const
439
v8::Local<v8::Value> v8Value;
440
if (!getKey(key, v8Value))
443
if (!v8Value->IsArray())
446
v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
447
for (size_t i = 0; i < v8Array->Length(); ++i) {
448
v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Uint32::New(i));
449
value.append(toWebCoreString(indexedValue));
455
bool Dictionary::get(const String& key, ArrayValue& value) const
457
v8::Local<v8::Value> v8Value;
458
if (!getKey(key, v8Value))
461
if (!v8Value->IsArray())
465
ASSERT(m_isolate == v8::Isolate::GetCurrent());
466
value = ArrayValue(v8::Local<v8::Array>::Cast(v8Value), m_isolate);
470
bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMap) const
475
v8::Handle<v8::Object> options = m_options->ToObject();
476
if (options.IsEmpty())
479
v8::Local<v8::Array> properties = options->GetOwnPropertyNames();
480
if (properties.IsEmpty())
482
for (uint32_t i = 0; i < properties->Length(); ++i) {
483
v8::Local<v8::String> key = properties->Get(i)->ToString();
484
if (!options->Has(key))
487
v8::Local<v8::Value> value = options->Get(key);
488
String stringKey = toWebCoreString(key);
489
String stringValue = toWebCoreString(value);
490
if (!stringKey.isEmpty())
491
hashMap.set(stringKey, stringValue);
497
bool Dictionary::getOwnPropertyNames(Vector<String>& names) const
502
v8::Handle<v8::Object> options = m_options->ToObject();
503
if (options.IsEmpty())
506
v8::Local<v8::Array> properties = options->GetOwnPropertyNames();
507
if (properties.IsEmpty())
509
for (uint32_t i = 0; i < properties->Length(); ++i) {
510
v8::Local<v8::String> key = properties->Get(i)->ToString();
511
if (!options->Has(key))
513
names.append(toWebCoreString(key));
519
} // namespace WebCore