| 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_driver/device_info_service.h" | 5 #include "components/sync_driver/device_info_service.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 } | 95 } |
| 96 | 96 |
| 97 bool has_changes = false; | 97 bool has_changes = false; |
| 98 const DeviceInfo* local_info = | 98 const DeviceInfo* local_info = |
| 99 local_device_info_provider_->GetLocalDeviceInfo(); | 99 local_device_info_provider_->GetLocalDeviceInfo(); |
| 100 std::string local_guid = local_info->guid(); | 100 std::string local_guid = local_info->guid(); |
| 101 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | 101 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 102 for (const auto& kv : entity_data_map) { | 102 for (const auto& kv : entity_data_map) { |
| 103 const DeviceInfoSpecifics& specifics = | 103 const DeviceInfoSpecifics& specifics = |
| 104 kv.second.value().specifics.device_info(); | 104 kv.second.value().specifics.device_info(); |
| 105 DCHECK_EQ(kv.first, DeviceInfoUtil::SpecificsToTag(specifics)); | 105 DCHECK_EQ(kv.first, specifics.cache_guid()); |
| 106 if (specifics.cache_guid() == local_guid) { | 106 if (specifics.cache_guid() == local_guid) { |
| 107 // Don't Put local data if it's the same as the remote copy. | 107 // Don't Put local data if it's the same as the remote copy. |
| 108 if (local_info->Equals(*CopyToModel(specifics))) { | 108 if (local_info->Equals(*CopyToModel(specifics))) { |
| 109 local_guids_to_put.erase(local_guid); | 109 local_guids_to_put.erase(local_guid); |
| 110 } else { | 110 } else { |
| 111 // This device is valid right now and this entry is about to be | 111 // This device is valid right now and this entry is about to be |
| 112 // committed, use this as an opportunity to refresh the timestamp. | 112 // committed, use this as an opportunity to refresh the timestamp. |
| 113 all_data_[local_guid]->set_last_updated_timestamp( | 113 all_data_[local_guid]->set_last_updated_timestamp( |
| 114 syncer::TimeToProtoTime(Time::Now())); | 114 syncer::TimeToProtoTime(Time::Now())); |
| 115 } | 115 } |
| 116 } else { | 116 } else { |
| 117 // Remote data wins conflicts. | 117 // Remote data wins conflicts. |
| 118 local_guids_to_put.erase(specifics.cache_guid()); | 118 local_guids_to_put.erase(specifics.cache_guid()); |
| 119 has_changes = true; | 119 has_changes = true; |
| 120 StoreSpecifics(base::WrapUnique(new DeviceInfoSpecifics(specifics)), | 120 StoreSpecifics(base::WrapUnique(new DeviceInfoSpecifics(specifics)), |
| 121 batch.get()); | 121 batch.get()); |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 | 124 |
| 125 for (const std::string& guid : local_guids_to_put) { | 125 for (const std::string& guid : local_guids_to_put) { |
| 126 change_processor()->Put(DeviceInfoUtil::SpecificsToTag(*all_data_[guid]), | 126 change_processor()->Put(guid, CopyToEntityData(*all_data_[guid]), |
| 127 CopyToEntityData(*all_data_[guid]), | |
| 128 metadata_change_list.get()); | 127 metadata_change_list.get()); |
| 129 } | 128 } |
| 130 | 129 |
| 131 CommitAndNotify(std::move(batch), std::move(metadata_change_list), | 130 CommitAndNotify(std::move(batch), std::move(metadata_change_list), |
| 132 has_changes); | 131 has_changes); |
| 133 return SyncError(); | 132 return SyncError(); |
| 134 } | 133 } |
| 135 | 134 |
| 136 SyncError DeviceInfoService::ApplySyncChanges( | 135 SyncError DeviceInfoService::ApplySyncChanges( |
| 137 std::unique_ptr<MetadataChangeList> metadata_change_list, | 136 std::unique_ptr<MetadataChangeList> metadata_change_list, |
| 138 EntityChangeList entity_changes) { | 137 EntityChangeList entity_changes) { |
| 139 DCHECK(has_provider_initialized_); | 138 DCHECK(has_provider_initialized_); |
| 140 DCHECK(has_metadata_loaded_); | 139 DCHECK(has_metadata_loaded_); |
| 141 | 140 |
| 142 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | 141 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 143 bool has_changes = false; | 142 bool has_changes = false; |
| 144 for (EntityChange& change : entity_changes) { | 143 for (EntityChange& change : entity_changes) { |
| 145 const std::string guid = | 144 const std::string guid = change.storage_key(); |
| 146 DeviceInfoUtil::TagToCacheGuid(change.client_tag()); | |
| 147 // Each device is the authoritative source for itself, ignore any remote | 145 // Each device is the authoritative source for itself, ignore any remote |
| 148 // changes that have our local cache guid. | 146 // changes that have our local cache guid. |
| 149 if (guid == local_device_info_provider_->GetLocalDeviceInfo()->guid()) { | 147 if (guid == local_device_info_provider_->GetLocalDeviceInfo()->guid()) { |
| 150 continue; | 148 continue; |
| 151 } | 149 } |
| 152 | 150 |
| 153 if (change.type() == EntityChange::ACTION_DELETE) { | 151 if (change.type() == EntityChange::ACTION_DELETE) { |
| 154 has_changes |= DeleteSpecifics(guid, batch.get()); | 152 has_changes |= DeleteSpecifics(guid, batch.get()); |
| 155 } else { | 153 } else { |
| 156 const DeviceInfoSpecifics& specifics = | 154 const DeviceInfoSpecifics& specifics = |
| 157 change.data().specifics.device_info(); | 155 change.data().specifics.device_info(); |
| 158 DCHECK(guid == specifics.cache_guid()); | 156 DCHECK(guid == specifics.cache_guid()); |
| 159 StoreSpecifics(base::WrapUnique(new DeviceInfoSpecifics(specifics)), | 157 StoreSpecifics(base::WrapUnique(new DeviceInfoSpecifics(specifics)), |
| 160 batch.get()); | 158 batch.get()); |
| 161 has_changes = true; | 159 has_changes = true; |
| 162 } | 160 } |
| 163 } | 161 } |
| 164 | 162 |
| 165 CommitAndNotify(std::move(batch), std::move(metadata_change_list), | 163 CommitAndNotify(std::move(batch), std::move(metadata_change_list), |
| 166 has_changes); | 164 has_changes); |
| 167 return SyncError(); | 165 return SyncError(); |
| 168 } | 166 } |
| 169 | 167 |
| 170 void DeviceInfoService::GetData(ClientTagList client_tags, | 168 void DeviceInfoService::GetData(StorageKeyList storage_keys, |
| 171 DataCallback callback) { | 169 DataCallback callback) { |
| 172 DCHECK(has_metadata_loaded_); | 170 DCHECK(has_metadata_loaded_); |
| 173 | 171 |
| 174 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl()); | 172 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl()); |
| 175 for (const auto& tag : client_tags) { | 173 for (const auto& key : storage_keys) { |
| 176 const auto& iter = all_data_.find(DeviceInfoUtil::TagToCacheGuid(tag)); | 174 const auto& iter = all_data_.find(key); |
| 177 if (iter != all_data_.end()) { | 175 if (iter != all_data_.end()) { |
| 178 DCHECK_EQ(tag, DeviceInfoUtil::SpecificsToTag(*iter->second)); | 176 DCHECK_EQ(key, iter->second->cache_guid()); |
| 179 batch->Put(tag, CopyToEntityData(*iter->second)); | 177 batch->Put(key, CopyToEntityData(*iter->second)); |
| 180 } | 178 } |
| 181 } | 179 } |
| 182 | 180 |
| 183 callback.Run(SyncError(), std::move(batch)); | 181 callback.Run(SyncError(), std::move(batch)); |
| 184 } | 182 } |
| 185 | 183 |
| 186 void DeviceInfoService::GetAllData(DataCallback callback) { | 184 void DeviceInfoService::GetAllData(DataCallback callback) { |
| 187 DCHECK(has_metadata_loaded_); | 185 DCHECK(has_metadata_loaded_); |
| 188 | 186 |
| 189 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl()); | 187 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl()); |
| 190 for (const auto& kv : all_data_) { | 188 for (const auto& kv : all_data_) { |
| 191 batch->Put(DeviceInfoUtil::SpecificsToTag(*kv.second), | 189 batch->Put(kv.first, CopyToEntityData(*kv.second)); |
| 192 CopyToEntityData(*kv.second)); | |
| 193 } | 190 } |
| 194 | 191 |
| 195 callback.Run(SyncError(), std::move(batch)); | 192 callback.Run(SyncError(), std::move(batch)); |
| 196 } | 193 } |
| 197 | 194 |
| 198 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) { | 195 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) { |
| 199 DCHECK(entity_data.specifics.has_device_info()); | 196 DCHECK(entity_data.specifics.has_device_info()); |
| 200 return DeviceInfoUtil::SpecificsToTag(entity_data.specifics.device_info()); | 197 return DeviceInfoUtil::SpecificsToTag(entity_data.specifics.device_info()); |
| 201 } | 198 } |
| 202 | 199 |
| 200 std::string DeviceInfoService::GetStorageKey( |
| 201 const syncer_v2::EntityData& entity_data) { |
| 202 DCHECK(entity_data.specifics.has_device_info()); |
| 203 return entity_data.specifics.device_info().cache_guid(); |
| 204 } |
| 205 |
| 203 void DeviceInfoService::OnChangeProcessorSet() { | 206 void DeviceInfoService::OnChangeProcessorSet() { |
| 204 // We've recieved a new processor that needs metadata. If we're still in the | 207 // We've recieved a new processor that needs metadata. If we're still in the |
| 205 // process of loading data and/or metadata, then |has_metadata_loaded_| is | 208 // process of loading data and/or metadata, then |has_metadata_loaded_| is |
| 206 // false and we'll wait for those async reads to happen. If we've already | 209 // false and we'll wait for those async reads to happen. If we've already |
| 207 // loaded metadata and then subsequently we get a new processor, we must not | 210 // loaded metadata and then subsequently we get a new processor, we must not |
| 208 // have created the processor ourselves because we had no metadata. So there | 211 // have created the processor ourselves because we had no metadata. So there |
| 209 // must not be any metadata on disk. | 212 // must not be any metadata on disk. |
| 210 if (has_metadata_loaded_) { | 213 if (has_metadata_loaded_) { |
| 211 change_processor()->OnMetadataLoaded(SyncError(), | 214 change_processor()->OnMetadataLoaded(SyncError(), |
| 212 base::WrapUnique(new MetadataBatch())); | 215 base::WrapUnique(new MetadataBatch())); |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 if (!change_processor()) { | 472 if (!change_processor()) { |
| 470 return; | 473 return; |
| 471 } | 474 } |
| 472 | 475 |
| 473 std::unique_ptr<DeviceInfoSpecifics> specifics = | 476 std::unique_ptr<DeviceInfoSpecifics> specifics = |
| 474 CopyToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo()); | 477 CopyToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo()); |
| 475 specifics->set_last_updated_timestamp(syncer::TimeToProtoTime(Time::Now())); | 478 specifics->set_last_updated_timestamp(syncer::TimeToProtoTime(Time::Now())); |
| 476 | 479 |
| 477 std::unique_ptr<MetadataChangeList> metadata_change_list = | 480 std::unique_ptr<MetadataChangeList> metadata_change_list = |
| 478 CreateMetadataChangeList(); | 481 CreateMetadataChangeList(); |
| 479 change_processor()->Put(DeviceInfoUtil::SpecificsToTag(*specifics), | 482 change_processor()->Put(specifics->cache_guid(), CopyToEntityData(*specifics), |
| 480 CopyToEntityData(*specifics), | |
| 481 metadata_change_list.get()); | 483 metadata_change_list.get()); |
| 482 | 484 |
| 483 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | 485 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 484 StoreSpecifics(std::move(specifics), batch.get()); | 486 StoreSpecifics(std::move(specifics), batch.get()); |
| 485 | 487 |
| 486 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true); | 488 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true); |
| 487 pulse_timer_.Start( | 489 pulse_timer_.Start( |
| 488 FROM_HERE, DeviceInfoUtil::kPulseInterval, | 490 FROM_HERE, DeviceInfoUtil::kPulseInterval, |
| 489 base::Bind(&DeviceInfoService::SendLocalData, base::Unretained(this))); | 491 base::Bind(&DeviceInfoService::SendLocalData, base::Unretained(this))); |
| 490 } | 492 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 Time DeviceInfoService::GetLastUpdateTime( | 529 Time DeviceInfoService::GetLastUpdateTime( |
| 528 const DeviceInfoSpecifics& specifics) { | 530 const DeviceInfoSpecifics& specifics) { |
| 529 if (specifics.has_last_updated_timestamp()) { | 531 if (specifics.has_last_updated_timestamp()) { |
| 530 return syncer::ProtoTimeToTime(specifics.last_updated_timestamp()); | 532 return syncer::ProtoTimeToTime(specifics.last_updated_timestamp()); |
| 531 } else { | 533 } else { |
| 532 return Time(); | 534 return Time(); |
| 533 } | 535 } |
| 534 } | 536 } |
| 535 | 537 |
| 536 } // namespace sync_driver_v2 | 538 } // namespace sync_driver_v2 |
| OLD | NEW |