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