blob: 589b20b91406e83ccc16896183f413b68e14c3e8 [file] [log] [blame]
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +00001/*
2 * Copyright (C) 2011 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010029#include "public/platform/WebIDBKey.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000030
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010031#include "modules/indexeddb/IDBKey.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000032
33using namespace WebCore;
34
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000035namespace blink {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000036
37WebIDBKey WebIDBKey::createArray(const WebVector<WebIDBKey>& array)
38{
39 WebIDBKey key;
40 key.assignArray(array);
41 return key;
42}
43
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000044WebIDBKey WebIDBKey::createBinary(const WebData& binary)
45{
46 WebIDBKey key;
47 key.assignBinary(binary);
48 return key;
49}
50
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000051WebIDBKey WebIDBKey::createString(const WebString& string)
52{
53 WebIDBKey key;
54 key.assignString(string);
55 return key;
56}
57
58WebIDBKey WebIDBKey::createDate(double date)
59{
60 WebIDBKey key;
61 key.assignDate(date);
62 return key;
63}
64
65WebIDBKey WebIDBKey::createNumber(double number)
66{
67 WebIDBKey key;
68 key.assignNumber(number);
69 return key;
70}
71
72WebIDBKey WebIDBKey::createInvalid()
73{
74 WebIDBKey key;
75 key.assignInvalid();
76 return key;
77}
78
79WebIDBKey WebIDBKey::createNull()
80{
81 WebIDBKey key;
82 key.assignNull();
83 return key;
84}
85
86void WebIDBKey::assign(const WebIDBKey& value)
87{
88 m_private = value.m_private;
89}
90
91static PassRefPtr<IDBKey> convertFromWebIDBKeyArray(const WebVector<WebIDBKey>& array)
92{
93 IDBKey::KeyArray keys;
94 keys.reserveCapacity(array.size());
95 for (size_t i = 0; i < array.size(); ++i) {
Ben Murdoch0019e4e2013-07-18 11:57:54 +010096 switch (array[i].keyType()) {
97 case WebIDBKeyTypeArray:
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000098 keys.append(convertFromWebIDBKeyArray(array[i].array()));
99 break;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000100 case WebIDBKeyTypeBinary:
101 keys.append(IDBKey::createBinary(array[i].binary()));
102 break;
Ben Murdoch0019e4e2013-07-18 11:57:54 +0100103 case WebIDBKeyTypeString:
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000104 keys.append(IDBKey::createString(array[i].string()));
105 break;
Ben Murdoch0019e4e2013-07-18 11:57:54 +0100106 case WebIDBKeyTypeDate:
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000107 keys.append(IDBKey::createDate(array[i].date()));
108 break;
Ben Murdoch0019e4e2013-07-18 11:57:54 +0100109 case WebIDBKeyTypeNumber:
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000110 keys.append(IDBKey::createNumber(array[i].number()));
111 break;
Ben Murdoch0019e4e2013-07-18 11:57:54 +0100112 case WebIDBKeyTypeInvalid:
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000113 keys.append(IDBKey::createInvalid());
114 break;
Ben Murdoch0019e4e2013-07-18 11:57:54 +0100115 case WebIDBKeyTypeNull:
116 case WebIDBKeyTypeMin:
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000117 ASSERT_NOT_REACHED();
118 break;
119 }
120 }
121 return IDBKey::createArray(keys);
122}
123
124static void convertToWebIDBKeyArray(const IDBKey::KeyArray& array, WebVector<WebIDBKey>& result)
125{
126 WebVector<WebIDBKey> keys(array.size());
127 WebVector<WebIDBKey> subkeys;
128 for (size_t i = 0; i < array.size(); ++i) {
129 RefPtr<IDBKey> key = array[i];
130 switch (key->type()) {
131 case IDBKey::ArrayType:
132 convertToWebIDBKeyArray(key->array(), subkeys);
133 keys[i] = WebIDBKey::createArray(subkeys);
134 break;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000135 case IDBKey::BinaryType:
136 keys[i] = WebIDBKey::createBinary(key->binary());
137 break;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000138 case IDBKey::StringType:
139 keys[i] = WebIDBKey::createString(key->string());
140 break;
141 case IDBKey::DateType:
142 keys[i] = WebIDBKey::createDate(key->date());
143 break;
144 case IDBKey::NumberType:
145 keys[i] = WebIDBKey::createNumber(key->number());
146 break;
147 case IDBKey::InvalidType:
148 keys[i] = WebIDBKey::createInvalid();
149 break;
150 case IDBKey::MinType:
151 ASSERT_NOT_REACHED();
152 break;
153 }
154 }
155 result.swap(keys);
156}
157
158void WebIDBKey::assignArray(const WebVector<WebIDBKey>& array)
159{
160 m_private = convertFromWebIDBKeyArray(array);
161}
162
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000163void WebIDBKey::assignBinary(const WebData& binary)
164{
165 m_private = IDBKey::createBinary(binary);
166}
167
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000168void WebIDBKey::assignString(const WebString& string)
169{
170 m_private = IDBKey::createString(string);
171}
172
173void WebIDBKey::assignDate(double date)
174{
175 m_private = IDBKey::createDate(date);
176}
177
178void WebIDBKey::assignNumber(double number)
179{
180 m_private = IDBKey::createNumber(number);
181}
182
183void WebIDBKey::assignInvalid()
184{
185 m_private = IDBKey::createInvalid();
186}
187
188void WebIDBKey::assignNull()
189{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000190 m_private.reset();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000191}
192
193void WebIDBKey::reset()
194{
195 m_private.reset();
196}
197
Ben Murdoch0019e4e2013-07-18 11:57:54 +0100198WebIDBKeyType WebIDBKey::keyType() const
199{
200 if (!m_private.get())
201 return WebIDBKeyTypeNull;
202 return static_cast<WebIDBKeyType>(m_private->type());
203}
204
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000205bool WebIDBKey::isValid() const
206{
207 if (!m_private.get())
208 return false;
209 return m_private->isValid();
210}
211
212WebVector<WebIDBKey> WebIDBKey::array() const
213{
214 WebVector<WebIDBKey> keys;
215 convertToWebIDBKeyArray(m_private->array(), keys);
216 return keys;
217}
218
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000219WebData WebIDBKey::binary() const
220{
221 return m_private->binary();
222}
223
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000224WebString WebIDBKey::string() const
225{
226 return m_private->string();
227}
228
229double WebIDBKey::date() const
230{
231 return m_private->date();
232}
233
234double WebIDBKey::number() const
235{
236 return m_private->number();
237}
238
239WebIDBKey::WebIDBKey(const PassRefPtr<IDBKey>& value)
240 : m_private(value)
241{
242}
243
244WebIDBKey& WebIDBKey::operator=(const PassRefPtr<IDBKey>& value)
245{
246 m_private = value;
247 return *this;
248}
249
250WebIDBKey::operator PassRefPtr<IDBKey>() const
251{
252 return m_private.get();
253}
254
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000255} // namespace blink