OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_STRUCT_TRAITS_H_ | |
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_STRUCT_TRAITS_H_ | |
7 | |
8 #include "content/browser/indexed_db/indexed_db_metadata.h" | |
9 #include "mojo/common/common_type_converters.h" | |
10 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | |
11 #include "third_party/WebKit/public/platform/modules/indexeddb/indexed_db.mojom. h" | |
12 | |
13 namespace mojo { | |
14 | |
15 template <> | |
16 struct EnumTraits<indexed_db::mojom::KeyPathType, blink::WebIDBKeyPathType> { | |
17 static indexed_db::mojom::KeyPathType ToMojom(blink::WebIDBKeyPathType input); | |
18 static bool FromMojom(indexed_db::mojom::KeyPathType input, | |
19 blink::WebIDBKeyPathType* output); | |
20 }; | |
21 | |
22 template <> | |
23 struct StructTraits<indexed_db::mojom::ErrorInfo, | |
24 content::IndexedDBDatabaseError> { | |
25 static int16_t code(const content::IndexedDBDatabaseError& r) { | |
26 return r.code(); | |
27 } | |
28 static String message(const content::IndexedDBDatabaseError& r) { | |
29 return String::From(r.message()); | |
30 } | |
31 static bool Read(indexed_db::mojom::ErrorInfoDataView data, | |
32 content::IndexedDBDatabaseError* out) { | |
33 mojo::String message; | |
34 if (!data.ReadMessage(&message)) | |
35 return false; | |
36 | |
37 *out = content::IndexedDBDatabaseError(data.code(), | |
38 message.To<base::string16>()); | |
39 | |
40 return true; | |
41 } | |
42 }; | |
43 | |
44 template <> | |
45 struct StructTraits<indexed_db::mojom::KeyPath, | |
46 content::IndexedDBKeyPath> { | |
47 static blink::WebIDBKeyPathType type(const content::IndexedDBKeyPath& r) { | |
48 return r.type(); | |
49 } | |
50 static String string(const content::IndexedDBKeyPath& r) { | |
51 if (r.type() == blink::WebIDBKeyPathTypeString) | |
52 return String::From(r.string()); | |
53 else | |
54 return String(); | |
55 } | |
56 static Array<String> array(const content::IndexedDBKeyPath& r) { | |
57 if (r.type() == blink::WebIDBKeyPathTypeArray) { | |
58 Array<String> array; | |
59 for (const auto& string : r.array()) | |
60 array.push_back(String::From(string)); | |
61 return array; | |
62 } else { | |
63 return Array<String>(); | |
64 } | |
65 } | |
66 static bool Read(indexed_db::mojom::KeyPathDataView data, | |
67 content::IndexedDBKeyPath* out) { | |
68 if (data.type() == indexed_db::mojom::KeyPathType::NONE) { | |
69 *out = content::IndexedDBKeyPath(); | |
70 return true; | |
71 } | |
72 indexed_db::mojom::KeyPathDataPtr key_path_data; | |
73 if (!data.ReadData(&key_path_data)) | |
74 return false; | |
75 | |
76 switch (data.type()) { | |
77 case indexed_db::mojom::KeyPathType::NONE: | |
78 NOTREACHED(); | |
79 return false; | |
80 case indexed_db::mojom::KeyPathType::STRING: | |
81 if (!key_path_data->is_str()) | |
82 return false; | |
83 *out = content::IndexedDBKeyPath(key_path_data->get_str().To<base::strin g16>()); | |
84 return true; | |
85 case indexed_db::mojom::KeyPathType::ARRAY: | |
86 if (!key_path_data->is_arr()) | |
87 return false; | |
88 // TODO(cmumford): finishme. | |
89 NOTREACHED(); | |
90 return true; | |
91 } | |
92 | |
93 NOTREACHED(); | |
94 return false; | |
95 } | |
96 }; | |
97 | |
98 template <> | |
99 struct StructTraits<indexed_db::mojom::IndexMetadata, | |
100 content::IndexedDBIndexMetadata> { | |
101 static String name(const content::IndexedDBIndexMetadata& r) { | |
102 return String::From(r.name); | |
103 } | |
104 static int64_t id(const content::IndexedDBIndexMetadata& r) { | |
105 return r.id; | |
106 } | |
107 static content::IndexedDBKeyPath keyPath(const content::IndexedDBIndexMetadata & r) { | |
108 return r.key_path; | |
109 } | |
110 static bool unique(const content::IndexedDBIndexMetadata& r) { | |
111 return r.unique; | |
112 } | |
113 static bool multiEntry(const content::IndexedDBIndexMetadata& r) { | |
114 return r.multi_entry; | |
115 } | |
116 static bool Read(indexed_db::mojom::IndexMetadataDataView data, | |
117 content::IndexedDBIndexMetadata* out) { | |
118 base::string16 name; | |
119 if (!data.ReadName(&name)) | |
120 return false; | |
121 | |
122 content::IndexedDBKeyPath key_path; | |
123 if (!data.ReadKeyPath(&key_path)) | |
124 return false; | |
125 | |
126 *out = content::IndexedDBIndexMetadata(name, | |
127 data.id(), | |
128 key_path, | |
129 data.unique(), | |
130 data.multi_entry()); | |
131 | |
132 return true; | |
133 } | |
134 }; | |
135 | |
136 #if 0 | |
137 // TODO(cmumford): Put type converters into indexed_db_type_converters.h | |
138 template <> | |
139 struct TypeConverter<content::IndexedDBIndexMetadata, indexed_db::mojom::IndexMe tadata> { | |
140 static content::IndexedDBIndexMetadata Convert( | |
141 const indexed_db::mojom::IndexMetadata& metadata); | |
142 }; | |
143 | |
144 // TODO(cmumford): Probably need only one of these (maybe zero?). | |
145 template <> | |
146 struct TypeConverter<indexed_db::mojom::IndexMetadata, content::IndexedDBIndexMe tadata> { | |
147 static indexed_db::mojom::IndexMetadata Convert( | |
148 const content::IndexedDBIndexMetadata& metadata); | |
149 }; | |
150 #endif | |
151 | |
152 template <> | |
153 struct StructTraits<indexed_db::mojom::ObjectStoreMetadata, | |
cmumford
2016/07/11 17:19:45
Compile failure: https://paste.googleplex.com/5175
| |
154 content::IndexedDBObjectStoreMetadata> { | |
155 // TODO(cmumford): Make this a StringPiece? | |
156 static String name(const content::IndexedDBObjectStoreMetadata& r) { | |
157 return String::From(r.name); | |
158 } | |
159 static int64_t id(const content::IndexedDBObjectStoreMetadata& r) { | |
160 return r.id; | |
161 } | |
162 static content::IndexedDBKeyPath keyPath(const content::IndexedDBObjectStoreMe tadata& r) { | |
163 return r.key_path; | |
164 } | |
165 static bool auto_increment(const content::IndexedDBObjectStoreMetadata& r) { | |
166 return r.auto_increment; | |
167 } | |
168 static int64_t max_index_id(const content::IndexedDBObjectStoreMetadata& r) { | |
169 return r.max_index_id; | |
170 } | |
171 static Map<int64_t, indexed_db::mojom::IndexMetadata> indexes(const content::I ndexedDBObjectStoreMetadata& r) { | |
172 // TODO(cmumford): Determine the correct way to do this. | |
173 //return Map<int64_t, indexed_db::mojom::IndexMetadata>::From(r.indexes); | |
174 Map<int64_t, indexed_db::mojom::IndexMetadata> map; | |
175 | |
176 return map; | |
177 } | |
178 static bool Read(indexed_db::mojom::ObjectStoreMetadataDataView data, | |
179 content::IndexedDBObjectStoreMetadata* out) { | |
180 mojo::String name; | |
181 if (!data.ReadName(&name)) | |
182 return false; | |
183 | |
184 content::IndexedDBKeyPath key_path; | |
185 if (!data.ReadKeyPath(&key_path)) | |
186 return false; | |
187 | |
188 // TODO(cmumford): Handle the indexes. | |
189 *out = content::IndexedDBObjectStoreMetadata(name.To<base::string16>(), | |
190 data.id(), | |
191 key_path, | |
192 data.auto_increment(), | |
193 data.max_index_id()); | |
194 | |
195 return true; | |
196 } | |
197 }; | |
198 | |
199 #if 0 | |
200 // TODO(cmumford): Put type converters into indexed_db_type_converters.h | |
201 template <> | |
202 struct TypeConverter<content::IndexedDBObjectStoreMetadata, indexed_db::mojom::O bjectStoreMetadata> { | |
203 static content::IndexedDBObjectStoreMetadata Convert( | |
204 const indexed_db::mojom::ObjectStoreMetadata& metadata); | |
205 }; | |
206 template <> | |
207 struct TypeConverter<indexed_db::mojom::ObjectStoreMetadata, content::IndexedDBO bjectStoreMetadata> { | |
208 static indexed_db::mojom::ObjectStoreMetadata Convert( | |
209 const content::IndexedDBObjectStoreMetadata& metadata); | |
210 }; | |
211 #endif | |
212 | |
213 template <> | |
214 struct StructTraits<indexed_db::mojom::DatabaseMetadata, | |
215 content::IndexedDBDatabaseMetadata> { | |
216 static String name(const content::IndexedDBDatabaseMetadata& r) { | |
217 return String::From(r.name); | |
218 } | |
219 static int64_t id(const content::IndexedDBDatabaseMetadata& r) { | |
220 return r.id; | |
221 } | |
222 static int64_t version(const content::IndexedDBDatabaseMetadata& r) { | |
223 return r.version; | |
224 } | |
225 static int64_t max_object_store_id(const content::IndexedDBDatabaseMetadata& r ) { | |
226 return r.max_object_store_id; | |
227 } | |
228 static Map<int64_t, indexed_db::mojom::ObjectStoreMetadata> object_stores(cons t content::IndexedDBDatabaseMetadata& r) { | |
229 //return Map<int64_t, indexed_db::mojom::ObjectStoreMetadata>::From(r.object _stores); | |
230 Map<int64_t, indexed_db::mojom::ObjectStoreMetadata> map; | |
231 return map; | |
232 } | |
233 static bool Read(indexed_db::mojom::DatabaseMetadataDataView data, | |
234 content::IndexedDBDatabaseMetadata* out) { | |
235 mojo::String name; | |
236 if (!data.ReadName(&name)) | |
237 return false; | |
238 | |
239 *out = content::IndexedDBDatabaseMetadata(name.To<base::string16>(), | |
240 data.id(), | |
241 data.version(), | |
242 data.max_object_store_id()); | |
243 | |
244 return true; | |
245 } | |
246 }; | |
247 | |
248 } | |
249 | |
250 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_STRUCT_TRAITS_H_ | |
OLD | NEW |