1 // Copyright 2014 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_processor.h" 6 7 #include <map> 8 9 #include "sync/engine/commit_contribution.h" 10 #include "sync/engine/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 CommitProcessor::CommitProcessor(CommitContributorMap* commit_contributor_map) 18 : commit_contributor_map_(commit_contributor_map) {} 19 20 CommitProcessor::~CommitProcessor() {} 21 22 void CommitProcessor::GatherCommitContributions( 23 ModelTypeSet commit_types, 24 size_t max_entries, 25 ContributionMap* contributions) { 26 size_t num_entries = 0; 27 for (ModelTypeSet::Iterator it = commit_types.First(); 28 it.Good(); it.Inc()) { 29 CommitContributorMap::iterator cm_it = 30 commit_contributor_map_->find(it.Get()); 31 if (cm_it == commit_contributor_map_->end()) { 32 NOTREACHED() 33 << "Could not find requested type " << ModelTypeToString(it.Get()) 34 << " in contributor map."; 35 continue; 36 } 37 size_t spaces_remaining = max_entries - num_entries; 38 scoped_ptr<CommitContribution> contribution = 39 cm_it->second->GetContribution(spaces_remaining); 40 if (contribution) { 41 num_entries += contribution->GetNumEntries(); 42 contributions->insert(std::make_pair(it.Get(), contribution.release())); 43 } 44 if (num_entries >= max_entries) { 45 DCHECK_EQ(num_entries, max_entries) 46 << "Number of commit entries exceeeds maximum"; 47 break; 48 } 49 } 50 } 51 52 } // namespace syncer 53