OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/sync/core/processor_entity_tracker.h" | 5 #include "components/sync/core/processor_entity_tracker.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/sha1.h" | 8 #include "base/sha1.h" |
9 #include "components/sync/base/time.h" | 9 #include "components/sync/base/time.h" |
10 #include "components/sync/core/non_blocking_sync_common.h" | 10 #include "components/sync/core/non_blocking_sync_common.h" |
11 #include "components/sync/syncable/syncable_util.h" | 11 #include "components/sync/syncable/syncable_util.h" |
12 | 12 |
13 namespace syncer_v2 { | 13 namespace syncer { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 void HashSpecifics(const sync_pb::EntitySpecifics& specifics, | 17 void HashSpecifics(const sync_pb::EntitySpecifics& specifics, |
18 std::string* hash) { | 18 std::string* hash) { |
19 DCHECK_GT(specifics.ByteSize(), 0); | 19 DCHECK_GT(specifics.ByteSize(), 0); |
20 base::Base64Encode(base::SHA1HashString(specifics.SerializeAsString()), hash); | 20 base::Base64Encode(base::SHA1HashString(specifics.SerializeAsString()), hash); |
21 } | 21 } |
22 | 22 |
23 } // namespace | 23 } // namespace |
24 | 24 |
25 std::unique_ptr<ProcessorEntityTracker> ProcessorEntityTracker::CreateNew( | 25 std::unique_ptr<ProcessorEntityTracker> ProcessorEntityTracker::CreateNew( |
26 const std::string& storage_key, | 26 const std::string& storage_key, |
27 const std::string& client_tag_hash, | 27 const std::string& client_tag_hash, |
28 const std::string& id, | 28 const std::string& id, |
29 base::Time creation_time) { | 29 base::Time creation_time) { |
30 // Initialize metadata | 30 // Initialize metadata |
31 sync_pb::EntityMetadata metadata; | 31 sync_pb::EntityMetadata metadata; |
32 metadata.set_client_tag_hash(client_tag_hash); | 32 metadata.set_client_tag_hash(client_tag_hash); |
33 if (!id.empty()) | 33 if (!id.empty()) |
34 metadata.set_server_id(id); | 34 metadata.set_server_id(id); |
35 metadata.set_sequence_number(0); | 35 metadata.set_sequence_number(0); |
36 metadata.set_acked_sequence_number(0); | 36 metadata.set_acked_sequence_number(0); |
37 metadata.set_server_version(kUncommittedVersion); | 37 metadata.set_server_version(kUncommittedVersion); |
38 metadata.set_creation_time(syncer::TimeToProtoTime(creation_time)); | 38 metadata.set_creation_time(TimeToProtoTime(creation_time)); |
39 | 39 |
40 return std::unique_ptr<ProcessorEntityTracker>( | 40 return std::unique_ptr<ProcessorEntityTracker>( |
41 new ProcessorEntityTracker(storage_key, &metadata)); | 41 new ProcessorEntityTracker(storage_key, &metadata)); |
42 } | 42 } |
43 | 43 |
44 std::unique_ptr<ProcessorEntityTracker> | 44 std::unique_ptr<ProcessorEntityTracker> |
45 ProcessorEntityTracker::CreateFromMetadata(const std::string& storage_key, | 45 ProcessorEntityTracker::CreateFromMetadata(const std::string& storage_key, |
46 sync_pb::EntityMetadata* metadata) { | 46 sync_pb::EntityMetadata* metadata) { |
47 return std::unique_ptr<ProcessorEntityTracker>( | 47 return std::unique_ptr<ProcessorEntityTracker>( |
48 new ProcessorEntityTracker(storage_key, metadata)); | 48 new ProcessorEntityTracker(storage_key, metadata)); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 // commit and this should follow, or the pending commit needs to be requeued. | 123 // commit and this should follow, or the pending commit needs to be requeued. |
124 commit_requested_sequence_number_ = metadata_.acked_sequence_number(); | 124 commit_requested_sequence_number_ = metadata_.acked_sequence_number(); |
125 } | 125 } |
126 | 126 |
127 void ProcessorEntityTracker::RecordAcceptedUpdate( | 127 void ProcessorEntityTracker::RecordAcceptedUpdate( |
128 const UpdateResponseData& update) { | 128 const UpdateResponseData& update) { |
129 DCHECK(!IsUnsynced()); | 129 DCHECK(!IsUnsynced()); |
130 RecordIgnoredUpdate(update); | 130 RecordIgnoredUpdate(update); |
131 metadata_.set_is_deleted(update.entity->is_deleted()); | 131 metadata_.set_is_deleted(update.entity->is_deleted()); |
132 metadata_.set_modification_time( | 132 metadata_.set_modification_time( |
133 syncer::TimeToProtoTime(update.entity->modification_time)); | 133 TimeToProtoTime(update.entity->modification_time)); |
134 UpdateSpecificsHash(update.entity->specifics); | 134 UpdateSpecificsHash(update.entity->specifics); |
135 } | 135 } |
136 | 136 |
137 void ProcessorEntityTracker::RecordForcedUpdate( | 137 void ProcessorEntityTracker::RecordForcedUpdate( |
138 const UpdateResponseData& update) { | 138 const UpdateResponseData& update) { |
139 DCHECK(IsUnsynced()); | 139 DCHECK(IsUnsynced()); |
140 // There was a conflict and the server just won it. Explicitly ack all | 140 // There was a conflict and the server just won it. Explicitly ack all |
141 // pending commits so they are never enqueued again. | 141 // pending commits so they are never enqueued again. |
142 metadata_.set_acked_sequence_number(metadata_.sequence_number()); | 142 metadata_.set_acked_sequence_number(metadata_.sequence_number()); |
143 commit_data_.reset(); | 143 commit_data_.reset(); |
144 RecordAcceptedUpdate(update); | 144 RecordAcceptedUpdate(update); |
145 } | 145 } |
146 | 146 |
147 void ProcessorEntityTracker::MakeLocalChange(std::unique_ptr<EntityData> data) { | 147 void ProcessorEntityTracker::MakeLocalChange(std::unique_ptr<EntityData> data) { |
148 DCHECK(!metadata_.client_tag_hash().empty()); | 148 DCHECK(!metadata_.client_tag_hash().empty()); |
149 DCHECK_EQ(metadata_.client_tag_hash(), data->client_tag_hash); | 149 DCHECK_EQ(metadata_.client_tag_hash(), data->client_tag_hash); |
150 | 150 |
151 if (data->modification_time.is_null()) { | 151 if (data->modification_time.is_null()) { |
152 data->modification_time = base::Time::Now(); | 152 data->modification_time = base::Time::Now(); |
153 } | 153 } |
154 | 154 |
155 IncrementSequenceNumber(); | 155 IncrementSequenceNumber(); |
156 UpdateSpecificsHash(data->specifics); | 156 UpdateSpecificsHash(data->specifics); |
157 metadata_.set_modification_time( | 157 metadata_.set_modification_time(TimeToProtoTime(data->modification_time)); |
158 syncer::TimeToProtoTime(data->modification_time)); | |
159 metadata_.set_is_deleted(false); | 158 metadata_.set_is_deleted(false); |
160 | 159 |
161 data->id = metadata_.server_id(); | 160 data->id = metadata_.server_id(); |
162 data->creation_time = syncer::ProtoTimeToTime(metadata_.creation_time()); | 161 data->creation_time = ProtoTimeToTime(metadata_.creation_time()); |
163 commit_data_.reset(); | 162 commit_data_.reset(); |
164 CacheCommitData(data.get()); | 163 CacheCommitData(data.get()); |
165 } | 164 } |
166 | 165 |
167 void ProcessorEntityTracker::Delete() { | 166 void ProcessorEntityTracker::Delete() { |
168 IncrementSequenceNumber(); | 167 IncrementSequenceNumber(); |
169 metadata_.set_modification_time(syncer::TimeToProtoTime(base::Time::Now())); | 168 metadata_.set_modification_time(TimeToProtoTime(base::Time::Now())); |
170 metadata_.set_is_deleted(true); | 169 metadata_.set_is_deleted(true); |
171 metadata_.clear_specifics_hash(); | 170 metadata_.clear_specifics_hash(); |
172 // Clear any cached pending commit data. | 171 // Clear any cached pending commit data. |
173 commit_data_.reset(); | 172 commit_data_.reset(); |
174 } | 173 } |
175 | 174 |
176 void ProcessorEntityTracker::InitializeCommitRequestData( | 175 void ProcessorEntityTracker::InitializeCommitRequestData( |
177 CommitRequestData* request) { | 176 CommitRequestData* request) { |
178 if (!metadata_.is_deleted()) { | 177 if (!metadata_.is_deleted()) { |
179 DCHECK(HasCommitData()); | 178 DCHECK(HasCommitData()); |
180 DCHECK_EQ(commit_data_->client_tag_hash, metadata_.client_tag_hash()); | 179 DCHECK_EQ(commit_data_->client_tag_hash, metadata_.client_tag_hash()); |
181 request->entity = commit_data_; | 180 request->entity = commit_data_; |
182 } else { | 181 } else { |
183 // Make an EntityData with empty specifics to indicate deletion. This is | 182 // Make an EntityData with empty specifics to indicate deletion. This is |
184 // done lazily here to simplify loading a pending deletion on startup. | 183 // done lazily here to simplify loading a pending deletion on startup. |
185 EntityData data; | 184 EntityData data; |
186 data.client_tag_hash = metadata_.client_tag_hash(); | 185 data.client_tag_hash = metadata_.client_tag_hash(); |
187 data.id = metadata_.server_id(); | 186 data.id = metadata_.server_id(); |
188 data.creation_time = syncer::ProtoTimeToTime(metadata_.creation_time()); | 187 data.creation_time = ProtoTimeToTime(metadata_.creation_time()); |
189 data.modification_time = | 188 data.modification_time = ProtoTimeToTime(metadata_.modification_time()); |
190 syncer::ProtoTimeToTime(metadata_.modification_time()); | |
191 request->entity = data.PassToPtr(); | 189 request->entity = data.PassToPtr(); |
192 } | 190 } |
193 | 191 |
194 request->sequence_number = metadata_.sequence_number(); | 192 request->sequence_number = metadata_.sequence_number(); |
195 request->base_version = metadata_.server_version(); | 193 request->base_version = metadata_.server_version(); |
196 request->specifics_hash = metadata_.specifics_hash(); | 194 request->specifics_hash = metadata_.specifics_hash(); |
197 commit_requested_sequence_number_ = metadata_.sequence_number(); | 195 commit_requested_sequence_number_ = metadata_.sequence_number(); |
198 } | 196 } |
199 | 197 |
200 void ProcessorEntityTracker::ReceiveCommitResponse( | 198 void ProcessorEntityTracker::ReceiveCommitResponse( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 240 |
243 void ProcessorEntityTracker::UpdateSpecificsHash( | 241 void ProcessorEntityTracker::UpdateSpecificsHash( |
244 const sync_pb::EntitySpecifics& specifics) { | 242 const sync_pb::EntitySpecifics& specifics) { |
245 if (specifics.ByteSize() > 0) { | 243 if (specifics.ByteSize() > 0) { |
246 HashSpecifics(specifics, metadata_.mutable_specifics_hash()); | 244 HashSpecifics(specifics, metadata_.mutable_specifics_hash()); |
247 } else { | 245 } else { |
248 metadata_.clear_specifics_hash(); | 246 metadata_.clear_specifics_hash(); |
249 } | 247 } |
250 } | 248 } |
251 | 249 |
252 } // namespace syncer_v2 | 250 } // namespace syncer |
OLD | NEW |