Chromium Code Reviews| 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::SetEnabledSyncDirectoryTypes( | |
| 23 const std::vector<scoped_refptr<ModelSafeWorker> > workers, | |
| 24 const ModelSafeRoutingInfo& routing_info, | |
| 25 syncable::Directory* directory) { | |
| 26 std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker> > workers_map; | |
| 27 for (size_t i = 0u; i < workers.size(); ++i) { | |
| 28 workers_map.insert( | |
| 29 std::make_pair(workers[i]->GetModelSafeGroup(), workers[i])); | |
| 30 } | |
| 31 | |
| 32 STLDeleteValues<CommitContributorMap>(&commit_contributor_map_); | |
| 33 for (ModelSafeRoutingInfo::const_iterator routing_iter = routing_info.begin(); | |
| 34 routing_iter != routing_info.end(); ++routing_iter) { | |
| 35 ModelType type = routing_iter->first; | |
| 36 ModelSafeGroup group = routing_iter->second; | |
| 37 std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker> >::iterator | |
| 38 worker_it = workers_map.find(group); | |
| 39 DCHECK(worker_it != workers_map.end()); | |
| 40 scoped_refptr<ModelSafeWorker> worker = worker_it->second; | |
| 41 | |
| 42 SyncDirectoryCommitContributor* contributor = | |
| 43 new SyncDirectoryCommitContributor(directory, type); | |
| 44 commit_contributor_map_.insert(std::make_pair(type, contributor)); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 void CommitterList::GatherCommitContributions( | |
| 49 ModelTypeSet commit_types, | |
| 50 size_t max_entries, | |
| 51 ContributionMap* contributions) { | |
| 52 size_t num_entries = 0; | |
| 53 for (ModelTypeSet::Iterator it = commit_types.First(); | |
| 54 it.Good(); it.Inc()) { | |
| 55 CommitContributorMap::iterator cm_it = | |
| 56 commit_contributor_map_.find(it.Get()); | |
| 57 if (cm_it == commit_contributor_map_.end()) { | |
| 58 NOTREACHED() | |
| 59 << "Could not find requested type " << ModelTypeToString(it.Get()) | |
| 60 << " in contributor map."; | |
| 61 continue; | |
| 62 } | |
| 63 size_t spaces_remaining = max_entries - num_entries; | |
| 64 SyncDirectoryCommitContribution* contribution = | |
| 65 cm_it->second->GetContribution(spaces_remaining); | |
| 66 if (contribution) { | |
| 67 num_entries += contribution->GetNumEntries(); | |
| 68 contributions->insert(std::make_pair(it.Get(), contribution)); | |
| 69 } | |
| 70 if (num_entries == max_entries) { | |
| 71 break; // No point in continuting to iterate in this case. | |
| 72 } | |
|
Nicolas Zea
2013/12/18 22:36:10
for safety sake, perhaps also DCHECK_LT(num_entrie
rlarocque
2014/01/03 01:46:41
Done. Implemented with a NOTREACHED().
| |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void CommitterList::RegisterTypeForTest( | |
| 77 ModelType type, | |
| 78 SyncDirectoryCommitContributor* contributor) { | |
| 79 bool inserted = | |
| 80 commit_contributor_map_.insert(std::make_pair(type, contributor)).second; | |
| 81 DCHECK(inserted) << "Attempt to override existing type handler in map"; | |
| 82 } | |
| 83 | |
| 84 } // namespace syncer | |
| OLD | NEW |