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