Chromium Code Reviews| 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..58deb7ef7fc53c7da283ee3766274cb99f47ccee |
| --- /dev/null |
| +++ b/components/history/core/browser/typed_url_sync_metadata_database.cc |
| @@ -0,0 +1,148 @@ |
| +// Copyright 2017 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. |
| + |
| +#include "components/history/core/browser/typed_url_sync_metadata_database.h" |
| + |
| +#include "sql/statement.h" |
| + |
| +namespace history { |
| + |
| +// Description of database table: |
| +// |
| +// typed_url_sync_metadata |
| +// storage_key id in urls talbe, used by service to look up native data |
|
brettw
2017/03/13 19:58:59
talbe -> table
Gang Wu
2017/03/14 01:18:49
Done.
|
| +// with sync metadata records. |
| +// value Serialize sync EntityMetatada, which is for tracking sync |
|
brettw
2017/03/13 19:58:59
EntityMetatada -> EntityMetadata
Gang Wu
2017/03/14 01:18:49
Done.
|
| +// state of each typed url. |
| +// |
| +// typed_url_model_type_state |
| +// id should inly one record in the table and record's id should |
|
brettw
2017/03/13 19:58:59
This kind of thing is what the "meta" table is for
Gang Wu
2017/03/14 01:18:49
Yes, we did not realize there is a //sql/meta_tabl
brettw
2017/03/14 19:24:43
Can you pass the meta table to this class? (Be car
Gang Wu
2017/03/15 08:02:42
Done.
|
| +// always be 1. |
| +// value Serialize sync ModelTypeState, which is for tracking sync |
|
brettw
2017/03/13 19:58:59
"Serialize" -> "Serialization of"
Gang Wu
2017/03/14 01:18:49
Done.
|
| +// state of typed url datatype. |
| + |
| +TypedURLSyncMetadataDatabase::TypedURLSyncMetadataDatabase() {} |
| + |
| +TypedURLSyncMetadataDatabase::~TypedURLSyncMetadataDatabase() {} |
| + |
| +bool TypedURLSyncMetadataDatabase::GetAllSyncMetadata( |
| + syncer::MetadataBatch* metadata_batch) { |
| + syncer::EntityMetadataMap metadata_records_map; |
| + if (GetAllSyncEntityMetadata(&metadata_records_map)) { |
| + for (const auto& storayge_key_to_metadata : metadata_records_map) |
| + metadata_batch->AddMetadata(storayge_key_to_metadata.first, |
| + storayge_key_to_metadata.second); |
| + } else { |
| + return false; |
| + } |
| + |
| + sync_pb::ModelTypeState model_type_state; |
| + if (GetModelTypeState(&model_type_state)) { |
| + metadata_batch->SetModelTypeState(model_type_state); |
| + } else { |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( |
| + const std::string& storage_key, |
| + const sync_pb::EntityMetadata& metadata) { |
| + 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()); |
| + |
| + return s.Run(); |
| +} |
| + |
| +bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( |
| + const std::string& storage_key) { |
| + 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( |
| + const sync_pb::ModelTypeState& model_type_state) { |
| + // 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() { |
| + 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," |
|
brettw
2017/03/13 19:58:59
At the top of the file you describe the storage_ke
Gang Wu
2017/03/14 01:18:49
comment updated, here using string is because Sync
brettw
2017/03/14 19:24:43
I would prefer to have the database store the int
Gang Wu
2017/03/15 08:02:42
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::EntityMetadataMap* metadata_records) { |
| + 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( |
| + sync_pb::ModelTypeState* state) { |
| + 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 |