1 // Copyright 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.
5 #include "sync/engine/download.h"
9 #include "base/command_line.h"
10 #include "sync/engine/process_updates_util.h"
11 #include "sync/engine/sync_directory_update_handler.h"
12 #include "sync/engine/syncer.h"
13 #include "sync/engine/syncer_proto_util.h"
14 #include "sync/sessions/nudge_tracker.h"
15 #include "sync/syncable/directory.h"
16 #include "sync/syncable/nigori_handler.h"
17 #include "sync/syncable/syncable_read_transaction.h"
19 using sync_pb::DebugInfo;
23 using sessions::StatusController;
24 using sessions::SyncSession;
25 using sessions::SyncSessionContext;
30 typedef std::map<ModelType, size_t> TypeToIndexMap;
32 SyncerError HandleGetEncryptionKeyResponse(
33 const sync_pb::ClientToServerResponse& update_response,
34 syncable::Directory* dir) {
36 if (update_response.get_updates().encryption_keys_size() == 0) {
37 LOG(ERROR) << "Failed to receive encryption key from server.";
38 return SERVER_RESPONSE_VALIDATION_FAILED;
40 syncable::ReadTransaction trans(FROM_HERE, dir);
41 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
42 success = nigori_handler->SetKeystoreKeys(
43 update_response.get_updates().encryption_keys(),
46 DVLOG(1) << "GetUpdates returned "
47 << update_response.get_updates().encryption_keys_size()
48 << "encryption keys. Nigori keystore key "
49 << (success ? "" : "not ") << "updated.";
50 return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED);
53 sync_pb::SyncEnums::GetUpdatesOrigin ConvertConfigureSourceToOrigin(
54 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
57 case sync_pb::GetUpdatesCallerInfo::NEWLY_SUPPORTED_DATATYPE:
58 return sync_pb::SyncEnums::NEWLY_SUPPORTED_DATATYPE;
59 case sync_pb::GetUpdatesCallerInfo::MIGRATION:
60 return sync_pb::SyncEnums::MIGRATION;
61 case sync_pb::GetUpdatesCallerInfo::RECONFIGURATION:
62 return sync_pb::SyncEnums::RECONFIGURATION;
63 case sync_pb::GetUpdatesCallerInfo::NEW_CLIENT:
64 return sync_pb::SyncEnums::NEW_CLIENT;
67 return sync_pb::SyncEnums::UNKNOWN_ORIGIN;
71 bool ShouldRequestEncryptionKey(
72 SyncSessionContext* context) {
73 bool need_encryption_key = false;
74 if (context->keystore_encryption_enabled()) {
75 syncable::Directory* dir = context->directory();
76 syncable::ReadTransaction trans(FROM_HERE, dir);
77 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
78 need_encryption_key = nigori_handler->NeedKeystoreKey(&trans);
80 return need_encryption_key;
83 void AppendClientDebugInfoIfNeeded(
85 DebugInfo* debug_info) {
86 // We want to send the debug info only once per sync cycle. Check if it has
88 if (!session->status_controller().debug_info_sent()) {
89 DVLOG(1) << "Sending client debug info ...";
90 // Could be null in some unit tests.
91 if (session->context()->debug_info_getter()) {
92 session->context()->debug_info_getter()->GetAndClearDebugInfo(
95 session->mutable_status_controller()->set_debug_info_sent();
99 void InitDownloadUpdatesRequest(
100 SyncSession* session,
101 bool create_mobile_bookmarks_folder,
102 sync_pb::ClientToServerMessage* message,
103 ModelTypeSet proto_request_types) {
104 message->set_share(session->context()->account_name());
105 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES);
107 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates();
109 // We want folders for our associated types, always. If we were to set
110 // this to false, the server would send just the non-container items
111 // (e.g. Bookmark URLs but not their containing folders).
112 get_updates->set_fetch_folders(true);
114 DebugInfo* debug_info = message->mutable_debug_info();
115 AppendClientDebugInfoIfNeeded(session, debug_info);
117 get_updates->set_create_mobile_bookmarks_folder(
118 create_mobile_bookmarks_folder);
119 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
120 get_updates->set_need_encryption_key(need_encryption_key);
122 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
123 get_updates->mutable_caller_info()->set_notifications_enabled(
124 session->context()->notifications_enabled());
126 StatusController* status = session->mutable_status_controller();
127 status->set_updates_request_types(proto_request_types);
129 UpdateHandlerMap* handler_map = session->context()->update_handler_map();
131 for (ModelTypeSet::Iterator it = proto_request_types.First();
132 it.Good(); it.Inc()) {
133 UpdateHandlerMap::iterator handler_it = handler_map->find(it.Get());
134 DCHECK(handler_it != handler_map->end());
135 sync_pb::DataTypeProgressMarker* progress_marker =
136 get_updates->add_from_progress_marker();
137 handler_it->second->GetDownloadProgress(progress_marker);
141 // Builds a map of ModelTypes to indices to progress markers in the given
142 // |gu_response| message. The map is returned in the |index_map| parameter.
143 void PartitionProgressMarkersByType(
144 const sync_pb::GetUpdatesResponse& gu_response,
145 ModelTypeSet request_types,
146 TypeToIndexMap* index_map) {
147 for (int i = 0; i < gu_response.new_progress_marker_size(); ++i) {
148 int field_number = gu_response.new_progress_marker(i).data_type_id();
149 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
150 if (!IsRealDataType(model_type)) {
151 DLOG(WARNING) << "Unknown field number " << field_number;
154 if (!request_types.Has(model_type)) {
156 << "Skipping unexpected progress marker for non-enabled type "
157 << ModelTypeToString(model_type);
160 index_map->insert(std::make_pair(model_type, i));
164 // Examines the contents of the GetUpdates response message and forwards
165 // relevant data to the UpdateHandlers for processing and persisting.
166 bool ProcessUpdateResponseMessage(
167 const sync_pb::GetUpdatesResponse& gu_response,
168 ModelTypeSet proto_request_types,
169 UpdateHandlerMap* handler_map,
170 StatusController* status) {
171 TypeSyncEntityMap updates_by_type;
172 PartitionUpdatesByType(gu_response, proto_request_types, &updates_by_type);
173 DCHECK_EQ(proto_request_types.Size(), updates_by_type.size());
175 TypeToIndexMap progress_index_by_type;
176 PartitionProgressMarkersByType(gu_response,
178 &progress_index_by_type);
179 if (proto_request_types.Size() != progress_index_by_type.size()) {
180 NOTREACHED() << "Missing progress markers in GetUpdates response.";
184 // Iterate over these maps in parallel, processing updates for each type.
185 TypeToIndexMap::iterator progress_marker_iter =
186 progress_index_by_type.begin();
187 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin();
188 for ( ; (progress_marker_iter != progress_index_by_type.end()
189 && updates_iter != updates_by_type.end());
190 ++progress_marker_iter, ++updates_iter) {
191 DCHECK_EQ(progress_marker_iter->first, updates_iter->first);
192 ModelType type = progress_marker_iter->first;
194 UpdateHandlerMap::iterator update_handler_iter = handler_map->find(type);
196 if (update_handler_iter != handler_map->end()) {
197 update_handler_iter->second->ProcessGetUpdatesResponse(
198 gu_response.new_progress_marker(progress_marker_iter->second),
199 updates_iter->second,
203 << "Ignoring received updates of a type we can't handle. "
204 << "Type is: " << ModelTypeToString(type);
208 DCHECK(progress_marker_iter == progress_index_by_type.end()
209 && updates_iter == updates_by_type.end());
216 void BuildNormalDownloadUpdates(
217 SyncSession* session,
218 bool create_mobile_bookmarks_folder,
219 ModelTypeSet request_types,
220 const sessions::NudgeTracker& nudge_tracker,
221 sync_pb::ClientToServerMessage* client_to_server_message) {
222 InitDownloadUpdatesRequest(
224 create_mobile_bookmarks_folder,
225 client_to_server_message,
226 Intersection(request_types, ProtocolTypes()));
227 sync_pb::GetUpdatesMessage* get_updates =
228 client_to_server_message->mutable_get_updates();
230 // Request updates for all requested types.
231 DVLOG(1) << "Getting updates for types "
232 << ModelTypeSetToString(request_types);
233 DCHECK(!request_types.Empty());
235 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
236 get_updates->mutable_caller_info()->set_source(
237 nudge_tracker.updates_source());
239 // Set the new and improved version of source, too.
240 get_updates->set_get_updates_origin(sync_pb::SyncEnums::GU_TRIGGER);
242 // Fill in the notification hints.
243 for (int i = 0; i < get_updates->from_progress_marker_size(); ++i) {
244 sync_pb::DataTypeProgressMarker* progress_marker =
245 get_updates->mutable_from_progress_marker(i);
246 ModelType type = GetModelTypeFromSpecificsFieldNumber(
247 progress_marker->data_type_id());
249 DCHECK(!nudge_tracker.IsTypeThrottled(type))
250 << "Throttled types should have been removed from the request_types.";
252 nudge_tracker.SetLegacyNotificationHint(type, progress_marker);
253 nudge_tracker.FillProtoMessage(
255 progress_marker->mutable_get_update_triggers());
259 void BuildDownloadUpdatesForConfigure(
260 SyncSession* session,
261 bool create_mobile_bookmarks_folder,
262 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source,
263 ModelTypeSet request_types,
264 sync_pb::ClientToServerMessage* client_to_server_message) {
265 InitDownloadUpdatesRequest(
267 create_mobile_bookmarks_folder,
268 client_to_server_message,
269 Intersection(request_types, ProtocolTypes()));
270 sync_pb::GetUpdatesMessage* get_updates =
271 client_to_server_message->mutable_get_updates();
273 // Request updates for all enabled types.
274 DVLOG(1) << "Initial download for types "
275 << ModelTypeSetToString(request_types);
276 DCHECK(!request_types.Empty());
278 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
279 get_updates->mutable_caller_info()->set_source(source);
281 // Set the new and improved version of source, too.
282 sync_pb::SyncEnums::GetUpdatesOrigin origin =
283 ConvertConfigureSourceToOrigin(source);
284 get_updates->set_get_updates_origin(origin);
287 void BuildDownloadUpdatesForPoll(
288 SyncSession* session,
289 bool create_mobile_bookmarks_folder,
290 ModelTypeSet request_types,
291 sync_pb::ClientToServerMessage* client_to_server_message) {
292 InitDownloadUpdatesRequest(
294 create_mobile_bookmarks_folder,
295 client_to_server_message,
296 Intersection(request_types, ProtocolTypes()));
297 sync_pb::GetUpdatesMessage* get_updates =
298 client_to_server_message->mutable_get_updates();
300 DVLOG(1) << "Polling for types "
301 << ModelTypeSetToString(request_types);
302 DCHECK(!request_types.Empty());
304 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
305 get_updates->mutable_caller_info()->set_source(
306 sync_pb::GetUpdatesCallerInfo::PERIODIC);
308 // Set the new and improved version of source, too.
309 get_updates->set_get_updates_origin(sync_pb::SyncEnums::PERIODIC);
312 SyncerError ExecuteDownloadUpdates(
313 ModelTypeSet request_types,
314 SyncSession* session,
315 sync_pb::ClientToServerMessage* msg) {
316 sync_pb::ClientToServerResponse update_response;
317 StatusController* status = session->mutable_status_controller();
318 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
320 SyncerError result = SyncerProtoUtil::PostClientToServerMessage(
325 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString(
328 if (result != SYNCER_OK) {
329 status->mutable_updates_response()->Clear();
330 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates";
334 status->mutable_updates_response()->CopyFrom(update_response);
336 DVLOG(1) << "GetUpdates "
337 << " returned " << update_response.get_updates().entries_size()
338 << " updates and indicated "
339 << update_response.get_updates().changes_remaining()
340 << " updates left on server.";
342 if (need_encryption_key ||
343 update_response.get_updates().encryption_keys_size() > 0) {
344 syncable::Directory* dir = session->context()->directory();
345 status->set_last_get_key_result(
346 HandleGetEncryptionKeyResponse(update_response, dir));
349 const sync_pb::GetUpdatesResponse& gu_response =
350 update_response.get_updates();
351 status->increment_num_updates_downloaded_by(gu_response.entries_size());
352 DCHECK(gu_response.has_changes_remaining());
353 status->set_num_server_changes_remaining(gu_response.changes_remaining());
355 const ModelTypeSet proto_request_types =
356 Intersection(request_types, ProtocolTypes());
358 if (!ProcessUpdateResponseMessage(gu_response,
360 session->context()->update_handler_map(),
362 return SERVER_RESPONSE_VALIDATION_FAILED;
368 } // namespace syncer