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/syncable/syncable_util.h"
10 scoped_ptr
<ModelTypeEntity
> ModelTypeEntity::NewLocalItem(
11 const std::string
& client_tag
,
12 const sync_pb::EntitySpecifics
& specifics
,
14 return scoped_ptr
<ModelTypeEntity
>(new ModelTypeEntity(
20 std::string(), // Sync thread will assign the initial ID.
21 syncable::GenerateSyncableHash(GetModelTypeFromSpecifics(specifics
),
23 client_tag
, // As non-unique name.
30 scoped_ptr
<ModelTypeEntity
> ModelTypeEntity::FromServerUpdate(
31 const std::string
& id
,
32 const std::string
& client_tag_hash
,
33 const std::string
& non_unique_name
,
35 const sync_pb::EntitySpecifics
& specifics
,
39 return scoped_ptr
<ModelTypeEntity
>(new ModelTypeEntity(0,
53 ModelTypeEntity::ModelTypeEntity(int64 sequence_number
,
54 int64 commit_requested_sequence_number
,
55 int64 acked_sequence_number
,
58 const std::string
& id
,
59 const std::string
& client_tag_hash
,
60 const std::string
& non_unique_name
,
61 const sync_pb::EntitySpecifics
& specifics
,
65 : sequence_number_(sequence_number
),
66 commit_requested_sequence_number_(commit_requested_sequence_number
),
67 acked_sequence_number_(acked_sequence_number
),
68 base_version_(base_version
),
71 client_tag_hash_(client_tag_hash
),
72 non_unique_name_(non_unique_name
),
73 specifics_(specifics
),
79 ModelTypeEntity::~ModelTypeEntity() {
82 bool ModelTypeEntity::IsWriteRequired() const {
86 bool ModelTypeEntity::IsUnsynced() const {
87 return sequence_number_
> acked_sequence_number_
;
90 bool ModelTypeEntity::RequiresCommitRequest() const {
91 return sequence_number_
> commit_requested_sequence_number_
;
94 bool ModelTypeEntity::UpdateIsReflection(int64 update_version
) const {
95 return base_version_
>= update_version
;
98 bool ModelTypeEntity::UpdateIsInConflict(int64 update_version
) const {
99 return IsUnsynced() && !UpdateIsReflection(update_version
);
102 void ModelTypeEntity::ApplyUpdateFromServer(
103 int64 update_version
,
105 const sync_pb::EntitySpecifics
& specifics
,
107 // There was a conflict and the server just won it.
108 // This implicitly acks all outstanding commits because a received update
109 // will clobber any pending commits on the sync thread.
110 acked_sequence_number_
= sequence_number_
;
111 commit_requested_sequence_number_
= sequence_number_
;
113 base_version_
= update_version
;
114 specifics_
= specifics
;
118 void ModelTypeEntity::MakeLocalChange(
119 const sync_pb::EntitySpecifics
& specifics
) {
121 specifics_
= specifics
;
124 void ModelTypeEntity::Delete() {
130 void ModelTypeEntity::InitializeCommitRequestData(
131 CommitRequestData
* request
) const {
133 request
->client_tag_hash
= client_tag_hash_
;
134 request
->sequence_number
= sequence_number_
;
135 request
->base_version
= base_version_
;
136 request
->ctime
= ctime_
;
137 request
->mtime
= mtime_
;
138 request
->non_unique_name
= non_unique_name_
;
139 request
->deleted
= deleted_
;
140 request
->specifics
.CopyFrom(specifics_
);
143 void ModelTypeEntity::SetCommitRequestInProgress() {
144 commit_requested_sequence_number_
= sequence_number_
;
147 void ModelTypeEntity::ReceiveCommitResponse(const std::string
& id
,
148 int64 sequence_number
,
149 int64 response_version
) {
150 id_
= id
; // The server can assign us a new ID in a commit response.
151 acked_sequence_number_
= sequence_number
;
152 base_version_
= response_version
;
155 void ModelTypeEntity::ClearTransientSyncState() {
156 // If we have any unacknowledged commit requests outstatnding, they've been
157 // dropped and we should forget about them.
158 commit_requested_sequence_number_
= acked_sequence_number_
;
161 void ModelTypeEntity::ClearSyncState() {
162 base_version_
= kUncommittedVersion
;
164 sequence_number_
= 1;
165 commit_requested_sequence_number_
= 0;
166 acked_sequence_number_
= 0;
170 } // namespace syncer