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