1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
/*
* Copyright (C) 2011 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "bindings/modules/v8/IDBBindingUtilities.h"
#include "bindings/core/v8/SerializedScriptValue.h"
#include "bindings/core/v8/SerializedScriptValueFactory.h"
#include "bindings/core/v8/V8ArrayBufferView.h"
#include "bindings/core/v8/V8Binding.h"
#include "bindings/core/v8/V8DOMStringList.h"
#include "bindings/core/v8/V8HiddenValue.h"
#include "bindings/core/v8/V8Uint8Array.h"
#include "bindings/modules/v8/V8IDBCursor.h"
#include "bindings/modules/v8/V8IDBCursorWithValue.h"
#include "bindings/modules/v8/V8IDBDatabase.h"
#include "bindings/modules/v8/V8IDBIndex.h"
#include "bindings/modules/v8/V8IDBKeyRange.h"
#include "bindings/modules/v8/V8IDBObjectStore.h"
#include "bindings/modules/v8/V8IDBRequest.h"
#include "modules/indexeddb/IDBKey.h"
#include "modules/indexeddb/IDBKeyPath.h"
#include "modules/indexeddb/IDBKeyRange.h"
#include "modules/indexeddb/IDBTracing.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "platform/SharedBuffer.h"
#include "wtf/MathExtras.h"
#include "wtf/Vector.h"
namespace blink {
static v8::Local<v8::Value> deserializeIDBValueBuffer(v8::Isolate*, SharedBuffer*, const Vector<blink::WebBlobInfo>*);
static v8::Local<v8::Value> toV8(const IDBKeyPath& value, v8::Local<v8::Object> creationContext, v8::Isolate* isolate)
{
switch (value.type()) {
case IDBKeyPath::NullType:
return v8::Null(isolate);
case IDBKeyPath::StringType:
return v8String(isolate, value.string());
case IDBKeyPath::ArrayType:
RefPtrWillBeRawPtr<DOMStringList> keyPaths = DOMStringList::create();
for (Vector<String>::const_iterator it = value.array().begin(); it != value.array().end(); ++it)
keyPaths->append(*it);
return toV8(keyPaths.release(), creationContext, isolate);
}
ASSERT_NOT_REACHED();
return v8::Undefined(isolate);
}
static v8::Local<v8::Value> toV8(const IDBKey* key, v8::Local<v8::Object> creationContext, v8::Isolate* isolate)
{
if (!key) {
// This should be undefined, not null.
// Spec: http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#idl-def-IDBKeyRange
return v8Undefined();
}
switch (key->type()) {
case IDBKey::InvalidType:
case IDBKey::MinType:
ASSERT_NOT_REACHED();
return v8Undefined();
case IDBKey::NumberType:
return v8::Number::New(isolate, key->number());
case IDBKey::StringType:
return v8String(isolate, key->string());
case IDBKey::BinaryType:
return toV8(DOMUint8Array::create(reinterpret_cast<const unsigned char*>(key->binary()->data()), key->binary()->size()), creationContext, isolate);
case IDBKey::DateType:
return v8::Date::New(isolate, key->date());
case IDBKey::ArrayType:
{
v8::Local<v8::Array> array = v8::Array::New(isolate, key->array().size());
for (size_t i = 0; i < key->array().size(); ++i)
array->Set(i, toV8(key->array()[i].get(), creationContext, isolate));
return array;
}
}
ASSERT_NOT_REACHED();
return v8Undefined();
}
static v8::Local<v8::Value> toV8(const IDBAny* impl, v8::Local<v8::Object> creationContext, v8::Isolate* isolate)
{
if (!impl)
return v8::Null(isolate);
switch (impl->type()) {
case IDBAny::UndefinedType:
return v8::Undefined(isolate);
case IDBAny::NullType:
return v8::Null(isolate);
case IDBAny::DOMStringListType:
return toV8(impl->domStringList(), creationContext, isolate);
case IDBAny::IDBCursorType: {
// Ensure request wrapper is kept alive at least as long as the cursor wrapper,
// so that event listeners are retained.
v8::Local<v8::Value> cursor = toV8(impl->idbCursor(), creationContext, isolate);
v8::Local<v8::Value> request = toV8(impl->idbCursor()->request(), creationContext, isolate);
// FIXME: Due to race at worker shutdown, V8 may return empty handles.
if (!cursor.IsEmpty())
V8HiddenValue::setHiddenValue(isolate, cursor->ToObject(isolate), V8HiddenValue::idbCursorRequest(isolate), request);
return cursor;
}
case IDBAny::IDBCursorWithValueType: {
// Ensure request wrapper is kept alive at least as long as the cursor wrapper,
// so that event listeners are retained.
v8::Local<v8::Value> cursor = toV8(impl->idbCursorWithValue(), creationContext, isolate);
v8::Local<v8::Value> request = toV8(impl->idbCursorWithValue()->request(), creationContext, isolate);
// FIXME: Due to race at worker shutdown, V8 may return empty handles.
if (!cursor.IsEmpty())
V8HiddenValue::setHiddenValue(isolate, cursor->ToObject(isolate), V8HiddenValue::idbCursorRequest(isolate), request);
return cursor;
}
case IDBAny::IDBDatabaseType:
return toV8(impl->idbDatabase(), creationContext, isolate);
case IDBAny::IDBIndexType:
return toV8(impl->idbIndex(), creationContext, isolate);
case IDBAny::IDBObjectStoreType:
return toV8(impl->idbObjectStore(), creationContext, isolate);
case IDBAny::BufferType:
return deserializeIDBValueBuffer(isolate, impl->buffer(), impl->blobInfo());
case IDBAny::IntegerType:
return v8::Number::New(isolate, impl->integer());
case IDBAny::KeyType:
return toV8(impl->key(), creationContext, isolate);
case IDBAny::KeyPathType:
return toV8(impl->keyPath(), creationContext, isolate);
case IDBAny::BufferKeyAndKeyPathType: {
v8::Local<v8::Value> value = deserializeIDBValueBuffer(isolate, impl->buffer(), impl->blobInfo());
v8::Local<v8::Value> key = toV8(impl->key(), creationContext, isolate);
bool injected = injectV8KeyIntoV8Value(isolate, key, value, impl->keyPath());
ASSERT_UNUSED(injected, injected);
return value;
}
}
ASSERT_NOT_REACHED();
return v8::Undefined(isolate);
}
static const size_t maximumDepth = 2000;
static IDBKey* createIDBKeyFromValue(v8::Isolate* isolate, v8::Local<v8::Value> value, Vector<v8::Local<v8::Array> >& stack, bool allowExperimentalTypes = false)
{
if (value->IsNumber() && !std::isnan(value->NumberValue()))
return IDBKey::createNumber(value->NumberValue());
if (value->IsString())
return IDBKey::createString(toCoreString(value.As<v8::String>()));
if (value->IsDate() && !std::isnan(value->NumberValue()))
return IDBKey::createDate(value->NumberValue());
if (value->IsUint8Array() && (allowExperimentalTypes || RuntimeEnabledFeatures::indexedDBExperimentalEnabled())) {
// Per discussion in https://www.w3.org/Bugs/Public/show_bug.cgi?id=23332 the
// input type is constrained to Uint8Array to match the output type.
DOMArrayBufferView* view = blink::V8ArrayBufferView::toImpl(value->ToObject(isolate));
const char* start = static_cast<const char*>(view->baseAddress());
size_t length = view->byteLength();
return IDBKey::createBinary(SharedBuffer::create(start, length));
}
if (value->IsArray()) {
v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(value);
if (stack.contains(array))
return 0;
if (stack.size() >= maximumDepth)
return 0;
stack.append(array);
IDBKey::KeyArray subkeys;
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; ++i) {
v8::Local<v8::Value> item = array->Get(v8::Int32::New(isolate, i));
IDBKey* subkey = createIDBKeyFromValue(isolate, item, stack, allowExperimentalTypes);
if (!subkey)
subkeys.append(IDBKey::createInvalid());
else
subkeys.append(subkey);
}
stack.removeLast();
return IDBKey::createArray(subkeys);
}
return 0;
}
static IDBKey* createIDBKeyFromValue(v8::Isolate* isolate, v8::Local<v8::Value> value, bool allowExperimentalTypes = false)
{
Vector<v8::Local<v8::Array> > stack;
if (IDBKey* key = createIDBKeyFromValue(isolate, value, stack, allowExperimentalTypes))
return key;
return IDBKey::createInvalid();
}
template<typename T>
static bool getValueFrom(T indexOrName, v8::Local<v8::Value>& v8Value, v8::Isolate* isolate)
{
v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
if (!object->Has(indexOrName))
return false;
v8Value = object->Get(indexOrName);
return true;
}
template<typename T>
static bool setValue(v8::Local<v8::Value>& v8Object, T indexOrName, const v8::Local<v8::Value>& v8Value)
{
v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Object);
return object->Set(indexOrName, v8Value);
}
static bool get(v8::Isolate* isolate, v8::Local<v8::Value>& object, const String& keyPathElement, v8::Local<v8::Value>& result)
{
if (object->IsString() && keyPathElement == "length") {
int32_t length = v8::Local<v8::String>::Cast(object)->Length();
result = v8::Number::New(isolate, length);
return true;
}
return object->IsObject() && getValueFrom(v8String(isolate, keyPathElement), result, isolate);
}
static bool canSet(v8::Local<v8::Value>& object, const String& keyPathElement)
{
return object->IsObject();
}
static bool set(v8::Isolate* isolate, v8::Local<v8::Value>& object, const String& keyPathElement, const v8::Local<v8::Value>& v8Value)
{
return canSet(object, keyPathElement) && setValue(object, v8String(isolate, keyPathElement), v8Value);
}
static v8::Local<v8::Value> getNthValueOnKeyPath(v8::Isolate* isolate, v8::Local<v8::Value>& rootValue, const Vector<String>& keyPathElements, size_t index)
{
v8::Local<v8::Value> currentValue(rootValue);
ASSERT(index <= keyPathElements.size());
for (size_t i = 0; i < index; ++i) {
v8::Local<v8::Value> parentValue(currentValue);
if (!get(isolate, parentValue, keyPathElements[i], currentValue))
return v8Undefined();
}
return currentValue;
}
static bool canInjectNthValueOnKeyPath(v8::Isolate* isolate, v8::Local<v8::Value>& rootValue, const Vector<String>& keyPathElements, size_t index)
{
if (!rootValue->IsObject())
return false;
v8::Local<v8::Value> currentValue(rootValue);
ASSERT(index <= keyPathElements.size());
for (size_t i = 0; i < index; ++i) {
v8::Local<v8::Value> parentValue(currentValue);
const String& keyPathElement = keyPathElements[i];
if (!get(isolate, parentValue, keyPathElement, currentValue))
return canSet(parentValue, keyPathElement);
}
return true;
}
static v8::Local<v8::Value> ensureNthValueOnKeyPath(v8::Isolate* isolate, v8::Local<v8::Value>& rootValue, const Vector<String>& keyPathElements, size_t index)
{
v8::Local<v8::Value> currentValue(rootValue);
ASSERT(index <= keyPathElements.size());
for (size_t i = 0; i < index; ++i) {
v8::Local<v8::Value> parentValue(currentValue);
const String& keyPathElement = keyPathElements[i];
if (!get(isolate, parentValue, keyPathElement, currentValue)) {
v8::Local<v8::Object> object = v8::Object::New(isolate);
if (!set(isolate, parentValue, keyPathElement, object))
return v8Undefined();
currentValue = object;
}
}
return currentValue;
}
static IDBKey* createIDBKeyFromScriptValueAndKeyPathInternal(v8::Isolate* isolate, const ScriptValue& value, const String& keyPath, bool allowExperimentalTypes)
{
Vector<String> keyPathElements;
IDBKeyPathParseError error;
IDBParseKeyPath(keyPath, keyPathElements, error);
ASSERT(error == IDBKeyPathParseErrorNone);
ASSERT(isolate->InContext());
v8::HandleScope handleScope(isolate);
v8::Local<v8::Value> v8Value(value.v8Value());
v8::Local<v8::Value> v8Key(getNthValueOnKeyPath(isolate, v8Value, keyPathElements, keyPathElements.size()));
if (v8Key.IsEmpty())
return 0;
return createIDBKeyFromValue(isolate, v8Key, allowExperimentalTypes);
}
static IDBKey* createIDBKeyFromScriptValueAndKeyPathInternal(v8::Isolate* isolate, const ScriptValue& value, const IDBKeyPath& keyPath, bool allowExperimentalTypes = false)
{
ASSERT(!keyPath.isNull());
v8::HandleScope handleScope(isolate);
if (keyPath.type() == IDBKeyPath::ArrayType) {
IDBKey::KeyArray result;
const Vector<String>& array = keyPath.array();
for (size_t i = 0; i < array.size(); ++i) {
IDBKey* key = createIDBKeyFromScriptValueAndKeyPathInternal(isolate, value, array[i], allowExperimentalTypes);
if (!key)
return 0;
result.append(key);
}
return IDBKey::createArray(result);
}
ASSERT(keyPath.type() == IDBKeyPath::StringType);
return createIDBKeyFromScriptValueAndKeyPathInternal(isolate, value, keyPath.string(), allowExperimentalTypes);
}
IDBKey* createIDBKeyFromScriptValueAndKeyPath(v8::Isolate* isolate, const ScriptValue& value, const IDBKeyPath& keyPath)
{
IDB_TRACE("createIDBKeyFromScriptValueAndKeyPath");
return createIDBKeyFromScriptValueAndKeyPathInternal(isolate, value, keyPath);
}
static v8::Local<v8::Value> deserializeIDBValueBuffer(v8::Isolate* isolate, SharedBuffer* buffer, const Vector<blink::WebBlobInfo>* blobInfo)
{
ASSERT(isolate->InContext());
if (!buffer)
return v8::Null(isolate);
// FIXME: The extra copy here can be eliminated by allowing SerializedScriptValue to take a raw const char* or const uint8_t*.
Vector<uint8_t> value;
value.append(buffer->data(), buffer->size());
RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValueFactory::instance().createFromWireBytes(value);
return serializedValue->deserialize(isolate, 0, blobInfo);
}
bool injectV8KeyIntoV8Value(v8::Isolate* isolate, v8::Local<v8::Value> key, v8::Local<v8::Value> value, const IDBKeyPath& keyPath)
{
IDB_TRACE("injectIDBV8KeyIntoV8Value");
ASSERT(isolate->InContext());
ASSERT(keyPath.type() == IDBKeyPath::StringType);
Vector<String> keyPathElements;
IDBKeyPathParseError error;
IDBParseKeyPath(keyPath.string(), keyPathElements, error);
ASSERT(error == IDBKeyPathParseErrorNone);
if (!keyPathElements.size())
return false;
v8::HandleScope handleScope(isolate);
v8::Local<v8::Value> parent(ensureNthValueOnKeyPath(isolate, value, keyPathElements, keyPathElements.size() - 1));
if (parent.IsEmpty())
return false;
if (!set(isolate, parent, keyPathElements.last(), key))
return false;
return true;
}
bool canInjectIDBKeyIntoScriptValue(v8::Isolate* isolate, const ScriptValue& scriptValue, const IDBKeyPath& keyPath)
{
IDB_TRACE("canInjectIDBKeyIntoScriptValue");
ASSERT(keyPath.type() == IDBKeyPath::StringType);
Vector<String> keyPathElements;
IDBKeyPathParseError error;
IDBParseKeyPath(keyPath.string(), keyPathElements, error);
ASSERT(error == IDBKeyPathParseErrorNone);
if (!keyPathElements.size())
return false;
v8::Local<v8::Value> v8Value(scriptValue.v8Value());
return canInjectNthValueOnKeyPath(isolate, v8Value, keyPathElements, keyPathElements.size() - 1);
}
ScriptValue idbAnyToScriptValue(ScriptState* scriptState, IDBAny* any)
{
v8::Isolate* isolate = scriptState->isolate();
v8::HandleScope handleScope(isolate);
v8::Local<v8::Value> v8Value(toV8(any, scriptState->context()->Global(), isolate));
return ScriptValue(scriptState, v8Value);
}
ScriptValue idbKeyToScriptValue(ScriptState* scriptState, IDBKey* key)
{
v8::Isolate* isolate = scriptState->isolate();
v8::HandleScope handleScope(isolate);
v8::Local<v8::Value> v8Value(toV8(key, scriptState->context()->Global(), isolate));
return ScriptValue(scriptState, v8Value);
}
IDBKey* scriptValueToIDBKey(v8::Isolate* isolate, const ScriptValue& scriptValue)
{
ASSERT(isolate->InContext());
v8::HandleScope handleScope(isolate);
v8::Local<v8::Value> v8Value(scriptValue.v8Value());
return createIDBKeyFromValue(isolate, v8Value);
}
IDBKeyRange* scriptValueToIDBKeyRange(v8::Isolate* isolate, const ScriptValue& scriptValue)
{
v8::HandleScope handleScope(isolate);
v8::Local<v8::Value> value(scriptValue.v8Value());
return V8IDBKeyRange::toImplWithTypeCheck(isolate, value);
}
ScriptValue deserializeScriptValue(ScriptState* scriptState, SerializedScriptValue* serializedValue, const Vector<blink::WebBlobInfo>* blobInfo)
{
v8::Isolate* isolate = scriptState->isolate();
v8::HandleScope handleScope(isolate);
if (serializedValue)
return ScriptValue(scriptState, serializedValue->deserialize(isolate, 0, blobInfo));
return ScriptValue(scriptState, v8::Null(isolate));
}
#if ENABLE(ASSERT)
void assertPrimaryKeyValidOrInjectable(ScriptState* scriptState, PassRefPtr<SharedBuffer> buffer, const Vector<blink::WebBlobInfo>* blobInfo, IDBKey* key, const IDBKeyPath& keyPath)
{
ScriptState::Scope scope(scriptState);
v8::Isolate* isolate = scriptState->isolate();
ScriptValue keyValue = idbKeyToScriptValue(scriptState, key);
ScriptValue scriptValue(scriptState, deserializeIDBValueBuffer(isolate, buffer.get(), blobInfo));
// This assertion is about already persisted data, so allow experimental types.
const bool allowExperimentalTypes = true;
IDBKey* expectedKey = createIDBKeyFromScriptValueAndKeyPathInternal(isolate, scriptValue, keyPath, allowExperimentalTypes);
ASSERT(!expectedKey || expectedKey->isEqual(key));
bool injected = injectV8KeyIntoV8Value(isolate, keyValue.v8Value(), scriptValue.v8Value(), keyPath);
ASSERT_UNUSED(injected, injected);
}
#endif
} // namespace blink
|