Home | History | Annotate | Download | only in web
      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 blink {
     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::createBinary(const WebData& binary)
     45 {
     46     WebIDBKey key;
     47     key.assignBinary(binary);
     48     return key;
     49 }
     50 
     51 WebIDBKey WebIDBKey::createString(const WebString& string)
     52 {
     53     WebIDBKey key;
     54     key.assignString(string);
     55     return key;
     56 }
     57 
     58 WebIDBKey WebIDBKey::createDate(double date)
     59 {
     60     WebIDBKey key;
     61     key.assignDate(date);
     62     return key;
     63 }
     64 
     65 WebIDBKey WebIDBKey::createNumber(double number)
     66 {
     67     WebIDBKey key;
     68     key.assignNumber(number);
     69     return key;
     70 }
     71 
     72 WebIDBKey WebIDBKey::createInvalid()
     73 {
     74     WebIDBKey key;
     75     key.assignInvalid();
     76     return key;
     77 }
     78 
     79 WebIDBKey WebIDBKey::createNull()
     80 {
     81     WebIDBKey key;
     82     key.assignNull();
     83     return key;
     84 }
     85 
     86 void WebIDBKey::assign(const WebIDBKey& value)
     87 {
     88     m_private = value.m_private;
     89 }
     90 
     91 static 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) {
     96         switch (array[i].keyType()) {
     97         case WebIDBKeyTypeArray:
     98             keys.append(convertFromWebIDBKeyArray(array[i].array()));
     99             break;
    100         case WebIDBKeyTypeBinary:
    101             keys.append(IDBKey::createBinary(array[i].binary()));
    102             break;
    103         case WebIDBKeyTypeString:
    104             keys.append(IDBKey::createString(array[i].string()));
    105             break;
    106         case WebIDBKeyTypeDate:
    107             keys.append(IDBKey::createDate(array[i].date()));
    108             break;
    109         case WebIDBKeyTypeNumber:
    110             keys.append(IDBKey::createNumber(array[i].number()));
    111             break;
    112         case WebIDBKeyTypeInvalid:
    113             keys.append(IDBKey::createInvalid());
    114             break;
    115         case WebIDBKeyTypeNull:
    116         case WebIDBKeyTypeMin:
    117             ASSERT_NOT_REACHED();
    118             break;
    119         }
    120     }
    121     return IDBKey::createArray(keys);
    122 }
    123 
    124 static 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         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;
    135         case IDBKey::BinaryType:
    136             keys[i] = WebIDBKey::createBinary(key->binary());
    137             break;
    138         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 
    158 void WebIDBKey::assignArray(const WebVector<WebIDBKey>& array)
    159 {
    160     m_private = convertFromWebIDBKeyArray(array);
    161 }
    162 
    163 void WebIDBKey::assignBinary(const WebData& binary)
    164 {
    165     m_private = IDBKey::createBinary(binary);
    166 }
    167 
    168 void WebIDBKey::assignString(const WebString& string)
    169 {
    170     m_private = IDBKey::createString(string);
    171 }
    172 
    173 void WebIDBKey::assignDate(double date)
    174 {
    175     m_private = IDBKey::createDate(date);
    176 }
    177 
    178 void WebIDBKey::assignNumber(double number)
    179 {
    180     m_private = IDBKey::createNumber(number);
    181 }
    182 
    183 void WebIDBKey::assignInvalid()
    184 {
    185     m_private = IDBKey::createInvalid();
    186 }
    187 
    188 void WebIDBKey::assignNull()
    189 {
    190     m_private.reset();
    191 }
    192 
    193 void WebIDBKey::reset()
    194 {
    195     m_private.reset();
    196 }
    197 
    198 WebIDBKeyType WebIDBKey::keyType() const
    199 {
    200     if (!m_private.get())
    201         return WebIDBKeyTypeNull;
    202     return static_cast<WebIDBKeyType>(m_private->type());
    203 }
    204 
    205 bool WebIDBKey::isValid() const
    206 {
    207     if (!m_private.get())
    208         return false;
    209     return m_private->isValid();
    210 }
    211 
    212 WebVector<WebIDBKey> WebIDBKey::array() const
    213 {
    214     WebVector<WebIDBKey> keys;
    215     convertToWebIDBKeyArray(m_private->array(), keys);
    216     return keys;
    217 }
    218 
    219 WebData WebIDBKey::binary() const
    220 {
    221     return m_private->binary();
    222 }
    223 
    224 WebString WebIDBKey::string() const
    225 {
    226     return m_private->string();
    227 }
    228 
    229 double WebIDBKey::date() const
    230 {
    231     return m_private->date();
    232 }
    233 
    234 double WebIDBKey::number() const
    235 {
    236     return m_private->number();
    237 }
    238 
    239 WebIDBKey::WebIDBKey(IDBKey* value)
    240     : m_private(value)
    241 {
    242 }
    243 
    244 WebIDBKey& WebIDBKey::operator=(IDBKey* value)
    245 {
    246     m_private = value;
    247     return *this;
    248 }
    249 
    250 WebIDBKey::operator IDBKey*() const
    251 {
    252     return m_private.get();
    253 }
    254 
    255 } // namespace blink
    256