Index: components/history/core/browser/typed_url_sync_metadata_database.cc |
diff --git a/components/history/core/browser/typed_url_sync_metadata_database.cc b/components/history/core/browser/typed_url_sync_metadata_database.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4bc1e7789de4956add1e14dcb939ea15e5ca6739 |
--- /dev/null |
+++ b/components/history/core/browser/typed_url_sync_metadata_database.cc |
@@ -0,0 +1,161 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
sky
2017/03/01 04:15:24
no (c)
Gang Wu
2017/03/02 05:41:12
Done.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/history/core/browser/typed_url_sync_metadata_database.h" |
+ |
+#include "sql/statement.h" |
+ |
+namespace history { |
+ |
+TypedURLSyncMetadataDatabase::TypedURLSyncMetadataDatabase() {} |
+ |
+TypedURLSyncMetadataDatabase::~TypedURLSyncMetadataDatabase() {} |
+ |
+bool TypedURLSyncMetadataDatabase::GetAllSyncMetadata( |
+ syncer::ModelType model_type, |
+ syncer::MetadataBatch* metadata_batch) { |
+ DCHECK_EQ(model_type, syncer::TYPED_URLS) |
+ << "Only the TYPED_URLS model type is supported"; |
+ syncer::EntityMetadataMap metadata_records; |
+ if (GetAllSyncEntityMetadata(model_type, &metadata_records)) { |
+ for (const auto& pair : metadata_records) { |
sky
2017/03/01 04:15:24
no {}
Gang Wu
2017/03/02 05:41:12
Done.
|
+ metadata_batch->AddMetadata(pair.first, pair.second); |
sky
2017/03/01 04:15:24
Using std::pair for this class makes for hard to u
Gang Wu
2017/03/02 05:41:12
This is C++ STL auto iterator for std::map, I cann
|
+ } |
+ } else { |
+ return false; |
+ } |
+ |
+ sync_pb::ModelTypeState model_type_state; |
+ if (GetModelTypeState(model_type, &model_type_state)) { |
+ metadata_batch->SetModelTypeState(model_type_state); |
+ } else { |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( |
+ syncer::ModelType model_type, |
+ const std::string& storage_key, |
+ const sync_pb::EntityMetadata& metadata) { |
+ DCHECK_EQ(model_type, syncer::TYPED_URLS) |
+ << "Only the TYPED_URLS model type is supported"; |
+ |
+ sql::Statement s(GetDB().GetUniqueStatement( |
+ "INSERT OR REPLACE INTO typed_url_sync_metadata " |
+ "(storage_key, value) VALUES(?, ?)")); |
+ s.BindString(0, storage_key); |
+ s.BindString(1, metadata.SerializeAsString()); |
sky
2017/03/01 04:15:24
Who handles versioning of this string?
Gang Wu
2017/03/02 05:41:12
protobuf will control backward compatibility for m
|
+ |
+ return s.Run(); |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( |
+ syncer::ModelType model_type, |
+ const std::string& storage_key) { |
+ DCHECK_EQ(model_type, syncer::TYPED_URLS) |
+ << "Only the TYPED_URLS model type is supported"; |
+ |
+ sql::Statement s(GetDB().GetUniqueStatement( |
+ "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); |
+ s.BindString(0, storage_key); |
+ |
+ return s.Run(); |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( |
+ syncer::ModelType model_type, |
+ const sync_pb::ModelTypeState& model_type_state) { |
+ DCHECK_EQ(model_type, syncer::TYPED_URLS) |
+ << "Only the TYPED_URLS model type is supported"; |
+ |
+ // Hardcode the id to force a collision, ensuring that there remains only a |
+ // single entry. |
+ sql::Statement s(GetDB().GetUniqueStatement( |
+ "INSERT OR REPLACE INTO typed_url_model_type_state (id, value) " |
+ "VALUES(1,?)")); |
+ s.BindString(0, model_type_state.SerializeAsString()); |
+ |
+ return s.Run(); |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::ClearModelTypeState( |
+ syncer::ModelType model_type) { |
+ DCHECK_EQ(model_type, syncer::TYPED_URLS) |
+ << "Only the TYPED_URLS model type is supported"; |
+ |
+ sql::Statement s(GetDB().GetUniqueStatement( |
+ "DELETE FROM typed_url_model_type_state WHERE id=1")); |
+ |
+ return s.Run(); |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::InitSyncTable() { |
+ return InitTypedURLSyncMetadataTable() && InitModelTypeStateTable(); |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::InitTypedURLSyncMetadataTable() { |
+ if (!GetDB().DoesTableExist("typed_url_sync_metadata")) { |
+ if (!GetDB().Execute("CREATE TABLE typed_url_sync_metadata (" |
+ "storage_key VARCHAR PRIMARY KEY NOT NULL," |
sky
2017/03/01 04:15:23
Document what storage_key is. In fact please docum
Gang Wu
2017/03/02 05:41:12
Done.
|
+ "value BLOB)")) { |
+ NOTREACHED(); |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::InitModelTypeStateTable() { |
+ if (!GetDB().DoesTableExist("typed_url_model_type_state")) { |
+ if (!GetDB().Execute("CREATE TABLE typed_url_model_type_state (id INTEGER " |
+ "PRIMARY KEY, value BLOB)")) { |
+ NOTREACHED(); |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::GetAllSyncEntityMetadata( |
+ syncer::ModelType model_type, |
+ syncer::EntityMetadataMap* metadata_records) { |
+ DCHECK_EQ(model_type, syncer::TYPED_URLS) |
+ << "Only the TYPED_URLS model type is supported"; |
+ |
+ sql::Statement s(GetDB().GetUniqueStatement( |
+ "SELECT storage_key, value FROM typed_url_sync_metadata")); |
+ |
+ while (s.Step()) { |
+ std::string storage_key = s.ColumnString(0); |
+ std::string serialized_metadata = s.ColumnString(1); |
+ sync_pb::EntityMetadata metadata_record; |
+ if (metadata_record.ParseFromString(serialized_metadata)) { |
+ metadata_records->insert(std::make_pair(storage_key, metadata_record)); |
+ } else { |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
+bool TypedURLSyncMetadataDatabase::GetModelTypeState( |
+ syncer::ModelType model_type, |
+ sync_pb::ModelTypeState* state) { |
+ DCHECK_EQ(model_type, syncer::TYPED_URLS) |
+ << "Only the TYPED_URLS model type is supported"; |
+ |
+ sql::Statement s(GetDB().GetUniqueStatement( |
+ "SELECT value FROM typed_url_model_type_state WHERE id=1")); |
+ |
+ if (!s.Step()) { |
+ return true; |
+ } |
+ |
+ std::string serialized_state = s.ColumnString(0); |
+ return state->ParseFromString(serialized_state); |
+} |
+ |
+} // namespace history |