Fix UserSessionManager::Shutdown ordering.
[chromium-blink-merge.git] / sync / engine / commit_util.cc
blobed298b345c9854c6ccc56223d49f8e1c90d6f941
1 // Copyright 2013 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/commit_util.h"
7 #include <limits>
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/strings/string_util.h"
13 #include "sync/engine/syncer_proto_util.h"
14 #include "sync/internal_api/public/base/attachment_id_proto.h"
15 #include "sync/internal_api/public/base/unique_position.h"
16 #include "sync/protocol/bookmark_specifics.pb.h"
17 #include "sync/protocol/sync.pb.h"
18 #include "sync/sessions/sync_session.h"
19 #include "sync/syncable/directory.h"
20 #include "sync/syncable/entry.h"
21 #include "sync/syncable/model_neutral_mutable_entry.h"
22 #include "sync/syncable/syncable_base_transaction.h"
23 #include "sync/syncable/syncable_base_write_transaction.h"
24 #include "sync/syncable/syncable_changes_version.h"
25 #include "sync/syncable/syncable_proto_util.h"
26 #include "sync/syncable/syncable_util.h"
27 #include "sync/util/time.h"
29 using std::set;
30 using std::string;
31 using std::vector;
33 namespace syncer {
35 using syncable::Entry;
36 using syncable::Id;
38 namespace commit_util {
40 void AddExtensionsActivityToMessage(
41 ExtensionsActivity* activity,
42 ExtensionsActivity::Records* extensions_activity_buffer,
43 sync_pb::CommitMessage* message) {
44 // This isn't perfect, since the set of extensions activity may not correlate
45 // exactly with the items being committed. That's OK as long as we're looking
46 // for a rough estimate of extensions activity, not an precise mapping of
47 // which commits were triggered by which extension.
49 // We will push this list of extensions activity back into the
50 // ExtensionsActivityMonitor if this commit fails. That's why we must keep a
51 // copy of these records in the session.
52 activity->GetAndClearRecords(extensions_activity_buffer);
54 const ExtensionsActivity::Records& records = *extensions_activity_buffer;
55 for (ExtensionsActivity::Records::const_iterator it =
56 records.begin();
57 it != records.end(); ++it) {
58 sync_pb::ChromiumExtensionsActivity* activity_message =
59 message->add_extensions_activity();
60 activity_message->set_extension_id(it->second.extension_id);
61 activity_message->set_bookmark_writes_since_last_commit(
62 it->second.bookmark_write_count);
66 void AddClientConfigParamsToMessage(
67 ModelTypeSet enabled_types,
68 sync_pb::CommitMessage* message) {
69 sync_pb::ClientConfigParams* config_params = message->mutable_config_params();
70 for (ModelTypeSet::Iterator it = enabled_types.First(); it.Good(); it.Inc()) {
71 if (ProxyTypes().Has(it.Get()))
72 continue;
73 int field_number = GetSpecificsFieldNumberFromModelType(it.Get());
74 config_params->mutable_enabled_type_ids()->Add(field_number);
76 config_params->set_tabs_datatype_enabled(
77 enabled_types.Has(syncer::PROXY_TABS));
80 namespace {
82 void SetEntrySpecifics(const Entry& meta_entry,
83 sync_pb::SyncEntity* sync_entry) {
84 // Add the new style extension and the folder bit.
85 sync_entry->mutable_specifics()->CopyFrom(meta_entry.GetSpecifics());
86 sync_entry->set_folder(meta_entry.GetIsDir());
88 CHECK(!sync_entry->specifics().password().has_client_only_encrypted_data());
89 DCHECK_EQ(meta_entry.GetModelType(), GetModelType(*sync_entry));
92 void SetAttachmentIds(const Entry& meta_entry,
93 sync_pb::SyncEntity* sync_entry) {
94 const sync_pb::AttachmentMetadata& attachment_metadata =
95 meta_entry.GetAttachmentMetadata();
96 for (int i = 0; i < attachment_metadata.record_size(); ++i) {
97 *sync_entry->add_attachment_id() = attachment_metadata.record(i).id();
101 } // namespace
103 void BuildCommitItem(
104 const syncable::Entry& meta_entry,
105 sync_pb::SyncEntity* sync_entry) {
106 syncable::Id id = meta_entry.GetId();
107 sync_entry->set_id_string(SyncableIdToProto(id));
109 string name = meta_entry.GetNonUniqueName();
110 CHECK(!name.empty()); // Make sure this isn't an update.
111 // Note: Truncation is also performed in WriteNode::SetTitle(..). But this
112 // call is still necessary to handle any title changes that might originate
113 // elsewhere, or already be persisted in the directory.
114 base::TruncateUTF8ToByteSize(name, 255, &name);
115 sync_entry->set_name(name);
117 // Set the non_unique_name. If we do, the server ignores
118 // the |name| value (using |non_unique_name| instead), and will return
119 // in the CommitResponse a unique name if one is generated.
120 // We send both because it may aid in logging.
121 sync_entry->set_non_unique_name(name);
123 if (!meta_entry.GetUniqueClientTag().empty()) {
124 sync_entry->set_client_defined_unique_tag(
125 meta_entry.GetUniqueClientTag());
128 // Deleted items with server-unknown parent ids can be a problem so we set
129 // the parent to 0. (TODO(sync): Still true in protocol?).
130 Id new_parent_id;
131 if (meta_entry.GetIsDel() &&
132 !meta_entry.GetParentId().ServerKnows()) {
133 new_parent_id = syncable::BaseTransaction::root_id();
134 } else {
135 new_parent_id = meta_entry.GetParentId();
138 if (meta_entry.ShouldMaintainHierarchy()) {
139 sync_entry->set_parent_id_string(SyncableIdToProto(new_parent_id));
142 // If our parent has changed, send up the old one so the server
143 // can correctly deal with multiple parents.
144 // TODO(nick): With the server keeping track of the primary sync parent,
145 // it should not be necessary to provide the old_parent_id: the version
146 // number should suffice.
147 Id server_parent_id = meta_entry.GetServerParentId();
148 if (new_parent_id != server_parent_id && !server_parent_id.IsNull() &&
149 0 != meta_entry.GetBaseVersion() &&
150 syncable::CHANGES_VERSION != meta_entry.GetBaseVersion()) {
151 sync_entry->set_old_parent_id(SyncableIdToProto(server_parent_id));
154 int64 version = meta_entry.GetBaseVersion();
155 if (syncable::CHANGES_VERSION == version || 0 == version) {
156 // Undeletions are only supported for items that have a client tag.
157 DCHECK(!id.ServerKnows() ||
158 !meta_entry.GetUniqueClientTag().empty())
159 << meta_entry;
161 // Version 0 means to create or undelete an object.
162 sync_entry->set_version(0);
163 } else {
164 DCHECK(id.ServerKnows()) << meta_entry;
165 sync_entry->set_version(meta_entry.GetBaseVersion());
167 sync_entry->set_ctime(TimeToProtoTime(meta_entry.GetCtime()));
168 sync_entry->set_mtime(TimeToProtoTime(meta_entry.GetMtime()));
170 SetAttachmentIds(meta_entry, sync_entry);
172 // Handle bookmarks separately.
173 if (meta_entry.GetSpecifics().has_bookmark()) {
174 if (meta_entry.GetIsDel()) {
175 sync_entry->set_deleted(true);
176 } else {
177 // Both insert_after_item_id and position_in_parent fields are set only
178 // for legacy reasons. See comments in sync.proto for more information.
179 const Id& prev_id = meta_entry.GetPredecessorId();
180 string prev_id_string =
181 prev_id.IsNull() ? string() : prev_id.GetServerId();
182 sync_entry->set_insert_after_item_id(prev_id_string);
183 sync_entry->set_position_in_parent(
184 meta_entry.GetUniquePosition().ToInt64());
185 meta_entry.GetUniquePosition().ToProto(
186 sync_entry->mutable_unique_position());
188 // Always send specifics for bookmarks.
189 SetEntrySpecifics(meta_entry, sync_entry);
190 return;
193 // Deletion is final on the server, let's move things and then delete them.
194 if (meta_entry.GetIsDel()) {
195 sync_entry->set_deleted(true);
197 sync_pb::EntitySpecifics type_only_specifics;
198 AddDefaultFieldValue(meta_entry.GetModelType(),
199 sync_entry->mutable_specifics());
200 } else {
201 SetEntrySpecifics(meta_entry, sync_entry);
205 // Helpers for ProcessSingleCommitResponse.
206 namespace {
208 void LogServerError(const sync_pb::CommitResponse_EntryResponse& res) {
209 if (res.has_error_message())
210 LOG(WARNING) << " " << res.error_message();
211 else
212 LOG(WARNING) << " No detailed error message returned from server";
215 const string& GetResultingPostCommitName(
216 const sync_pb::SyncEntity& committed_entry,
217 const sync_pb::CommitResponse_EntryResponse& entry_response) {
218 const string& response_name =
219 SyncerProtoUtil::NameFromCommitEntryResponse(entry_response);
220 if (!response_name.empty())
221 return response_name;
222 return SyncerProtoUtil::NameFromSyncEntity(committed_entry);
225 bool UpdateVersionAfterCommit(
226 const sync_pb::SyncEntity& committed_entry,
227 const sync_pb::CommitResponse_EntryResponse& entry_response,
228 const syncable::Id& pre_commit_id,
229 syncable::ModelNeutralMutableEntry* local_entry) {
230 int64 old_version = local_entry->GetBaseVersion();
231 int64 new_version = entry_response.version();
232 bool bad_commit_version = false;
233 if (committed_entry.deleted() &&
234 !local_entry->GetUniqueClientTag().empty()) {
235 // If the item was deleted, and it's undeletable (uses the client tag),
236 // change the version back to zero. We must set the version to zero so
237 // that the server knows to re-create the item if it gets committed
238 // later for undeletion.
239 new_version = 0;
240 } else if (!pre_commit_id.ServerKnows()) {
241 bad_commit_version = 0 == new_version;
242 } else {
243 bad_commit_version = old_version > new_version;
245 if (bad_commit_version) {
246 LOG(ERROR) << "Bad version in commit return for " << *local_entry
247 << " new_id:" << SyncableIdFromProto(entry_response.id_string())
248 << " new_version:" << entry_response.version();
249 return false;
252 // Update the base version and server version. The base version must change
253 // here, even if syncing_was_set is false; that's because local changes were
254 // on top of the successfully committed version.
255 local_entry->PutBaseVersion(new_version);
256 DVLOG(1) << "Commit is changing base version of " << local_entry->GetId()
257 << " to: " << new_version;
258 local_entry->PutServerVersion(new_version);
259 return true;
262 bool ChangeIdAfterCommit(
263 const sync_pb::CommitResponse_EntryResponse& entry_response,
264 const syncable::Id& pre_commit_id,
265 syncable::ModelNeutralMutableEntry* local_entry) {
266 syncable::BaseWriteTransaction* trans = local_entry->base_write_transaction();
267 const syncable::Id& entry_response_id =
268 SyncableIdFromProto(entry_response.id_string());
269 if (entry_response_id != pre_commit_id) {
270 if (pre_commit_id.ServerKnows()) {
271 // The server can sometimes generate a new ID on commit; for example,
272 // when committing an undeletion.
273 DVLOG(1) << " ID changed while committing an old entry. "
274 << pre_commit_id << " became " << entry_response_id << ".";
276 syncable::ModelNeutralMutableEntry same_id(
277 trans,
278 syncable::GET_BY_ID,
279 entry_response_id);
280 // We should trap this before this function.
281 if (same_id.good()) {
282 LOG(ERROR) << "ID clash with id " << entry_response_id
283 << " during commit " << same_id;
284 return false;
286 ChangeEntryIDAndUpdateChildren(trans, local_entry, entry_response_id);
287 DVLOG(1) << "Changing ID to " << entry_response_id;
289 return true;
292 void UpdateServerFieldsAfterCommit(
293 const sync_pb::SyncEntity& committed_entry,
294 const sync_pb::CommitResponse_EntryResponse& entry_response,
295 syncable::ModelNeutralMutableEntry* local_entry) {
297 // We just committed an entry successfully, and now we want to make our view
298 // of the server state consistent with the server state. We must be careful;
299 // |entry_response| and |committed_entry| have some identically named
300 // fields. We only want to consider fields from |committed_entry| when there
301 // is not an overriding field in the |entry_response|. We do not want to
302 // update the server data from the local data in the entry -- it's possible
303 // that the local data changed during the commit, and even if not, the server
304 // has the last word on the values of several properties.
306 local_entry->PutServerIsDel(committed_entry.deleted());
307 if (committed_entry.deleted()) {
308 // Don't clobber any other fields of deleted objects.
309 return;
312 local_entry->PutServerIsDir(
313 (committed_entry.folder() ||
314 committed_entry.bookmarkdata().bookmark_folder()));
315 local_entry->PutServerSpecifics(committed_entry.specifics());
316 local_entry->PutServerAttachmentMetadata(
317 CreateAttachmentMetadata(committed_entry.attachment_id()));
318 local_entry->PutServerMtime(ProtoTimeToTime(committed_entry.mtime()));
319 local_entry->PutServerCtime(ProtoTimeToTime(committed_entry.ctime()));
320 if (committed_entry.has_unique_position()) {
321 local_entry->PutServerUniquePosition(
322 UniquePosition::FromProto(
323 committed_entry.unique_position()));
326 // TODO(nick): The server doesn't set entry_response.server_parent_id in
327 // practice; to update SERVER_PARENT_ID appropriately here we'd need to
328 // get the post-commit ID of the parent indicated by
329 // committed_entry.parent_id_string(). That should be inferrable from the
330 // information we have, but it's a bit convoluted to pull it out directly.
331 // Getting this right is important: SERVER_PARENT_ID gets fed back into
332 // old_parent_id during the next commit.
333 local_entry->PutServerParentId(local_entry->GetParentId());
334 local_entry->PutServerNonUniqueName(
335 GetResultingPostCommitName(committed_entry, entry_response));
337 if (local_entry->GetIsUnappliedUpdate()) {
338 // This shouldn't happen; an unapplied update shouldn't be committed, and
339 // if it were, the commit should have failed. But if it does happen: we've
340 // just overwritten the update info, so clear the flag.
341 local_entry->PutIsUnappliedUpdate(false);
345 void ProcessSuccessfulCommitResponse(
346 const sync_pb::SyncEntity& committed_entry,
347 const sync_pb::CommitResponse_EntryResponse& entry_response,
348 const syncable::Id& pre_commit_id,
349 syncable::ModelNeutralMutableEntry* local_entry,
350 bool dirty_sync_was_set, set<syncable::Id>* deleted_folders) {
351 DCHECK(local_entry->GetIsUnsynced());
353 // Update SERVER_VERSION and BASE_VERSION.
354 if (!UpdateVersionAfterCommit(committed_entry, entry_response, pre_commit_id,
355 local_entry)) {
356 LOG(ERROR) << "Bad version in commit return for " << *local_entry
357 << " new_id:" << SyncableIdFromProto(entry_response.id_string())
358 << " new_version:" << entry_response.version();
359 return;
362 // If the server gave us a new ID, apply it.
363 if (!ChangeIdAfterCommit(entry_response, pre_commit_id, local_entry)) {
364 return;
367 // Update our stored copy of the server state.
368 UpdateServerFieldsAfterCommit(committed_entry, entry_response, local_entry);
370 // If the item doesn't need to be committed again (an item might need to be
371 // committed again if it changed locally during the commit), we can remove
372 // it from the unsynced list.
373 if (!dirty_sync_was_set) {
374 local_entry->PutIsUnsynced(false);
377 // Make a note of any deleted folders, whose children would have
378 // been recursively deleted.
379 // TODO(nick): Here, commit_message.deleted() would be more correct than
380 // local_entry->GetIsDel(). For example, an item could be renamed, and then
381 // deleted during the commit of the rename. Unit test & fix.
382 if (local_entry->GetIsDir() && local_entry->GetIsDel()) {
383 deleted_folders->insert(local_entry->GetId());
387 } // namespace
389 sync_pb::CommitResponse::ResponseType
390 ProcessSingleCommitResponse(
391 syncable::BaseWriteTransaction* trans,
392 const sync_pb::CommitResponse_EntryResponse& server_entry,
393 const sync_pb::SyncEntity& commit_request_entry,
394 int64 metahandle,
395 set<syncable::Id>* deleted_folders) {
396 syncable::ModelNeutralMutableEntry local_entry(
397 trans,
398 syncable::GET_BY_HANDLE,
399 metahandle);
400 CHECK(local_entry.good());
401 bool dirty_sync_was_set = local_entry.GetDirtySync();
402 local_entry.PutDirtySync(false);
403 local_entry.PutSyncing(false);
405 sync_pb::CommitResponse::ResponseType response = server_entry.response_type();
406 if (!sync_pb::CommitResponse::ResponseType_IsValid(response)) {
407 LOG(ERROR) << "Commit response has unknown response type! Possibly out "
408 "of date client?";
409 return sync_pb::CommitResponse::INVALID_MESSAGE;
411 if (sync_pb::CommitResponse::TRANSIENT_ERROR == response) {
412 DVLOG(1) << "Transient Error Committing: " << local_entry;
413 LogServerError(server_entry);
414 return sync_pb::CommitResponse::TRANSIENT_ERROR;
416 if (sync_pb::CommitResponse::INVALID_MESSAGE == response) {
417 LOG(ERROR) << "Error Commiting: " << local_entry;
418 LogServerError(server_entry);
419 return response;
421 if (sync_pb::CommitResponse::CONFLICT == response) {
422 DVLOG(1) << "Conflict Committing: " << local_entry;
423 return response;
425 if (sync_pb::CommitResponse::RETRY == response) {
426 DVLOG(1) << "Retry Committing: " << local_entry;
427 return response;
429 if (sync_pb::CommitResponse::OVER_QUOTA == response) {
430 LOG(WARNING) << "Hit deprecated OVER_QUOTA Committing: " << local_entry;
431 return response;
433 if (!server_entry.has_id_string()) {
434 LOG(ERROR) << "Commit response has no id";
435 return sync_pb::CommitResponse::INVALID_MESSAGE;
438 // Implied by the IsValid call above, but here for clarity.
439 DCHECK_EQ(sync_pb::CommitResponse::SUCCESS, response) << response;
440 // Check to see if we've been given the ID of an existing entry. If so treat
441 // it as an error response and retry later.
442 const syncable::Id& server_entry_id =
443 SyncableIdFromProto(server_entry.id_string());
444 if (local_entry.GetId() != server_entry_id) {
445 Entry e(trans, syncable::GET_BY_ID, server_entry_id);
446 if (e.good()) {
447 LOG(ERROR)
448 << "Got duplicate id when commiting id: "
449 << local_entry.GetId()
450 << ". Treating as an error return";
451 return sync_pb::CommitResponse::INVALID_MESSAGE;
455 if (server_entry.version() == 0) {
456 LOG(WARNING) << "Server returned a zero version on a commit response.";
459 ProcessSuccessfulCommitResponse(commit_request_entry, server_entry,
460 local_entry.GetId(), &local_entry, dirty_sync_was_set, deleted_folders);
461 return response;
464 } // namespace commit_util
466 } // namespace syncer