Index: components/sync/model_impl/model_type_store_impl.cc |
diff --git a/components/sync/model_impl/model_type_store_impl.cc b/components/sync/model_impl/model_type_store_impl.cc |
index 8c828230611e690252507dfe868a13af2e2111a9..542dfb90a84bef2574eea7aa31484694c1586ee9 100644 |
--- a/components/sync/model_impl/model_type_store_impl.cc |
+++ b/components/sync/model_impl/model_type_store_impl.cc |
@@ -4,6 +4,7 @@ |
#include "components/sync/model_impl/model_type_store_impl.h" |
+#include <string> |
#include <utility> |
#include "base/bind.h" |
@@ -13,6 +14,8 @@ |
#include "base/task_runner_util.h" |
#include "base/threading/thread_task_runner_handle.h" |
#include "components/sync/model_impl/model_type_store_backend.h" |
+#include "components/sync/protocol/entity_metadata.pb.h" |
+#include "components/sync/protocol/model_type_state.pb.h" |
#include "third_party/leveldatabase/src/include/leveldb/env.h" |
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
@@ -66,7 +69,8 @@ ModelTypeStoreImpl::ModelTypeStoreImpl( |
ModelType type, |
scoped_refptr<ModelTypeStoreBackend> backend, |
scoped_refptr<base::SequencedTaskRunner> backend_task_runner) |
- : backend_(backend), |
+ : type_(type), |
+ backend_(backend), |
backend_task_runner_(backend_task_runner), |
data_prefix_(FormatDataPrefix(type)), |
metadata_prefix_(FormatMetaPrefix(type)), |
@@ -227,7 +231,8 @@ void ModelTypeStoreImpl::ReadMetadataRecordsDone( |
Result result) { |
DCHECK(CalledOnValidThread()); |
if (result != Result::SUCCESS) { |
- callback.Run(result, std::move(metadata_records), std::string()); |
+ callback.Run(MakeSyncError("Reading metadata failed."), |
+ base::MakeUnique<MetadataBatch>()); |
return; |
} |
@@ -255,23 +260,57 @@ void ModelTypeStoreImpl::ReadAllMetadataDone( |
std::unique_ptr<IdList> missing_id_list, |
Result result) { |
DCHECK(CalledOnValidThread()); |
+ |
if (result != Result::SUCCESS) { |
- callback.Run(result, std::move(metadata_records), std::string()); |
+ callback.Run(MakeSyncError("Reading metadata failed."), |
+ base::MakeUnique<MetadataBatch>()); |
return; |
} |
+ std::string global_metadata = ""; |
if (!missing_id_list->empty()) { |
- // Missing global metadata record is not an error. We shouild return empty |
- // string in this case. |
- DCHECK((*missing_id_list)[0] == global_metadata_key_); |
+ // Missing global metadata record is not an error; we can just return the |
+ // default instance using the empty string above. |
+ DCHECK_EQ(global_metadata_key_, (*missing_id_list)[0]); |
DCHECK(global_metadata_records->empty()); |
- callback.Run(Result::SUCCESS, std::move(metadata_records), std::string()); |
+ } else { |
+ DCHECK_EQ(1U, global_metadata_records->size()); |
+ DCHECK_EQ(global_metadata_key_, (*global_metadata_records)[0].id); |
+ global_metadata = (*global_metadata_records)[0].value; |
+ } |
+ |
+ DeserializeMetadata(callback, global_metadata, std::move(metadata_records)); |
+} |
+ |
+void ModelTypeStoreImpl::DeserializeMetadata( |
+ const ReadMetadataCallback& callback, |
+ const std::string& global_metadata, |
+ std::unique_ptr<RecordList> metadata_records) { |
+ auto metadata_batch = base::MakeUnique<MetadataBatch>(); |
+ |
+ sync_pb::ModelTypeState state; |
+ if (!state.ParseFromString(global_metadata)) { |
+ callback.Run(MakeSyncError("Failed to deserialize model type state."), |
+ base::MakeUnique<MetadataBatch>()); |
return; |
} |
- DCHECK(!global_metadata_records->empty()); |
- DCHECK((*global_metadata_records)[0].id == global_metadata_key_); |
- callback.Run(Result::SUCCESS, std::move(metadata_records), |
- (*global_metadata_records)[0].value); |
+ metadata_batch->SetModelTypeState(state); |
+ |
+ for (const Record& r : *metadata_records.get()) { |
+ sync_pb::EntityMetadata entity_metadata; |
+ if (!entity_metadata.ParseFromString(r.value)) { |
+ callback.Run(MakeSyncError("Failed to deserialize entity metadata."), |
+ base::MakeUnique<MetadataBatch>()); |
+ return; |
+ } |
+ metadata_batch->AddMetadata(r.id, entity_metadata); |
+ } |
+ |
+ callback.Run(SyncError(), std::move(metadata_batch)); |
+} |
+ |
+SyncError ModelTypeStoreImpl::MakeSyncError(const std::string& msg) { |
+ return SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, msg, type_); |
} |
std::unique_ptr<ModelTypeStore::WriteBatch> |