OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "sync/engine/syncer_proto_util.h" | 5 #include "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 "sync/engine/net/server_connection_manager.h" | 9 #include "sync/engine/net/server_connection_manager.h" |
10 #include "sync/engine/syncer.h" | 10 #include "sync/engine/syncer.h" |
11 #include "sync/engine/syncer_types.h" | 11 #include "sync/engine/syncer_types.h" |
12 #include "sync/engine/throttled_data_type_tracker.h" | 12 #include "sync/engine/throttled_data_type_tracker.h" |
13 #include "sync/engine/traffic_logger.h" | 13 #include "sync/engine/traffic_logger.h" |
14 #include "sync/internal_api/public/base/model_type.h" | 14 #include "sync/internal_api/public/base/model_type.h" |
15 #include "sync/protocol/sync.pb.h" | 15 #include "sync/protocol/sync.pb.h" |
16 #include "sync/protocol/sync_enums.pb.h" | 16 #include "sync/protocol/sync_enums.pb.h" |
17 #include "sync/protocol/sync_protocol_error.h" | 17 #include "sync/protocol/sync_protocol_error.h" |
18 #include "sync/sessions/sync_session.h" | 18 #include "sync/sessions/sync_session.h" |
19 #include "sync/syncable/directory.h" | 19 #include "sync/syncable/directory.h" |
20 #include "sync/syncable/entry.h" | 20 #include "sync/syncable/entry.h" |
21 #include "sync/syncable/syncable-inl.h" | 21 #include "sync/syncable/syncable-inl.h" |
22 #include "sync/syncable/syncable_proto_util.h" | |
22 #include "sync/util/time.h" | 23 #include "sync/util/time.h" |
23 | 24 |
24 using std::string; | 25 using std::string; |
25 using std::stringstream; | 26 using std::stringstream; |
27 using sync_pb::ClientToServerMessage; | |
28 using sync_pb::ClientToServerResponse; | |
26 | 29 |
27 namespace syncer { | 30 namespace syncer { |
28 | 31 |
29 using sessions::SyncSession; | 32 using sessions::SyncSession; |
30 using syncable::BASE_VERSION; | 33 using syncable::BASE_VERSION; |
31 using syncable::CTIME; | 34 using syncable::CTIME; |
32 using syncable::ID; | 35 using syncable::ID; |
33 using syncable::IS_DEL; | 36 using syncable::IS_DEL; |
34 using syncable::IS_DIR; | 37 using syncable::IS_DIR; |
35 using syncable::IS_UNSYNCED; | 38 using syncable::IS_UNSYNCED; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 default: | 103 default: |
101 NOTREACHED(); | 104 NOTREACHED(); |
102 return UNSET; | 105 return UNSET; |
103 } | 106 } |
104 } | 107 } |
105 | 108 |
106 } // namespace | 109 } // namespace |
107 | 110 |
108 // static | 111 // static |
109 void SyncerProtoUtil::HandleMigrationDoneResponse( | 112 void SyncerProtoUtil::HandleMigrationDoneResponse( |
110 const sync_pb::ClientToServerResponse* response, | 113 const ClientToServerResponse* response, |
111 sessions::SyncSession* session) { | 114 sessions::SyncSession* session) { |
112 LOG_IF(ERROR, 0 >= response->migrated_data_type_id_size()) | 115 LOG_IF(ERROR, 0 >= response->migrated_data_type_id_size()) |
113 << "MIGRATION_DONE but no types specified."; | 116 << "MIGRATION_DONE but no types specified."; |
114 syncer::ModelTypeSet to_migrate; | 117 syncer::ModelTypeSet to_migrate; |
115 for (int i = 0; i < response->migrated_data_type_id_size(); i++) { | 118 for (int i = 0; i < response->migrated_data_type_id_size(); i++) { |
116 to_migrate.Put(syncer::GetModelTypeFromSpecificsFieldNumber( | 119 to_migrate.Put(syncer::GetModelTypeFromSpecificsFieldNumber( |
117 response->migrated_data_type_id(i))); | 120 response->migrated_data_type_id(i))); |
118 } | 121 } |
119 // TODO(akalin): This should be a set union. | 122 // TODO(akalin): This should be a set union. |
120 session->mutable_status_controller()-> | 123 session->mutable_status_controller()-> |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 if (!dir->store_birthday().empty()) | 161 if (!dir->store_birthday().empty()) |
159 msg->set_store_birthday(dir->store_birthday()); | 162 msg->set_store_birthday(dir->store_birthday()); |
160 } | 163 } |
161 | 164 |
162 // static | 165 // static |
163 bool SyncerProtoUtil::PostAndProcessHeaders(ServerConnectionManager* scm, | 166 bool SyncerProtoUtil::PostAndProcessHeaders(ServerConnectionManager* scm, |
164 sessions::SyncSession* session, | 167 sessions::SyncSession* session, |
165 const ClientToServerMessage& msg, | 168 const ClientToServerMessage& msg, |
166 ClientToServerResponse* response) { | 169 ClientToServerResponse* response) { |
167 ServerConnectionManager::PostBufferParams params; | 170 ServerConnectionManager::PostBufferParams params; |
171 DCHECK(msg.has_protocol_version()); | |
172 DCHECK_EQ(msg.protocol_version(), | |
173 ClientToServerMessage::default_instance().protocol_version()); | |
168 msg.SerializeToString(¶ms.buffer_in); | 174 msg.SerializeToString(¶ms.buffer_in); |
169 | 175 |
170 ScopedServerStatusWatcher server_status_watcher(scm, ¶ms.response); | 176 ScopedServerStatusWatcher server_status_watcher(scm, ¶ms.response); |
171 // Fills in params.buffer_out and params.response. | 177 // Fills in params.buffer_out and params.response. |
172 if (!scm->PostBufferWithCachedAuth(¶ms, &server_status_watcher)) { | 178 if (!scm->PostBufferWithCachedAuth(¶ms, &server_status_watcher)) { |
173 LOG(WARNING) << "Error posting from syncer:" << params.response; | 179 LOG(WARNING) << "Error posting from syncer:" << params.response; |
174 return false; | 180 return false; |
175 } | 181 } |
176 | 182 |
177 std::string new_token = params.response.update_client_auth_header; | 183 std::string new_token = params.response.update_client_auth_header; |
(...skipping 14 matching lines...) Expand all Loading... | |
192 return false; | 198 return false; |
193 default: | 199 default: |
194 return true; | 200 return true; |
195 } | 201 } |
196 } | 202 } |
197 | 203 |
198 return false; | 204 return false; |
199 } | 205 } |
200 | 206 |
201 base::TimeDelta SyncerProtoUtil::GetThrottleDelay( | 207 base::TimeDelta SyncerProtoUtil::GetThrottleDelay( |
202 const sync_pb::ClientToServerResponse& response) { | 208 const ClientToServerResponse& response) { |
203 base::TimeDelta throttle_delay = | 209 base::TimeDelta throttle_delay = |
204 base::TimeDelta::FromSeconds(kSyncDelayAfterThrottled); | 210 base::TimeDelta::FromSeconds(kSyncDelayAfterThrottled); |
205 if (response.has_client_command()) { | 211 if (response.has_client_command()) { |
206 const sync_pb::ClientCommand& command = response.client_command(); | 212 const sync_pb::ClientCommand& command = response.client_command(); |
207 if (command.has_throttle_delay_seconds()) { | 213 if (command.has_throttle_delay_seconds()) { |
208 throttle_delay = | 214 throttle_delay = |
209 base::TimeDelta::FromSeconds(command.throttle_delay_seconds()); | 215 base::TimeDelta::FromSeconds(command.throttle_delay_seconds()); |
210 } | 216 } |
211 } | 217 } |
212 return throttle_delay; | 218 return throttle_delay; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 return syncer::DISABLE_SYNC_ON_CLIENT; | 288 return syncer::DISABLE_SYNC_ON_CLIENT; |
283 case sync_pb::SyncEnums::UNKNOWN_ACTION: | 289 case sync_pb::SyncEnums::UNKNOWN_ACTION: |
284 return syncer::UNKNOWN_ACTION; | 290 return syncer::UNKNOWN_ACTION; |
285 default: | 291 default: |
286 NOTREACHED(); | 292 NOTREACHED(); |
287 return syncer::UNKNOWN_ACTION; | 293 return syncer::UNKNOWN_ACTION; |
288 } | 294 } |
289 } | 295 } |
290 | 296 |
291 syncer::SyncProtocolError ConvertErrorPBToLocalType( | 297 syncer::SyncProtocolError ConvertErrorPBToLocalType( |
292 const sync_pb::ClientToServerResponse::Error& error) { | 298 const ClientToServerResponse::Error& error) { |
293 syncer::SyncProtocolError sync_protocol_error; | 299 syncer::SyncProtocolError sync_protocol_error; |
294 sync_protocol_error.error_type = ConvertSyncProtocolErrorTypePBToLocalType( | 300 sync_protocol_error.error_type = ConvertSyncProtocolErrorTypePBToLocalType( |
295 error.error_type()); | 301 error.error_type()); |
296 sync_protocol_error.error_description = error.error_description(); | 302 sync_protocol_error.error_description = error.error_description(); |
297 sync_protocol_error.url = error.url(); | 303 sync_protocol_error.url = error.url(); |
298 sync_protocol_error.action = ConvertClientActionPBToLocalClientAction( | 304 sync_protocol_error.action = ConvertClientActionPBToLocalClientAction( |
299 error.action()); | 305 error.action()); |
300 | 306 |
301 if (error.error_data_type_ids_size() > 0) { | 307 if (error.error_data_type_ids_size() > 0) { |
302 // THROTTLED is currently the only error code that uses |error_data_types|. | 308 // THROTTLED is currently the only error code that uses |error_data_types|. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 case syncer::NOT_MY_BIRTHDAY: | 414 case syncer::NOT_MY_BIRTHDAY: |
409 return SERVER_RETURN_NOT_MY_BIRTHDAY; | 415 return SERVER_RETURN_NOT_MY_BIRTHDAY; |
410 default: | 416 default: |
411 NOTREACHED(); | 417 NOTREACHED(); |
412 return UNSET; | 418 return UNSET; |
413 } | 419 } |
414 } | 420 } |
415 | 421 |
416 // static | 422 // static |
417 bool SyncerProtoUtil::Compare(const syncable::Entry& local_entry, | 423 bool SyncerProtoUtil::Compare(const syncable::Entry& local_entry, |
418 const SyncEntity& server_entry) { | 424 const sync_pb::SyncEntity& server_entry) { |
419 const std::string name = NameFromSyncEntity(server_entry); | 425 const std::string name = NameFromSyncEntity(server_entry); |
420 | 426 |
421 CHECK(local_entry.Get(ID) == server_entry.id()) << | 427 CHECK(local_entry.Get(ID) == SyncableIdFromProto(server_entry.id_string())) << |
akalin
2012/07/11 01:42:22
CHECK_EQ?
rlarocque
2012/07/11 19:22:16
Done.
| |
422 " SyncerProtoUtil::Compare precondition not met."; | 428 " SyncerProtoUtil::Compare precondition not met."; |
423 CHECK(server_entry.version() == local_entry.Get(BASE_VERSION)) << | 429 CHECK(server_entry.version() == local_entry.Get(BASE_VERSION)) << |
424 " SyncerProtoUtil::Compare precondition not met."; | 430 " SyncerProtoUtil::Compare precondition not met."; |
425 CHECK(!local_entry.Get(IS_UNSYNCED)) << | 431 CHECK(!local_entry.Get(IS_UNSYNCED)) << |
426 " SyncerProtoUtil::Compare precondition not met."; | 432 " SyncerProtoUtil::Compare precondition not met."; |
427 | 433 |
428 if (local_entry.Get(IS_DEL) && server_entry.deleted()) | 434 if (local_entry.Get(IS_DEL) && server_entry.deleted()) |
429 return true; | 435 return true; |
430 if (local_entry.Get(CTIME) != ProtoTimeToTime(server_entry.ctime())) { | 436 if (local_entry.Get(CTIME) != ProtoTimeToTime(server_entry.ctime())) { |
431 LOG(WARNING) << "ctime mismatch"; | 437 LOG(WARNING) << "ctime mismatch"; |
432 return false; | 438 return false; |
433 } | 439 } |
434 | 440 |
435 // These checks are somewhat prolix, but they're easier to debug than a big | 441 // These checks are somewhat prolix, but they're easier to debug than a big |
436 // boolean statement. | 442 // boolean statement. |
437 string client_name = local_entry.Get(syncable::NON_UNIQUE_NAME); | 443 string client_name = local_entry.Get(syncable::NON_UNIQUE_NAME); |
438 if (client_name != name) { | 444 if (client_name != name) { |
439 LOG(WARNING) << "Client name mismatch"; | 445 LOG(WARNING) << "Client name mismatch"; |
440 return false; | 446 return false; |
441 } | 447 } |
442 if (local_entry.Get(PARENT_ID) != server_entry.parent_id()) { | 448 if (local_entry.Get(PARENT_ID) != |
449 SyncableIdFromProto(server_entry.parent_id_string())) { | |
443 LOG(WARNING) << "Parent ID mismatch"; | 450 LOG(WARNING) << "Parent ID mismatch"; |
444 return false; | 451 return false; |
445 } | 452 } |
446 if (local_entry.Get(IS_DIR) != server_entry.IsFolder()) { | 453 if (local_entry.Get(IS_DIR) != IsFolder(server_entry)) { |
447 LOG(WARNING) << "Dir field mismatch"; | 454 LOG(WARNING) << "Dir field mismatch"; |
448 return false; | 455 return false; |
449 } | 456 } |
450 if (local_entry.Get(IS_DEL) != server_entry.deleted()) { | 457 if (local_entry.Get(IS_DEL) != server_entry.deleted()) { |
451 LOG(WARNING) << "Deletion mismatch"; | 458 LOG(WARNING) << "Deletion mismatch"; |
452 return false; | 459 return false; |
453 } | 460 } |
454 if (!local_entry.Get(IS_DIR) && | 461 if (!local_entry.Get(IS_DIR) && |
455 (local_entry.Get(MTIME) != ProtoTimeToTime(server_entry.mtime()))) { | 462 (local_entry.Get(MTIME) != ProtoTimeToTime(server_entry.mtime()))) { |
456 LOG(WARNING) << "mtime mismatch"; | 463 LOG(WARNING) << "mtime mismatch"; |
(...skipping 28 matching lines...) Expand all Loading... | |
485 // static | 492 // static |
486 const std::string& SyncerProtoUtil::NameFromSyncEntity( | 493 const std::string& SyncerProtoUtil::NameFromSyncEntity( |
487 const sync_pb::SyncEntity& entry) { | 494 const sync_pb::SyncEntity& entry) { |
488 if (entry.has_non_unique_name()) | 495 if (entry.has_non_unique_name()) |
489 return entry.non_unique_name(); | 496 return entry.non_unique_name(); |
490 return entry.name(); | 497 return entry.name(); |
491 } | 498 } |
492 | 499 |
493 // static | 500 // static |
494 const std::string& SyncerProtoUtil::NameFromCommitEntryResponse( | 501 const std::string& SyncerProtoUtil::NameFromCommitEntryResponse( |
495 const CommitResponse_EntryResponse& entry) { | 502 const sync_pb::CommitResponse_EntryResponse& entry) { |
496 if (entry.has_non_unique_name()) | 503 if (entry.has_non_unique_name()) |
497 return entry.non_unique_name(); | 504 return entry.non_unique_name(); |
498 return entry.name(); | 505 return entry.name(); |
499 } | 506 } |
500 | 507 |
501 std::string SyncerProtoUtil::SyncEntityDebugString( | 508 std::string SyncerProtoUtil::SyncEntityDebugString( |
502 const sync_pb::SyncEntity& entry) { | 509 const sync_pb::SyncEntity& entry) { |
503 const std::string& mtime_str = | 510 const std::string& mtime_str = |
504 GetTimeDebugString(ProtoTimeToTime(entry.mtime())); | 511 GetTimeDebugString(ProtoTimeToTime(entry.mtime())); |
505 const std::string& ctime_str = | 512 const std::string& ctime_str = |
(...skipping 21 matching lines...) Expand all Loading... | |
527 output.append("GetUpdatesResponse:\n"); | 534 output.append("GetUpdatesResponse:\n"); |
528 for (int i = 0; i < response.entries_size(); i++) { | 535 for (int i = 0; i < response.entries_size(); i++) { |
529 output.append(SyncerProtoUtil::SyncEntityDebugString(response.entries(i))); | 536 output.append(SyncerProtoUtil::SyncEntityDebugString(response.entries(i))); |
530 output.append("\n"); | 537 output.append("\n"); |
531 } | 538 } |
532 return output; | 539 return output; |
533 } | 540 } |
534 } // namespace | 541 } // namespace |
535 | 542 |
536 std::string SyncerProtoUtil::ClientToServerResponseDebugString( | 543 std::string SyncerProtoUtil::ClientToServerResponseDebugString( |
537 const sync_pb::ClientToServerResponse& response) { | 544 const ClientToServerResponse& response) { |
538 // Add more handlers as needed. | 545 // Add more handlers as needed. |
539 std::string output; | 546 std::string output; |
540 if (response.has_get_updates()) | 547 if (response.has_get_updates()) |
541 output.append(GetUpdatesResponseString(response.get_updates())); | 548 output.append(GetUpdatesResponseString(response.get_updates())); |
542 return output; | 549 return output; |
543 } | 550 } |
544 | 551 |
545 } // namespace syncer | 552 } // namespace syncer |
OLD | NEW |