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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 } | 156 } |
154 | 157 |
155 // static | 158 // static |
156 void SyncerProtoUtil::AddRequestBirthday(syncable::Directory* dir, | 159 void SyncerProtoUtil::AddRequestBirthday(syncable::Directory* dir, |
157 ClientToServerMessage* msg) { | 160 ClientToServerMessage* msg) { |
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 |
166 void SyncerProtoUtil::SetProtocolVersion(ClientToServerMessage* msg) { | |
167 int current_version = | |
akalin
2012/07/11 23:10:40
const
| |
168 ClientToServerMessage::default_instance().protocol_version(); | |
169 msg->set_protocol_version(current_version); | |
170 } | |
171 | |
172 // static | |
163 bool SyncerProtoUtil::PostAndProcessHeaders(ServerConnectionManager* scm, | 173 bool SyncerProtoUtil::PostAndProcessHeaders(ServerConnectionManager* scm, |
164 sessions::SyncSession* session, | 174 sessions::SyncSession* session, |
165 const ClientToServerMessage& msg, | 175 const ClientToServerMessage& msg, |
166 ClientToServerResponse* response) { | 176 ClientToServerResponse* response) { |
167 ServerConnectionManager::PostBufferParams params; | 177 ServerConnectionManager::PostBufferParams params; |
178 DCHECK(msg.has_protocol_version()); | |
akalin
2012/07/11 23:10:40
maybe this is important enough to use a CHECK?
rlarocque
2012/07/11 23:45:36
I'm always in favor of harsher CHECKs, but I'm not
| |
179 DCHECK_EQ(msg.protocol_version(), | |
180 ClientToServerMessage::default_instance().protocol_version()); | |
168 msg.SerializeToString(¶ms.buffer_in); | 181 msg.SerializeToString(¶ms.buffer_in); |
169 | 182 |
170 ScopedServerStatusWatcher server_status_watcher(scm, ¶ms.response); | 183 ScopedServerStatusWatcher server_status_watcher(scm, ¶ms.response); |
171 // Fills in params.buffer_out and params.response. | 184 // Fills in params.buffer_out and params.response. |
172 if (!scm->PostBufferWithCachedAuth(¶ms, &server_status_watcher)) { | 185 if (!scm->PostBufferWithCachedAuth(¶ms, &server_status_watcher)) { |
173 LOG(WARNING) << "Error posting from syncer:" << params.response; | 186 LOG(WARNING) << "Error posting from syncer:" << params.response; |
174 return false; | 187 return false; |
175 } | 188 } |
176 | 189 |
177 std::string new_token = params.response.update_client_auth_header; | 190 std::string new_token = params.response.update_client_auth_header; |
(...skipping 14 matching lines...) Expand all Loading... | |
192 return false; | 205 return false; |
193 default: | 206 default: |
194 return true; | 207 return true; |
195 } | 208 } |
196 } | 209 } |
197 | 210 |
198 return false; | 211 return false; |
199 } | 212 } |
200 | 213 |
201 base::TimeDelta SyncerProtoUtil::GetThrottleDelay( | 214 base::TimeDelta SyncerProtoUtil::GetThrottleDelay( |
202 const sync_pb::ClientToServerResponse& response) { | 215 const ClientToServerResponse& response) { |
203 base::TimeDelta throttle_delay = | 216 base::TimeDelta throttle_delay = |
204 base::TimeDelta::FromSeconds(kSyncDelayAfterThrottled); | 217 base::TimeDelta::FromSeconds(kSyncDelayAfterThrottled); |
205 if (response.has_client_command()) { | 218 if (response.has_client_command()) { |
206 const sync_pb::ClientCommand& command = response.client_command(); | 219 const sync_pb::ClientCommand& command = response.client_command(); |
207 if (command.has_throttle_delay_seconds()) { | 220 if (command.has_throttle_delay_seconds()) { |
208 throttle_delay = | 221 throttle_delay = |
209 base::TimeDelta::FromSeconds(command.throttle_delay_seconds()); | 222 base::TimeDelta::FromSeconds(command.throttle_delay_seconds()); |
210 } | 223 } |
211 } | 224 } |
212 return throttle_delay; | 225 return throttle_delay; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 return syncer::DISABLE_SYNC_ON_CLIENT; | 295 return syncer::DISABLE_SYNC_ON_CLIENT; |
283 case sync_pb::SyncEnums::UNKNOWN_ACTION: | 296 case sync_pb::SyncEnums::UNKNOWN_ACTION: |
284 return syncer::UNKNOWN_ACTION; | 297 return syncer::UNKNOWN_ACTION; |
285 default: | 298 default: |
286 NOTREACHED(); | 299 NOTREACHED(); |
287 return syncer::UNKNOWN_ACTION; | 300 return syncer::UNKNOWN_ACTION; |
288 } | 301 } |
289 } | 302 } |
290 | 303 |
291 syncer::SyncProtocolError ConvertErrorPBToLocalType( | 304 syncer::SyncProtocolError ConvertErrorPBToLocalType( |
292 const sync_pb::ClientToServerResponse::Error& error) { | 305 const ClientToServerResponse::Error& error) { |
293 syncer::SyncProtocolError sync_protocol_error; | 306 syncer::SyncProtocolError sync_protocol_error; |
294 sync_protocol_error.error_type = ConvertSyncProtocolErrorTypePBToLocalType( | 307 sync_protocol_error.error_type = ConvertSyncProtocolErrorTypePBToLocalType( |
295 error.error_type()); | 308 error.error_type()); |
296 sync_protocol_error.error_description = error.error_description(); | 309 sync_protocol_error.error_description = error.error_description(); |
297 sync_protocol_error.url = error.url(); | 310 sync_protocol_error.url = error.url(); |
298 sync_protocol_error.action = ConvertClientActionPBToLocalClientAction( | 311 sync_protocol_error.action = ConvertClientActionPBToLocalClientAction( |
299 error.action()); | 312 error.action()); |
300 | 313 |
301 if (error.error_data_type_ids_size() > 0) { | 314 if (error.error_data_type_ids_size() > 0) { |
302 // THROTTLED is currently the only error code that uses |error_data_types|. | 315 // 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: | 421 case syncer::NOT_MY_BIRTHDAY: |
409 return SERVER_RETURN_NOT_MY_BIRTHDAY; | 422 return SERVER_RETURN_NOT_MY_BIRTHDAY; |
410 default: | 423 default: |
411 NOTREACHED(); | 424 NOTREACHED(); |
412 return UNSET; | 425 return UNSET; |
413 } | 426 } |
414 } | 427 } |
415 | 428 |
416 // static | 429 // static |
417 bool SyncerProtoUtil::Compare(const syncable::Entry& local_entry, | 430 bool SyncerProtoUtil::Compare(const syncable::Entry& local_entry, |
418 const SyncEntity& server_entry) { | 431 const sync_pb::SyncEntity& server_entry) { |
419 const std::string name = NameFromSyncEntity(server_entry); | 432 const std::string name = NameFromSyncEntity(server_entry); |
420 | 433 |
421 CHECK(local_entry.Get(ID) == server_entry.id()) << | 434 CHECK_EQ(local_entry.Get(ID), SyncableIdFromProto(server_entry.id_string())); |
422 " SyncerProtoUtil::Compare precondition not met."; | 435 CHECK_EQ(server_entry.version(), local_entry.Get(BASE_VERSION)); |
423 CHECK(server_entry.version() == local_entry.Get(BASE_VERSION)) << | 436 CHECK(!local_entry.Get(IS_UNSYNCED)); |
424 " SyncerProtoUtil::Compare precondition not met."; | |
425 CHECK(!local_entry.Get(IS_UNSYNCED)) << | |
426 " SyncerProtoUtil::Compare precondition not met."; | |
427 | 437 |
428 if (local_entry.Get(IS_DEL) && server_entry.deleted()) | 438 if (local_entry.Get(IS_DEL) && server_entry.deleted()) |
429 return true; | 439 return true; |
430 if (local_entry.Get(CTIME) != ProtoTimeToTime(server_entry.ctime())) { | 440 if (local_entry.Get(CTIME) != ProtoTimeToTime(server_entry.ctime())) { |
431 LOG(WARNING) << "ctime mismatch"; | 441 LOG(WARNING) << "ctime mismatch"; |
432 return false; | 442 return false; |
433 } | 443 } |
434 | 444 |
435 // These checks are somewhat prolix, but they're easier to debug than a big | 445 // These checks are somewhat prolix, but they're easier to debug than a big |
436 // boolean statement. | 446 // boolean statement. |
437 string client_name = local_entry.Get(syncable::NON_UNIQUE_NAME); | 447 string client_name = local_entry.Get(syncable::NON_UNIQUE_NAME); |
438 if (client_name != name) { | 448 if (client_name != name) { |
439 LOG(WARNING) << "Client name mismatch"; | 449 LOG(WARNING) << "Client name mismatch"; |
440 return false; | 450 return false; |
441 } | 451 } |
442 if (local_entry.Get(PARENT_ID) != server_entry.parent_id()) { | 452 if (local_entry.Get(PARENT_ID) != |
453 SyncableIdFromProto(server_entry.parent_id_string())) { | |
443 LOG(WARNING) << "Parent ID mismatch"; | 454 LOG(WARNING) << "Parent ID mismatch"; |
444 return false; | 455 return false; |
445 } | 456 } |
446 if (local_entry.Get(IS_DIR) != server_entry.IsFolder()) { | 457 if (local_entry.Get(IS_DIR) != IsFolder(server_entry)) { |
447 LOG(WARNING) << "Dir field mismatch"; | 458 LOG(WARNING) << "Dir field mismatch"; |
448 return false; | 459 return false; |
449 } | 460 } |
450 if (local_entry.Get(IS_DEL) != server_entry.deleted()) { | 461 if (local_entry.Get(IS_DEL) != server_entry.deleted()) { |
451 LOG(WARNING) << "Deletion mismatch"; | 462 LOG(WARNING) << "Deletion mismatch"; |
452 return false; | 463 return false; |
453 } | 464 } |
454 if (!local_entry.Get(IS_DIR) && | 465 if (!local_entry.Get(IS_DIR) && |
455 (local_entry.Get(MTIME) != ProtoTimeToTime(server_entry.mtime()))) { | 466 (local_entry.Get(MTIME) != ProtoTimeToTime(server_entry.mtime()))) { |
456 LOG(WARNING) << "mtime mismatch"; | 467 LOG(WARNING) << "mtime mismatch"; |
(...skipping 28 matching lines...) Expand all Loading... | |
485 // static | 496 // static |
486 const std::string& SyncerProtoUtil::NameFromSyncEntity( | 497 const std::string& SyncerProtoUtil::NameFromSyncEntity( |
487 const sync_pb::SyncEntity& entry) { | 498 const sync_pb::SyncEntity& entry) { |
488 if (entry.has_non_unique_name()) | 499 if (entry.has_non_unique_name()) |
489 return entry.non_unique_name(); | 500 return entry.non_unique_name(); |
490 return entry.name(); | 501 return entry.name(); |
491 } | 502 } |
492 | 503 |
493 // static | 504 // static |
494 const std::string& SyncerProtoUtil::NameFromCommitEntryResponse( | 505 const std::string& SyncerProtoUtil::NameFromCommitEntryResponse( |
495 const CommitResponse_EntryResponse& entry) { | 506 const sync_pb::CommitResponse_EntryResponse& entry) { |
496 if (entry.has_non_unique_name()) | 507 if (entry.has_non_unique_name()) |
497 return entry.non_unique_name(); | 508 return entry.non_unique_name(); |
498 return entry.name(); | 509 return entry.name(); |
499 } | 510 } |
500 | 511 |
501 std::string SyncerProtoUtil::SyncEntityDebugString( | 512 std::string SyncerProtoUtil::SyncEntityDebugString( |
502 const sync_pb::SyncEntity& entry) { | 513 const sync_pb::SyncEntity& entry) { |
503 const std::string& mtime_str = | 514 const std::string& mtime_str = |
504 GetTimeDebugString(ProtoTimeToTime(entry.mtime())); | 515 GetTimeDebugString(ProtoTimeToTime(entry.mtime())); |
505 const std::string& ctime_str = | 516 const std::string& ctime_str = |
(...skipping 21 matching lines...) Expand all Loading... | |
527 output.append("GetUpdatesResponse:\n"); | 538 output.append("GetUpdatesResponse:\n"); |
528 for (int i = 0; i < response.entries_size(); i++) { | 539 for (int i = 0; i < response.entries_size(); i++) { |
529 output.append(SyncerProtoUtil::SyncEntityDebugString(response.entries(i))); | 540 output.append(SyncerProtoUtil::SyncEntityDebugString(response.entries(i))); |
530 output.append("\n"); | 541 output.append("\n"); |
531 } | 542 } |
532 return output; | 543 return output; |
533 } | 544 } |
534 } // namespace | 545 } // namespace |
535 | 546 |
536 std::string SyncerProtoUtil::ClientToServerResponseDebugString( | 547 std::string SyncerProtoUtil::ClientToServerResponseDebugString( |
537 const sync_pb::ClientToServerResponse& response) { | 548 const ClientToServerResponse& response) { |
538 // Add more handlers as needed. | 549 // Add more handlers as needed. |
539 std::string output; | 550 std::string output; |
540 if (response.has_get_updates()) | 551 if (response.has_get_updates()) |
541 output.append(GetUpdatesResponseString(response.get_updates())); | 552 output.append(GetUpdatesResponseString(response.get_updates())); |
542 return output; | 553 return output; |
543 } | 554 } |
544 | 555 |
545 } // namespace syncer | 556 } // namespace syncer |
OLD | NEW |