OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/committer_list.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "sync/engine/sync_directory_commit_contribution.h" |
| 10 #include "sync/engine/sync_directory_commit_contributor.h" |
| 11 #include "sync/protocol/sync.pb.h" |
| 12 |
| 13 namespace syncer { |
| 14 |
| 15 typedef std::map<ModelType, size_t> TypeToIndexMap; |
| 16 |
| 17 CommitterList::CommitterList() |
| 18 : commit_contributor_deleter_(&commit_contributor_map_) {} |
| 19 |
| 20 CommitterList::~CommitterList() {} |
| 21 |
| 22 void CommitterList::RegisterType( |
| 23 ModelType type, |
| 24 SyncDirectoryCommitContributor* contributor) { |
| 25 bool inserted = |
| 26 commit_contributor_map_.insert(std::make_pair(type, contributor)).second; |
| 27 DCHECK(inserted) << "Attempt to override existing type handler in map"; |
| 28 } |
| 29 |
| 30 void CommitterList::GatherCommitContributions( |
| 31 ModelTypeSet commit_types, |
| 32 size_t max_entries, |
| 33 ContributionMap* contributions) { |
| 34 size_t num_entries = 0; |
| 35 for (ModelTypeSet::Iterator it = commit_types.First(); |
| 36 it.Good(); it.Inc()) { |
| 37 CommitContributorMap::iterator cm_it = |
| 38 commit_contributor_map_.find(it.Get()); |
| 39 if (cm_it == commit_contributor_map_.end()) { |
| 40 NOTREACHED() |
| 41 << "Could not find requested type " << ModelTypeToString(it.Get()) |
| 42 << " in contributor map."; |
| 43 continue; |
| 44 } |
| 45 size_t spaces_remaining = max_entries - num_entries; |
| 46 SyncDirectoryCommitContribution* contribution = |
| 47 cm_it->second->GetContribution(spaces_remaining); |
| 48 if (contribution) { |
| 49 num_entries += contribution->GetNumEntries(); |
| 50 contributions->insert(std::make_pair(it.Get(), contribution)); |
| 51 } |
| 52 if (num_entries >= max_entries) { |
| 53 DCHECK_EQ(num_entries, max_entries) |
| 54 << "Number of commit entries exceeeds maximum"; |
| 55 break; |
| 56 } |
| 57 } |
| 58 } |
| 59 |
| 60 } // namespace syncer |
OLD | NEW |