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
3 // found in the LICENSE file.
5 #include "sync/engine/model_type_entity.h"
6 #include "sync/internal_api/public/non_blocking_sync_common.h"
7 #include "sync/syncable/syncable_util.h"
11 scoped_ptr
<ModelTypeEntity
> ModelTypeEntity::NewLocalItem(
12 const std::string
& client_tag
,
13 const sync_pb::EntitySpecifics
& specifics
,
15 return scoped_ptr
<ModelTypeEntity
>(new ModelTypeEntity(
16 1, 0, 0, syncer_v2::kUncommittedVersion
, true,
17 std::string(), // Sync thread will assign the initial ID.
18 syncable::GenerateSyncableHash(GetModelTypeFromSpecifics(specifics
),
20 client_tag
, // As non-unique name.
21 specifics
, false, now
, now
, std::string()));
24 scoped_ptr
<ModelTypeEntity
> ModelTypeEntity::FromServerUpdate(
25 const std::string
& id
,
26 const std::string
& client_tag_hash
,
27 const std::string
& non_unique_name
,
29 const sync_pb::EntitySpecifics
& specifics
,
33 const std::string
& encryption_key_name
) {
34 return scoped_ptr
<ModelTypeEntity
>(new ModelTypeEntity(0,
46 encryption_key_name
));
49 ModelTypeEntity::ModelTypeEntity(int64 sequence_number
,
50 int64 commit_requested_sequence_number
,
51 int64 acked_sequence_number
,
54 const std::string
& id
,
55 const std::string
& client_tag_hash
,
56 const std::string
& non_unique_name
,
57 const sync_pb::EntitySpecifics
& specifics
,
61 const std::string
& encryption_key_name
)
62 : sequence_number_(sequence_number
),
63 commit_requested_sequence_number_(commit_requested_sequence_number
),
64 acked_sequence_number_(acked_sequence_number
),
65 base_version_(base_version
),
68 client_tag_hash_(client_tag_hash
),
69 non_unique_name_(non_unique_name
),
70 specifics_(specifics
),
74 encryption_key_name_(encryption_key_name
) {
77 ModelTypeEntity::~ModelTypeEntity() {
80 bool ModelTypeEntity::IsWriteRequired() const {
84 bool ModelTypeEntity::IsUnsynced() const {
85 return sequence_number_
> acked_sequence_number_
;
88 bool ModelTypeEntity::RequiresCommitRequest() const {
89 return sequence_number_
> commit_requested_sequence_number_
;
92 bool ModelTypeEntity::UpdateIsReflection(int64 update_version
) const {
93 return base_version_
>= update_version
;
96 bool ModelTypeEntity::UpdateIsInConflict(int64 update_version
) const {
97 return IsUnsynced() && !UpdateIsReflection(update_version
);
100 void ModelTypeEntity::ApplyUpdateFromServer(
101 int64 update_version
,
103 const sync_pb::EntitySpecifics
& specifics
,
105 const std::string
& encryption_key_name
) {
106 // There was a conflict and the server just won it.
107 // This implicitly acks all outstanding commits because a received update
108 // will clobber any pending commits on the sync thread.
109 acked_sequence_number_
= sequence_number_
;
110 commit_requested_sequence_number_
= sequence_number_
;
112 base_version_
= update_version
;
113 specifics_
= specifics
;
117 void ModelTypeEntity::MakeLocalChange(
118 const sync_pb::EntitySpecifics
& specifics
) {
120 specifics_
= specifics
;
123 void ModelTypeEntity::UpdateDesiredEncryptionKey(const std::string
& name
) {
124 if (encryption_key_name_
== name
)
127 DVLOG(2) << id_
<< ": Encryption triggered commit: " << encryption_key_name_
130 // Schedule commit with the expectation that the worker will re-encrypt with
131 // the latest encryption key as it does.
135 void ModelTypeEntity::Delete() {
141 void ModelTypeEntity::InitializeCommitRequestData(
142 syncer_v2::CommitRequestData
* request
) const {
144 request
->client_tag_hash
= client_tag_hash_
;
145 request
->sequence_number
= sequence_number_
;
146 request
->base_version
= base_version_
;
147 request
->ctime
= ctime_
;
148 request
->mtime
= mtime_
;
149 request
->non_unique_name
= non_unique_name_
;
150 request
->deleted
= deleted_
;
151 request
->specifics
.CopyFrom(specifics_
);
154 void ModelTypeEntity::SetCommitRequestInProgress() {
155 commit_requested_sequence_number_
= sequence_number_
;
158 void ModelTypeEntity::ReceiveCommitResponse(
159 const std::string
& id
,
160 int64 sequence_number
,
161 int64 response_version
,
162 const std::string
& encryption_key_name
) {
163 id_
= id
; // The server can assign us a new ID in a commit response.
164 acked_sequence_number_
= sequence_number
;
165 base_version_
= response_version
;
166 encryption_key_name_
= encryption_key_name
;
169 void ModelTypeEntity::ClearTransientSyncState() {
170 // If we have any unacknowledged commit requests outstatnding, they've been
171 // dropped and we should forget about them.
172 commit_requested_sequence_number_
= acked_sequence_number_
;
175 void ModelTypeEntity::ClearSyncState() {
176 base_version_
= syncer_v2::kUncommittedVersion
;
178 sequence_number_
= 1;
179 commit_requested_sequence_number_
= 0;
180 acked_sequence_number_
= 0;
184 } // namespace syncer