Split uri_beacons target from bluetooth target.
[chromium-blink-merge.git] / sync / engine / get_updates_processor.cc
bloba33c70de3c4a2967cf6848b6ae81aac7569a6578
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.
5 #include "sync/engine/get_updates_processor.h"
7 #include <map>
9 #include "base/trace_event/trace_event.h"
10 #include "sync/engine/get_updates_delegate.h"
11 #include "sync/engine/syncer_proto_util.h"
12 #include "sync/engine/update_handler.h"
13 #include "sync/internal_api/public/events/get_updates_response_event.h"
14 #include "sync/protocol/sync.pb.h"
15 #include "sync/sessions/status_controller.h"
16 #include "sync/sessions/sync_session.h"
17 #include "sync/syncable/directory.h"
18 #include "sync/syncable/nigori_handler.h"
19 #include "sync/syncable/syncable_read_transaction.h"
21 typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList;
22 typedef std::map<syncer::ModelType, SyncEntityList> TypeSyncEntityMap;
24 namespace syncer {
26 typedef std::map<ModelType, size_t> TypeToIndexMap;
28 namespace {
30 bool ShouldRequestEncryptionKey(sessions::SyncSessionContext* context) {
31 syncable::Directory* dir = context->directory();
32 syncable::ReadTransaction trans(FROM_HERE, dir);
33 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
34 return nigori_handler->NeedKeystoreKey(&trans);
38 SyncerError HandleGetEncryptionKeyResponse(
39 const sync_pb::ClientToServerResponse& update_response,
40 syncable::Directory* dir) {
41 bool success = false;
42 if (update_response.get_updates().encryption_keys_size() == 0) {
43 LOG(ERROR) << "Failed to receive encryption key from server.";
44 return SERVER_RESPONSE_VALIDATION_FAILED;
46 syncable::ReadTransaction trans(FROM_HERE, dir);
47 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
48 success = nigori_handler->SetKeystoreKeys(
49 update_response.get_updates().encryption_keys(),
50 &trans);
52 DVLOG(1) << "GetUpdates returned "
53 << update_response.get_updates().encryption_keys_size()
54 << "encryption keys. Nigori keystore key "
55 << (success ? "" : "not ") << "updated.";
56 return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED);
59 // Given a GetUpdates response, iterates over all the returned items and
60 // divides them according to their type. Outputs a map from model types to
61 // received SyncEntities. The output map will have entries (possibly empty)
62 // for all types in |requested_types|.
63 void PartitionUpdatesByType(const sync_pb::GetUpdatesResponse& gu_response,
64 ModelTypeSet requested_types,
65 TypeSyncEntityMap* updates_by_type) {
66 int update_count = gu_response.entries().size();
67 for (ModelTypeSet::Iterator it = requested_types.First();
68 it.Good(); it.Inc()) {
69 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList()));
71 for (int i = 0; i < update_count; ++i) {
72 const sync_pb::SyncEntity& update = gu_response.entries(i);
73 ModelType type = GetModelType(update);
74 if (!IsRealDataType(type)) {
75 NOTREACHED() << "Received update with invalid type.";
76 continue;
79 TypeSyncEntityMap::iterator it = updates_by_type->find(type);
80 if (it == updates_by_type->end()) {
81 DLOG(WARNING)
82 << "Received update for unexpected type or the type is throttled:"
83 << ModelTypeToString(type);
84 continue;
87 it->second.push_back(&update);
91 // Builds a map of ModelTypes to indices to progress markers in the given
92 // |gu_response| message. The map is returned in the |index_map| parameter.
93 void PartitionProgressMarkersByType(
94 const sync_pb::GetUpdatesResponse& gu_response,
95 ModelTypeSet request_types,
96 TypeToIndexMap* index_map) {
97 for (int i = 0; i < gu_response.new_progress_marker_size(); ++i) {
98 int field_number = gu_response.new_progress_marker(i).data_type_id();
99 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
100 if (!IsRealDataType(model_type)) {
101 DLOG(WARNING) << "Unknown field number " << field_number;
102 continue;
104 if (!request_types.Has(model_type)) {
105 DLOG(WARNING)
106 << "Skipping unexpected progress marker for non-enabled type "
107 << ModelTypeToString(model_type);
108 continue;
110 index_map->insert(std::make_pair(model_type, i));
114 void PartitionContextMutationsByType(
115 const sync_pb::GetUpdatesResponse& gu_response,
116 ModelTypeSet request_types,
117 TypeToIndexMap* index_map) {
118 for (int i = 0; i < gu_response.context_mutations_size(); ++i) {
119 int field_number = gu_response.context_mutations(i).data_type_id();
120 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
121 if (!IsRealDataType(model_type)) {
122 DLOG(WARNING) << "Unknown field number " << field_number;
123 continue;
125 if (!request_types.Has(model_type)) {
126 DLOG(WARNING)
127 << "Skipping unexpected context mutation for non-enabled type "
128 << ModelTypeToString(model_type);
129 continue;
131 index_map->insert(std::make_pair(model_type, i));
135 // Initializes the parts of the GetUpdatesMessage that depend on shared state,
136 // like the ShouldRequestEncryptionKey() status. This is kept separate from the
137 // other of the message-building functions to make the rest of the code easier
138 // to test.
139 void InitDownloadUpdatesContext(
140 sessions::SyncSession* session,
141 bool create_mobile_bookmarks_folder,
142 sync_pb::ClientToServerMessage* message) {
143 message->set_share(session->context()->account_name());
144 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES);
146 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates();
148 // We want folders for our associated types, always. If we were to set
149 // this to false, the server would send just the non-container items
150 // (e.g. Bookmark URLs but not their containing folders).
151 get_updates->set_fetch_folders(true);
153 get_updates->set_create_mobile_bookmarks_folder(
154 create_mobile_bookmarks_folder);
155 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
156 get_updates->set_need_encryption_key(need_encryption_key);
158 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
159 get_updates->mutable_caller_info()->set_notifications_enabled(
160 session->context()->notifications_enabled());
163 } // namespace
165 GetUpdatesProcessor::GetUpdatesProcessor(UpdateHandlerMap* update_handler_map,
166 const GetUpdatesDelegate& delegate)
167 : update_handler_map_(update_handler_map), delegate_(delegate) {}
169 GetUpdatesProcessor::~GetUpdatesProcessor() {}
171 SyncerError GetUpdatesProcessor::DownloadUpdates(
172 ModelTypeSet* request_types,
173 sessions::SyncSession* session,
174 bool create_mobile_bookmarks_folder) {
175 TRACE_EVENT0("sync", "DownloadUpdates");
177 sync_pb::ClientToServerMessage message;
178 InitDownloadUpdatesContext(session,
179 create_mobile_bookmarks_folder,
180 &message);
181 PrepareGetUpdates(*request_types, &message);
183 SyncerError result = ExecuteDownloadUpdates(request_types, session, &message);
184 session->mutable_status_controller()->set_last_download_updates_result(
185 result);
186 return result;
189 void GetUpdatesProcessor::PrepareGetUpdates(
190 ModelTypeSet gu_types,
191 sync_pb::ClientToServerMessage* message) {
192 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates();
194 for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) {
195 UpdateHandlerMap::iterator handler_it = update_handler_map_->find(it.Get());
196 DCHECK(handler_it != update_handler_map_->end())
197 << "Failed to look up handler for " << ModelTypeToString(it.Get());
198 sync_pb::DataTypeProgressMarker* progress_marker =
199 get_updates->add_from_progress_marker();
200 handler_it->second->GetDownloadProgress(progress_marker);
201 progress_marker->clear_gc_directive();
203 sync_pb::DataTypeContext context;
204 handler_it->second->GetDataTypeContext(&context);
205 if (!context.context().empty())
206 get_updates->add_client_contexts()->Swap(&context);
209 delegate_.HelpPopulateGuMessage(get_updates);
212 SyncerError GetUpdatesProcessor::ExecuteDownloadUpdates(
213 ModelTypeSet* request_types,
214 sessions::SyncSession* session,
215 sync_pb::ClientToServerMessage* msg) {
216 sync_pb::ClientToServerResponse update_response;
217 sessions::StatusController* status = session->mutable_status_controller();
218 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
220 if (session->context()->debug_info_getter()) {
221 sync_pb::DebugInfo* debug_info = msg->mutable_debug_info();
222 CopyClientDebugInfo(session->context()->debug_info_getter(), debug_info);
225 session->SendProtocolEvent(
226 *(delegate_.GetNetworkRequestEvent(base::Time::Now(), *msg)));
228 ModelTypeSet partial_failure_data_types;
230 SyncerError result = SyncerProtoUtil::PostClientToServerMessage(
231 msg, &update_response, session, &partial_failure_data_types);
233 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString(
234 update_response);
236 if (result == SERVER_RETURN_PARTIAL_FAILURE) {
237 request_types->RemoveAll(partial_failure_data_types);
238 } else if (result != SYNCER_OK) {
239 GetUpdatesResponseEvent response_event(
240 base::Time::Now(), update_response, result);
241 session->SendProtocolEvent(response_event);
243 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates";
244 return result;
247 DVLOG(1) << "GetUpdates returned "
248 << update_response.get_updates().entries_size()
249 << " updates.";
252 if (session->context()->debug_info_getter()) {
253 // Clear debug info now that we have successfully sent it to the server.
254 DVLOG(1) << "Clearing client debug info.";
255 session->context()->debug_info_getter()->ClearDebugInfo();
258 if (need_encryption_key ||
259 update_response.get_updates().encryption_keys_size() > 0) {
260 syncable::Directory* dir = session->context()->directory();
261 status->set_last_get_key_result(
262 HandleGetEncryptionKeyResponse(update_response, dir));
265 SyncerError process_result =
266 ProcessResponse(update_response.get_updates(), *request_types, status);
268 GetUpdatesResponseEvent response_event(
269 base::Time::Now(), update_response, process_result);
270 session->SendProtocolEvent(response_event);
272 DVLOG(1) << "GetUpdates result: " << process_result;
274 return process_result;
277 SyncerError GetUpdatesProcessor::ProcessResponse(
278 const sync_pb::GetUpdatesResponse& gu_response,
279 ModelTypeSet request_types,
280 sessions::StatusController* status) {
281 status->increment_num_updates_downloaded_by(gu_response.entries_size());
283 // The changes remaining field is used to prevent the client from looping. If
284 // that field is being set incorrectly, we're in big trouble.
285 if (!gu_response.has_changes_remaining()) {
286 return SERVER_RESPONSE_VALIDATION_FAILED;
289 syncer::SyncerError result =
290 ProcessGetUpdatesResponse(request_types, gu_response, status);
291 if (result != syncer::SYNCER_OK)
292 return result;
294 if (gu_response.changes_remaining() == 0) {
295 return SYNCER_OK;
296 } else {
297 return SERVER_MORE_TO_DOWNLOAD;
301 syncer::SyncerError GetUpdatesProcessor::ProcessGetUpdatesResponse(
302 ModelTypeSet gu_types,
303 const sync_pb::GetUpdatesResponse& gu_response,
304 sessions::StatusController* status_controller) {
305 TypeSyncEntityMap updates_by_type;
306 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type);
307 DCHECK_EQ(gu_types.Size(), updates_by_type.size());
309 TypeToIndexMap progress_index_by_type;
310 PartitionProgressMarkersByType(gu_response,
311 gu_types,
312 &progress_index_by_type);
313 if (gu_types.Size() != progress_index_by_type.size()) {
314 NOTREACHED() << "Missing progress markers in GetUpdates response.";
315 return syncer::SERVER_RESPONSE_VALIDATION_FAILED;
318 TypeToIndexMap context_by_type;
319 PartitionContextMutationsByType(gu_response, gu_types, &context_by_type);
321 // Iterate over these maps in parallel, processing updates for each type.
322 TypeToIndexMap::iterator progress_marker_iter =
323 progress_index_by_type.begin();
324 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin();
325 for (; (progress_marker_iter != progress_index_by_type.end()
326 && updates_iter != updates_by_type.end());
327 ++progress_marker_iter, ++updates_iter) {
328 DCHECK_EQ(progress_marker_iter->first, updates_iter->first);
329 ModelType type = progress_marker_iter->first;
331 UpdateHandlerMap::iterator update_handler_iter =
332 update_handler_map_->find(type);
334 sync_pb::DataTypeContext context;
335 TypeToIndexMap::iterator context_iter = context_by_type.find(type);
336 if (context_iter != context_by_type.end())
337 context.CopyFrom(gu_response.context_mutations(context_iter->second));
339 if (update_handler_iter != update_handler_map_->end()) {
340 syncer::SyncerError result =
341 update_handler_iter->second->ProcessGetUpdatesResponse(
342 gu_response.new_progress_marker(progress_marker_iter->second),
343 context,
344 updates_iter->second,
345 status_controller);
346 if (result != syncer::SYNCER_OK)
347 return result;
348 } else {
349 DLOG(WARNING)
350 << "Ignoring received updates of a type we can't handle. "
351 << "Type is: " << ModelTypeToString(type);
352 continue;
355 DCHECK(progress_marker_iter == progress_index_by_type.end() &&
356 updates_iter == updates_by_type.end());
358 return syncer::SYNCER_OK;
361 void GetUpdatesProcessor::ApplyUpdates(
362 ModelTypeSet gu_types,
363 sessions::StatusController* status_controller) {
364 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_);
367 void GetUpdatesProcessor::CopyClientDebugInfo(
368 sessions::DebugInfoGetter* debug_info_getter,
369 sync_pb::DebugInfo* debug_info) {
370 DVLOG(1) << "Copying client debug info to send.";
371 debug_info_getter->GetDebugInfo(debug_info);
374 } // namespace syncer