| 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 WriteBatch::CreateMetadataChangeList(); | 113 return WriteBatch::CreateMetadataChangeList(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 ModelError DeviceInfoSyncBridge::MergeSyncData( | 116 base::Optional<ModelError> 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 const DeviceInfo* local_info = | 121 const DeviceInfo* local_info = |
| 122 local_device_info_provider_->GetLocalDeviceInfo(); | 122 local_device_info_provider_->GetLocalDeviceInfo(); |
| 123 // If our dependency was yanked out from beneath us, we cannot correctly | 123 // If our dependency was yanked out from beneath us, we cannot correctly |
| 124 // handle this request, and all our data will be deleted soon. | 124 // handle this request, and all our data will be deleted soon. |
| 125 if (local_info == nullptr) { | 125 if (local_info == nullptr) { |
| 126 return ModelError(); | 126 return {}; |
| 127 } | 127 } |
| 128 | 128 |
| 129 // Local data should typically be near empty, with the only possible value | 129 // Local data should typically be near empty, with the only possible value |
| 130 // corresponding to this device. This is because on signout all device info | 130 // corresponding to this device. This is because on signout all device info |
| 131 // data is blown away. However, this simplification is being ignored here and | 131 // data is blown away. However, this simplification is being ignored here and |
| 132 // a full difference is going to be calculated to explore what other bridge | 132 // a full difference is going to be calculated to explore what other bridge |
| 133 // implementations may look like. | 133 // implementations may look like. |
| 134 std::set<std::string> local_guids_to_put; | 134 std::set<std::string> local_guids_to_put; |
| 135 for (const auto& kv : all_data_) { | 135 for (const auto& kv : all_data_) { |
| 136 local_guids_to_put.insert(kv.first); | 136 local_guids_to_put.insert(kv.first); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 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 batch->TransferMetadataChanges(std::move(metadata_change_list)); | 165 batch->TransferMetadataChanges(std::move(metadata_change_list)); |
| 166 CommitAndNotify(std::move(batch), has_changes); | 166 CommitAndNotify(std::move(batch), has_changes); |
| 167 return ModelError(); | 167 return {}; |
| 168 } | 168 } |
| 169 | 169 |
| 170 ModelError DeviceInfoSyncBridge::ApplySyncChanges( | 170 base::Optional<ModelError> 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 const DeviceInfo* local_info = | 174 const DeviceInfo* local_info = |
| 175 local_device_info_provider_->GetLocalDeviceInfo(); | 175 local_device_info_provider_->GetLocalDeviceInfo(); |
| 176 // If our dependency was yanked out from beneath us, we cannot correctly | 176 // If our dependency was yanked out from beneath us, we cannot correctly |
| 177 // handle this request, and all our data will be deleted soon. | 177 // handle this request, and all our data will be deleted soon. |
| 178 if (local_info == nullptr) { | 178 if (local_info == nullptr) { |
| 179 return ModelError(); | 179 return {}; |
| 180 } | 180 } |
| 181 | 181 |
| 182 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | 182 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 183 bool has_changes = false; | 183 bool has_changes = false; |
| 184 for (EntityChange& change : entity_changes) { | 184 for (EntityChange& change : entity_changes) { |
| 185 const std::string guid = change.storage_key(); | 185 const std::string guid = change.storage_key(); |
| 186 // Each device is the authoritative source for itself, ignore any remote | 186 // Each device is the authoritative source for itself, ignore any remote |
| 187 // changes that have our local cache guid. | 187 // changes that have our local cache guid. |
| 188 if (guid == local_info->guid()) { | 188 if (guid == local_info->guid()) { |
| 189 continue; | 189 continue; |
| 190 } | 190 } |
| 191 | 191 |
| 192 if (change.type() == EntityChange::ACTION_DELETE) { | 192 if (change.type() == EntityChange::ACTION_DELETE) { |
| 193 has_changes |= DeleteSpecifics(guid, batch.get()); | 193 has_changes |= DeleteSpecifics(guid, batch.get()); |
| 194 } else { | 194 } else { |
| 195 const DeviceInfoSpecifics& specifics = | 195 const DeviceInfoSpecifics& specifics = |
| 196 change.data().specifics.device_info(); | 196 change.data().specifics.device_info(); |
| 197 DCHECK(guid == specifics.cache_guid()); | 197 DCHECK(guid == specifics.cache_guid()); |
| 198 StoreSpecifics(base::MakeUnique<DeviceInfoSpecifics>(specifics), | 198 StoreSpecifics(base::MakeUnique<DeviceInfoSpecifics>(specifics), |
| 199 batch.get()); | 199 batch.get()); |
| 200 has_changes = true; | 200 has_changes = true; |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 | 203 |
| 204 batch->TransferMetadataChanges(std::move(metadata_change_list)); | 204 batch->TransferMetadataChanges(std::move(metadata_change_list)); |
| 205 CommitAndNotify(std::move(batch), has_changes); | 205 CommitAndNotify(std::move(batch), has_changes); |
| 206 return ModelError(); | 206 return {}; |
| 207 } | 207 } |
| 208 | 208 |
| 209 void DeviceInfoSyncBridge::GetData(StorageKeyList storage_keys, | 209 void DeviceInfoSyncBridge::GetData(StorageKeyList storage_keys, |
| 210 DataCallback callback) { | 210 DataCallback callback) { |
| 211 auto batch = base::MakeUnique<MutableDataBatch>(); | 211 auto batch = base::MakeUnique<MutableDataBatch>(); |
| 212 for (const auto& key : storage_keys) { | 212 for (const auto& key : storage_keys) { |
| 213 const auto& iter = all_data_.find(key); | 213 const auto& iter = all_data_.find(key); |
| 214 if (iter != all_data_.end()) { | 214 if (iter != all_data_.end()) { |
| 215 DCHECK_EQ(key, iter->second->cache_guid()); | 215 DCHECK_EQ(key, iter->second->cache_guid()); |
| 216 batch->Put(key, CopyToEntityData(*iter->second)); | 216 batch->Put(key, CopyToEntityData(*iter->second)); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 } | 373 } |
| 374 | 374 |
| 375 void DeviceInfoSyncBridge::LoadMetadataIfReady() { | 375 void DeviceInfoSyncBridge::LoadMetadataIfReady() { |
| 376 if (has_data_loaded_ && has_provider_initialized_) { | 376 if (has_data_loaded_ && has_provider_initialized_) { |
| 377 store_->ReadAllMetadata(base::Bind(&DeviceInfoSyncBridge::OnReadAllMetadata, | 377 store_->ReadAllMetadata(base::Bind(&DeviceInfoSyncBridge::OnReadAllMetadata, |
| 378 base::AsWeakPtr(this))); | 378 base::AsWeakPtr(this))); |
| 379 } | 379 } |
| 380 } | 380 } |
| 381 | 381 |
| 382 void DeviceInfoSyncBridge::OnReadAllMetadata( | 382 void DeviceInfoSyncBridge::OnReadAllMetadata( |
| 383 ModelError error, | 383 base::Optional<ModelError> error, |
| 384 std::unique_ptr<MetadataBatch> metadata_batch) { | 384 std::unique_ptr<MetadataBatch> metadata_batch) { |
| 385 if (error.IsSet()) { | 385 if (error) { |
| 386 change_processor()->ReportError(error); | 386 change_processor()->ReportError(error.value()); |
| 387 return; | 387 return; |
| 388 } | 388 } |
| 389 | 389 |
| 390 change_processor()->OnMetadataLoaded(std::move(metadata_batch)); | 390 change_processor()->OnMetadataLoaded(std::move(metadata_batch)); |
| 391 ReconcileLocalAndStored(); | 391 ReconcileLocalAndStored(); |
| 392 } | 392 } |
| 393 | 393 |
| 394 void DeviceInfoSyncBridge::OnCommit(Result result) { | 394 void DeviceInfoSyncBridge::OnCommit(Result result) { |
| 395 if (result != Result::SUCCESS) { | 395 if (result != Result::SUCCESS) { |
| 396 change_processor()->ReportError(FROM_HERE, "Failed a write to store."); | 396 change_processor()->ReportError(FROM_HERE, "Failed a write to store."); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 | 473 |
| 474 int DeviceInfoSyncBridge::CountActiveDevices(const Time now) const { | 474 int DeviceInfoSyncBridge::CountActiveDevices(const Time now) const { |
| 475 return std::count_if(all_data_.begin(), all_data_.end(), | 475 return std::count_if(all_data_.begin(), all_data_.end(), |
| 476 [now](ClientIdToSpecifics::const_reference pair) { | 476 [now](ClientIdToSpecifics::const_reference pair) { |
| 477 return DeviceInfoUtil::IsActive( | 477 return DeviceInfoUtil::IsActive( |
| 478 GetLastUpdateTime(*pair.second), now); | 478 GetLastUpdateTime(*pair.second), now); |
| 479 }); | 479 }); |
| 480 } | 480 } |
| 481 | 481 |
| 482 } // namespace syncer | 482 } // namespace syncer |
| OLD | NEW |