Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / sync / engine / syncer_proto_util.cc
blobbfd8151bef03405bd0b8913391b0ab53ef16d5ac
1 // Copyright (c) 2012 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/syncer_proto_util.h"
7 #include "base/format_macros.h"
8 #include "base/strings/stringprintf.h"
9 #include "google_apis/google_api_keys.h"
10 #include "sync/engine/net/server_connection_manager.h"
11 #include "sync/engine/syncer.h"
12 #include "sync/engine/syncer_types.h"
13 #include "sync/engine/traffic_logger.h"
14 #include "sync/internal_api/public/base/model_type.h"
15 #include "sync/protocol/sync_enums.pb.h"
16 #include "sync/protocol/sync_protocol_error.h"
17 #include "sync/sessions/sync_session.h"
18 #include "sync/syncable/directory.h"
19 #include "sync/syncable/entry.h"
20 #include "sync/syncable/syncable-inl.h"
21 #include "sync/syncable/syncable_proto_util.h"
22 #include "sync/util/time.h"
24 using std::string;
25 using std::stringstream;
26 using sync_pb::ClientToServerMessage;
27 using sync_pb::ClientToServerResponse;
29 namespace syncer {
31 using sessions::SyncSession;
32 using syncable::BASE_VERSION;
33 using syncable::CTIME;
34 using syncable::ID;
35 using syncable::IS_DEL;
36 using syncable::IS_DIR;
37 using syncable::IS_UNSYNCED;
38 using syncable::MTIME;
39 using syncable::PARENT_ID;
41 namespace {
43 // Time to backoff syncing after receiving a throttled response.
44 const int kSyncDelayAfterThrottled = 2 * 60 * 60; // 2 hours
46 void LogResponseProfilingData(const ClientToServerResponse& response) {
47 if (response.has_profiling_data()) {
48 stringstream response_trace;
49 response_trace << "Server response trace:";
51 if (response.profiling_data().has_user_lookup_time()) {
52 response_trace << " user lookup: "
53 << response.profiling_data().user_lookup_time() << "ms";
56 if (response.profiling_data().has_meta_data_write_time()) {
57 response_trace << " meta write: "
58 << response.profiling_data().meta_data_write_time()
59 << "ms";
62 if (response.profiling_data().has_meta_data_read_time()) {
63 response_trace << " meta read: "
64 << response.profiling_data().meta_data_read_time() << "ms";
67 if (response.profiling_data().has_file_data_write_time()) {
68 response_trace << " file write: "
69 << response.profiling_data().file_data_write_time()
70 << "ms";
73 if (response.profiling_data().has_file_data_read_time()) {
74 response_trace << " file read: "
75 << response.profiling_data().file_data_read_time() << "ms";
78 if (response.profiling_data().has_total_request_time()) {
79 response_trace << " total time: "
80 << response.profiling_data().total_request_time() << "ms";
82 DVLOG(1) << response_trace.str();
86 SyncerError ServerConnectionErrorAsSyncerError(
87 const HttpResponse::ServerConnectionCode server_status) {
88 switch (server_status) {
89 case HttpResponse::CONNECTION_UNAVAILABLE:
90 return NETWORK_CONNECTION_UNAVAILABLE;
91 case HttpResponse::IO_ERROR:
92 return NETWORK_IO_ERROR;
93 case HttpResponse::SYNC_SERVER_ERROR:
94 // FIXME what does this mean?
95 return SYNC_SERVER_ERROR;
96 case HttpResponse::SYNC_AUTH_ERROR:
97 return SYNC_AUTH_ERROR;
98 case HttpResponse::RETRY:
99 return SERVER_RETURN_TRANSIENT_ERROR;
100 case HttpResponse::SERVER_CONNECTION_OK:
101 case HttpResponse::NONE:
102 default:
103 NOTREACHED();
104 return UNSET;
108 SyncProtocolErrorType ConvertSyncProtocolErrorTypePBToLocalType(
109 const sync_pb::SyncEnums::ErrorType& error_type) {
110 switch (error_type) {
111 case sync_pb::SyncEnums::SUCCESS:
112 return SYNC_SUCCESS;
113 case sync_pb::SyncEnums::NOT_MY_BIRTHDAY:
114 return NOT_MY_BIRTHDAY;
115 case sync_pb::SyncEnums::THROTTLED:
116 return THROTTLED;
117 case sync_pb::SyncEnums::CLEAR_PENDING:
118 return CLEAR_PENDING;
119 case sync_pb::SyncEnums::TRANSIENT_ERROR:
120 return TRANSIENT_ERROR;
121 case sync_pb::SyncEnums::MIGRATION_DONE:
122 return MIGRATION_DONE;
123 case sync_pb::SyncEnums::DISABLED_BY_ADMIN:
124 return DISABLED_BY_ADMIN;
125 case sync_pb::SyncEnums::UNKNOWN:
126 return UNKNOWN_ERROR;
127 case sync_pb::SyncEnums::USER_NOT_ACTIVATED:
128 case sync_pb::SyncEnums::AUTH_INVALID:
129 case sync_pb::SyncEnums::ACCESS_DENIED:
130 return INVALID_CREDENTIAL;
131 default:
132 NOTREACHED();
133 return UNKNOWN_ERROR;
137 ClientAction ConvertClientActionPBToLocalClientAction(
138 const sync_pb::SyncEnums::Action& action) {
139 switch (action) {
140 case sync_pb::SyncEnums::UPGRADE_CLIENT:
141 return UPGRADE_CLIENT;
142 case sync_pb::SyncEnums::CLEAR_USER_DATA_AND_RESYNC:
143 return CLEAR_USER_DATA_AND_RESYNC;
144 case sync_pb::SyncEnums::ENABLE_SYNC_ON_ACCOUNT:
145 return ENABLE_SYNC_ON_ACCOUNT;
146 case sync_pb::SyncEnums::STOP_AND_RESTART_SYNC:
147 return STOP_AND_RESTART_SYNC;
148 case sync_pb::SyncEnums::DISABLE_SYNC_ON_CLIENT:
149 return DISABLE_SYNC_ON_CLIENT;
150 case sync_pb::SyncEnums::UNKNOWN_ACTION:
151 return UNKNOWN_ACTION;
152 default:
153 NOTREACHED();
154 return UNKNOWN_ACTION;
158 } // namespace
160 ModelTypeSet GetTypesToMigrate(const ClientToServerResponse& response) {
161 ModelTypeSet to_migrate;
162 for (int i = 0; i < response.migrated_data_type_id_size(); i++) {
163 int field_number = response.migrated_data_type_id(i);
164 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
165 if (!IsRealDataType(model_type)) {
166 DLOG(WARNING) << "Unknown field number " << field_number;
167 continue;
169 to_migrate.Put(model_type);
171 return to_migrate;
174 SyncProtocolError ConvertErrorPBToLocalType(
175 const sync_pb::ClientToServerResponse_Error& error) {
176 SyncProtocolError sync_protocol_error;
177 sync_protocol_error.error_type = ConvertSyncProtocolErrorTypePBToLocalType(
178 error.error_type());
179 sync_protocol_error.error_description = error.error_description();
180 sync_protocol_error.url = error.url();
181 sync_protocol_error.action = ConvertClientActionPBToLocalClientAction(
182 error.action());
184 if (error.error_data_type_ids_size() > 0) {
185 // THROTTLED is currently the only error code that uses |error_data_types|.
186 DCHECK_EQ(error.error_type(), sync_pb::SyncEnums::THROTTLED);
187 for (int i = 0; i < error.error_data_type_ids_size(); ++i) {
188 int field_number = error.error_data_type_ids(i);
189 ModelType model_type =
190 GetModelTypeFromSpecificsFieldNumber(field_number);
191 if (!IsRealDataType(model_type)) {
192 DLOG(WARNING) << "Unknown field number " << field_number;
193 continue;
195 sync_protocol_error.error_data_types.Put(model_type);
199 return sync_protocol_error;
202 // static
203 bool SyncerProtoUtil::VerifyResponseBirthday(
204 const ClientToServerResponse& response,
205 syncable::Directory* dir) {
207 std::string local_birthday = dir->store_birthday();
209 if (local_birthday.empty()) {
210 if (!response.has_store_birthday()) {
211 LOG(WARNING) << "Expected a birthday on first sync.";
212 return false;
215 DVLOG(1) << "New store birthday: " << response.store_birthday();
216 dir->set_store_birthday(response.store_birthday());
217 return true;
220 // Error situation, but we're not stuck.
221 if (!response.has_store_birthday()) {
222 LOG(WARNING) << "No birthday in server response?";
223 return true;
226 if (response.store_birthday() != local_birthday) {
227 LOG(WARNING) << "Birthday changed, showing syncer stuck";
228 return false;
231 return true;
234 // static
235 bool SyncerProtoUtil::IsSyncDisabledByAdmin(
236 const sync_pb::ClientToServerResponse& response) {
237 return (response.has_error_code() &&
238 response.error_code() == sync_pb::SyncEnums::DISABLED_BY_ADMIN);
241 // static
242 void SyncerProtoUtil::AddRequestBirthday(syncable::Directory* dir,
243 ClientToServerMessage* msg) {
244 if (!dir->store_birthday().empty())
245 msg->set_store_birthday(dir->store_birthday());
248 // static
249 void SyncerProtoUtil::AddBagOfChips(syncable::Directory* dir,
250 ClientToServerMessage* msg) {
251 msg->mutable_bag_of_chips()->ParseFromString(dir->bag_of_chips());
254 // static
255 void SyncerProtoUtil::SetProtocolVersion(ClientToServerMessage* msg) {
256 const int current_version =
257 ClientToServerMessage::default_instance().protocol_version();
258 msg->set_protocol_version(current_version);
261 // static
262 bool SyncerProtoUtil::PostAndProcessHeaders(ServerConnectionManager* scm,
263 sessions::SyncSession* session,
264 const ClientToServerMessage& msg,
265 ClientToServerResponse* response) {
266 ServerConnectionManager::PostBufferParams params;
267 DCHECK(msg.has_protocol_version());
268 DCHECK_EQ(msg.protocol_version(),
269 ClientToServerMessage::default_instance().protocol_version());
270 msg.SerializeToString(&params.buffer_in);
272 ScopedServerStatusWatcher server_status_watcher(scm, &params.response);
273 // Fills in params.buffer_out and params.response.
274 if (!scm->PostBufferWithCachedAuth(&params, &server_status_watcher)) {
275 LOG(WARNING) << "Error posting from syncer:" << params.response;
276 return false;
279 if (response->ParseFromString(params.buffer_out)) {
280 // TODO(tim): This is an egregious layering violation (bug 35060).
281 switch (response->error_code()) {
282 case sync_pb::SyncEnums::ACCESS_DENIED:
283 case sync_pb::SyncEnums::AUTH_INVALID:
284 case sync_pb::SyncEnums::USER_NOT_ACTIVATED:
285 // Fires on ScopedServerStatusWatcher
286 params.response.server_status = HttpResponse::SYNC_AUTH_ERROR;
287 return false;
288 default:
289 return true;
293 return false;
296 base::TimeDelta SyncerProtoUtil::GetThrottleDelay(
297 const ClientToServerResponse& response) {
298 base::TimeDelta throttle_delay =
299 base::TimeDelta::FromSeconds(kSyncDelayAfterThrottled);
300 if (response.has_client_command()) {
301 const sync_pb::ClientCommand& command = response.client_command();
302 if (command.has_throttle_delay_seconds()) {
303 throttle_delay =
304 base::TimeDelta::FromSeconds(command.throttle_delay_seconds());
307 return throttle_delay;
310 namespace {
312 // Helper function for an assertion in PostClientToServerMessage.
313 bool IsVeryFirstGetUpdates(const ClientToServerMessage& message) {
314 if (!message.has_get_updates())
315 return false;
316 DCHECK_LT(0, message.get_updates().from_progress_marker_size());
317 for (int i = 0; i < message.get_updates().from_progress_marker_size(); ++i) {
318 if (!message.get_updates().from_progress_marker(i).token().empty())
319 return false;
321 return true;
324 // TODO(lipalani) : Rename these function names as per the CR for issue 7740067.
325 SyncProtocolError ConvertLegacyErrorCodeToNewError(
326 const sync_pb::SyncEnums::ErrorType& error_type) {
327 SyncProtocolError error;
328 error.error_type = ConvertSyncProtocolErrorTypePBToLocalType(error_type);
329 if (error_type == sync_pb::SyncEnums::CLEAR_PENDING ||
330 error_type == sync_pb::SyncEnums::NOT_MY_BIRTHDAY) {
331 error.action = DISABLE_SYNC_ON_CLIENT;
332 } else if (error_type == sync_pb::SyncEnums::DISABLED_BY_ADMIN) {
333 error.action = STOP_SYNC_FOR_DISABLED_ACCOUNT;
334 } // There is no other action we can compute for legacy server.
335 return error;
338 } // namespace
340 // static
341 SyncerError SyncerProtoUtil::PostClientToServerMessage(
342 ClientToServerMessage* msg,
343 ClientToServerResponse* response,
344 SyncSession* session) {
345 CHECK(response);
346 DCHECK(!msg->get_updates().has_from_timestamp()); // Deprecated.
347 DCHECK(!msg->get_updates().has_requested_types()); // Deprecated.
349 // Add must-have fields.
350 SetProtocolVersion(msg);
351 AddRequestBirthday(session->context()->directory(), msg);
352 DCHECK(msg->has_store_birthday() || IsVeryFirstGetUpdates(*msg));
353 AddBagOfChips(session->context()->directory(), msg);
354 msg->set_api_key(google_apis::GetAPIKey());
355 msg->mutable_client_status()->CopyFrom(session->context()->client_status());
356 msg->set_invalidator_client_id(session->context()->invalidator_client_id());
358 syncable::Directory* dir = session->context()->directory();
360 LogClientToServerMessage(*msg);
361 session->context()->traffic_recorder()->RecordClientToServerMessage(*msg);
362 if (!PostAndProcessHeaders(session->context()->connection_manager(), session,
363 *msg, response)) {
364 // There was an error establishing communication with the server.
365 // We can not proceed beyond this point.
366 const HttpResponse::ServerConnectionCode server_status =
367 session->context()->connection_manager()->server_status();
369 DCHECK_NE(server_status, HttpResponse::NONE);
370 DCHECK_NE(server_status, HttpResponse::SERVER_CONNECTION_OK);
372 return ServerConnectionErrorAsSyncerError(server_status);
375 LogClientToServerResponse(*response);
376 session->context()->traffic_recorder()->RecordClientToServerResponse(
377 *response);
379 // Persist a bag of chips if it has been sent by the server.
380 PersistBagOfChips(dir, *response);
382 SyncProtocolError sync_protocol_error;
384 // The DISABLED_BY_ADMIN error overrides other errors sent by the server.
385 if (IsSyncDisabledByAdmin(*response)) {
386 sync_protocol_error.error_type = DISABLED_BY_ADMIN;
387 sync_protocol_error.action = STOP_SYNC_FOR_DISABLED_ACCOUNT;
388 } else if (!VerifyResponseBirthday(*response, dir)) {
389 // If sync isn't disabled, first check for a birthday mismatch error.
390 sync_protocol_error.error_type = NOT_MY_BIRTHDAY;
391 sync_protocol_error.action = DISABLE_SYNC_ON_CLIENT;
392 } else if (response->has_error()) {
393 // This is a new server. Just get the error from the protocol.
394 sync_protocol_error = ConvertErrorPBToLocalType(response->error());
395 } else {
396 // Legacy server implementation. Compute the error based on |error_code|.
397 sync_protocol_error = ConvertLegacyErrorCodeToNewError(
398 response->error_code());
401 // Now set the error into the status so the layers above us could read it.
402 sessions::StatusController* status = session->mutable_status_controller();
403 status->set_sync_protocol_error(sync_protocol_error);
405 // Inform the delegate of the error we got.
406 session->delegate()->OnSyncProtocolError(session->TakeSnapshot());
408 // Update our state for any other commands we've received.
409 if (response->has_client_command()) {
410 const sync_pb::ClientCommand& command = response->client_command();
411 if (command.has_max_commit_batch_size()) {
412 session->context()->set_max_commit_batch_size(
413 command.max_commit_batch_size());
416 if (command.has_set_sync_long_poll_interval()) {
417 session->delegate()->OnReceivedLongPollIntervalUpdate(
418 base::TimeDelta::FromSeconds(command.set_sync_long_poll_interval()));
421 if (command.has_set_sync_poll_interval()) {
422 session->delegate()->OnReceivedShortPollIntervalUpdate(
423 base::TimeDelta::FromSeconds(command.set_sync_poll_interval()));
426 if (command.has_sessions_commit_delay_seconds()) {
427 session->delegate()->OnReceivedSessionsCommitDelay(
428 base::TimeDelta::FromSeconds(
429 command.sessions_commit_delay_seconds()));
432 if (command.has_client_invalidation_hint_buffer_size()) {
433 session->delegate()->OnReceivedClientInvalidationHintBufferSize(
434 command.client_invalidation_hint_buffer_size());
438 // Now do any special handling for the error type and decide on the return
439 // value.
440 switch (sync_protocol_error.error_type) {
441 case UNKNOWN_ERROR:
442 LOG(WARNING) << "Sync protocol out-of-date. The server is using a more "
443 << "recent version.";
444 return SERVER_RETURN_UNKNOWN_ERROR;
445 case SYNC_SUCCESS:
446 LogResponseProfilingData(*response);
447 return SYNCER_OK;
448 case THROTTLED:
449 if (sync_protocol_error.error_data_types.Empty()) {
450 DLOG(WARNING) << "Client fully throttled by syncer.";
451 session->delegate()->OnThrottled(GetThrottleDelay(*response));
452 } else {
453 DLOG(WARNING) << "Some types throttled by syncer.";
454 session->delegate()->OnTypesThrottled(
455 sync_protocol_error.error_data_types,
456 GetThrottleDelay(*response));
458 return SERVER_RETURN_THROTTLED;
459 case TRANSIENT_ERROR:
460 return SERVER_RETURN_TRANSIENT_ERROR;
461 case MIGRATION_DONE:
462 LOG_IF(ERROR, 0 >= response->migrated_data_type_id_size())
463 << "MIGRATION_DONE but no types specified.";
464 // TODO(akalin): This should be a set union.
465 session->mutable_status_controller()->
466 set_types_needing_local_migration(GetTypesToMigrate(*response));
467 return SERVER_RETURN_MIGRATION_DONE;
468 case CLEAR_PENDING:
469 return SERVER_RETURN_CLEAR_PENDING;
470 case NOT_MY_BIRTHDAY:
471 return SERVER_RETURN_NOT_MY_BIRTHDAY;
472 case DISABLED_BY_ADMIN:
473 return SERVER_RETURN_DISABLED_BY_ADMIN;
474 default:
475 NOTREACHED();
476 return UNSET;
480 // static
481 bool SyncerProtoUtil::ShouldMaintainPosition(
482 const sync_pb::SyncEntity& sync_entity) {
483 // Maintain positions for bookmarks that are not server-defined top-level
484 // folders.
485 return GetModelType(sync_entity) == BOOKMARKS
486 && !(sync_entity.folder() &&
487 !sync_entity.server_defined_unique_tag().empty());
490 // static
491 void SyncerProtoUtil::CopyProtoBytesIntoBlob(const std::string& proto_bytes,
492 syncable::Blob* blob) {
493 syncable::Blob proto_blob(proto_bytes.begin(), proto_bytes.end());
494 blob->swap(proto_blob);
497 // static
498 bool SyncerProtoUtil::ProtoBytesEqualsBlob(const std::string& proto_bytes,
499 const syncable::Blob& blob) {
500 if (proto_bytes.size() != blob.size())
501 return false;
502 return std::equal(proto_bytes.begin(), proto_bytes.end(), blob.begin());
505 // static
506 void SyncerProtoUtil::CopyBlobIntoProtoBytes(const syncable::Blob& blob,
507 std::string* proto_bytes) {
508 std::string blob_string(blob.begin(), blob.end());
509 proto_bytes->swap(blob_string);
512 // static
513 const std::string& SyncerProtoUtil::NameFromSyncEntity(
514 const sync_pb::SyncEntity& entry) {
515 if (entry.has_non_unique_name())
516 return entry.non_unique_name();
517 return entry.name();
520 // static
521 const std::string& SyncerProtoUtil::NameFromCommitEntryResponse(
522 const sync_pb::CommitResponse_EntryResponse& entry) {
523 if (entry.has_non_unique_name())
524 return entry.non_unique_name();
525 return entry.name();
528 // static
529 void SyncerProtoUtil::PersistBagOfChips(syncable::Directory* dir,
530 const sync_pb::ClientToServerResponse& response) {
531 if (!response.has_new_bag_of_chips())
532 return;
533 std::string bag_of_chips;
534 if (response.new_bag_of_chips().SerializeToString(&bag_of_chips))
535 dir->set_bag_of_chips(bag_of_chips);
538 std::string SyncerProtoUtil::SyncEntityDebugString(
539 const sync_pb::SyncEntity& entry) {
540 const std::string& mtime_str =
541 GetTimeDebugString(ProtoTimeToTime(entry.mtime()));
542 const std::string& ctime_str =
543 GetTimeDebugString(ProtoTimeToTime(entry.ctime()));
544 return base::StringPrintf(
545 "id: %s, parent_id: %s, "
546 "version: %" PRId64"d, "
547 "mtime: %" PRId64"d (%s), "
548 "ctime: %" PRId64"d (%s), "
549 "name: %s, sync_timestamp: %" PRId64"d, "
550 "%s ",
551 entry.id_string().c_str(),
552 entry.parent_id_string().c_str(),
553 entry.version(),
554 entry.mtime(), mtime_str.c_str(),
555 entry.ctime(), ctime_str.c_str(),
556 entry.name().c_str(), entry.sync_timestamp(),
557 entry.deleted() ? "deleted, ":"");
560 namespace {
561 std::string GetUpdatesResponseString(
562 const sync_pb::GetUpdatesResponse& response) {
563 std::string output;
564 output.append("GetUpdatesResponse:\n");
565 for (int i = 0; i < response.entries_size(); i++) {
566 output.append(SyncerProtoUtil::SyncEntityDebugString(response.entries(i)));
567 output.append("\n");
569 return output;
571 } // namespace
573 std::string SyncerProtoUtil::ClientToServerResponseDebugString(
574 const ClientToServerResponse& response) {
575 // Add more handlers as needed.
576 std::string output;
577 if (response.has_get_updates())
578 output.append(GetUpdatesResponseString(response.get_updates()));
579 return output;
582 } // namespace syncer