Home | History | Annotate | Download | only in indexed_db
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/common/indexed_db/indexed_db_param_traits.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 #include "content/common/indexed_db/indexed_db_key.h"
     10 #include "content/common/indexed_db/indexed_db_key_path.h"
     11 #include "content/common/indexed_db/indexed_db_key_range.h"
     12 #include "ipc/ipc_message_utils.h"
     13 
     14 using content::IndexedDBKey;
     15 using content::IndexedDBKeyPath;
     16 using content::IndexedDBKeyRange;
     17 
     18 using WebKit::WebIDBKeyPathTypeArray;
     19 using WebKit::WebIDBKeyPathTypeNull;
     20 using WebKit::WebIDBKeyPathTypeString;
     21 using WebKit::WebIDBKeyType;
     22 using WebKit::WebIDBKeyTypeArray;
     23 using WebKit::WebIDBKeyTypeDate;
     24 using WebKit::WebIDBKeyTypeInvalid;
     25 using WebKit::WebIDBKeyTypeMin;
     26 using WebKit::WebIDBKeyTypeNull;
     27 using WebKit::WebIDBKeyTypeNumber;
     28 using WebKit::WebIDBKeyTypeString;
     29 
     30 namespace IPC {
     31 
     32 void ParamTraits<IndexedDBKey>::Write(Message* m, const param_type& p) {
     33   WriteParam(m, static_cast<int>(p.type()));
     34   switch (p.type()) {
     35     case WebIDBKeyTypeArray:
     36       WriteParam(m, p.array());
     37       return;
     38     case WebIDBKeyTypeString:
     39       WriteParam(m, p.string());
     40       return;
     41     case WebIDBKeyTypeDate:
     42       WriteParam(m, p.date());
     43       return;
     44     case WebIDBKeyTypeNumber:
     45       WriteParam(m, p.number());
     46       return;
     47     case WebIDBKeyTypeInvalid:
     48     case WebIDBKeyTypeNull:
     49       return;
     50     case WebIDBKeyTypeMin:
     51       NOTREACHED();
     52       return;
     53   }
     54 }
     55 
     56 bool ParamTraits<IndexedDBKey>::Read(const Message* m,
     57                                      PickleIterator* iter,
     58                                      param_type* r) {
     59   int type;
     60   if (!ReadParam(m, iter, &type))
     61     return false;
     62   WebIDBKeyType web_type = static_cast<WebIDBKeyType>(type);
     63 
     64   switch (web_type) {
     65     case WebIDBKeyTypeArray: {
     66       std::vector<IndexedDBKey> array;
     67       if (!ReadParam(m, iter, &array))
     68         return false;
     69       *r = IndexedDBKey(array);
     70       return true;
     71     }
     72     case WebIDBKeyTypeString: {
     73       string16 string;
     74       if (!ReadParam(m, iter, &string))
     75         return false;
     76       *r = IndexedDBKey(string);
     77       return true;
     78     }
     79     case WebIDBKeyTypeDate:
     80     case WebIDBKeyTypeNumber: {
     81       double number;
     82       if (!ReadParam(m, iter, &number))
     83         return false;
     84       *r = IndexedDBKey(number, web_type);
     85       return true;
     86     }
     87     case WebIDBKeyTypeInvalid:
     88     case WebIDBKeyTypeNull:
     89       *r = IndexedDBKey(web_type);
     90       return true;
     91     case WebIDBKeyTypeMin:
     92       NOTREACHED();
     93       return false;
     94   }
     95   NOTREACHED();
     96   return false;
     97 }
     98 
     99 void ParamTraits<IndexedDBKey>::Log(const param_type& p, std::string* l) {
    100   l->append("<IndexedDBKey>(");
    101   LogParam(static_cast<int>(p.type()), l);
    102   l->append(", ");
    103   l->append("[");
    104   std::vector<IndexedDBKey>::const_iterator it = p.array().begin();
    105   while (it != p.array().end()) {
    106     Log(*it, l);
    107     ++it;
    108     if (it != p.array().end())
    109       l->append(", ");
    110   }
    111   l->append("], ");
    112   LogParam(p.string(), l);
    113   l->append(", ");
    114   LogParam(p.date(), l);
    115   l->append(", ");
    116   LogParam(p.number(), l);
    117   l->append(")");
    118 }
    119 
    120 void ParamTraits<IndexedDBKeyPath>::Write(Message* m, const param_type& p) {
    121   WriteParam(m, static_cast<int>(p.type()));
    122   switch (p.type()) {
    123     case WebIDBKeyPathTypeArray:
    124       WriteParam(m, p.array());
    125       return;
    126     case WebIDBKeyPathTypeString:
    127       WriteParam(m, p.string());
    128       return;
    129     case WebIDBKeyPathTypeNull:
    130       return;
    131   }
    132   NOTREACHED();
    133 }
    134 
    135 bool ParamTraits<IndexedDBKeyPath>::Read(const Message* m,
    136                                          PickleIterator* iter,
    137                                          param_type* r) {
    138   int type;
    139   if (!ReadParam(m, iter, &type))
    140     return false;
    141 
    142   switch (type) {
    143     case WebIDBKeyPathTypeArray: {
    144       std::vector<string16> array;
    145       if (!ReadParam(m, iter, &array))
    146         return false;
    147       *r = IndexedDBKeyPath(array);
    148       return true;
    149     }
    150     case WebIDBKeyPathTypeString: {
    151       string16 string;
    152       if (!ReadParam(m, iter, &string))
    153         return false;
    154       *r = IndexedDBKeyPath(string);
    155       return true;
    156     }
    157     case WebIDBKeyPathTypeNull:
    158       *r = IndexedDBKeyPath();
    159       return true;
    160   }
    161   NOTREACHED();
    162   return false;
    163 }
    164 
    165 void ParamTraits<IndexedDBKeyPath>::Log(const param_type& p, std::string* l) {
    166   l->append("<IndexedDBKeyPath>(");
    167   LogParam(static_cast<int>(p.type()), l);
    168   l->append(", ");
    169   LogParam(p.string(), l);
    170   l->append(", ");
    171   l->append("[");
    172   std::vector<string16>::const_iterator it = p.array().begin();
    173   while (it != p.array().end()) {
    174     LogParam(*it, l);
    175     ++it;
    176     if (it != p.array().end())
    177       l->append(", ");
    178   }
    179   l->append("])");
    180 }
    181 
    182 void ParamTraits<IndexedDBKeyRange>::Write(Message* m, const param_type& p) {
    183   WriteParam(m, p.lower());
    184   WriteParam(m, p.upper());
    185   WriteParam(m, p.lowerOpen());
    186   WriteParam(m, p.upperOpen());
    187 }
    188 
    189 bool ParamTraits<IndexedDBKeyRange>::Read(const Message* m,
    190                                           PickleIterator* iter,
    191                                           param_type* r) {
    192   IndexedDBKey lower;
    193   if (!ReadParam(m, iter, &lower))
    194     return false;
    195 
    196   IndexedDBKey upper;
    197   if (!ReadParam(m, iter, &upper))
    198     return false;
    199 
    200   bool lower_open;
    201   if (!ReadParam(m, iter, &lower_open))
    202     return false;
    203 
    204   bool upper_open;
    205   if (!ReadParam(m, iter, &upper_open))
    206     return false;
    207 
    208   *r = IndexedDBKeyRange(lower, upper, lower_open, upper_open);
    209   return true;
    210 }
    211 
    212 void ParamTraits<IndexedDBKeyRange>::Log(const param_type& p, std::string* l) {
    213   l->append("<IndexedDBKeyRange>(lower=");
    214   LogParam(p.lower(), l);
    215   l->append(", upper=");
    216   LogParam(p.upper(), l);
    217   l->append(", lower_open=");
    218   LogParam(p.lowerOpen(), l);
    219   l->append(", upper_open=");
    220   LogParam(p.upperOpen(), l);
    221   l->append(")");
    222 }
    223 
    224 }  // namespace IPC
    225