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..49cf4bc7a830dfc1b21436592f539ddb6e28a986 |
--- /dev/null |
+++ b/content/browser/indexed_db/indexed_db_struct_traits.h |
@@ -0,0 +1,260 @@ |
+// 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 <string> |
+ |
+#include "content/browser/indexed_db/indexed_db_data_loss_info.h" |
+#include "content/browser/indexed_db/indexed_db_metadata.h" |
+#include "mojo/common/common_type_converters.h" |
+// TODO(cmumford): This is wrong. |
+#include "third_party/WebKit/Source/modules/indexeddb/indexed_db.mojom.h" |
+#include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.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 EnumTraits<indexed_db::mojom::DataLoss, blink::WebIDBDataLoss> { |
+ static indexed_db::mojom::DataLoss ToMojom(blink::WebIDBDataLoss input); |
+ static bool FromMojom(indexed_db::mojom::DataLoss input, |
+ blink::WebIDBDataLoss* output); |
+}; |
+ |
+template <> |
+struct StructTraits<indexed_db::mojom::DataLossInfo, |
+ content::IndexedDBDataLossInfo> { |
+ static blink::WebIDBDataLoss status(const content::IndexedDBDataLossInfo& r) { |
+ return r.status; |
+ } |
+ static String message(const content::IndexedDBDataLossInfo& r) { |
+ return String::From(r.message); |
+ } |
+ static bool Read(indexed_db::mojom::DataLossInfoDataView data, |
+ content::IndexedDBDataLossInfo* out) { |
+ if (!data.ReadStatus(&out->status)) |
+ return false; |
+ if (!data.ReadMessage(&out->message)) |
+ return false; |
+ return true; |
+ } |
+}; |
+ |
+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 indexed_db::mojom::KeyPathDataPtr data( |
+ const content::IndexedDBKeyPath& r) { |
+ indexed_db::mojom::KeyPathDataPtr data = |
+ indexed_db::mojom::KeyPathData::New(); |
+ switch (r.type()) { |
+ case blink::WebIDBKeyPathTypeNull: |
+ break; |
+ case blink::WebIDBKeyPathTypeString: |
+ data->set_str(String::From(r.string())); |
+ break; |
+ case blink::WebIDBKeyPathTypeArray: |
+ NOTREACHED(); |
+ // Maybe missing a type converter. Confirm with Yuzhu. |
+ // data->set_arr(Array<mojo::String>::From(r.array())); |
+ break; |
+ } |
+ return data; |
+ } |
+ 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( |
+ base::UTF8ToUTF16(key_path_data->get_str())); |
+ 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 key_path( |
+ const content::IndexedDBIndexMetadata& r) { |
+ return r.key_path; |
+ } |
+ static bool unique(const content::IndexedDBIndexMetadata& r) { |
+ return r.unique; |
+ } |
+ static bool multi_entry(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; |
+ } |
+}; |
+ |
+template <> |
+struct StructTraits<indexed_db::mojom::ObjectStoreMetadata, |
+ 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 key_path( |
+ 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, content::IndexedDBIndexMetadata> indexes( |
+ const content::IndexedDBObjectStoreMetadata& r) { |
+ return Map<int64_t, content::IndexedDBIndexMetadata>::From(r.indexes); |
+ } |
+ 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; |
+ } |
+}; |
+ |
+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, content::IndexedDBObjectStoreMetadata> object_stores( |
+ const content::IndexedDBDatabaseMetadata& r) { |
+ return Map<int64_t, content::IndexedDBObjectStoreMetadata>::From( |
+ r.object_stores); |
+ } |
+ 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; |
+ } |
+}; |
+ |
+} // namespace mojo |
+ |
+#endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_STRUCT_TRAITS_H_ |