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 14using content::IndexedDBKey; 15using content::IndexedDBKeyPath; 16using content::IndexedDBKeyRange; 17 18using blink::WebIDBKeyPathTypeArray; 19using blink::WebIDBKeyPathTypeNull; 20using blink::WebIDBKeyPathTypeString; 21using blink::WebIDBKeyType; 22using blink::WebIDBKeyTypeArray; 23using blink::WebIDBKeyTypeBinary; 24using blink::WebIDBKeyTypeDate; 25using blink::WebIDBKeyTypeInvalid; 26using blink::WebIDBKeyTypeMin; 27using blink::WebIDBKeyTypeNull; 28using blink::WebIDBKeyTypeNumber; 29using blink::WebIDBKeyTypeString; 30 31namespace IPC { 32 33void ParamTraits<IndexedDBKey>::Write(Message* m, const param_type& p) { 34 WriteParam(m, static_cast<int>(p.type())); 35 switch (p.type()) { 36 case WebIDBKeyTypeArray: 37 WriteParam(m, p.array()); 38 return; 39 case WebIDBKeyTypeBinary: 40 WriteParam(m, p.binary()); 41 return; 42 case WebIDBKeyTypeString: 43 WriteParam(m, p.string()); 44 return; 45 case WebIDBKeyTypeDate: 46 WriteParam(m, p.date()); 47 return; 48 case WebIDBKeyTypeNumber: 49 WriteParam(m, p.number()); 50 return; 51 case WebIDBKeyTypeInvalid: 52 case WebIDBKeyTypeNull: 53 return; 54 case WebIDBKeyTypeMin: 55 default: 56 NOTREACHED(); 57 return; 58 } 59} 60 61bool ParamTraits<IndexedDBKey>::Read(const Message* m, 62 PickleIterator* iter, 63 param_type* r) { 64 int type; 65 if (!ReadParam(m, iter, &type)) 66 return false; 67 WebIDBKeyType web_type = static_cast<WebIDBKeyType>(type); 68 69 switch (web_type) { 70 case WebIDBKeyTypeArray: { 71 std::vector<IndexedDBKey> array; 72 if (!ReadParam(m, iter, &array)) 73 return false; 74 *r = IndexedDBKey(array); 75 return true; 76 } 77 case WebIDBKeyTypeBinary: { 78 std::string binary; 79 if (!ReadParam(m, iter, &binary)) 80 return false; 81 *r = IndexedDBKey(binary); 82 return true; 83 } 84 case WebIDBKeyTypeString: { 85 base::string16 string; 86 if (!ReadParam(m, iter, &string)) 87 return false; 88 *r = IndexedDBKey(string); 89 return true; 90 } 91 case WebIDBKeyTypeDate: 92 case WebIDBKeyTypeNumber: { 93 double number; 94 if (!ReadParam(m, iter, &number)) 95 return false; 96 *r = IndexedDBKey(number, web_type); 97 return true; 98 } 99 case WebIDBKeyTypeInvalid: 100 case WebIDBKeyTypeNull: 101 *r = IndexedDBKey(web_type); 102 return true; 103 case WebIDBKeyTypeMin: 104 default: 105 NOTREACHED(); 106 return false; 107 } 108} 109 110void ParamTraits<IndexedDBKey>::Log(const param_type& p, std::string* l) { 111 l->append("<IndexedDBKey>("); 112 LogParam(static_cast<int>(p.type()), l); 113 l->append(", "); 114 l->append("["); 115 std::vector<IndexedDBKey>::const_iterator it = p.array().begin(); 116 while (it != p.array().end()) { 117 Log(*it, l); 118 ++it; 119 if (it != p.array().end()) 120 l->append(", "); 121 } 122 l->append("], "); 123 LogParam(p.binary(), l); 124 l->append(", "); 125 LogParam(p.string(), l); 126 l->append(", "); 127 LogParam(p.date(), l); 128 l->append(", "); 129 LogParam(p.number(), l); 130 l->append(")"); 131} 132 133void ParamTraits<IndexedDBKeyPath>::Write(Message* m, const param_type& p) { 134 WriteParam(m, static_cast<int>(p.type())); 135 switch (p.type()) { 136 case WebIDBKeyPathTypeArray: 137 WriteParam(m, p.array()); 138 return; 139 case WebIDBKeyPathTypeString: 140 WriteParam(m, p.string()); 141 return; 142 case WebIDBKeyPathTypeNull: 143 return; 144 default: 145 NOTREACHED(); 146 return; 147 } 148} 149 150bool ParamTraits<IndexedDBKeyPath>::Read(const Message* m, 151 PickleIterator* iter, 152 param_type* r) { 153 int type; 154 if (!ReadParam(m, iter, &type)) 155 return false; 156 157 switch (type) { 158 case WebIDBKeyPathTypeArray: { 159 std::vector<base::string16> array; 160 if (!ReadParam(m, iter, &array)) 161 return false; 162 *r = IndexedDBKeyPath(array); 163 return true; 164 } 165 case WebIDBKeyPathTypeString: { 166 base::string16 string; 167 if (!ReadParam(m, iter, &string)) 168 return false; 169 *r = IndexedDBKeyPath(string); 170 return true; 171 } 172 case WebIDBKeyPathTypeNull: 173 *r = IndexedDBKeyPath(); 174 return true; 175 default: 176 NOTREACHED(); 177 return false; 178 } 179} 180 181void ParamTraits<IndexedDBKeyPath>::Log(const param_type& p, std::string* l) { 182 l->append("<IndexedDBKeyPath>("); 183 LogParam(static_cast<int>(p.type()), l); 184 l->append(", "); 185 LogParam(p.string(), l); 186 l->append(", "); 187 l->append("["); 188 std::vector<base::string16>::const_iterator it = p.array().begin(); 189 while (it != p.array().end()) { 190 LogParam(*it, l); 191 ++it; 192 if (it != p.array().end()) 193 l->append(", "); 194 } 195 l->append("])"); 196} 197 198void ParamTraits<IndexedDBKeyRange>::Write(Message* m, const param_type& p) { 199 WriteParam(m, p.lower()); 200 WriteParam(m, p.upper()); 201 WriteParam(m, p.lowerOpen()); 202 WriteParam(m, p.upperOpen()); 203} 204 205bool ParamTraits<IndexedDBKeyRange>::Read(const Message* m, 206 PickleIterator* iter, 207 param_type* r) { 208 IndexedDBKey lower; 209 if (!ReadParam(m, iter, &lower)) 210 return false; 211 212 IndexedDBKey upper; 213 if (!ReadParam(m, iter, &upper)) 214 return false; 215 216 bool lower_open; 217 if (!ReadParam(m, iter, &lower_open)) 218 return false; 219 220 bool upper_open; 221 if (!ReadParam(m, iter, &upper_open)) 222 return false; 223 224 *r = IndexedDBKeyRange(lower, upper, lower_open, upper_open); 225 return true; 226} 227 228void ParamTraits<IndexedDBKeyRange>::Log(const param_type& p, std::string* l) { 229 l->append("<IndexedDBKeyRange>(lower="); 230 LogParam(p.lower(), l); 231 l->append(", upper="); 232 LogParam(p.upper(), l); 233 l->append(", lower_open="); 234 LogParam(p.lowerOpen(), l); 235 l->append(", upper_open="); 236 LogParam(p.upperOpen(), l); 237 l->append(")"); 238} 239 240} // namespace IPC 241