1 /* 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" 29 #include "public/platform/WebIDBKey.h" 30 31 #include "modules/indexeddb/IDBKey.h" 32 33 namespace blink { 34 35 WebIDBKey WebIDBKey::createArray(const WebVector<WebIDBKey>& array) 36 { 37 WebIDBKey key; 38 key.assignArray(array); 39 return key; 40 } 41 42 WebIDBKey WebIDBKey::createBinary(const WebData& binary) 43 { 44 WebIDBKey key; 45 key.assignBinary(binary); 46 return key; 47 } 48 49 WebIDBKey WebIDBKey::createString(const WebString& string) 50 { 51 WebIDBKey key; 52 key.assignString(string); 53 return key; 54 } 55 56 WebIDBKey WebIDBKey::createDate(double date) 57 { 58 WebIDBKey key; 59 key.assignDate(date); 60 return key; 61 } 62 63 WebIDBKey WebIDBKey::createNumber(double number) 64 { 65 WebIDBKey key; 66 key.assignNumber(number); 67 return key; 68 } 69 70 WebIDBKey WebIDBKey::createInvalid() 71 { 72 WebIDBKey key; 73 key.assignInvalid(); 74 return key; 75 } 76 77 WebIDBKey WebIDBKey::createNull() 78 { 79 WebIDBKey key; 80 key.assignNull(); 81 return key; 82 } 83 84 void WebIDBKey::assign(const WebIDBKey& value) 85 { 86 m_private = value.m_private; 87 } 88 89 static IDBKey* convertFromWebIDBKeyArray(const WebVector<WebIDBKey>& array) 90 { 91 IDBKey::KeyArray keys; 92 keys.reserveCapacity(array.size()); 93 for (size_t i = 0; i < array.size(); ++i) { 94 switch (array[i].keyType()) { 95 case WebIDBKeyTypeArray: 96 keys.append(convertFromWebIDBKeyArray(array[i].array())); 97 break; 98 case WebIDBKeyTypeBinary: 99 keys.append(IDBKey::createBinary(array[i].binary())); 100 break; 101 case WebIDBKeyTypeString: 102 keys.append(IDBKey::createString(array[i].string())); 103 break; 104 case WebIDBKeyTypeDate: 105 keys.append(IDBKey::createDate(array[i].date())); 106 break; 107 case WebIDBKeyTypeNumber: 108 keys.append(IDBKey::createNumber(array[i].number())); 109 break; 110 case WebIDBKeyTypeInvalid: 111 keys.append(IDBKey::createInvalid()); 112 break; 113 case WebIDBKeyTypeNull: 114 case WebIDBKeyTypeMin: 115 ASSERT_NOT_REACHED(); 116 break; 117 } 118 } 119 return IDBKey::createArray(keys); 120 } 121 122 static void convertToWebIDBKeyArray(const IDBKey::KeyArray& array, WebVector<WebIDBKey>& result) 123 { 124 WebVector<WebIDBKey> keys(array.size()); 125 WebVector<WebIDBKey> subkeys; 126 for (size_t i = 0; i < array.size(); ++i) { 127 IDBKey* key = array[i]; 128 switch (key->type()) { 129 case IDBKey::ArrayType: 130 convertToWebIDBKeyArray(key->array(), subkeys); 131 keys[i] = WebIDBKey::createArray(subkeys); 132 break; 133 case IDBKey::BinaryType: 134 keys[i] = WebIDBKey::createBinary(key->binary()); 135 break; 136 case IDBKey::StringType: 137 keys[i] = WebIDBKey::createString(key->string()); 138 break; 139 case IDBKey::DateType: 140 keys[i] = WebIDBKey::createDate(key->date()); 141 break; 142 case IDBKey::NumberType: 143 keys[i] = WebIDBKey::createNumber(key->number()); 144 break; 145 case IDBKey::InvalidType: 146 keys[i] = WebIDBKey::createInvalid(); 147 break; 148 case IDBKey::MinType: 149 ASSERT_NOT_REACHED(); 150 break; 151 } 152 } 153 result.swap(keys); 154 } 155 156 void WebIDBKey::assignArray(const WebVector<WebIDBKey>& array) 157 { 158 m_private = convertFromWebIDBKeyArray(array); 159 } 160 161 void WebIDBKey::assignBinary(const WebData& binary) 162 { 163 m_private = IDBKey::createBinary(binary); 164 } 165 166 void WebIDBKey::assignString(const WebString& string) 167 { 168 m_private = IDBKey::createString(string); 169 } 170 171 void WebIDBKey::assignDate(double date) 172 { 173 m_private = IDBKey::createDate(date); 174 } 175 176 void WebIDBKey::assignNumber(double number) 177 { 178 m_private = IDBKey::createNumber(number); 179 } 180 181 void WebIDBKey::assignInvalid() 182 { 183 m_private = IDBKey::createInvalid(); 184 } 185 186 void WebIDBKey::assignNull() 187 { 188 m_private.reset(); 189 } 190 191 void WebIDBKey::reset() 192 { 193 m_private.reset(); 194 } 195 196 WebIDBKeyType WebIDBKey::keyType() const 197 { 198 if (!m_private.get()) 199 return WebIDBKeyTypeNull; 200 return static_cast<WebIDBKeyType>(m_private->type()); 201 } 202 203 bool WebIDBKey::isValid() const 204 { 205 if (!m_private.get()) 206 return false; 207 return m_private->isValid(); 208 } 209 210 WebVector<WebIDBKey> WebIDBKey::array() const 211 { 212 WebVector<WebIDBKey> keys; 213 convertToWebIDBKeyArray(m_private->array(), keys); 214 return keys; 215 } 216 217 WebData WebIDBKey::binary() const 218 { 219 return m_private->binary(); 220 } 221 222 WebString WebIDBKey::string() const 223 { 224 return m_private->string(); 225 } 226 227 double WebIDBKey::date() const 228 { 229 return m_private->date(); 230 } 231 232 double WebIDBKey::number() const 233 { 234 return m_private->number(); 235 } 236 237 WebIDBKey::WebIDBKey(IDBKey* value) 238 : m_private(value) 239 { 240 } 241 242 WebIDBKey& WebIDBKey::operator=(IDBKey* value) 243 { 244 m_private = value; 245 return *this; 246 } 247 248 WebIDBKey::operator IDBKey*() const 249 { 250 return m_private.get(); 251 } 252 253 } // namespace blink 254