| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/engine/syncer_proto_util.h" | 5 #include "chrome/browser/sync/engine/syncer_proto_util.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "chrome/browser/sync/engine/net/server_connection_manager.h" | 9 #include "chrome/browser/sync/engine/net/server_connection_manager.h" |
| 10 #include "chrome/browser/sync/engine/syncer.h" | 10 #include "chrome/browser/sync/engine/syncer.h" |
| 11 #include "chrome/browser/sync/engine/syncer_types.h" | 11 #include "chrome/browser/sync/engine/syncer_types.h" |
| 12 #include "chrome/browser/sync/engine/syncer_util.h" | 12 #include "chrome/browser/sync/engine/syncer_util.h" |
| 13 #include "chrome/browser/sync/protocol/service_constants.h" | 13 #include "chrome/browser/sync/protocol/service_constants.h" |
| 14 #include "chrome/browser/sync/protocol/sync.pb.h" | 14 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 15 #include "chrome/browser/sync/protocol/sync_protocol_error.h" |
| 15 #include "chrome/browser/sync/sessions/sync_session.h" | 16 #include "chrome/browser/sync/sessions/sync_session.h" |
| 16 #include "chrome/browser/sync/syncable/directory_manager.h" | 17 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 17 #include "chrome/browser/sync/syncable/model_type.h" | 18 #include "chrome/browser/sync/syncable/model_type.h" |
| 18 #include "chrome/browser/sync/syncable/syncable-inl.h" | 19 #include "chrome/browser/sync/syncable/syncable-inl.h" |
| 19 #include "chrome/browser/sync/syncable/syncable.h" | 20 #include "chrome/browser/sync/syncable/syncable.h" |
| 20 | 21 |
| 22 using browser_sync::SyncProtocolErrorType; |
| 21 using std::string; | 23 using std::string; |
| 22 using std::stringstream; | 24 using std::stringstream; |
| 23 using syncable::BASE_VERSION; | 25 using syncable::BASE_VERSION; |
| 24 using syncable::CTIME; | 26 using syncable::CTIME; |
| 25 using syncable::ID; | 27 using syncable::ID; |
| 26 using syncable::IS_DEL; | 28 using syncable::IS_DEL; |
| 27 using syncable::IS_DIR; | 29 using syncable::IS_DIR; |
| 28 using syncable::IS_UNSYNCED; | 30 using syncable::IS_UNSYNCED; |
| 29 using syncable::MTIME; | 31 using syncable::MTIME; |
| 30 using syncable::PARENT_ID; | 32 using syncable::PARENT_ID; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 95 } |
| 94 session->status_controller()->set_types_needing_local_migration(to_migrate); | 96 session->status_controller()->set_types_needing_local_migration(to_migrate); |
| 95 } | 97 } |
| 96 | 98 |
| 97 // static | 99 // static |
| 98 bool SyncerProtoUtil::VerifyResponseBirthday(syncable::Directory* dir, | 100 bool SyncerProtoUtil::VerifyResponseBirthday(syncable::Directory* dir, |
| 99 const ClientToServerResponse* response) { | 101 const ClientToServerResponse* response) { |
| 100 | 102 |
| 101 std::string local_birthday = dir->store_birthday(); | 103 std::string local_birthday = dir->store_birthday(); |
| 102 | 104 |
| 105 // TODO(lipalani) : Remove this check here. When the new implementation |
| 106 // becomes the default this check should go away. This is handled by the |
| 107 // switch case in the new implementation. |
| 103 if (response->error_code() == ClientToServerResponse::CLEAR_PENDING || | 108 if (response->error_code() == ClientToServerResponse::CLEAR_PENDING || |
| 104 response->error_code() == ClientToServerResponse::NOT_MY_BIRTHDAY) { | 109 response->error_code() == ClientToServerResponse::NOT_MY_BIRTHDAY) { |
| 105 // Birthday verification failures result in stopping sync and deleting | 110 // Birthday verification failures result in stopping sync and deleting |
| 106 // local sync data. | 111 // local sync data. |
| 107 return false; | 112 return false; |
| 108 } | 113 } |
| 109 | 114 |
| 110 if (local_birthday.empty()) { | 115 if (local_birthday.empty()) { |
| 111 if (!response->has_store_birthday()) { | 116 if (!response->has_store_birthday()) { |
| 112 LOG(WARNING) << "Expected a birthday on first sync."; | 117 LOG(WARNING) << "Expected a birthday on first sync."; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 if (!message.has_get_updates()) | 209 if (!message.has_get_updates()) |
| 205 return false; | 210 return false; |
| 206 DCHECK_LT(0, message.get_updates().from_progress_marker_size()); | 211 DCHECK_LT(0, message.get_updates().from_progress_marker_size()); |
| 207 for (int i = 0; i < message.get_updates().from_progress_marker_size(); ++i) { | 212 for (int i = 0; i < message.get_updates().from_progress_marker_size(); ++i) { |
| 208 if (!message.get_updates().from_progress_marker(i).token().empty()) | 213 if (!message.get_updates().from_progress_marker(i).token().empty()) |
| 209 return false; | 214 return false; |
| 210 } | 215 } |
| 211 return true; | 216 return true; |
| 212 } | 217 } |
| 213 | 218 |
| 219 SyncProtocolErrorType ConvertSyncProtocolErrorTypePBToLocalType( |
| 220 const sync_pb::ClientToServerResponse::ErrorType& error_type) { |
| 221 switch (error_type) { |
| 222 case ClientToServerResponse::SUCCESS: |
| 223 return browser_sync::SYNC_SUCCESS; |
| 224 case ClientToServerResponse::NOT_MY_BIRTHDAY: |
| 225 return browser_sync::NOT_MY_BIRTHDAY; |
| 226 case ClientToServerResponse::THROTTLED: |
| 227 return browser_sync::THROTTLED; |
| 228 case ClientToServerResponse::CLEAR_PENDING: |
| 229 return browser_sync::CLEAR_PENDING; |
| 230 case ClientToServerResponse::TRANSIENT_ERROR: |
| 231 return browser_sync::TRANSIENT_ERROR; |
| 232 case ClientToServerResponse::MIGRATION_DONE: |
| 233 return browser_sync::MIGRATION_DONE; |
| 234 case ClientToServerResponse::UNKNOWN: |
| 235 return browser_sync::UNKNOWN_ERROR; |
| 236 case ClientToServerResponse::USER_NOT_ACTIVATED: |
| 237 case ClientToServerResponse::AUTH_INVALID: |
| 238 case ClientToServerResponse::ACCESS_DENIED: |
| 239 return browser_sync::INVALID_CREDENTIAL; |
| 240 default: |
| 241 NOTREACHED(); |
| 242 return browser_sync::UNKNOWN_ERROR; |
| 243 } |
| 244 } |
| 245 |
| 246 browser_sync::ClientAction ConvertClientActionPBToLocalClientAction( |
| 247 const sync_pb::ClientToServerResponse::Error::Action& action) { |
| 248 switch (action) { |
| 249 case ClientToServerResponse::Error::UPGRADE_CLIENT: |
| 250 return browser_sync::UPGRADE_CLIENT; |
| 251 case ClientToServerResponse::Error::CLEAR_USER_DATA_AND_RESYNC: |
| 252 return browser_sync::CLEAR_USER_DATA_AND_RESYNC; |
| 253 case ClientToServerResponse::Error::ENABLE_SYNC_ON_ACCOUNT: |
| 254 return browser_sync::ENABLE_SYNC_ON_ACCOUNT; |
| 255 case ClientToServerResponse::Error::STOP_AND_RESTART_SYNC: |
| 256 return browser_sync::STOP_AND_RESTART_SYNC; |
| 257 case ClientToServerResponse::Error::DISABLE_SYNC_ON_CLIENT: |
| 258 return browser_sync::DISABLE_SYNC_ON_CLIENT; |
| 259 case ClientToServerResponse::Error::UNKNOWN_ACTION: |
| 260 return browser_sync::UNKNOWN_ACTION; |
| 261 default: |
| 262 NOTREACHED(); |
| 263 return browser_sync::UNKNOWN_ACTION; |
| 264 } |
| 265 } |
| 266 |
| 267 browser_sync::SyncProtocolError ConvertErrorPBToLocalType( |
| 268 const sync_pb::ClientToServerResponse::Error& error) { |
| 269 browser_sync::SyncProtocolError sync_protocol_error; |
| 270 sync_protocol_error.error_type = ConvertSyncProtocolErrorTypePBToLocalType( |
| 271 error.error_type()); |
| 272 sync_protocol_error.error_description = error.error_description(); |
| 273 sync_protocol_error.url = error.url(); |
| 274 sync_protocol_error.action = ConvertClientActionPBToLocalClientAction( |
| 275 error.action()); |
| 276 |
| 277 return sync_protocol_error; |
| 278 } |
| 279 |
| 214 } // namespace | 280 } // namespace |
| 215 | 281 |
| 216 // static | 282 // static |
| 217 bool SyncerProtoUtil::PostClientToServerMessage( | 283 bool SyncerProtoUtil::PostClientToServerMessage( |
| 218 const ClientToServerMessage& msg, | 284 const ClientToServerMessage& msg, |
| 219 ClientToServerResponse* response, | 285 ClientToServerResponse* response, |
| 220 SyncSession* session) { | 286 SyncSession* session) { |
| 221 | 287 |
| 222 CHECK(response); | 288 CHECK(response); |
| 223 DCHECK(!msg.get_updates().has_from_timestamp()); // Deprecated. | 289 DCHECK(!msg.get_updates().has_from_timestamp()); // Deprecated. |
| 224 DCHECK(!msg.get_updates().has_requested_types()); // Deprecated. | 290 DCHECK(!msg.get_updates().has_requested_types()); // Deprecated. |
| 225 DCHECK(msg.has_store_birthday() || IsVeryFirstGetUpdates(msg)) | 291 DCHECK(msg.has_store_birthday() || IsVeryFirstGetUpdates(msg)) |
| 226 << "Must call AddRequestBirthday to set birthday."; | 292 << "Must call AddRequestBirthday to set birthday."; |
| 227 | 293 |
| 228 ScopedDirLookup dir(session->context()->directory_manager(), | 294 ScopedDirLookup dir(session->context()->directory_manager(), |
| 229 session->context()->account_name()); | 295 session->context()->account_name()); |
| 230 if (!dir.good()) | 296 if (!dir.good()) |
| 231 return false; | 297 return false; |
| 232 | 298 |
| 233 if (!PostAndProcessHeaders(session->context()->connection_manager(), session, | 299 if (!PostAndProcessHeaders(session->context()->connection_manager(), session, |
| 234 msg, response)) | 300 msg, response)) |
| 235 return false; | 301 return false; |
| 236 | 302 |
| 303 if (response->has_error()) { |
| 304 // We are talking to a server that is capable of sending the |error| tag. |
| 305 browser_sync::SyncProtocolError sync_protocol_error = |
| 306 ConvertErrorPBToLocalType(response->error()); |
| 307 |
| 308 // Birthday mismatch overrides any error that is sent by the server. |
| 309 if (!VerifyResponseBirthday(dir, response)) { |
| 310 sync_protocol_error.error_type = browser_sync::NOT_MY_BIRTHDAY; |
| 311 sync_protocol_error.action = |
| 312 browser_sync::DISABLE_SYNC_ON_CLIENT; |
| 313 } |
| 314 |
| 315 // Now set the error into the status so the layers above us could read it. |
| 316 sessions::StatusController* status = session->status_controller(); |
| 317 status->set_sync_protocol_error(sync_protocol_error); |
| 318 |
| 319 // Inform the delegate of the error we got. |
| 320 session->delegate()->OnSyncProtocolError(session->TakeSnapshot()); |
| 321 |
| 322 // Now do any special handling for the error type and decide on the return |
| 323 // value. |
| 324 switch (sync_protocol_error.error_type) { |
| 325 case browser_sync::UNKNOWN_ERROR: |
| 326 LOG(WARNING) << "Sync protocol out-of-date. The server is using a more " |
| 327 << "recent version."; |
| 328 return false; |
| 329 case browser_sync::SYNC_SUCCESS: |
| 330 LogResponseProfilingData(*response); |
| 331 return true; |
| 332 case browser_sync::THROTTLED: |
| 333 LOG(WARNING) << "Client silenced by server."; |
| 334 session->delegate()->OnSilencedUntil(base::TimeTicks::Now() + |
| 335 GetThrottleDelay(*response)); |
| 336 return false; |
| 337 case browser_sync::TRANSIENT_ERROR: |
| 338 return false; |
| 339 case browser_sync::MIGRATION_DONE: |
| 340 HandleMigrationDoneResponse(response, session); |
| 341 return false; |
| 342 default: |
| 343 NOTREACHED(); |
| 344 return false; |
| 345 } |
| 346 } |
| 347 |
| 348 // TODO(lipalani): Plug this legacy implementation to the new error framework. |
| 349 // New implementation code would have returned before by now. This is waiting |
| 350 // on the frontend code being implemented. Otherwise ripping this would break |
| 351 // sync. |
| 237 if (!VerifyResponseBirthday(dir, response)) { | 352 if (!VerifyResponseBirthday(dir, response)) { |
| 238 session->status_controller()->set_syncer_stuck(true); | 353 session->status_controller()->set_syncer_stuck(true); |
| 239 session->delegate()->OnShouldStopSyncingPermanently(); | 354 session->delegate()->OnShouldStopSyncingPermanently(); |
| 240 return false; | 355 return false; |
| 241 } | 356 } |
| 242 | 357 |
| 243 switch (response->error_code()) { | 358 switch (response->error_code()) { |
| 244 case ClientToServerResponse::UNKNOWN: | 359 case ClientToServerResponse::UNKNOWN: |
| 245 LOG(WARNING) << "Sync protocol out-of-date. The server is using a more " | 360 LOG(WARNING) << "Sync protocol out-of-date. The server is using a more " |
| 246 << "recent version."; | 361 << "recent version."; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 std::string SyncerProtoUtil::ClientToServerResponseDebugString( | 505 std::string SyncerProtoUtil::ClientToServerResponseDebugString( |
| 391 const sync_pb::ClientToServerResponse& response) { | 506 const sync_pb::ClientToServerResponse& response) { |
| 392 // Add more handlers as needed. | 507 // Add more handlers as needed. |
| 393 std::string output; | 508 std::string output; |
| 394 if (response.has_get_updates()) | 509 if (response.has_get_updates()) |
| 395 output.append(GetUpdatesResponseString(response.get_updates())); | 510 output.append(GetUpdatesResponseString(response.get_updates())); |
| 396 return output; | 511 return output; |
| 397 } | 512 } |
| 398 | 513 |
| 399 } // namespace browser_sync | 514 } // namespace browser_sync |
| OLD | NEW |