Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: components/history/core/browser/typed_url_sync_metadata_database.cc

Issue 2961723003: [USS] Implement ApplySyncChanges and OnURLVisited/Modified/Deleted. (Closed)
Patch Set: handle nullptr case in bridge Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // static
99 URLID TypedURLSyncMetadataDatabase::StorageKeyToURLID(
100 const std::string& storage_key) {
101 URLID storage_key_int = 0;
102 DCHECK_EQ(storage_key.size(), sizeof(storage_key_int));
103 base::ReadBigEndian(storage_key.data(), &storage_key_int);
104 // Make sure storage_key_int is set.
105 DCHECK_NE(storage_key_int, 0);
106 return storage_key_int;
107 }
108
111 bool TypedURLSyncMetadataDatabase::InitSyncTable() { 109 bool TypedURLSyncMetadataDatabase::InitSyncTable() {
112 if (!GetDB().DoesTableExist("typed_url_sync_metadata")) { 110 if (!GetDB().DoesTableExist("typed_url_sync_metadata")) {
113 if (!GetDB().Execute("CREATE TABLE typed_url_sync_metadata (" 111 if (!GetDB().Execute("CREATE TABLE typed_url_sync_metadata ("
114 "storage_key INTEGER PRIMARY KEY NOT NULL," 112 "storage_key INTEGER PRIMARY KEY NOT NULL,"
115 "value BLOB)")) { 113 "value BLOB)")) {
116 NOTREACHED(); 114 NOTREACHED();
117 return false; 115 return false;
118 } 116 }
119 } 117 }
120 return true; 118 return true;
(...skipping 26 matching lines...) Expand all
147 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); 145 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0);
148 std::string serialized_state; 146 std::string serialized_state;
149 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) { 147 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) {
150 return true; 148 return true;
151 } 149 }
152 150
153 return state->ParseFromString(serialized_state); 151 return state->ParseFromString(serialized_state);
154 } 152 }
155 153
156 } // namespace history 154 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698