Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sync/engine/commit.h" | |
| 6 | |
| 7 #include "base/debug/trace_event.h" | |
| 8 #include "sync/engine/build_commit_command.h" | |
| 9 #include "sync/engine/get_commit_ids_command.h" | |
| 10 #include "sync/engine/process_commit_response_command.h" | |
| 11 #include "sync/engine/syncer_proto_util.h" | |
| 12 #include "sync/sessions/sync_session.h" | |
| 13 | |
| 14 using syncable::SYNCER; | |
| 15 using syncable::WriteTransaction; | |
| 16 | |
| 17 namespace browser_sync { | |
| 18 | |
| 19 using sessions::SyncSession; | |
| 20 using sessions::StatusController; | |
| 21 | |
| 22 // Helper function that finds sync items that are ready to be committed to the | |
| 23 // server and serializes them into a commit message protobuf. It will return | |
| 24 // false iff there are no entries ready to be committed at this time. | |
| 25 // | |
| 26 // The OrderedCommitSet parameter is an output parameter which will contain | |
| 27 // the set of all items which are to be committed. The number of items in | |
| 28 // the set shall not exceed the maximum batch size. (The default batch size | |
| 29 // is currently 25, though it can be overwritten by the server.) | |
| 30 // | |
| 31 // The ClientToServerMessage parameter is an output parameter which will contain | |
| 32 // the commit message which should be sent to the server. It is valid iff the | |
| 33 // return value of this function is true. | |
| 34 bool PrepareCommitMessage(sessions::SyncSession* session, | |
| 35 sessions::OrderedCommitSet* commit_set, | |
| 36 ClientToServerMessage* commit_message) { | |
| 37 TRACE_EVENT0("sync", "PrepareCommitMessage"); | |
| 38 | |
| 39 commit_set->Clear(); | |
| 40 commit_message->Clear(); | |
| 41 | |
| 42 WriteTransaction trans(FROM_HERE, SYNCER, session->context()->directory()); | |
| 43 sessions::ScopedSetSessionWriteTransaction set_trans(session, &trans); | |
| 44 | |
| 45 // Fetch the items to commit. | |
| 46 const size_t batch_size = session->context()->max_commit_batch_size(); | |
| 47 GetCommitIdsCommand get_commit_ids_command(batch_size, commit_set); | |
| 48 get_commit_ids_command.Execute(session); | |
| 49 | |
| 50 DVLOG(1) << "Commit message will contain " << commit_set->Size() << " items."; | |
| 51 if (commit_set->Empty()) | |
| 52 return false; | |
| 53 | |
| 54 // Serialize the message. | |
| 55 BuildCommitCommand build_commit_command(*commit_set, commit_message); | |
| 56 build_commit_command.Execute(session); | |
| 57 | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 SyncerError BuildAndPostCommits(Syncer* syncer, | |
|
tim (not reviewing)
2012/05/15 22:25:00
Hmm. Now it feels like we're trying to code around
| |
| 62 sessions::SyncSession* session) { | |
| 63 StatusController* status_controller = session->mutable_status_controller(); | |
| 64 | |
| 65 sessions::OrderedCommitSet commit_set(session->routing_info()); | |
| 66 ClientToServerMessage commit_message; | |
| 67 while (PrepareCommitMessage(session, &commit_set, &commit_message) | |
| 68 && !syncer->ExitRequested()) { | |
| 69 ClientToServerResponse commit_response; | |
| 70 | |
| 71 DVLOG(1) << "Sending commit message."; | |
| 72 TRACE_EVENT_BEGIN0("sync", "PostCommit"); | |
| 73 status_controller->set_last_post_commit_result( | |
| 74 SyncerProtoUtil::PostClientToServerMessage(commit_message, | |
|
tim (not reviewing)
2012/05/15 22:25:00
indent
| |
| 75 &commit_response, | |
| 76 session)); | |
| 77 TRACE_EVENT_END0("sync", "PostCommit"); | |
| 78 | |
| 79 // ProcessCommitResponse includes some code that cleans up after a failure | |
| 80 // to post a commit message, so we must run it regardless of whether or not | |
| 81 // the commit succeeds. | |
| 82 | |
| 83 TRACE_EVENT_BEGIN0("sync", "ProcessCommitResponse"); | |
| 84 ProcessCommitResponseCommand process_response_command( | |
| 85 commit_set, commit_message, commit_response); | |
| 86 status_controller->set_last_process_commit_response_result( | |
| 87 process_response_command.Execute(session)); | |
| 88 TRACE_EVENT_END0("sync", "ProcessCommitResponse"); | |
| 89 | |
| 90 // Exit early if either the commit or the response processing failed. | |
| 91 if (status_controller->last_post_commit_result() != SYNCER_OK) | |
| 92 return status_controller->last_post_commit_result(); | |
| 93 if (status_controller->last_process_commit_response_result() != SYNCER_OK) | |
| 94 return status_controller->last_process_commit_response_result(); | |
| 95 } | |
| 96 | |
| 97 return SYNCER_OK; | |
| 98 } | |
| 99 | |
| 100 } // namespace browser_sync | |
| OLD | NEW |