OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/history/core/browser/typed_url_sync_metadata_database.h" |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "sql/statement.h" |
| 9 |
| 10 namespace history { |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Key in sql::MetaTable, the value will be Serialization of sync |
| 15 // ModelTypeState, which is for tracking sync state of typed url datatype. |
| 16 const char kTypedURLModelTypeStateKey[] = "typed_url_model_type_state"; |
| 17 |
| 18 } // namespace |
| 19 |
| 20 // Description of database table: |
| 21 // |
| 22 // typed_url_sync_metadata |
| 23 // storage_key the rowid of an entry in urls table, used by service to |
| 24 // look up native data with sync metadata records. |
| 25 // value Serialize sync EntityMetadata, which is for tracking sync |
| 26 // state of each typed url. |
| 27 |
| 28 TypedURLSyncMetadataDatabase::TypedURLSyncMetadataDatabase() {} |
| 29 |
| 30 TypedURLSyncMetadataDatabase::~TypedURLSyncMetadataDatabase() {} |
| 31 |
| 32 bool TypedURLSyncMetadataDatabase::GetAllSyncMetadata( |
| 33 syncer::MetadataBatch* metadata_batch) { |
| 34 syncer::EntityMetadataMap metadata_records_map; |
| 35 |
| 36 // TODO(gangwu): crbug.com/701670 remove metadata_records_map to reduce memory |
| 37 // usage and time. |
| 38 if (GetAllSyncEntityMetadata(&metadata_records_map)) { |
| 39 for (const auto& storage_key_to_metadata : metadata_records_map) { |
| 40 metadata_batch->AddMetadata(storage_key_to_metadata.first, |
| 41 storage_key_to_metadata.second); |
| 42 } |
| 43 } else { |
| 44 return false; |
| 45 } |
| 46 |
| 47 sync_pb::ModelTypeState model_type_state; |
| 48 if (GetModelTypeState(&model_type_state)) { |
| 49 metadata_batch->SetModelTypeState(model_type_state); |
| 50 } else { |
| 51 return false; |
| 52 } |
| 53 |
| 54 return true; |
| 55 } |
| 56 |
| 57 bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( |
| 58 const std::string& storage_key, |
| 59 const sync_pb::EntityMetadata& metadata) { |
| 60 int64_t storage_key_int = 0; |
| 61 if (!base::StringToInt64(storage_key, &storage_key_int)) { |
| 62 return false; |
| 63 } |
| 64 sql::Statement s(GetDB().GetUniqueStatement( |
| 65 "INSERT OR REPLACE INTO typed_url_sync_metadata " |
| 66 "(storage_key, value) VALUES(?, ?)")); |
| 67 s.BindInt64(0, storage_key_int); |
| 68 s.BindString(1, metadata.SerializeAsString()); |
| 69 |
| 70 return s.Run(); |
| 71 } |
| 72 |
| 73 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( |
| 74 const std::string& storage_key) { |
| 75 int64_t storage_key_int = 0; |
| 76 if (!base::StringToInt64(storage_key, &storage_key_int)) { |
| 77 return false; |
| 78 } |
| 79 sql::Statement s(GetDB().GetUniqueStatement( |
| 80 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); |
| 81 s.BindInt64(0, storage_key_int); |
| 82 |
| 83 return s.Run(); |
| 84 } |
| 85 |
| 86 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( |
| 87 const sync_pb::ModelTypeState& model_type_state) { |
| 88 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); |
| 89 |
| 90 std::string serialized_state = model_type_state.SerializeAsString(); |
| 91 return GetMetaTable().SetValue(kTypedURLModelTypeStateKey, serialized_state); |
| 92 } |
| 93 |
| 94 bool TypedURLSyncMetadataDatabase::ClearModelTypeState() { |
| 95 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); |
| 96 return GetMetaTable().DeleteKey(kTypedURLModelTypeStateKey); |
| 97 } |
| 98 |
| 99 bool TypedURLSyncMetadataDatabase::InitSyncTable() { |
| 100 if (!GetDB().DoesTableExist("typed_url_sync_metadata")) { |
| 101 if (!GetDB().Execute("CREATE TABLE typed_url_sync_metadata (" |
| 102 "storage_key INTEGER PRIMARY KEY NOT NULL," |
| 103 "value BLOB)")) { |
| 104 NOTREACHED(); |
| 105 return false; |
| 106 } |
| 107 } |
| 108 return true; |
| 109 } |
| 110 |
| 111 bool TypedURLSyncMetadataDatabase::GetAllSyncEntityMetadata( |
| 112 syncer::EntityMetadataMap* metadata_records) { |
| 113 sql::Statement s(GetDB().GetUniqueStatement( |
| 114 "SELECT storage_key, value FROM typed_url_sync_metadata")); |
| 115 |
| 116 while (s.Step()) { |
| 117 std::string storage_key = base::Int64ToString(s.ColumnInt64(0)); |
| 118 std::string serialized_metadata = s.ColumnString(1); |
| 119 sync_pb::EntityMetadata metadata_record; |
| 120 if (metadata_record.ParseFromString(serialized_metadata)) { |
| 121 metadata_records->insert(std::make_pair(storage_key, metadata_record)); |
| 122 } else { |
| 123 return false; |
| 124 } |
| 125 } |
| 126 return true; |
| 127 } |
| 128 |
| 129 bool TypedURLSyncMetadataDatabase::GetModelTypeState( |
| 130 sync_pb::ModelTypeState* state) { |
| 131 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); |
| 132 std::string serialized_state; |
| 133 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) { |
| 134 return true; |
| 135 } |
| 136 |
| 137 return state->ParseFromString(serialized_state); |
| 138 } |
| 139 |
| 140 } // namespace history |
OLD | NEW |