Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(372)

Side by Side Diff: sync/engine/updater_list.cc

Issue 93433006: sync: Introduce ModelTypeRegistry and helpers (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes + delete sync_sessions_unittest.cc Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/updater_list.h"
6
7 #include <map>
8
9 #include "sync/engine/sync_directory_update_handler.h"
10 #include "sync/protocol/sync.pb.h"
11
12 namespace syncer {
13
14 typedef std::map<ModelType, size_t> TypeToIndexMap;
15
16 UpdaterList::UpdaterList()
17 : update_handler_deleter_(&update_handler_map_) {}
18
19 UpdaterList::~UpdaterList() {}
20
21 void UpdaterList::RegisterType(
22 ModelType type,
23 SyncDirectoryUpdateHandler* handler) {
24 bool inserted =
25 update_handler_map_.insert(std::make_pair(type, handler)).second;
26 DCHECK(inserted) << "Attempt to override existing type handler in map";
27 }
28
29 void UpdaterList::PrepareGetUpdates(
30 ModelTypeSet gu_types,
31 sync_pb::GetUpdatesMessage* get_updates) {
32 for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) {
33 UpdateHandlerMap::iterator handler_it = update_handler_map_.find(it.Get());
34 DCHECK(handler_it != update_handler_map_.end());
35 sync_pb::DataTypeProgressMarker* progress_marker =
36 get_updates->add_from_progress_marker();
37 handler_it->second->GetDownloadProgress(progress_marker);
38 }
39 }
40
41 namespace {
42
43 // Given a GetUpdates response, iterates over all the returned items and
44 // divides them according to their type. Outputs a map from model types to
45 // received SyncEntities. The output map will have entries (possibly empty)
46 // for all types in |requested_types|.
47 void PartitionUpdatesByType(
48 const sync_pb::GetUpdatesResponse& updates,
49 ModelTypeSet requested_types,
50 TypeSyncEntityMap* updates_by_type) {
51 int update_count = updates.entries().size();
52 for (ModelTypeSet::Iterator it = requested_types.First();
53 it.Good(); it.Inc()) {
54 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList()));
55 }
56 for (int i = 0; i < update_count; ++i) {
57 const sync_pb::SyncEntity& update = updates.entries(i);
58 ModelType type = GetModelType(update);
59 if (!IsRealDataType(type)) {
60 NOTREACHED() << "Received update with invalid type.";
61 continue;
62 }
63
64 TypeSyncEntityMap::iterator it = updates_by_type->find(type);
65 if (it == updates_by_type->end()) {
66 NOTREACHED() << "Received update for unexpected type "
67 << ModelTypeToString(type);
68 continue;
69 }
70
71 it->second.push_back(&update);
72 }
73 }
74
75 // Builds a map of ModelTypes to indices to progress markers in the given
76 // |gu_response| message. The map is returned in the |index_map| parameter.
77 void PartitionProgressMarkersByType(
78 const sync_pb::GetUpdatesResponse& gu_response,
79 ModelTypeSet request_types,
80 TypeToIndexMap* index_map) {
81 for (int i = 0; i < gu_response.new_progress_marker_size(); ++i) {
82 int field_number = gu_response.new_progress_marker(i).data_type_id();
83 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
84 if (!IsRealDataType(model_type)) {
85 DLOG(WARNING) << "Unknown field number " << field_number;
86 continue;
87 }
88 if (!request_types.Has(model_type)) {
89 DLOG(WARNING)
90 << "Skipping unexpected progress marker for non-enabled type "
91 << ModelTypeToString(model_type);
92 continue;
93 }
94 index_map->insert(std::make_pair(model_type, i));
95 }
96 }
97
98 } // namespace
99
100 bool UpdaterList::ProcessGetUpdatesResponse(
101 ModelTypeSet gu_types,
102 const sync_pb::GetUpdatesResponse& gu_response,
103 sessions::StatusController* status_controller) {
104 TypeSyncEntityMap updates_by_type;
105 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type);
106 DCHECK_EQ(gu_types.Size(), updates_by_type.size());
107
108 TypeToIndexMap progress_index_by_type;
109 PartitionProgressMarkersByType(gu_response,
110 gu_types,
111 &progress_index_by_type);
112 if (gu_types.Size() != progress_index_by_type.size()) {
113 NOTREACHED() << "Missing progress markers in GetUpdates response.";
114 return false;
115 }
116
117 // Iterate over these maps in parallel, processing updates for each type.
118 TypeToIndexMap::iterator progress_marker_iter =
119 progress_index_by_type.begin();
120 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin();
121 for (; (progress_marker_iter != progress_index_by_type.end()
122 && updates_iter != updates_by_type.end());
123 ++progress_marker_iter, ++updates_iter) {
124 DCHECK_EQ(progress_marker_iter->first, updates_iter->first);
125 ModelType type = progress_marker_iter->first;
126
127 UpdateHandlerMap::iterator update_handler_iter =
128 update_handler_map_.find(type);
129
130 if (update_handler_iter != update_handler_map_.end()) {
131 update_handler_iter->second->ProcessGetUpdatesResponse(
132 gu_response.new_progress_marker(progress_marker_iter->second),
133 updates_iter->second,
134 status_controller);
135 } else {
136 DLOG(WARNING)
137 << "Ignoring received updates of a type we can't handle. "
138 << "Type is: " << ModelTypeToString(type);
139 continue;
140 }
141 }
142 DCHECK(progress_marker_iter == progress_index_by_type.end()
143 && updates_iter == updates_by_type.end());
Nicolas Zea 2014/01/07 23:11:40 nit: have && at end of previous line
rlarocque 2014/01/08 01:37:24 Done.
144
145 return true;
146 }
147
148 void UpdaterList::ApplyUpdatesForAllTypes(
149 sessions::StatusController* status_controller) {
150 for (UpdateHandlerMap::iterator it = update_handler_map_.begin();
151 it != update_handler_map_.end(); ++it) {
152 it->second->ApplyUpdates(status_controller);
153 }
154 }
155
156 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698