Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / common / indexed_db / indexed_db_param_traits.cc
blob9ca0ceceeaca9df4694c4fd11688f38f4091f7b9
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.
5 #include "content/common/indexed_db/indexed_db_param_traits.h"
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"
14 using content::IndexedDBKey;
15 using content::IndexedDBKeyPath;
16 using content::IndexedDBKeyRange;
18 using blink::WebIDBKeyPathTypeArray;
19 using blink::WebIDBKeyPathTypeNull;
20 using blink::WebIDBKeyPathTypeString;
21 using blink::WebIDBKeyType;
22 using blink::WebIDBKeyTypeArray;
23 using blink::WebIDBKeyTypeBinary;
24 using blink::WebIDBKeyTypeDate;
25 using blink::WebIDBKeyTypeInvalid;
26 using blink::WebIDBKeyTypeMin;
27 using blink::WebIDBKeyTypeNull;
28 using blink::WebIDBKeyTypeNumber;
29 using blink::WebIDBKeyTypeString;
31 namespace IPC {
33 void 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;
61 bool ParamTraits<IndexedDBKey>::Read(const Message* m,
62 base::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);
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;
77 case WebIDBKeyTypeBinary: {
78 std::string binary;
79 if (!ReadParam(m, iter, &binary))
80 return false;
81 *r = IndexedDBKey(binary);
82 return true;
84 case WebIDBKeyTypeString: {
85 base::string16 string;
86 if (!ReadParam(m, iter, &string))
87 return false;
88 *r = IndexedDBKey(string);
89 return true;
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;
99 case WebIDBKeyTypeInvalid:
100 case WebIDBKeyTypeNull:
101 *r = IndexedDBKey(web_type);
102 return true;
103 case WebIDBKeyTypeMin:
104 default:
105 NOTREACHED();
106 return false;
110 void ParamTraits<IndexedDBKey>::Log(const param_type& p, std::string* l) {
111 l->append("<IndexedDBKey>(");
112 switch(p.type()) {
113 case WebIDBKeyTypeArray: {
114 l->append("array=");
115 l->append("[");
116 bool first = true;
117 for (const IndexedDBKey& key : p.array()) {
118 if (!first)
119 l->append(", ");
120 first = false;
121 Log(key, l);
123 l->append("]");
124 break;
126 case WebIDBKeyTypeBinary:
127 l->append("binary=");
128 LogParam(p.binary(), l);
129 break;
130 case WebIDBKeyTypeString:
131 l->append("string=");
132 LogParam(p.string(), l);
133 break;
134 case WebIDBKeyTypeDate:
135 l->append("date=");
136 LogParam(p.date(), l);
137 break;
138 case WebIDBKeyTypeNumber:
139 l->append("number=");
140 LogParam(p.number(), l);
141 break;
142 case WebIDBKeyTypeInvalid:
143 l->append("invalid");
144 break;
145 case WebIDBKeyTypeNull:
146 l->append("null");
147 break;
148 case WebIDBKeyTypeMin:
149 default:
150 NOTREACHED();
151 break;
153 l->append(")");
156 void ParamTraits<IndexedDBKeyPath>::Write(Message* m, const param_type& p) {
157 WriteParam(m, static_cast<int>(p.type()));
158 switch (p.type()) {
159 case WebIDBKeyPathTypeArray:
160 WriteParam(m, p.array());
161 return;
162 case WebIDBKeyPathTypeString:
163 WriteParam(m, p.string());
164 return;
165 case WebIDBKeyPathTypeNull:
166 return;
167 default:
168 NOTREACHED();
169 return;
173 bool ParamTraits<IndexedDBKeyPath>::Read(const Message* m,
174 base::PickleIterator* iter,
175 param_type* r) {
176 int type;
177 if (!ReadParam(m, iter, &type))
178 return false;
180 switch (type) {
181 case WebIDBKeyPathTypeArray: {
182 std::vector<base::string16> array;
183 if (!ReadParam(m, iter, &array))
184 return false;
185 *r = IndexedDBKeyPath(array);
186 return true;
188 case WebIDBKeyPathTypeString: {
189 base::string16 string;
190 if (!ReadParam(m, iter, &string))
191 return false;
192 *r = IndexedDBKeyPath(string);
193 return true;
195 case WebIDBKeyPathTypeNull:
196 *r = IndexedDBKeyPath();
197 return true;
198 default:
199 NOTREACHED();
200 return false;
204 void ParamTraits<IndexedDBKeyPath>::Log(const param_type& p, std::string* l) {
205 l->append("<IndexedDBKeyPath>(");
206 switch (p.type()) {
207 case WebIDBKeyPathTypeArray: {
208 l->append("array=[");
209 bool first = true;
210 for (const base::string16& entry : p.array()) {
211 if (!first)
212 l->append(", ");
213 first = false;
214 LogParam(entry, l);
216 l->append("]");
217 break;
219 case WebIDBKeyPathTypeString:
220 l->append("string=");
221 LogParam(p.string(), l);
222 break;
223 case WebIDBKeyPathTypeNull:
224 l->append("null");
225 break;
226 default:
227 NOTREACHED();
228 break;
230 l->append(")");
233 void ParamTraits<IndexedDBKeyRange>::Write(Message* m, const param_type& p) {
234 WriteParam(m, p.lower());
235 WriteParam(m, p.upper());
236 WriteParam(m, p.lower_open());
237 WriteParam(m, p.upper_open());
240 bool ParamTraits<IndexedDBKeyRange>::Read(const Message* m,
241 base::PickleIterator* iter,
242 param_type* r) {
243 IndexedDBKey lower;
244 if (!ReadParam(m, iter, &lower))
245 return false;
247 IndexedDBKey upper;
248 if (!ReadParam(m, iter, &upper))
249 return false;
251 bool lower_open;
252 if (!ReadParam(m, iter, &lower_open))
253 return false;
255 bool upper_open;
256 if (!ReadParam(m, iter, &upper_open))
257 return false;
259 *r = IndexedDBKeyRange(lower, upper, lower_open, upper_open);
260 return true;
263 void ParamTraits<IndexedDBKeyRange>::Log(const param_type& p, std::string* l) {
264 l->append("<IndexedDBKeyRange>(lower=");
265 LogParam(p.lower(), l);
266 l->append(", upper=");
267 LogParam(p.upper(), l);
268 l->append(", lower_open=");
269 LogParam(p.lower_open(), l);
270 l->append(", upper_open=");
271 LogParam(p.upper_open(), l);
272 l->append(")");
275 } // namespace IPC