| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/sync/device_info/device_info_sync_bridge.h" | 5 #include "components/sync/device_info/device_info_sync_bridge.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 DeviceInfoSyncBridge::~DeviceInfoSyncBridge() {} | 109 DeviceInfoSyncBridge::~DeviceInfoSyncBridge() {} |
| 110 | 110 |
| 111 std::unique_ptr<MetadataChangeList> | 111 std::unique_ptr<MetadataChangeList> |
| 112 DeviceInfoSyncBridge::CreateMetadataChangeList() { | 112 DeviceInfoSyncBridge::CreateMetadataChangeList() { |
| 113 return WriteBatch::CreateMetadataChangeList(); | 113 return WriteBatch::CreateMetadataChangeList(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 SyncError DeviceInfoSyncBridge::MergeSyncData( | 116 SyncError DeviceInfoSyncBridge::MergeSyncData( |
| 117 std::unique_ptr<MetadataChangeList> metadata_change_list, | 117 std::unique_ptr<MetadataChangeList> metadata_change_list, |
| 118 EntityDataMap entity_data_map) { | 118 EntityDataMap entity_data_map) { |
| 119 DCHECK(has_provider_initialized_); | |
| 120 DCHECK(change_processor()->IsTrackingMetadata()); | 119 DCHECK(change_processor()->IsTrackingMetadata()); |
| 121 const DeviceInfo* local_info = | 120 const DeviceInfo* local_info = |
| 122 local_device_info_provider_->GetLocalDeviceInfo(); | 121 local_device_info_provider_->GetLocalDeviceInfo(); |
| 123 // If our dependency was yanked out from beneath us, we cannot correctly | 122 // If our dependency was yanked out from beneath us, we cannot correctly |
| 124 // handle this request, and all our data will be deleted soon. | 123 // handle this request, and all our data will be deleted soon. |
| 125 if (local_info == nullptr) { | 124 if (local_info == nullptr) { |
| 126 return SyncError(); | 125 return SyncError(); |
| 127 } | 126 } |
| 128 | 127 |
| 129 // Local data should typically be near empty, with the only possible value | 128 // Local data should typically be near empty, with the only possible value |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } | 162 } |
| 164 | 163 |
| 165 batch->TransferMetadataChanges(std::move(metadata_change_list)); | 164 batch->TransferMetadataChanges(std::move(metadata_change_list)); |
| 166 CommitAndNotify(std::move(batch), has_changes); | 165 CommitAndNotify(std::move(batch), has_changes); |
| 167 return SyncError(); | 166 return SyncError(); |
| 168 } | 167 } |
| 169 | 168 |
| 170 SyncError DeviceInfoSyncBridge::ApplySyncChanges( | 169 SyncError DeviceInfoSyncBridge::ApplySyncChanges( |
| 171 std::unique_ptr<MetadataChangeList> metadata_change_list, | 170 std::unique_ptr<MetadataChangeList> metadata_change_list, |
| 172 EntityChangeList entity_changes) { | 171 EntityChangeList entity_changes) { |
| 173 DCHECK(has_provider_initialized_); | |
| 174 const DeviceInfo* local_info = | 172 const DeviceInfo* local_info = |
| 175 local_device_info_provider_->GetLocalDeviceInfo(); | 173 local_device_info_provider_->GetLocalDeviceInfo(); |
| 176 // If our dependency was yanked out from beneath us, we cannot correctly | 174 // If our dependency was yanked out from beneath us, we cannot correctly |
| 177 // handle this request, and all our data will be deleted soon. | 175 // handle this request, and all our data will be deleted soon. |
| 178 if (local_info == nullptr) { | 176 if (local_info == nullptr) { |
| 179 return SyncError(); | 177 return SyncError(); |
| 180 } | 178 } |
| 181 | 179 |
| 182 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | 180 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 183 bool has_changes = false; | 181 bool has_changes = false; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 if (iter != all_data_.end()) { | 314 if (iter != all_data_.end()) { |
| 317 batch->DeleteData(guid); | 315 batch->DeleteData(guid); |
| 318 all_data_.erase(iter); | 316 all_data_.erase(iter); |
| 319 return true; | 317 return true; |
| 320 } else { | 318 } else { |
| 321 return false; | 319 return false; |
| 322 } | 320 } |
| 323 } | 321 } |
| 324 | 322 |
| 325 void DeviceInfoSyncBridge::OnProviderInitialized() { | 323 void DeviceInfoSyncBridge::OnProviderInitialized() { |
| 324 DCHECK(local_device_info_provider_->GetLocalDeviceInfo() != nullptr); |
| 326 // Now that the provider has initialized, remove the subscription. The bridge | 325 // Now that the provider has initialized, remove the subscription. The bridge |
| 327 // should only need to give the processor metadata upon initialization. If | 326 // should only need to give the processor metadata upon initialization. If |
| 328 // sync is disabled and enabled, our provider will try to retrigger this | 327 // sync is disabled and enabled, our provider will try to retrigger this |
| 329 // event, but we do not want to send any more metadata to the processor. | 328 // event, but we do not want to send any more metadata to the processor. |
| 330 // TODO(skym, crbug.com/672600): Handle re-initialization and start the pulse | 329 // TODO(skym, crbug.com/672600): Handle re-initialization and start the pulse |
| 331 // timer. | 330 // timer. |
| 332 subscription_.reset(); | 331 subscription_.reset(); |
| 333 | |
| 334 has_provider_initialized_ = true; | |
| 335 LoadMetadataIfReady(); | 332 LoadMetadataIfReady(); |
| 336 } | 333 } |
| 337 | 334 |
| 338 void DeviceInfoSyncBridge::OnStoreCreated( | 335 void DeviceInfoSyncBridge::OnStoreCreated( |
| 339 Result result, | 336 Result result, |
| 340 std::unique_ptr<ModelTypeStore> store) { | 337 std::unique_ptr<ModelTypeStore> store) { |
| 341 if (result == Result::SUCCESS) { | 338 if (result == Result::SUCCESS) { |
| 342 std::swap(store_, store); | 339 std::swap(store_, store); |
| 343 store_->ReadAllData(base::Bind(&DeviceInfoSyncBridge::OnReadAllData, | 340 store_->ReadAllData(base::Bind(&DeviceInfoSyncBridge::OnReadAllData, |
| 344 base::AsWeakPtr(this))); | 341 base::AsWeakPtr(this))); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 363 } else { | 360 } else { |
| 364 ReportStartupErrorToSync("Failed to deserialize specifics."); | 361 ReportStartupErrorToSync("Failed to deserialize specifics."); |
| 365 } | 362 } |
| 366 } | 363 } |
| 367 | 364 |
| 368 has_data_loaded_ = true; | 365 has_data_loaded_ = true; |
| 369 LoadMetadataIfReady(); | 366 LoadMetadataIfReady(); |
| 370 } | 367 } |
| 371 | 368 |
| 372 void DeviceInfoSyncBridge::LoadMetadataIfReady() { | 369 void DeviceInfoSyncBridge::LoadMetadataIfReady() { |
| 373 if (has_data_loaded_ && has_provider_initialized_) { | 370 if (has_data_loaded_ && |
| 371 local_device_info_provider_->GetLocalDeviceInfo() != nullptr) { |
| 374 store_->ReadAllMetadata(base::Bind(&DeviceInfoSyncBridge::OnReadAllMetadata, | 372 store_->ReadAllMetadata(base::Bind(&DeviceInfoSyncBridge::OnReadAllMetadata, |
| 375 base::AsWeakPtr(this))); | 373 base::AsWeakPtr(this))); |
| 376 } | 374 } |
| 377 } | 375 } |
| 378 | 376 |
| 379 void DeviceInfoSyncBridge::OnReadAllMetadata( | 377 void DeviceInfoSyncBridge::OnReadAllMetadata( |
| 380 SyncError error, | 378 SyncError error, |
| 381 std::unique_ptr<MetadataBatch> metadata_batch) { | 379 std::unique_ptr<MetadataBatch> metadata_batch) { |
| 382 change_processor()->OnMetadataLoaded(error, std::move(metadata_batch)); | 380 change_processor()->OnMetadataLoaded(error, std::move(metadata_batch)); |
| 383 ReconcileLocalAndStored(); | 381 ReconcileLocalAndStored(); |
| 384 } | 382 } |
| 385 | 383 |
| 386 void DeviceInfoSyncBridge::OnCommit(Result result) { | 384 void DeviceInfoSyncBridge::OnCommit(Result result) { |
| 387 if (result != Result::SUCCESS) { | 385 if (result != Result::SUCCESS) { |
| 388 change_processor()->CreateAndUploadError(FROM_HERE, | 386 change_processor()->CreateAndUploadError(FROM_HERE, |
| 389 "Failed a write to store."); | 387 "Failed a write to store."); |
| 390 } | 388 } |
| 391 } | 389 } |
| 392 | 390 |
| 393 void DeviceInfoSyncBridge::ReconcileLocalAndStored() { | 391 void DeviceInfoSyncBridge::ReconcileLocalAndStored() { |
| 394 // On initial syncing we will have a change processor here, but it will not be | |
| 395 // tracking changes. We need to persist a copy of our local device info to | |
| 396 // disk, but the Put call to the processor will be ignored. That should be | |
| 397 // fine however, as the discrepancy will be picked up later in merge. We don't | |
| 398 // bother trying to track this case and act intelligently because simply not | |
| 399 // much of a benefit in doing so. | |
| 400 DCHECK(has_provider_initialized_); | |
| 401 | |
| 402 const DeviceInfo* current_info = | 392 const DeviceInfo* current_info = |
| 403 local_device_info_provider_->GetLocalDeviceInfo(); | 393 local_device_info_provider_->GetLocalDeviceInfo(); |
| 404 // Must ensure |pulse_timer_| is started even if sync is in the process of | 394 // Must ensure |pulse_timer_| is started even if sync is in the process of |
| 405 // being disabled. TODO(skym, crbug.com/672600): Remove this timer Start(), as | 395 // being disabled. TODO(skym, crbug.com/672600): Remove this timer Start(), as |
| 406 // it should be started when the provider re-initializes instead. | 396 // it should be started when the provider re-initializes instead. |
| 407 if (current_info == nullptr) { | 397 if (current_info == nullptr) { |
| 408 pulse_timer_.Start(FROM_HERE, DeviceInfoUtil::kPulseInterval, | 398 pulse_timer_.Start(FROM_HERE, DeviceInfoUtil::kPulseInterval, |
| 409 base::Bind(&DeviceInfoSyncBridge::SendLocalData, | 399 base::Bind(&DeviceInfoSyncBridge::SendLocalData, |
| 410 base::Unretained(this))); | 400 base::Unretained(this))); |
| 411 return; | 401 return; |
| 412 } | 402 } |
| 413 auto iter = all_data_.find(current_info->guid()); | 403 auto iter = all_data_.find(current_info->guid()); |
| 414 | 404 |
| 415 // Convert to DeviceInfo for Equals function. | 405 // Convert to DeviceInfo for Equals function. |
| 416 if (iter != all_data_.end() && | 406 if (iter != all_data_.end() && |
| 417 current_info->Equals(*SpecificsToModel(*iter->second))) { | 407 current_info->Equals(*SpecificsToModel(*iter->second))) { |
| 418 const TimeDelta pulse_delay(DeviceInfoUtil::CalculatePulseDelay( | 408 const TimeDelta pulse_delay(DeviceInfoUtil::CalculatePulseDelay( |
| 419 GetLastUpdateTime(*iter->second), Time::Now())); | 409 GetLastUpdateTime(*iter->second), Time::Now())); |
| 420 if (!pulse_delay.is_zero()) { | 410 if (!pulse_delay.is_zero()) { |
| 421 pulse_timer_.Start(FROM_HERE, pulse_delay, | 411 pulse_timer_.Start(FROM_HERE, pulse_delay, |
| 422 base::Bind(&DeviceInfoSyncBridge::SendLocalData, | 412 base::Bind(&DeviceInfoSyncBridge::SendLocalData, |
| 423 base::Unretained(this))); | 413 base::Unretained(this))); |
| 424 return; | 414 return; |
| 425 } | 415 } |
| 426 } | 416 } |
| 427 SendLocalData(); | 417 SendLocalData(); |
| 428 } | 418 } |
| 429 | 419 |
| 430 void DeviceInfoSyncBridge::SendLocalData() { | 420 void DeviceInfoSyncBridge::SendLocalData() { |
| 431 DCHECK(has_provider_initialized_); | |
| 432 | |
| 433 // It is possible that the provider no longer has data for us, such as when | 421 // It is possible that the provider no longer has data for us, such as when |
| 434 // the user signs out. No-op this pulse, but keep the timer going in case sync | 422 // the user signs out. No-op this pulse, but keep the timer going in case sync |
| 435 // is enabled later. | 423 // is enabled later. |
| 436 if (local_device_info_provider_->GetLocalDeviceInfo() != nullptr) { | 424 if (local_device_info_provider_->GetLocalDeviceInfo() != nullptr) { |
| 437 std::unique_ptr<DeviceInfoSpecifics> specifics = | 425 std::unique_ptr<DeviceInfoSpecifics> specifics = |
| 438 ModelToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo(), | 426 ModelToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo(), |
| 439 TimeToProtoTime(Time::Now())); | 427 TimeToProtoTime(Time::Now())); |
| 440 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | 428 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 441 | 429 |
| 442 if (change_processor()->IsTrackingMetadata()) { | 430 if (change_processor()->IsTrackingMetadata()) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 473 } | 461 } |
| 474 | 462 |
| 475 void DeviceInfoSyncBridge::ReportStartupErrorToSync(const std::string& msg) { | 463 void DeviceInfoSyncBridge::ReportStartupErrorToSync(const std::string& msg) { |
| 476 // TODO(skym): Shouldn't need to log this here, reporting should always log. | 464 // TODO(skym): Shouldn't need to log this here, reporting should always log. |
| 477 LOG(WARNING) << msg; | 465 LOG(WARNING) << msg; |
| 478 change_processor()->OnMetadataLoaded( | 466 change_processor()->OnMetadataLoaded( |
| 479 change_processor()->CreateAndUploadError(FROM_HERE, msg), nullptr); | 467 change_processor()->CreateAndUploadError(FROM_HERE, msg), nullptr); |
| 480 } | 468 } |
| 481 | 469 |
| 482 } // namespace syncer | 470 } // namespace syncer |
| OLD | NEW |