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