1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/engine_impl/get_updates_processor.h"
#include <stddef.h>
#include <utility>
#include "base/trace_event/trace_event.h"
#include "components/sync/engine_impl/cycle/status_controller.h"
#include "components/sync/engine_impl/cycle/sync_cycle.h"
#include "components/sync/engine_impl/events/get_updates_response_event.h"
#include "components/sync/engine_impl/get_updates_delegate.h"
#include "components/sync/engine_impl/syncer_proto_util.h"
#include "components/sync/engine_impl/update_handler.h"
#include "components/sync/syncable/directory.h"
#include "components/sync/syncable/nigori_handler.h"
#include "components/sync/syncable/syncable_read_transaction.h"
namespace syncer {
namespace {
typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList;
typedef std::map<ModelType, SyncEntityList> TypeSyncEntityMap;
typedef std::map<ModelType, size_t> TypeToIndexMap;
bool ShouldRequestEncryptionKey(SyncCycleContext* context) {
syncable::Directory* dir = context->directory();
syncable::ReadTransaction trans(FROM_HERE, dir);
syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
return nigori_handler->NeedKeystoreKey(&trans);
}
SyncerError HandleGetEncryptionKeyResponse(
const sync_pb::ClientToServerResponse& update_response,
syncable::Directory* dir) {
bool success = false;
if (update_response.get_updates().encryption_keys_size() == 0) {
LOG(ERROR) << "Failed to receive encryption key from server.";
return SERVER_RESPONSE_VALIDATION_FAILED;
}
syncable::ReadTransaction trans(FROM_HERE, dir);
syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
success = nigori_handler->SetKeystoreKeys(
update_response.get_updates().encryption_keys(), &trans);
DVLOG(1) << "GetUpdates returned "
<< update_response.get_updates().encryption_keys_size()
<< "encryption keys. Nigori keystore key " << (success ? "" : "not ")
<< "updated.";
return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED);
}
// Given a GetUpdates response, iterates over all the returned items and
// divides them according to their type. Outputs a map from model types to
// received SyncEntities. The output map will have entries (possibly empty)
// for all types in |requested_types|.
void PartitionUpdatesByType(const sync_pb::GetUpdatesResponse& gu_response,
ModelTypeSet requested_types,
TypeSyncEntityMap* updates_by_type) {
int update_count = gu_response.entries().size();
for (ModelTypeSet::Iterator it = requested_types.First(); it.Good();
it.Inc()) {
updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList()));
}
for (int i = 0; i < update_count; ++i) {
const sync_pb::SyncEntity& update = gu_response.entries(i);
ModelType type = GetModelType(update);
if (!IsRealDataType(type)) {
NOTREACHED() << "Received update with invalid type.";
continue;
}
TypeSyncEntityMap::iterator it = updates_by_type->find(type);
if (it == updates_by_type->end()) {
DLOG(WARNING) << "Received update for unexpected type, or the type is "
"throttled or failed with partial failure:"
<< ModelTypeToString(type);
continue;
}
it->second.push_back(&update);
}
}
// Builds a map of ModelTypes to indices to progress markers in the given
// |gu_response| message. The map is returned in the |index_map| parameter.
void PartitionProgressMarkersByType(
const sync_pb::GetUpdatesResponse& gu_response,
ModelTypeSet request_types,
TypeToIndexMap* index_map) {
for (int i = 0; i < gu_response.new_progress_marker_size(); ++i) {
int field_number = gu_response.new_progress_marker(i).data_type_id();
ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
if (!IsRealDataType(model_type)) {
DLOG(WARNING) << "Unknown field number " << field_number;
continue;
}
if (!request_types.Has(model_type)) {
DLOG(WARNING)
<< "Skipping unexpected progress marker for non-enabled type "
<< ModelTypeToString(model_type);
continue;
}
index_map->insert(std::make_pair(model_type, i));
}
}
void PartitionContextMutationsByType(
const sync_pb::GetUpdatesResponse& gu_response,
ModelTypeSet request_types,
TypeToIndexMap* index_map) {
for (int i = 0; i < gu_response.context_mutations_size(); ++i) {
int field_number = gu_response.context_mutations(i).data_type_id();
ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
if (!IsRealDataType(model_type)) {
DLOG(WARNING) << "Unknown field number " << field_number;
continue;
}
if (!request_types.Has(model_type)) {
DLOG(WARNING)
<< "Skipping unexpected context mutation for non-enabled type "
<< ModelTypeToString(model_type);
continue;
}
index_map->insert(std::make_pair(model_type, i));
}
}
// Initializes the parts of the GetUpdatesMessage that depend on shared state,
// like the ShouldRequestEncryptionKey() status. This is kept separate from the
// other of the message-building functions to make the rest of the code easier
// to test.
void InitDownloadUpdatesContext(SyncCycle* cycle,
bool create_mobile_bookmarks_folder,
sync_pb::ClientToServerMessage* message) {
message->set_share(cycle->context()->account_name());
message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES);
sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates();
// We want folders for our associated types, always. If we were to set
// this to false, the server would send just the non-container items
// (e.g. Bookmark URLs but not their containing folders).
get_updates->set_fetch_folders(true);
get_updates->set_create_mobile_bookmarks_folder(
create_mobile_bookmarks_folder);
bool need_encryption_key = ShouldRequestEncryptionKey(cycle->context());
get_updates->set_need_encryption_key(need_encryption_key);
// Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
get_updates->mutable_caller_info()->set_notifications_enabled(
cycle->context()->notifications_enabled());
}
} // namespace
GetUpdatesProcessor::GetUpdatesProcessor(UpdateHandlerMap* update_handler_map,
const GetUpdatesDelegate& delegate)
: update_handler_map_(update_handler_map), delegate_(delegate) {}
GetUpdatesProcessor::~GetUpdatesProcessor() {}
SyncerError GetUpdatesProcessor::DownloadUpdates(
ModelTypeSet* request_types,
SyncCycle* cycle,
bool create_mobile_bookmarks_folder) {
TRACE_EVENT0("sync", "DownloadUpdates");
sync_pb::ClientToServerMessage message;
InitDownloadUpdatesContext(cycle, create_mobile_bookmarks_folder, &message);
PrepareGetUpdates(*request_types, &message);
SyncerError result = ExecuteDownloadUpdates(request_types, cycle, &message);
cycle->mutable_status_controller()->set_last_download_updates_result(result);
return result;
}
void GetUpdatesProcessor::PrepareGetUpdates(
ModelTypeSet gu_types,
sync_pb::ClientToServerMessage* message) {
sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates();
for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) {
UpdateHandlerMap::iterator handler_it = update_handler_map_->find(it.Get());
DCHECK(handler_it != update_handler_map_->end())
<< "Failed to look up handler for " << ModelTypeToString(it.Get());
sync_pb::DataTypeProgressMarker* progress_marker =
get_updates->add_from_progress_marker();
handler_it->second->GetDownloadProgress(progress_marker);
progress_marker->clear_gc_directive();
sync_pb::DataTypeContext context;
handler_it->second->GetDataTypeContext(&context);
if (!context.context().empty())
get_updates->add_client_contexts()->Swap(&context);
}
delegate_.HelpPopulateGuMessage(get_updates);
}
SyncerError GetUpdatesProcessor::ExecuteDownloadUpdates(
ModelTypeSet* request_types,
SyncCycle* cycle,
sync_pb::ClientToServerMessage* msg) {
sync_pb::ClientToServerResponse update_response;
StatusController* status = cycle->mutable_status_controller();
bool need_encryption_key = ShouldRequestEncryptionKey(cycle->context());
if (cycle->context()->debug_info_getter()) {
sync_pb::DebugInfo* debug_info = msg->mutable_debug_info();
CopyClientDebugInfo(cycle->context()->debug_info_getter(), debug_info);
}
cycle->SendProtocolEvent(
*(delegate_.GetNetworkRequestEvent(base::Time::Now(), *msg)));
ModelTypeSet partial_failure_data_types;
SyncerError result = SyncerProtoUtil::PostClientToServerMessage(
msg, &update_response, cycle, &partial_failure_data_types);
DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString(
update_response);
if (!partial_failure_data_types.Empty()) {
request_types->RemoveAll(partial_failure_data_types);
}
if (result != SYNCER_OK) {
GetUpdatesResponseEvent response_event(base::Time::Now(), update_response,
result);
cycle->SendProtocolEvent(response_event);
// Sync authorization expires every 60 mintues, so SYNC_AUTH_ERROR will
// appear every 60 minutes, and then sync services will refresh the
// authorization. Therefore SYNC_AUTH_ERROR is excluded here to reduce the
// ERROR messages in the log.
if (result != SYNC_AUTH_ERROR) {
LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates";
}
return result;
}
DVLOG(1) << "GetUpdates returned "
<< update_response.get_updates().entries_size() << " updates.";
if (cycle->context()->debug_info_getter()) {
// Clear debug info now that we have successfully sent it to the server.
DVLOG(1) << "Clearing client debug info.";
cycle->context()->debug_info_getter()->ClearDebugInfo();
}
if (need_encryption_key ||
update_response.get_updates().encryption_keys_size() > 0) {
syncable::Directory* dir = cycle->context()->directory();
status->set_last_get_key_result(
HandleGetEncryptionKeyResponse(update_response, dir));
}
SyncerError process_result =
ProcessResponse(update_response.get_updates(), *request_types, status);
GetUpdatesResponseEvent response_event(base::Time::Now(), update_response,
process_result);
cycle->SendProtocolEvent(response_event);
DVLOG(1) << "GetUpdates result: " << process_result;
return process_result;
}
SyncerError GetUpdatesProcessor::ProcessResponse(
const sync_pb::GetUpdatesResponse& gu_response,
ModelTypeSet request_types,
StatusController* status) {
status->increment_num_updates_downloaded_by(gu_response.entries_size());
// The changes remaining field is used to prevent the client from looping. If
// that field is being set incorrectly, we're in big trouble.
if (!gu_response.has_changes_remaining()) {
return SERVER_RESPONSE_VALIDATION_FAILED;
}
SyncerError result =
ProcessGetUpdatesResponse(request_types, gu_response, status);
if (result != SYNCER_OK)
return result;
if (gu_response.changes_remaining() == 0) {
return SYNCER_OK;
} else {
return SERVER_MORE_TO_DOWNLOAD;
}
}
SyncerError GetUpdatesProcessor::ProcessGetUpdatesResponse(
ModelTypeSet gu_types,
const sync_pb::GetUpdatesResponse& gu_response,
StatusController* status_controller) {
TypeSyncEntityMap updates_by_type;
PartitionUpdatesByType(gu_response, gu_types, &updates_by_type);
DCHECK_EQ(gu_types.Size(), updates_by_type.size());
TypeToIndexMap progress_index_by_type;
PartitionProgressMarkersByType(gu_response, gu_types,
&progress_index_by_type);
if (gu_types.Size() != progress_index_by_type.size()) {
NOTREACHED() << "Missing progress markers in GetUpdates response.";
return SERVER_RESPONSE_VALIDATION_FAILED;
}
TypeToIndexMap context_by_type;
PartitionContextMutationsByType(gu_response, gu_types, &context_by_type);
// Iterate over these maps in parallel, processing updates for each type.
TypeToIndexMap::iterator progress_marker_iter =
progress_index_by_type.begin();
TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin();
for (; (progress_marker_iter != progress_index_by_type.end() &&
updates_iter != updates_by_type.end());
++progress_marker_iter, ++updates_iter) {
DCHECK_EQ(progress_marker_iter->first, updates_iter->first);
ModelType type = progress_marker_iter->first;
UpdateHandlerMap::iterator update_handler_iter =
update_handler_map_->find(type);
sync_pb::DataTypeContext context;
TypeToIndexMap::iterator context_iter = context_by_type.find(type);
if (context_iter != context_by_type.end())
context.CopyFrom(gu_response.context_mutations(context_iter->second));
if (update_handler_iter != update_handler_map_->end()) {
SyncerError result =
update_handler_iter->second->ProcessGetUpdatesResponse(
gu_response.new_progress_marker(progress_marker_iter->second),
context, updates_iter->second, status_controller);
if (result != SYNCER_OK)
return result;
} else {
DLOG(WARNING) << "Ignoring received updates of a type we can't handle. "
<< "Type is: " << ModelTypeToString(type);
continue;
}
}
DCHECK(progress_marker_iter == progress_index_by_type.end() &&
updates_iter == updates_by_type.end());
return SYNCER_OK;
}
void GetUpdatesProcessor::ApplyUpdates(ModelTypeSet gu_types,
StatusController* status_controller) {
status_controller->set_get_updates_request_types(gu_types);
delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_);
}
void GetUpdatesProcessor::CopyClientDebugInfo(
DebugInfoGetter* debug_info_getter,
sync_pb::DebugInfo* debug_info) {
DVLOG(1) << "Copying client debug info to send.";
debug_info_getter->GetDebugInfo(debug_info);
}
} // namespace syncer
|