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 "sync/internal_api/public/processor_entity_tracker.h" | 5 #include "sync/internal_api/public/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 "sync/internal_api/public/non_blocking_sync_common.h" | 12 #include "sync/internal_api/public/non_blocking_sync_common.h" |
13 #include "sync/syncable/syncable_util.h" | 13 #include "sync/syncable/syncable_util.h" |
14 #include "sync/util/time.h" | 14 #include "sync/util/time.h" |
15 | 15 |
16 namespace syncer_v2 { | 16 namespace syncer_v2 { |
17 | 17 |
| 18 namespace { |
| 19 |
| 20 void HashSpecifics(const sync_pb::EntitySpecifics& specifics, |
| 21 std::string* hash) { |
| 22 base::Base64Encode(base::SHA1HashString(specifics.SerializeAsString()), hash); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
18 scoped_ptr<ProcessorEntityTracker> ProcessorEntityTracker::CreateNew( | 27 scoped_ptr<ProcessorEntityTracker> ProcessorEntityTracker::CreateNew( |
19 const std::string& client_tag, | 28 const std::string& client_tag, |
20 const std::string& client_tag_hash, | 29 const std::string& client_tag_hash, |
21 const std::string& id, | 30 const std::string& id, |
22 base::Time creation_time) { | 31 base::Time creation_time) { |
23 // Initialize metadata | 32 // Initialize metadata |
24 sync_pb::EntityMetadata metadata; | 33 sync_pb::EntityMetadata metadata; |
25 metadata.set_client_tag_hash(client_tag_hash); | 34 metadata.set_client_tag_hash(client_tag_hash); |
26 if (!id.empty()) | 35 if (!id.empty()) |
27 metadata.set_server_id(id); | 36 metadata.set_server_id(id); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 data->client_tag_hash = metadata_.client_tag_hash(); | 68 data->client_tag_hash = metadata_.client_tag_hash(); |
60 } | 69 } |
61 commit_data_ = data->PassToPtr(); | 70 commit_data_ = data->PassToPtr(); |
62 DCHECK(HasCommitData()); | 71 DCHECK(HasCommitData()); |
63 } | 72 } |
64 | 73 |
65 bool ProcessorEntityTracker::HasCommitData() const { | 74 bool ProcessorEntityTracker::HasCommitData() const { |
66 return !commit_data_->client_tag_hash.empty(); | 75 return !commit_data_->client_tag_hash.empty(); |
67 } | 76 } |
68 | 77 |
| 78 bool ProcessorEntityTracker::MatchesSpecificsHash( |
| 79 const sync_pb::EntitySpecifics& specifics) const { |
| 80 DCHECK(specifics.ByteSize() > 0); |
| 81 std::string hash; |
| 82 HashSpecifics(specifics, &hash); |
| 83 return hash == metadata_.specifics_hash(); |
| 84 } |
| 85 |
69 bool ProcessorEntityTracker::IsUnsynced() const { | 86 bool ProcessorEntityTracker::IsUnsynced() const { |
70 return metadata_.sequence_number() > metadata_.acked_sequence_number(); | 87 return metadata_.sequence_number() > metadata_.acked_sequence_number(); |
71 } | 88 } |
72 | 89 |
73 bool ProcessorEntityTracker::RequiresCommitRequest() const { | 90 bool ProcessorEntityTracker::RequiresCommitRequest() const { |
74 return metadata_.sequence_number() > commit_requested_sequence_number_; | 91 return metadata_.sequence_number() > commit_requested_sequence_number_; |
75 } | 92 } |
76 | 93 |
77 bool ProcessorEntityTracker::RequiresCommitData() const { | 94 bool ProcessorEntityTracker::RequiresCommitData() const { |
78 return RequiresCommitRequest() && !HasCommitData() && !metadata_.is_deleted(); | 95 return RequiresCommitRequest() && !HasCommitData() && !metadata_.is_deleted(); |
(...skipping 10 matching lines...) Expand all Loading... |
89 bool ProcessorEntityTracker::UpdateIsInConflict(int64_t update_version) const { | 106 bool ProcessorEntityTracker::UpdateIsInConflict(int64_t update_version) const { |
90 return IsUnsynced() && !UpdateIsReflection(update_version); | 107 return IsUnsynced() && !UpdateIsReflection(update_version); |
91 } | 108 } |
92 | 109 |
93 void ProcessorEntityTracker::ApplyUpdateFromServer( | 110 void ProcessorEntityTracker::ApplyUpdateFromServer( |
94 const UpdateResponseData& response_data) { | 111 const UpdateResponseData& response_data) { |
95 DCHECK(metadata_.has_client_tag_hash()); | 112 DCHECK(metadata_.has_client_tag_hash()); |
96 DCHECK(!metadata_.client_tag_hash().empty()); | 113 DCHECK(!metadata_.client_tag_hash().empty()); |
97 DCHECK(metadata_.has_sequence_number()); | 114 DCHECK(metadata_.has_sequence_number()); |
98 | 115 |
99 // TODO(stanisc): crbug/561829: Filter out update if specifics hash hasn't | |
100 // changed. | |
101 | |
102 // TODO(stanisc): crbug/521867: Understand and verify the conflict resolution | 116 // TODO(stanisc): crbug/521867: Understand and verify the conflict resolution |
103 // logic here. | 117 // logic here. |
104 // There was a conflict and the server just won it. | 118 // There was a conflict and the server just won it. |
105 // This implicitly acks all outstanding commits because a received update | 119 // This implicitly acks all outstanding commits because a received update |
106 // will clobber any pending commits on the sync thread. | 120 // will clobber any pending commits on the sync thread. |
107 metadata_.set_acked_sequence_number(metadata_.sequence_number()); | 121 metadata_.set_acked_sequence_number(metadata_.sequence_number()); |
108 commit_requested_sequence_number_ = metadata_.sequence_number(); | 122 commit_requested_sequence_number_ = metadata_.sequence_number(); |
109 | 123 |
110 metadata_.set_is_deleted(response_data.entity->is_deleted()); | 124 metadata_.set_is_deleted(response_data.entity->is_deleted()); |
111 metadata_.set_server_version(response_data.response_version); | 125 metadata_.set_server_version(response_data.response_version); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 210 |
197 void ProcessorEntityTracker::IncrementSequenceNumber() { | 211 void ProcessorEntityTracker::IncrementSequenceNumber() { |
198 DCHECK(metadata_.has_sequence_number()); | 212 DCHECK(metadata_.has_sequence_number()); |
199 metadata_.set_sequence_number(metadata_.sequence_number() + 1); | 213 metadata_.set_sequence_number(metadata_.sequence_number() + 1); |
200 } | 214 } |
201 | 215 |
202 // Update hash string for EntitySpecifics. | 216 // Update hash string for EntitySpecifics. |
203 void ProcessorEntityTracker::UpdateSpecificsHash( | 217 void ProcessorEntityTracker::UpdateSpecificsHash( |
204 const sync_pb::EntitySpecifics& specifics) { | 218 const sync_pb::EntitySpecifics& specifics) { |
205 if (specifics.ByteSize() > 0) { | 219 if (specifics.ByteSize() > 0) { |
206 std::string hash_input; | 220 HashSpecifics(specifics, metadata_.mutable_specifics_hash()); |
207 specifics.AppendToString(&hash_input); | |
208 base::Base64Encode(base::SHA1HashString(hash_input), | |
209 metadata_.mutable_specifics_hash()); | |
210 } else { | 221 } else { |
211 metadata_.clear_specifics_hash(); | 222 metadata_.clear_specifics_hash(); |
212 } | 223 } |
213 } | 224 } |
214 | 225 |
215 } // namespace syncer_v2 | 226 } // namespace syncer_v2 |
OLD | NEW |