OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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/get_updates_processor.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 GetUpdatesProcessor::GetUpdatesProcessor(UpdateHandlerMap* update_handler_map) | |
17 : update_handler_map_(update_handler_map) {} | |
18 | |
19 GetUpdatesProcessor::~GetUpdatesProcessor() {} | |
20 | |
21 void GetUpdatesProcessor::PrepareGetUpdates( | |
22 ModelTypeSet gu_types, | |
23 sync_pb::GetUpdatesMessage* get_updates) { | |
24 for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) { | |
25 UpdateHandlerMap::iterator handler_it = update_handler_map_->find(it.Get()); | |
26 DCHECK(handler_it != update_handler_map_->end()); | |
27 sync_pb::DataTypeProgressMarker* progress_marker = | |
28 get_updates->add_from_progress_marker(); | |
29 handler_it->second->GetDownloadProgress(progress_marker); | |
30 } | |
31 } | |
32 | |
33 namespace { | |
Nicolas Zea
2014/01/16 21:08:26
optional nit: I find it useful to have the anon na
rlarocque
2014/01/16 21:51:29
Done.
| |
34 | |
35 // Given a GetUpdates response, iterates over all the returned items and | |
36 // divides them according to their type. Outputs a map from model types to | |
37 // received SyncEntities. The output map will have entries (possibly empty) | |
38 // for all types in |requested_types|. | |
39 void PartitionUpdatesByType( | |
40 const sync_pb::GetUpdatesResponse& updates, | |
41 ModelTypeSet requested_types, | |
42 TypeSyncEntityMap* updates_by_type) { | |
43 int update_count = updates.entries().size(); | |
44 for (ModelTypeSet::Iterator it = requested_types.First(); | |
45 it.Good(); it.Inc()) { | |
46 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList())); | |
47 } | |
48 for (int i = 0; i < update_count; ++i) { | |
49 const sync_pb::SyncEntity& update = updates.entries(i); | |
50 ModelType type = GetModelType(update); | |
51 if (!IsRealDataType(type)) { | |
52 NOTREACHED() << "Received update with invalid type."; | |
53 continue; | |
54 } | |
55 | |
56 TypeSyncEntityMap::iterator it = updates_by_type->find(type); | |
57 if (it == updates_by_type->end()) { | |
58 NOTREACHED() << "Received update for unexpected type " | |
59 << ModelTypeToString(type); | |
60 continue; | |
61 } | |
62 | |
63 it->second.push_back(&update); | |
64 } | |
65 } | |
66 | |
67 // Builds a map of ModelTypes to indices to progress markers in the given | |
68 // |gu_response| message. The map is returned in the |index_map| parameter. | |
69 void PartitionProgressMarkersByType( | |
70 const sync_pb::GetUpdatesResponse& gu_response, | |
71 ModelTypeSet request_types, | |
72 TypeToIndexMap* index_map) { | |
73 for (int i = 0; i < gu_response.new_progress_marker_size(); ++i) { | |
74 int field_number = gu_response.new_progress_marker(i).data_type_id(); | |
75 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number); | |
76 if (!IsRealDataType(model_type)) { | |
77 DLOG(WARNING) << "Unknown field number " << field_number; | |
78 continue; | |
79 } | |
80 if (!request_types.Has(model_type)) { | |
81 DLOG(WARNING) | |
82 << "Skipping unexpected progress marker for non-enabled type " | |
83 << ModelTypeToString(model_type); | |
84 continue; | |
85 } | |
86 index_map->insert(std::make_pair(model_type, i)); | |
87 } | |
88 } | |
89 | |
90 } // namespace | |
91 | |
92 bool GetUpdatesProcessor::ProcessGetUpdatesResponse( | |
93 ModelTypeSet gu_types, | |
94 const sync_pb::GetUpdatesResponse& gu_response, | |
95 sessions::StatusController* status_controller) { | |
96 TypeSyncEntityMap updates_by_type; | |
97 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type); | |
98 DCHECK_EQ(gu_types.Size(), updates_by_type.size()); | |
99 | |
100 TypeToIndexMap progress_index_by_type; | |
101 PartitionProgressMarkersByType(gu_response, | |
102 gu_types, | |
103 &progress_index_by_type); | |
104 if (gu_types.Size() != progress_index_by_type.size()) { | |
105 NOTREACHED() << "Missing progress markers in GetUpdates response."; | |
106 return false; | |
107 } | |
108 | |
109 // Iterate over these maps in parallel, processing updates for each type. | |
110 TypeToIndexMap::iterator progress_marker_iter = | |
111 progress_index_by_type.begin(); | |
112 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin(); | |
113 for (; (progress_marker_iter != progress_index_by_type.end() | |
114 && updates_iter != updates_by_type.end()); | |
115 ++progress_marker_iter, ++updates_iter) { | |
116 DCHECK_EQ(progress_marker_iter->first, updates_iter->first); | |
117 ModelType type = progress_marker_iter->first; | |
118 | |
119 UpdateHandlerMap::iterator update_handler_iter = | |
120 update_handler_map_->find(type); | |
121 | |
122 if (update_handler_iter != update_handler_map_->end()) { | |
123 update_handler_iter->second->ProcessGetUpdatesResponse( | |
124 gu_response.new_progress_marker(progress_marker_iter->second), | |
125 updates_iter->second, | |
126 status_controller); | |
127 } else { | |
128 DLOG(WARNING) | |
129 << "Ignoring received updates of a type we can't handle. " | |
130 << "Type is: " << ModelTypeToString(type); | |
131 continue; | |
132 } | |
133 } | |
134 DCHECK(progress_marker_iter == progress_index_by_type.end() && | |
135 updates_iter == updates_by_type.end()); | |
136 | |
137 return true; | |
138 } | |
139 | |
140 void GetUpdatesProcessor::ApplyUpdatesForAllTypes( | |
141 sessions::StatusController* status_controller) { | |
142 for (UpdateHandlerMap::iterator it = update_handler_map_->begin(); | |
143 it != update_handler_map_->end(); ++it) { | |
144 it->second->ApplyUpdates(status_controller); | |
145 } | |
146 } | |
147 | |
148 } // namespace syncer | |
OLD | NEW |