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