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 namespace { |
| 17 |
| 18 // Given a GetUpdates response, iterates over all the returned items and |
| 19 // divides them according to their type. Outputs a map from model types to |
| 20 // received SyncEntities. The output map will have entries (possibly empty) |
| 21 // for all types in |requested_types|. |
| 22 void PartitionUpdatesByType( |
| 23 const sync_pb::GetUpdatesResponse& updates, |
| 24 ModelTypeSet requested_types, |
| 25 TypeSyncEntityMap* updates_by_type) { |
| 26 int update_count = updates.entries().size(); |
| 27 for (ModelTypeSet::Iterator it = requested_types.First(); |
| 28 it.Good(); it.Inc()) { |
| 29 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList())); |
| 30 } |
| 31 for (int i = 0; i < update_count; ++i) { |
| 32 const sync_pb::SyncEntity& update = updates.entries(i); |
| 33 ModelType type = GetModelType(update); |
| 34 if (!IsRealDataType(type)) { |
| 35 NOTREACHED() << "Received update with invalid type."; |
| 36 continue; |
| 37 } |
| 38 |
| 39 TypeSyncEntityMap::iterator it = updates_by_type->find(type); |
| 40 if (it == updates_by_type->end()) { |
| 41 NOTREACHED() << "Received update for unexpected type " |
| 42 << ModelTypeToString(type); |
| 43 continue; |
| 44 } |
| 45 |
| 46 it->second.push_back(&update); |
| 47 } |
| 48 } |
| 49 |
| 50 // Builds a map of ModelTypes to indices to progress markers in the given |
| 51 // |gu_response| message. The map is returned in the |index_map| parameter. |
| 52 void PartitionProgressMarkersByType( |
| 53 const sync_pb::GetUpdatesResponse& gu_response, |
| 54 ModelTypeSet request_types, |
| 55 TypeToIndexMap* index_map) { |
| 56 for (int i = 0; i < gu_response.new_progress_marker_size(); ++i) { |
| 57 int field_number = gu_response.new_progress_marker(i).data_type_id(); |
| 58 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number); |
| 59 if (!IsRealDataType(model_type)) { |
| 60 DLOG(WARNING) << "Unknown field number " << field_number; |
| 61 continue; |
| 62 } |
| 63 if (!request_types.Has(model_type)) { |
| 64 DLOG(WARNING) |
| 65 << "Skipping unexpected progress marker for non-enabled type " |
| 66 << ModelTypeToString(model_type); |
| 67 continue; |
| 68 } |
| 69 index_map->insert(std::make_pair(model_type, i)); |
| 70 } |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 GetUpdatesProcessor::GetUpdatesProcessor(UpdateHandlerMap* update_handler_map) |
| 76 : update_handler_map_(update_handler_map) {} |
| 77 |
| 78 GetUpdatesProcessor::~GetUpdatesProcessor() {} |
| 79 |
| 80 void GetUpdatesProcessor::PrepareGetUpdates( |
| 81 ModelTypeSet gu_types, |
| 82 sync_pb::GetUpdatesMessage* get_updates) { |
| 83 for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) { |
| 84 UpdateHandlerMap::iterator handler_it = update_handler_map_->find(it.Get()); |
| 85 DCHECK(handler_it != update_handler_map_->end()); |
| 86 sync_pb::DataTypeProgressMarker* progress_marker = |
| 87 get_updates->add_from_progress_marker(); |
| 88 handler_it->second->GetDownloadProgress(progress_marker); |
| 89 } |
| 90 } |
| 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 |