Chromium Code Reviews| Index: sync/engine/committer_list.cc |
| diff --git a/sync/engine/committer_list.cc b/sync/engine/committer_list.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..80888fc235771768a4bf1e9fc37b9c15838403a1 |
| --- /dev/null |
| +++ b/sync/engine/committer_list.cc |
| @@ -0,0 +1,84 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "sync/engine/committer_list.h" |
| + |
| +#include <map> |
| + |
| +#include "sync/engine/sync_directory_commit_contribution.h" |
| +#include "sync/engine/sync_directory_commit_contributor.h" |
| +#include "sync/protocol/sync.pb.h" |
| + |
| +namespace syncer { |
| + |
| +typedef std::map<ModelType, size_t> TypeToIndexMap; |
| + |
| +CommitterList::CommitterList() |
| + : commit_contributor_deleter_(&commit_contributor_map_) {} |
| + |
| +CommitterList::~CommitterList() {} |
| + |
| +void CommitterList::SetEnabledSyncDirectoryTypes( |
| + const std::vector<scoped_refptr<ModelSafeWorker> > workers, |
| + const ModelSafeRoutingInfo& routing_info, |
| + syncable::Directory* directory) { |
| + std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker> > workers_map; |
| + for (size_t i = 0u; i < workers.size(); ++i) { |
| + workers_map.insert( |
| + std::make_pair(workers[i]->GetModelSafeGroup(), workers[i])); |
| + } |
| + |
| + STLDeleteValues<CommitContributorMap>(&commit_contributor_map_); |
| + for (ModelSafeRoutingInfo::const_iterator routing_iter = routing_info.begin(); |
| + routing_iter != routing_info.end(); ++routing_iter) { |
| + ModelType type = routing_iter->first; |
| + ModelSafeGroup group = routing_iter->second; |
| + std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker> >::iterator |
| + worker_it = workers_map.find(group); |
| + DCHECK(worker_it != workers_map.end()); |
| + scoped_refptr<ModelSafeWorker> worker = worker_it->second; |
| + |
| + SyncDirectoryCommitContributor* contributor = |
| + new SyncDirectoryCommitContributor(directory, type); |
| + commit_contributor_map_.insert(std::make_pair(type, contributor)); |
| + } |
| +} |
| + |
| +void CommitterList::GatherCommitContributions( |
| + ModelTypeSet commit_types, |
| + size_t max_entries, |
| + ContributionMap* contributions) { |
| + size_t num_entries = 0; |
| + for (ModelTypeSet::Iterator it = commit_types.First(); |
| + it.Good(); it.Inc()) { |
| + CommitContributorMap::iterator cm_it = |
| + commit_contributor_map_.find(it.Get()); |
| + if (cm_it == commit_contributor_map_.end()) { |
| + NOTREACHED() |
| + << "Could not find requested type " << ModelTypeToString(it.Get()) |
| + << " in contributor map."; |
| + continue; |
| + } |
| + size_t spaces_remaining = max_entries - num_entries; |
| + SyncDirectoryCommitContribution* contribution = |
| + cm_it->second->GetContribution(spaces_remaining); |
| + if (contribution) { |
| + num_entries += contribution->GetNumEntries(); |
| + contributions->insert(std::make_pair(it.Get(), contribution)); |
| + } |
| + if (num_entries == max_entries) { |
| + break; // No point in continuting to iterate in this case. |
| + } |
|
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().
|
| + } |
| +} |
| + |
| +void CommitterList::RegisterTypeForTest( |
| + ModelType type, |
| + SyncDirectoryCommitContributor* contributor) { |
| + bool inserted = |
| + commit_contributor_map_.insert(std::make_pair(type, contributor)).second; |
| + DCHECK(inserted) << "Attempt to override existing type handler in map"; |
| +} |
| + |
| +} // namespace syncer |