Chromium Code Reviews| Index: content/browser/indexed_db/indexed_db_struct_traits.h |
| diff --git a/content/browser/indexed_db/indexed_db_struct_traits.h b/content/browser/indexed_db/indexed_db_struct_traits.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..749183e30f0c872127636743f5ba6defa49bacc3 |
| --- /dev/null |
| +++ b/content/browser/indexed_db/indexed_db_struct_traits.h |
| @@ -0,0 +1,250 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_STRUCT_TRAITS_H_ |
| +#define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_STRUCT_TRAITS_H_ |
| + |
| +#include "content/browser/indexed_db/indexed_db_metadata.h" |
| +#include "mojo/common/common_type_converters.h" |
| +#include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" |
| +#include "third_party/WebKit/public/platform/modules/indexeddb/indexed_db.mojom.h" |
| + |
| +namespace mojo { |
| + |
| +template <> |
| +struct EnumTraits<indexed_db::mojom::KeyPathType, blink::WebIDBKeyPathType> { |
| + static indexed_db::mojom::KeyPathType ToMojom(blink::WebIDBKeyPathType input); |
| + static bool FromMojom(indexed_db::mojom::KeyPathType input, |
| + blink::WebIDBKeyPathType* output); |
| +}; |
| + |
| +template <> |
| +struct StructTraits<indexed_db::mojom::ErrorInfo, |
| + content::IndexedDBDatabaseError> { |
| + static int16_t code(const content::IndexedDBDatabaseError& r) { |
| + return r.code(); |
| + } |
| + static String message(const content::IndexedDBDatabaseError& r) { |
| + return String::From(r.message()); |
| + } |
| + static bool Read(indexed_db::mojom::ErrorInfoDataView data, |
| + content::IndexedDBDatabaseError* out) { |
| + mojo::String message; |
| + if (!data.ReadMessage(&message)) |
| + return false; |
| + |
| + *out = content::IndexedDBDatabaseError(data.code(), |
| + message.To<base::string16>()); |
| + |
| + return true; |
| + } |
| +}; |
| + |
| +template <> |
| +struct StructTraits<indexed_db::mojom::KeyPath, |
| + content::IndexedDBKeyPath> { |
| + static blink::WebIDBKeyPathType type(const content::IndexedDBKeyPath& r) { |
| + return r.type(); |
| + } |
| + static String string(const content::IndexedDBKeyPath& r) { |
| + if (r.type() == blink::WebIDBKeyPathTypeString) |
| + return String::From(r.string()); |
| + else |
| + return String(); |
| + } |
| + static Array<String> array(const content::IndexedDBKeyPath& r) { |
| + if (r.type() == blink::WebIDBKeyPathTypeArray) { |
| + Array<String> array; |
| + for (const auto& string : r.array()) |
| + array.push_back(String::From(string)); |
| + return array; |
| + } else { |
| + return Array<String>(); |
| + } |
| + } |
| + static bool Read(indexed_db::mojom::KeyPathDataView data, |
| + content::IndexedDBKeyPath* out) { |
| + if (data.type() == indexed_db::mojom::KeyPathType::NONE) { |
| + *out = content::IndexedDBKeyPath(); |
| + return true; |
| + } |
| + indexed_db::mojom::KeyPathDataPtr key_path_data; |
| + if (!data.ReadData(&key_path_data)) |
| + return false; |
| + |
| + switch (data.type()) { |
| + case indexed_db::mojom::KeyPathType::NONE: |
| + NOTREACHED(); |
| + return false; |
| + case indexed_db::mojom::KeyPathType::STRING: |
| + if (!key_path_data->is_str()) |
| + return false; |
| + *out = content::IndexedDBKeyPath(key_path_data->get_str().To<base::string16>()); |
| + return true; |
| + case indexed_db::mojom::KeyPathType::ARRAY: |
| + if (!key_path_data->is_arr()) |
| + return false; |
| + // TODO(cmumford): finishme. |
| + NOTREACHED(); |
| + return true; |
| + } |
| + |
| + NOTREACHED(); |
| + return false; |
| + } |
| +}; |
| + |
| +template <> |
| +struct StructTraits<indexed_db::mojom::IndexMetadata, |
| + content::IndexedDBIndexMetadata> { |
| + static String name(const content::IndexedDBIndexMetadata& r) { |
| + return String::From(r.name); |
| + } |
| + static int64_t id(const content::IndexedDBIndexMetadata& r) { |
| + return r.id; |
| + } |
| + static content::IndexedDBKeyPath keyPath(const content::IndexedDBIndexMetadata& r) { |
| + return r.key_path; |
| + } |
| + static bool unique(const content::IndexedDBIndexMetadata& r) { |
| + return r.unique; |
| + } |
| + static bool multiEntry(const content::IndexedDBIndexMetadata& r) { |
| + return r.multi_entry; |
| + } |
| + static bool Read(indexed_db::mojom::IndexMetadataDataView data, |
| + content::IndexedDBIndexMetadata* out) { |
| + base::string16 name; |
| + if (!data.ReadName(&name)) |
| + return false; |
| + |
| + content::IndexedDBKeyPath key_path; |
| + if (!data.ReadKeyPath(&key_path)) |
| + return false; |
| + |
| + *out = content::IndexedDBIndexMetadata(name, |
| + data.id(), |
| + key_path, |
| + data.unique(), |
| + data.multi_entry()); |
| + |
| + return true; |
| + } |
| +}; |
| + |
| +#if 0 |
| +// TODO(cmumford): Put type converters into indexed_db_type_converters.h |
| +template <> |
| +struct TypeConverter<content::IndexedDBIndexMetadata, indexed_db::mojom::IndexMetadata> { |
| + static content::IndexedDBIndexMetadata Convert( |
| + const indexed_db::mojom::IndexMetadata& metadata); |
| +}; |
| + |
| +// TODO(cmumford): Probably need only one of these (maybe zero?). |
| +template <> |
| +struct TypeConverter<indexed_db::mojom::IndexMetadata, content::IndexedDBIndexMetadata> { |
| + static indexed_db::mojom::IndexMetadata Convert( |
| + const content::IndexedDBIndexMetadata& metadata); |
| +}; |
| +#endif |
| + |
| +template <> |
| +struct StructTraits<indexed_db::mojom::ObjectStoreMetadata, |
|
cmumford
2016/07/11 17:19:45
Compile failure: https://paste.googleplex.com/5175
|
| + content::IndexedDBObjectStoreMetadata> { |
| + // TODO(cmumford): Make this a StringPiece? |
| + static String name(const content::IndexedDBObjectStoreMetadata& r) { |
| + return String::From(r.name); |
| + } |
| + static int64_t id(const content::IndexedDBObjectStoreMetadata& r) { |
| + return r.id; |
| + } |
| + static content::IndexedDBKeyPath keyPath(const content::IndexedDBObjectStoreMetadata& r) { |
| + return r.key_path; |
| + } |
| + static bool auto_increment(const content::IndexedDBObjectStoreMetadata& r) { |
| + return r.auto_increment; |
| + } |
| + static int64_t max_index_id(const content::IndexedDBObjectStoreMetadata& r) { |
| + return r.max_index_id; |
| + } |
| + static Map<int64_t, indexed_db::mojom::IndexMetadata> indexes(const content::IndexedDBObjectStoreMetadata& r) { |
| + // TODO(cmumford): Determine the correct way to do this. |
| + //return Map<int64_t, indexed_db::mojom::IndexMetadata>::From(r.indexes); |
| + Map<int64_t, indexed_db::mojom::IndexMetadata> map; |
| + |
| + return map; |
| + } |
| + static bool Read(indexed_db::mojom::ObjectStoreMetadataDataView data, |
| + content::IndexedDBObjectStoreMetadata* out) { |
| + mojo::String name; |
| + if (!data.ReadName(&name)) |
| + return false; |
| + |
| + content::IndexedDBKeyPath key_path; |
| + if (!data.ReadKeyPath(&key_path)) |
| + return false; |
| + |
| + // TODO(cmumford): Handle the indexes. |
| + *out = content::IndexedDBObjectStoreMetadata(name.To<base::string16>(), |
| + data.id(), |
| + key_path, |
| + data.auto_increment(), |
| + data.max_index_id()); |
| + |
| + return true; |
| + } |
| +}; |
| + |
| +#if 0 |
| +// TODO(cmumford): Put type converters into indexed_db_type_converters.h |
| +template <> |
| +struct TypeConverter<content::IndexedDBObjectStoreMetadata, indexed_db::mojom::ObjectStoreMetadata> { |
| + static content::IndexedDBObjectStoreMetadata Convert( |
| + const indexed_db::mojom::ObjectStoreMetadata& metadata); |
| +}; |
| +template <> |
| +struct TypeConverter<indexed_db::mojom::ObjectStoreMetadata, content::IndexedDBObjectStoreMetadata> { |
| + static indexed_db::mojom::ObjectStoreMetadata Convert( |
| + const content::IndexedDBObjectStoreMetadata& metadata); |
| +}; |
| +#endif |
| + |
| +template <> |
| +struct StructTraits<indexed_db::mojom::DatabaseMetadata, |
| + content::IndexedDBDatabaseMetadata> { |
| + static String name(const content::IndexedDBDatabaseMetadata& r) { |
| + return String::From(r.name); |
| + } |
| + static int64_t id(const content::IndexedDBDatabaseMetadata& r) { |
| + return r.id; |
| + } |
| + static int64_t version(const content::IndexedDBDatabaseMetadata& r) { |
| + return r.version; |
| + } |
| + static int64_t max_object_store_id(const content::IndexedDBDatabaseMetadata& r) { |
| + return r.max_object_store_id; |
| + } |
| + static Map<int64_t, indexed_db::mojom::ObjectStoreMetadata> object_stores(const content::IndexedDBDatabaseMetadata& r) { |
| + //return Map<int64_t, indexed_db::mojom::ObjectStoreMetadata>::From(r.object_stores); |
| + Map<int64_t, indexed_db::mojom::ObjectStoreMetadata> map; |
| + return map; |
| + } |
| + static bool Read(indexed_db::mojom::DatabaseMetadataDataView data, |
| + content::IndexedDBDatabaseMetadata* out) { |
| + mojo::String name; |
| + if (!data.ReadName(&name)) |
| + return false; |
| + |
| + *out = content::IndexedDBDatabaseMetadata(name.To<base::string16>(), |
| + data.id(), |
| + data.version(), |
| + data.max_object_store_id()); |
| + |
| + return true; |
| + } |
| +}; |
| + |
| +} |
| + |
| +#endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_STRUCT_TRAITS_H_ |