| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/history/core/browser/typed_url_sync_metadata_database.h" | 5 #include "components/history/core/browser/typed_url_sync_metadata_database.h" |
| 6 | 6 |
| 7 #include "base/big_endian.h" | 7 #include "base/big_endian.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "components/history/core/browser/url_row.h" | |
| 10 #include "sql/statement.h" | 9 #include "sql/statement.h" |
| 11 | 10 |
| 12 namespace history { | 11 namespace history { |
| 13 | 12 |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 16 // Key in sql::MetaTable, the value will be Serialization of sync | 15 // Key in sql::MetaTable, the value will be Serialization of sync |
| 17 // ModelTypeState, which is for tracking sync state of typed url datatype. | 16 // ModelTypeState, which is for tracking sync state of typed url datatype. |
| 18 const char kTypedURLModelTypeStateKey[] = "typed_url_model_type_state"; | 17 const char kTypedURLModelTypeStateKey[] = "typed_url_model_type_state"; |
| 19 | 18 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 48 return true; | 47 return true; |
| 49 } | 48 } |
| 50 | 49 |
| 51 bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( | 50 bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( |
| 52 syncer::ModelType model_type, | 51 syncer::ModelType model_type, |
| 53 const std::string& storage_key, | 52 const std::string& storage_key, |
| 54 const sync_pb::EntityMetadata& metadata) { | 53 const sync_pb::EntityMetadata& metadata) { |
| 55 DCHECK_EQ(model_type, syncer::TYPED_URLS) | 54 DCHECK_EQ(model_type, syncer::TYPED_URLS) |
| 56 << "Only the TYPED_URLS model type is supported"; | 55 << "Only the TYPED_URLS model type is supported"; |
| 57 | 56 |
| 58 int64_t storage_key_int = 0; | |
| 59 DCHECK_EQ(storage_key.size(), sizeof(storage_key_int)); | |
| 60 base::ReadBigEndian(storage_key.data(), &storage_key_int); | |
| 61 // Make sure storage_key_int is set. | |
| 62 DCHECK_NE(storage_key_int, 0); | |
| 63 | |
| 64 sql::Statement s(GetDB().GetUniqueStatement( | 57 sql::Statement s(GetDB().GetUniqueStatement( |
| 65 "INSERT OR REPLACE INTO typed_url_sync_metadata " | 58 "INSERT OR REPLACE INTO typed_url_sync_metadata " |
| 66 "(storage_key, value) VALUES(?, ?)")); | 59 "(storage_key, value) VALUES(?, ?)")); |
| 67 s.BindInt64(0, storage_key_int); | 60 s.BindInt64(0, StorageKeyToURLID(storage_key)); |
| 68 s.BindString(1, metadata.SerializeAsString()); | 61 s.BindString(1, metadata.SerializeAsString()); |
| 69 | 62 |
| 70 return s.Run(); | 63 return s.Run(); |
| 71 } | 64 } |
| 72 | 65 |
| 73 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( | 66 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( |
| 74 syncer::ModelType model_type, | 67 syncer::ModelType model_type, |
| 75 const std::string& storage_key) { | 68 const std::string& storage_key) { |
| 76 DCHECK_EQ(model_type, syncer::TYPED_URLS) | 69 DCHECK_EQ(model_type, syncer::TYPED_URLS) |
| 77 << "Only the TYPED_URLS model type is supported"; | 70 << "Only the TYPED_URLS model type is supported"; |
| 78 | 71 |
| 79 int64_t storage_key_int = 0; | |
| 80 DCHECK_EQ(storage_key.size(), sizeof(storage_key_int)); | |
| 81 base::ReadBigEndian(storage_key.data(), &storage_key_int); | |
| 82 // Make sure storage_key_int is set. | |
| 83 DCHECK_NE(storage_key_int, 0); | |
| 84 | |
| 85 sql::Statement s(GetDB().GetUniqueStatement( | 72 sql::Statement s(GetDB().GetUniqueStatement( |
| 86 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); | 73 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); |
| 87 s.BindInt64(0, storage_key_int); | 74 s.BindInt64(0, StorageKeyToURLID(storage_key)); |
| 88 | 75 |
| 89 return s.Run(); | 76 return s.Run(); |
| 90 } | 77 } |
| 91 | 78 |
| 92 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( | 79 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( |
| 93 syncer::ModelType model_type, | 80 syncer::ModelType model_type, |
| 94 const sync_pb::ModelTypeState& model_type_state) { | 81 const sync_pb::ModelTypeState& model_type_state) { |
| 95 DCHECK_EQ(model_type, syncer::TYPED_URLS) | 82 DCHECK_EQ(model_type, syncer::TYPED_URLS) |
| 96 << "Only the TYPED_URLS model type is supported"; | 83 << "Only the TYPED_URLS model type is supported"; |
| 97 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); | 84 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); |
| 98 | 85 |
| 99 std::string serialized_state = model_type_state.SerializeAsString(); | 86 std::string serialized_state = model_type_state.SerializeAsString(); |
| 100 return GetMetaTable().SetValue(kTypedURLModelTypeStateKey, serialized_state); | 87 return GetMetaTable().SetValue(kTypedURLModelTypeStateKey, serialized_state); |
| 101 } | 88 } |
| 102 | 89 |
| 103 bool TypedURLSyncMetadataDatabase::ClearModelTypeState( | 90 bool TypedURLSyncMetadataDatabase::ClearModelTypeState( |
| 104 syncer::ModelType model_type) { | 91 syncer::ModelType model_type) { |
| 105 DCHECK_EQ(model_type, syncer::TYPED_URLS) | 92 DCHECK_EQ(model_type, syncer::TYPED_URLS) |
| 106 << "Only the TYPED_URLS model type is supported"; | 93 << "Only the TYPED_URLS model type is supported"; |
| 107 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); | 94 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); |
| 108 return GetMetaTable().DeleteKey(kTypedURLModelTypeStateKey); | 95 return GetMetaTable().DeleteKey(kTypedURLModelTypeStateKey); |
| 109 } | 96 } |
| 110 | 97 |
| 98 URLID TypedURLSyncMetadataDatabase::StorageKeyToURLID( |
| 99 const std::string& storage_key) { |
| 100 URLID storage_key_int = 0; |
| 101 DCHECK_EQ(storage_key.size(), sizeof(storage_key_int)); |
| 102 base::ReadBigEndian(storage_key.data(), &storage_key_int); |
| 103 // Make sure storage_key_int is set. |
| 104 DCHECK_NE(storage_key_int, 0); |
| 105 return storage_key_int; |
| 106 } |
| 107 |
| 111 bool TypedURLSyncMetadataDatabase::InitSyncTable() { | 108 bool TypedURLSyncMetadataDatabase::InitSyncTable() { |
| 112 if (!GetDB().DoesTableExist("typed_url_sync_metadata")) { | 109 if (!GetDB().DoesTableExist("typed_url_sync_metadata")) { |
| 113 if (!GetDB().Execute("CREATE TABLE typed_url_sync_metadata (" | 110 if (!GetDB().Execute("CREATE TABLE typed_url_sync_metadata (" |
| 114 "storage_key INTEGER PRIMARY KEY NOT NULL," | 111 "storage_key INTEGER PRIMARY KEY NOT NULL," |
| 115 "value BLOB)")) { | 112 "value BLOB)")) { |
| 116 NOTREACHED(); | 113 NOTREACHED(); |
| 117 return false; | 114 return false; |
| 118 } | 115 } |
| 119 } | 116 } |
| 120 return true; | 117 return true; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 147 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); | 144 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); |
| 148 std::string serialized_state; | 145 std::string serialized_state; |
| 149 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) { | 146 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) { |
| 150 return true; | 147 return true; |
| 151 } | 148 } |
| 152 | 149 |
| 153 return state->ParseFromString(serialized_state); | 150 return state->ParseFromString(serialized_state); |
| 154 } | 151 } |
| 155 | 152 |
| 156 } // namespace history | 153 } // namespace history |
| OLD | NEW |