initial import
[vuplus_webkit] / Source / WebCore / bindings / v8 / IDBBindingUtilities.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
13  *
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.
24  */
25
26 #include "config.h"
27 #include "IDBBindingUtilities.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "IDBDatabaseException.h"
32 #include "IDBKey.h"
33 #include "IDBKeyPath.h"
34 #include "SerializedScriptValue.h"
35 #include "V8Binding.h"
36 #include "V8IDBKey.h"
37 #include <wtf/Vector.h>
38
39 namespace WebCore {
40
41 PassRefPtr<IDBKey> createIDBKeyFromValue(v8::Handle<v8::Value> value)
42 {
43     if (value->IsNull())
44         return IDBKey::createNull();
45     if (value->IsNumber())
46         return IDBKey::createNumber(value->NumberValue());
47     if (value->IsString())
48         return IDBKey::createString(v8ValueToWebCoreString(value));
49     if (value->IsDate())
50         return IDBKey::createDate(value->NumberValue());
51
52     return 0; // Signals type error.
53 }
54
55 namespace {
56
57 template<typename T>
58 bool getValueFrom(T indexOrName, v8::Handle<v8::Value>& v8Value)
59 {
60     v8::Local<v8::Object> object = v8Value->ToObject();
61     if (!object->Has(indexOrName))
62         return false;
63     v8Value = object->Get(indexOrName);
64     return true;
65 }
66
67 template<typename T>
68 bool setValue(v8::Handle<v8::Value>& v8Object, T indexOrName, const v8::Handle<v8::Value>& v8Value)
69 {
70     v8::Local<v8::Object> object = v8Object->ToObject();
71     ASSERT(!object->Has(indexOrName));
72     return object->Set(indexOrName, v8Value);
73 }
74
75 bool get(v8::Handle<v8::Value>& object, const String& keyPathElement)
76 {
77     return object->IsObject() && getValueFrom(v8String(keyPathElement), object);
78 }
79
80 bool set(v8::Handle<v8::Value>& object, const String& keyPathElement, const v8::Handle<v8::Value>& v8Value)
81 {
82     return object->IsObject() && setValue(object, v8String(keyPathElement), v8Value);
83 }
84
85 v8::Handle<v8::Value> getNthValueOnKeyPath(v8::Handle<v8::Value>& rootValue, const Vector<String>& keyPathElements, size_t index)
86 {
87     v8::Handle<v8::Value> currentValue(rootValue);
88
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>();
93     }
94
95     return currentValue;
96 }
97
98 } // anonymous namespace
99
100 PassRefPtr<IDBKey> createIDBKeyFromSerializedValueAndKeyPath(PassRefPtr<SerializedScriptValue> value, const Vector<String>& keyPath)
101 {
102     V8LocalContext localContext;
103     v8::Handle<v8::Value> v8Value(value->deserialize());
104     v8::Handle<v8::Value> v8Key(getNthValueOnKeyPath(v8Value, keyPath, keyPath.size()));
105     if (v8Key.IsEmpty())
106         return 0;
107     return createIDBKeyFromValue(v8Key);
108 }
109
110 PassRefPtr<SerializedScriptValue> injectIDBKeyIntoSerializedValue(PassRefPtr<IDBKey> key, PassRefPtr<SerializedScriptValue> value, const Vector<String>& keyPath)
111 {
112     V8LocalContext localContext;
113     if (!keyPath.size())
114         return 0;
115
116     v8::Handle<v8::Value> v8Value(value->deserialize());
117     v8::Handle<v8::Value> parent(getNthValueOnKeyPath(v8Value, keyPath, keyPath.size() - 1));
118     if (parent.IsEmpty())
119         return 0;
120
121     if (!set(parent, keyPath.last(), toV8(key.get())))
122         return 0;
123
124     return SerializedScriptValue::create(v8Value);
125 }
126
127 } // namespace WebCore
128
129 #endif