Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / WebIDBKey.cpp
blob4a9b8fac60d2e9d18174a8c6e92cf530245a5862
1 /*
2 * Copyright (C) 2011 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 are
6 * met:
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.
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.
28 #include "config.h"
29 #include "public/platform/modules/indexeddb/WebIDBKey.h"
31 #include "modules/indexeddb/IDBKey.h"
33 namespace blink {
35 WebIDBKey WebIDBKey::createArray(const WebVector<WebIDBKey>& array)
37 WebIDBKey key;
38 key.assignArray(array);
39 return key;
42 WebIDBKey WebIDBKey::createBinary(const WebData& binary)
44 WebIDBKey key;
45 key.assignBinary(binary);
46 return key;
49 WebIDBKey WebIDBKey::createString(const WebString& string)
51 WebIDBKey key;
52 key.assignString(string);
53 return key;
56 WebIDBKey WebIDBKey::createDate(double date)
58 WebIDBKey key;
59 key.assignDate(date);
60 return key;
63 WebIDBKey WebIDBKey::createNumber(double number)
65 WebIDBKey key;
66 key.assignNumber(number);
67 return key;
70 WebIDBKey WebIDBKey::createInvalid()
72 WebIDBKey key;
73 key.assignInvalid();
74 return key;
77 WebIDBKey WebIDBKey::createNull()
79 WebIDBKey key;
80 key.assignNull();
81 return key;
84 #if BLINK_WEB_IMPLEMENTATION || !LINK_CORE_MODULES_SEPARATELY
85 void WebIDBKey::reset()
87 m_private.reset();
89 #endif
91 void WebIDBKey::assign(const WebIDBKey& value)
93 m_private = value.m_private;
96 static IDBKey* convertFromWebIDBKeyArray(const WebVector<WebIDBKey>& array)
98 IDBKey::KeyArray keys;
99 keys.reserveCapacity(array.size());
100 for (size_t i = 0; i < array.size(); ++i) {
101 switch (array[i].keyType()) {
102 case WebIDBKeyTypeArray:
103 keys.append(convertFromWebIDBKeyArray(array[i].array()));
104 break;
105 case WebIDBKeyTypeBinary:
106 keys.append(IDBKey::createBinary(array[i].binary()));
107 break;
108 case WebIDBKeyTypeString:
109 keys.append(IDBKey::createString(array[i].string()));
110 break;
111 case WebIDBKeyTypeDate:
112 keys.append(IDBKey::createDate(array[i].date()));
113 break;
114 case WebIDBKeyTypeNumber:
115 keys.append(IDBKey::createNumber(array[i].number()));
116 break;
117 case WebIDBKeyTypeInvalid:
118 keys.append(IDBKey::createInvalid());
119 break;
120 case WebIDBKeyTypeNull:
121 case WebIDBKeyTypeMin:
122 ASSERT_NOT_REACHED();
123 break;
126 return IDBKey::createArray(keys);
129 static void convertToWebIDBKeyArray(const IDBKey::KeyArray& array, WebVector<WebIDBKey>& result)
131 WebVector<WebIDBKey> keys(array.size());
132 WebVector<WebIDBKey> subkeys;
133 for (size_t i = 0; i < array.size(); ++i) {
134 IDBKey* key = array[i];
135 switch (key->type()) {
136 case IDBKey::ArrayType:
137 convertToWebIDBKeyArray(key->array(), subkeys);
138 keys[i] = WebIDBKey::createArray(subkeys);
139 break;
140 case IDBKey::BinaryType:
141 keys[i] = WebIDBKey::createBinary(key->binary());
142 break;
143 case IDBKey::StringType:
144 keys[i] = WebIDBKey::createString(key->string());
145 break;
146 case IDBKey::DateType:
147 keys[i] = WebIDBKey::createDate(key->date());
148 break;
149 case IDBKey::NumberType:
150 keys[i] = WebIDBKey::createNumber(key->number());
151 break;
152 case IDBKey::InvalidType:
153 keys[i] = WebIDBKey::createInvalid();
154 break;
155 case IDBKey::MinType:
156 ASSERT_NOT_REACHED();
157 break;
160 result.swap(keys);
163 void WebIDBKey::assignArray(const WebVector<WebIDBKey>& array)
165 m_private = convertFromWebIDBKeyArray(array);
168 void WebIDBKey::assignBinary(const WebData& binary)
170 m_private = IDBKey::createBinary(binary);
173 void WebIDBKey::assignString(const WebString& string)
175 m_private = IDBKey::createString(string);
178 void WebIDBKey::assignDate(double date)
180 m_private = IDBKey::createDate(date);
183 void WebIDBKey::assignNumber(double number)
185 m_private = IDBKey::createNumber(number);
188 void WebIDBKey::assignInvalid()
190 m_private = IDBKey::createInvalid();
193 void WebIDBKey::assignNull()
195 m_private.reset();
198 WebIDBKeyType WebIDBKey::keyType() const
200 if (!m_private.get())
201 return WebIDBKeyTypeNull;
202 return static_cast<WebIDBKeyType>(m_private->type());
205 bool WebIDBKey::isValid() const
207 if (!m_private.get())
208 return false;
209 return m_private->isValid();
212 WebVector<WebIDBKey> WebIDBKey::array() const
214 WebVector<WebIDBKey> keys;
215 convertToWebIDBKeyArray(m_private->array(), keys);
216 return keys;
219 WebData WebIDBKey::binary() const
221 return m_private->binary();
224 WebString WebIDBKey::string() const
226 return m_private->string();
229 double WebIDBKey::date() const
231 return m_private->date();
234 double WebIDBKey::number() const
236 return m_private->number();
239 } // namespace blink