| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/commit.h" | 5 #include "sync/engine/commit.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "sync/engine/commit_processor.h" |
| 8 #include "sync/engine/commit_util.h" | 9 #include "sync/engine/commit_util.h" |
| 9 #include "sync/engine/sync_directory_commit_contribution.h" | 10 #include "sync/engine/sync_directory_commit_contribution.h" |
| 10 #include "sync/engine/syncer.h" | 11 #include "sync/engine/syncer.h" |
| 11 #include "sync/engine/syncer_proto_util.h" | 12 #include "sync/engine/syncer_proto_util.h" |
| 12 #include "sync/sessions/sync_session.h" | 13 #include "sync/sessions/sync_session.h" |
| 13 | 14 |
| 14 namespace syncer { | 15 namespace syncer { |
| 15 | 16 |
| 16 Commit::Commit( | 17 Commit::Commit( |
| 17 const std::map<ModelType, SyncDirectoryCommitContribution*>& contributions, | 18 const std::map<ModelType, SyncDirectoryCommitContribution*>& contributions, |
| 18 const sync_pb::ClientToServerMessage& message, | 19 const sync_pb::ClientToServerMessage& message, |
| 19 ExtensionsActivity::Records extensions_activity_buffer) | 20 ExtensionsActivity::Records extensions_activity_buffer) |
| 20 : contributions_(contributions), | 21 : contributions_(contributions), |
| 21 deleter_(&contributions_), | 22 deleter_(&contributions_), |
| 22 message_(message), | 23 message_(message), |
| 23 extensions_activity_buffer_(extensions_activity_buffer), | 24 extensions_activity_buffer_(extensions_activity_buffer), |
| 24 cleaned_up_(false) { | 25 cleaned_up_(false) { |
| 25 } | 26 } |
| 26 | 27 |
| 27 Commit::~Commit() { | 28 Commit::~Commit() { |
| 28 DCHECK(cleaned_up_); | 29 DCHECK(cleaned_up_); |
| 29 } | 30 } |
| 30 | 31 |
| 31 Commit* Commit::Init( | 32 Commit* Commit::Init( |
| 32 ModelTypeSet requested_types, | 33 ModelTypeSet requested_types, |
| 34 ModelTypeSet enabled_types, |
| 33 size_t max_entries, | 35 size_t max_entries, |
| 34 const std::string& account_name, | 36 const std::string& account_name, |
| 35 const std::string& cache_guid, | 37 const std::string& cache_guid, |
| 36 CommitContributorMap* contributor_map, | 38 CommitProcessor* commit_processor, |
| 37 ExtensionsActivity* extensions_activity) { | 39 ExtensionsActivity* extensions_activity) { |
| 38 // Gather per-type contributions. | 40 // Gather per-type contributions. |
| 39 ContributionMap contributions; | 41 ContributionMap contributions; |
| 40 size_t num_entries = 0; | 42 commit_processor->GatherCommitContributions( |
| 41 for (ModelTypeSet::Iterator it = requested_types.First(); | 43 requested_types, |
| 42 it.Good(); it.Inc()) { | 44 max_entries, |
| 43 CommitContributorMap::iterator cm_it = contributor_map->find(it.Get()); | 45 &contributions); |
| 44 if (cm_it == contributor_map->end()) { | |
| 45 NOTREACHED() | |
| 46 << "Could not find requested type " << ModelTypeToString(it.Get()) | |
| 47 << " in contributor map."; | |
| 48 continue; | |
| 49 } | |
| 50 size_t spaces_remaining = max_entries - num_entries; | |
| 51 SyncDirectoryCommitContribution* contribution = | |
| 52 cm_it->second->GetContribution(spaces_remaining); | |
| 53 if (contribution) { | |
| 54 num_entries += contribution->GetNumEntries(); | |
| 55 contributions.insert(std::make_pair(it.Get(), contribution)); | |
| 56 } | |
| 57 if (num_entries == max_entries) { | |
| 58 break; // No point in continuting to iterate in this case. | |
| 59 } | |
| 60 } | |
| 61 | 46 |
| 62 // Give up if no one had anything to commit. | 47 // Give up if no one had anything to commit. |
| 63 if (contributions.empty()) | 48 if (contributions.empty()) |
| 64 return NULL; | 49 return NULL; |
| 65 | 50 |
| 66 sync_pb::ClientToServerMessage message; | 51 sync_pb::ClientToServerMessage message; |
| 67 message.set_message_contents(sync_pb::ClientToServerMessage::COMMIT); | 52 message.set_message_contents(sync_pb::ClientToServerMessage::COMMIT); |
| 68 message.set_share(account_name); | 53 message.set_share(account_name); |
| 69 | 54 |
| 70 sync_pb::CommitMessage* commit_message = message.mutable_commit(); | 55 sync_pb::CommitMessage* commit_message = message.mutable_commit(); |
| 71 commit_message->set_cache_guid(cache_guid); | 56 commit_message->set_cache_guid(cache_guid); |
| 72 | 57 |
| 73 // Set extensions activity if bookmark commits are present. | 58 // Set extensions activity if bookmark commits are present. |
| 74 ExtensionsActivity::Records extensions_activity_buffer; | 59 ExtensionsActivity::Records extensions_activity_buffer; |
| 75 ContributionMap::iterator it = contributions.find(syncer::BOOKMARKS); | 60 ContributionMap::iterator it = contributions.find(syncer::BOOKMARKS); |
| 76 if (it != contributions.end() && it->second->GetNumEntries() != 0) { | 61 if (it != contributions.end() && it->second->GetNumEntries() != 0) { |
| 77 commit_util::AddExtensionsActivityToMessage( | 62 commit_util::AddExtensionsActivityToMessage( |
| 78 extensions_activity, | 63 extensions_activity, |
| 79 &extensions_activity_buffer, | 64 &extensions_activity_buffer, |
| 80 commit_message); | 65 commit_message); |
| 81 } | 66 } |
| 82 | 67 |
| 83 // Set the client config params. | 68 // Set the client config params. |
| 84 ModelTypeSet enabled_types; | 69 commit_util::AddClientConfigParamsToMessage( |
| 85 for (CommitContributorMap::iterator it = contributor_map->begin(); | 70 enabled_types, |
| 86 it != contributor_map->end(); ++it) { | 71 commit_message); |
| 87 enabled_types.Put(it->first); | |
| 88 } | |
| 89 commit_util::AddClientConfigParamsToMessage(enabled_types, | |
| 90 commit_message); | |
| 91 | 72 |
| 92 // Finally, serialize all our contributions. | 73 // Finally, serialize all our contributions. |
| 93 for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it = | 74 for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it = |
| 94 contributions.begin(); it != contributions.end(); ++it) { | 75 contributions.begin(); it != contributions.end(); ++it) { |
| 95 it->second->AddToCommitMessage(&message); | 76 it->second->AddToCommitMessage(&message); |
| 96 } | 77 } |
| 97 | 78 |
| 98 // If we made it this far, then we've successfully prepared a commit message. | 79 // If we made it this far, then we've successfully prepared a commit message. |
| 99 return new Commit(contributions, message, extensions_activity_buffer); | 80 return new Commit(contributions, message, extensions_activity_buffer); |
| 100 } | 81 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 | 152 |
| 172 void Commit::CleanUp() { | 153 void Commit::CleanUp() { |
| 173 for (ContributionMap::iterator it = contributions_.begin(); | 154 for (ContributionMap::iterator it = contributions_.begin(); |
| 174 it != contributions_.end(); ++it) { | 155 it != contributions_.end(); ++it) { |
| 175 it->second->CleanUp(); | 156 it->second->CleanUp(); |
| 176 } | 157 } |
| 177 cleaned_up_ = true; | 158 cleaned_up_ = true; |
| 178 } | 159 } |
| 179 | 160 |
| 180 } // namespace syncer | 161 } // namespace syncer |
| OLD | NEW |