Fix UserSessionManager::Shutdown ordering.
[chromium-blink-merge.git] / sync / engine / model_type_entity.cc
blob2417d39a802c20a77234e98da1d5707438fd8101
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"
9 namespace syncer {
11 scoped_ptr<ModelTypeEntity> ModelTypeEntity::NewLocalItem(
12 const std::string& client_tag,
13 const sync_pb::EntitySpecifics& specifics,
14 base::Time now) {
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),
19 client_tag),
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,
28 int64 version,
29 const sync_pb::EntitySpecifics& specifics,
30 bool deleted,
31 base::Time ctime,
32 base::Time mtime,
33 const std::string& encryption_key_name) {
34 return scoped_ptr<ModelTypeEntity>(new ModelTypeEntity(0,
37 version,
38 true,
39 id,
40 client_tag_hash,
41 non_unique_name,
42 specifics,
43 deleted,
44 ctime,
45 mtime,
46 encryption_key_name));
49 ModelTypeEntity::ModelTypeEntity(int64 sequence_number,
50 int64 commit_requested_sequence_number,
51 int64 acked_sequence_number,
52 int64 base_version,
53 bool is_dirty,
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,
58 bool deleted,
59 base::Time ctime,
60 base::Time mtime,
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),
66 is_dirty_(is_dirty),
67 id_(id),
68 client_tag_hash_(client_tag_hash),
69 non_unique_name_(non_unique_name),
70 specifics_(specifics),
71 deleted_(deleted),
72 ctime_(ctime),
73 mtime_(mtime),
74 encryption_key_name_(encryption_key_name) {
77 ModelTypeEntity::~ModelTypeEntity() {
80 bool ModelTypeEntity::IsWriteRequired() const {
81 return is_dirty_;
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,
102 bool deleted,
103 const sync_pb::EntitySpecifics& specifics,
104 base::Time mtime,
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;
114 mtime_ = mtime;
117 void ModelTypeEntity::MakeLocalChange(
118 const sync_pb::EntitySpecifics& specifics) {
119 sequence_number_++;
120 specifics_ = specifics;
123 void ModelTypeEntity::UpdateDesiredEncryptionKey(const std::string& name) {
124 if (encryption_key_name_ == name)
125 return;
127 DVLOG(2) << id_ << ": Encryption triggered commit: " << encryption_key_name_
128 << " -> " << name;
130 // Schedule commit with the expectation that the worker will re-encrypt with
131 // the latest encryption key as it does.
132 sequence_number_++;
135 void ModelTypeEntity::Delete() {
136 sequence_number_++;
137 specifics_.Clear();
138 deleted_ = true;
141 void ModelTypeEntity::InitializeCommitRequestData(
142 syncer_v2::CommitRequestData* request) const {
143 request->id = id_;
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;
177 is_dirty_ = true;
178 sequence_number_ = 1;
179 commit_requested_sequence_number_ = 0;
180 acked_sequence_number_ = 0;
181 id_.clear();
184 } // namespace syncer