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 "IDBBindingUtilities.h"
29 #if ENABLE(INDEXED_DATABASE)
31 #include "IDBDatabaseException.h"
33 #include "IDBKeyPath.h"
34 #include "SerializedScriptValue.h"
35 #include "V8Binding.h"
37 #include <wtf/Vector.h>
41 PassRefPtr<IDBKey> createIDBKeyFromValue(v8::Handle<v8::Value> value)
44 return IDBKey::createNull();
45 if (value->IsNumber())
46 return IDBKey::createNumber(value->NumberValue());
47 if (value->IsString())
48 return IDBKey::createString(v8ValueToWebCoreString(value));
50 return IDBKey::createDate(value->NumberValue());
52 return 0; // Signals type error.
58 bool getValueFrom(T indexOrName, v8::Handle<v8::Value>& v8Value)
60 v8::Local<v8::Object> object = v8Value->ToObject();
61 if (!object->Has(indexOrName))
63 v8Value = object->Get(indexOrName);
68 bool setValue(v8::Handle<v8::Value>& v8Object, T indexOrName, const v8::Handle<v8::Value>& v8Value)
70 v8::Local<v8::Object> object = v8Object->ToObject();
71 ASSERT(!object->Has(indexOrName));
72 return object->Set(indexOrName, v8Value);
75 bool get(v8::Handle<v8::Value>& object, const String& keyPathElement)
77 return object->IsObject() && getValueFrom(v8String(keyPathElement), object);
80 bool set(v8::Handle<v8::Value>& object, const String& keyPathElement, const v8::Handle<v8::Value>& v8Value)
82 return object->IsObject() && setValue(object, v8String(keyPathElement), v8Value);
85 v8::Handle<v8::Value> getNthValueOnKeyPath(v8::Handle<v8::Value>& rootValue, const Vector<String>& keyPathElements, size_t index)
87 v8::Handle<v8::Value> currentValue(rootValue);
89 ASSERT(index <= keyPathElements.size());
90 for (size_t i = 0; i < index; ++i) {
91 if (!get(currentValue, keyPathElements[i]))
92 return v8::Handle<v8::Value>();
98 } // anonymous namespace
100 PassRefPtr<IDBKey> createIDBKeyFromSerializedValueAndKeyPath(PassRefPtr<SerializedScriptValue> value, const Vector<String>& keyPath)
102 V8LocalContext localContext;
103 v8::Handle<v8::Value> v8Value(value->deserialize());
104 v8::Handle<v8::Value> v8Key(getNthValueOnKeyPath(v8Value, keyPath, keyPath.size()));
107 return createIDBKeyFromValue(v8Key);
110 PassRefPtr<SerializedScriptValue> injectIDBKeyIntoSerializedValue(PassRefPtr<IDBKey> key, PassRefPtr<SerializedScriptValue> value, const Vector<String>& keyPath)
112 V8LocalContext localContext;
116 v8::Handle<v8::Value> v8Value(value->deserialize());
117 v8::Handle<v8::Value> parent(getNthValueOnKeyPath(v8Value, keyPath, keyPath.size() - 1));
118 if (parent.IsEmpty())
121 if (!set(parent, keyPath.last(), toV8(key.get())))
124 return SerializedScriptValue::create(v8Value);
127 } // namespace WebCore