| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 } | 103 } |
| 104 | 104 |
| 105 callback.Run( | 105 callback.Run( |
| 106 base::Bind(&DeviceInfoSyncBridge::OnStoreCreated, base::AsWeakPtr(this))); | 106 base::Bind(&DeviceInfoSyncBridge::OnStoreCreated, base::AsWeakPtr(this))); |
| 107 } | 107 } |
| 108 | 108 |
| 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 base::MakeUnique<SimpleMetadataChangeList>(); | 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_); | 119 DCHECK(has_provider_initialized_); |
| 120 DCHECK(change_processor()->IsTrackingMetadata()); | 120 DCHECK(change_processor()->IsTrackingMetadata()); |
| 121 | 121 |
| 122 // Local data should typically be near empty, with the only possible value | 122 // Local data should typically be near empty, with the only possible value |
| 123 // corresponding to this device. This is because on signout all device info | 123 // corresponding to this device. This is because on signout all device info |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 StoreSpecifics(base::MakeUnique<DeviceInfoSpecifics>(specifics), | 155 StoreSpecifics(base::MakeUnique<DeviceInfoSpecifics>(specifics), |
| 156 batch.get()); | 156 batch.get()); |
| 157 } | 157 } |
| 158 } | 158 } |
| 159 | 159 |
| 160 for (const std::string& guid : local_guids_to_put) { | 160 for (const std::string& guid : local_guids_to_put) { |
| 161 change_processor()->Put(guid, CopyToEntityData(*all_data_[guid]), | 161 change_processor()->Put(guid, CopyToEntityData(*all_data_[guid]), |
| 162 metadata_change_list.get()); | 162 metadata_change_list.get()); |
| 163 } | 163 } |
| 164 | 164 |
| 165 CommitAndNotify(std::move(batch), std::move(metadata_change_list), | 165 batch->TransferMetadataChanges(std::move(metadata_change_list)); |
| 166 has_changes); | 166 CommitAndNotify(std::move(batch), has_changes); |
| 167 return SyncError(); | 167 return SyncError(); |
| 168 } | 168 } |
| 169 | 169 |
| 170 SyncError DeviceInfoSyncBridge::ApplySyncChanges( | 170 SyncError DeviceInfoSyncBridge::ApplySyncChanges( |
| 171 std::unique_ptr<MetadataChangeList> metadata_change_list, | 171 std::unique_ptr<MetadataChangeList> metadata_change_list, |
| 172 EntityChangeList entity_changes) { | 172 EntityChangeList entity_changes) { |
| 173 DCHECK(has_provider_initialized_); | 173 DCHECK(has_provider_initialized_); |
| 174 | 174 |
| 175 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | 175 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 176 bool has_changes = false; | 176 bool has_changes = false; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 187 } else { | 187 } else { |
| 188 const DeviceInfoSpecifics& specifics = | 188 const DeviceInfoSpecifics& specifics = |
| 189 change.data().specifics.device_info(); | 189 change.data().specifics.device_info(); |
| 190 DCHECK(guid == specifics.cache_guid()); | 190 DCHECK(guid == specifics.cache_guid()); |
| 191 StoreSpecifics(base::MakeUnique<DeviceInfoSpecifics>(specifics), | 191 StoreSpecifics(base::MakeUnique<DeviceInfoSpecifics>(specifics), |
| 192 batch.get()); | 192 batch.get()); |
| 193 has_changes = true; | 193 has_changes = true; |
| 194 } | 194 } |
| 195 } | 195 } |
| 196 | 196 |
| 197 CommitAndNotify(std::move(batch), std::move(metadata_change_list), | 197 batch->TransferMetadataChanges(std::move(metadata_change_list)); |
| 198 has_changes); | 198 CommitAndNotify(std::move(batch), has_changes); |
| 199 return SyncError(); | 199 return SyncError(); |
| 200 } | 200 } |
| 201 | 201 |
| 202 void DeviceInfoSyncBridge::GetData(StorageKeyList storage_keys, | 202 void DeviceInfoSyncBridge::GetData(StorageKeyList storage_keys, |
| 203 DataCallback callback) { | 203 DataCallback callback) { |
| 204 auto batch = base::MakeUnique<MutableDataBatch>(); | 204 auto batch = base::MakeUnique<MutableDataBatch>(); |
| 205 for (const auto& key : storage_keys) { | 205 for (const auto& key : storage_keys) { |
| 206 const auto& iter = all_data_.find(key); | 206 const auto& iter = all_data_.find(key); |
| 207 if (iter != all_data_.end()) { | 207 if (iter != all_data_.end()) { |
| 208 DCHECK_EQ(key, iter->second->cache_guid()); | 208 DCHECK_EQ(key, iter->second->cache_guid()); |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 void DeviceInfoSyncBridge::SendLocalData() { | 436 void DeviceInfoSyncBridge::SendLocalData() { |
| 437 DCHECK(has_provider_initialized_); | 437 DCHECK(has_provider_initialized_); |
| 438 | 438 |
| 439 // It is possible that the provider no longer has data for us, such as when | 439 // It is possible that the provider no longer has data for us, such as when |
| 440 // the user signs out. No-op this pulse, but keep the timer going in case sync | 440 // the user signs out. No-op this pulse, but keep the timer going in case sync |
| 441 // is enabled later. | 441 // is enabled later. |
| 442 if (local_device_info_provider_->GetLocalDeviceInfo() != nullptr) { | 442 if (local_device_info_provider_->GetLocalDeviceInfo() != nullptr) { |
| 443 std::unique_ptr<DeviceInfoSpecifics> specifics = | 443 std::unique_ptr<DeviceInfoSpecifics> specifics = |
| 444 ModelToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo(), | 444 ModelToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo(), |
| 445 TimeToProtoTime(Time::Now())); | 445 TimeToProtoTime(Time::Now())); |
| 446 std::unique_ptr<MetadataChangeList> metadata_change_list = | 446 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 447 CreateMetadataChangeList(); | 447 |
| 448 if (change_processor()->IsTrackingMetadata()) { | 448 if (change_processor()->IsTrackingMetadata()) { |
| 449 change_processor()->Put(specifics->cache_guid(), | 449 change_processor()->Put(specifics->cache_guid(), |
| 450 CopyToEntityData(*specifics), | 450 CopyToEntityData(*specifics), |
| 451 metadata_change_list.get()); | 451 batch->GetMetadataChangeList()); |
| 452 } | 452 } |
| 453 | 453 |
| 454 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | |
| 455 StoreSpecifics(std::move(specifics), batch.get()); | 454 StoreSpecifics(std::move(specifics), batch.get()); |
| 456 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true); | 455 CommitAndNotify(std::move(batch), true); |
| 457 } | 456 } |
| 458 | 457 |
| 459 pulse_timer_.Start( | 458 pulse_timer_.Start( |
| 460 FROM_HERE, DeviceInfoUtil::kPulseInterval, | 459 FROM_HERE, DeviceInfoUtil::kPulseInterval, |
| 461 base::Bind(&DeviceInfoSyncBridge::SendLocalData, base::Unretained(this))); | 460 base::Bind(&DeviceInfoSyncBridge::SendLocalData, base::Unretained(this))); |
| 462 } | 461 } |
| 463 | 462 |
| 464 void DeviceInfoSyncBridge::CommitAndNotify( | 463 void DeviceInfoSyncBridge::CommitAndNotify(std::unique_ptr<WriteBatch> batch, |
| 465 std::unique_ptr<WriteBatch> batch, | 464 bool should_notify) { |
| 466 std::unique_ptr<MetadataChangeList> metadata_change_list, | |
| 467 bool should_notify) { | |
| 468 static_cast<SimpleMetadataChangeList*>(metadata_change_list.get()) | |
| 469 ->TransferChanges(store_.get(), batch.get()); | |
| 470 store_->CommitWriteBatch( | 465 store_->CommitWriteBatch( |
| 471 std::move(batch), | 466 std::move(batch), |
| 472 base::Bind(&DeviceInfoSyncBridge::OnCommit, base::AsWeakPtr(this))); | 467 base::Bind(&DeviceInfoSyncBridge::OnCommit, base::AsWeakPtr(this))); |
| 473 if (should_notify) { | 468 if (should_notify) { |
| 474 NotifyObservers(); | 469 NotifyObservers(); |
| 475 } | 470 } |
| 476 } | 471 } |
| 477 | 472 |
| 478 int DeviceInfoSyncBridge::CountActiveDevices(const Time now) const { | 473 int DeviceInfoSyncBridge::CountActiveDevices(const Time now) const { |
| 479 return std::count_if(all_data_.begin(), all_data_.end(), | 474 return std::count_if(all_data_.begin(), all_data_.end(), |
| 480 [now](ClientIdToSpecifics::const_reference pair) { | 475 [now](ClientIdToSpecifics::const_reference pair) { |
| 481 return DeviceInfoUtil::IsActive( | 476 return DeviceInfoUtil::IsActive( |
| 482 GetLastUpdateTime(*pair.second), now); | 477 GetLastUpdateTime(*pair.second), now); |
| 483 }); | 478 }); |
| 484 } | 479 } |
| 485 | 480 |
| 486 void DeviceInfoSyncBridge::ReportStartupErrorToSync(const std::string& msg) { | 481 void DeviceInfoSyncBridge::ReportStartupErrorToSync(const std::string& msg) { |
| 487 // TODO(skym): Shouldn't need to log this here, reporting should always log. | 482 // TODO(skym): Shouldn't need to log this here, reporting should always log. |
| 488 LOG(WARNING) << msg; | 483 LOG(WARNING) << msg; |
| 489 change_processor()->OnMetadataLoaded( | 484 change_processor()->OnMetadataLoaded( |
| 490 change_processor()->CreateAndUploadError(FROM_HERE, msg), nullptr); | 485 change_processor()->CreateAndUploadError(FROM_HERE, msg), nullptr); |
| 491 } | 486 } |
| 492 | 487 |
| 493 } // namespace syncer | 488 } // namespace syncer |
| OLD | NEW |