| 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_service.h" | 5 #include "components/sync/device_info/device_info_service.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "components/sync/api/entity_change.h" | 14 #include "components/sync/api/entity_change.h" |
| 15 #include "components/sync/api/metadata_batch.h" | 15 #include "components/sync/api/metadata_batch.h" |
| 16 #include "components/sync/api/sync_error.h" | 16 #include "components/sync/api/sync_error.h" |
| 17 #include "components/sync/base/time.h" | 17 #include "components/sync/base/time.h" |
| 18 #include "components/sync/core/data_batch_impl.h" | 18 #include "components/sync/core/data_batch_impl.h" |
| 19 #include "components/sync/device_info/device_info_util.h" | 19 #include "components/sync/device_info/device_info_util.h" |
| 20 #include "components/sync/protocol/data_type_state.pb.h" | 20 #include "components/sync/protocol/data_type_state.pb.h" |
| 21 #include "components/sync/protocol/sync.pb.h" | 21 #include "components/sync/protocol/sync.pb.h" |
| 22 | 22 |
| 23 namespace syncer { | 23 namespace sync_driver_v2 { |
| 24 | 24 |
| 25 using base::Time; | 25 using base::Time; |
| 26 using base::TimeDelta; | 26 using base::TimeDelta; |
| 27 using syncer::SyncError; |
| 28 using syncer_v2::DataBatchImpl; |
| 29 using syncer_v2::EntityChange; |
| 30 using syncer_v2::EntityChangeList; |
| 31 using syncer_v2::EntityData; |
| 32 using syncer_v2::EntityDataMap; |
| 33 using syncer_v2::MetadataBatch; |
| 34 using syncer_v2::MetadataChangeList; |
| 35 using syncer_v2::ModelTypeStore; |
| 36 using syncer_v2::SimpleMetadataChangeList; |
| 37 using sync_driver::DeviceInfo; |
| 38 using sync_driver::DeviceInfoUtil; |
| 27 using sync_pb::DataTypeState; | 39 using sync_pb::DataTypeState; |
| 28 using sync_pb::DeviceInfoSpecifics; | 40 using sync_pb::DeviceInfoSpecifics; |
| 29 using sync_pb::EntitySpecifics; | 41 using sync_pb::EntitySpecifics; |
| 30 | 42 |
| 31 using Record = ModelTypeStore::Record; | 43 using Record = ModelTypeStore::Record; |
| 32 using RecordList = ModelTypeStore::RecordList; | 44 using RecordList = ModelTypeStore::RecordList; |
| 33 using Result = ModelTypeStore::Result; | 45 using Result = ModelTypeStore::Result; |
| 34 using WriteBatch = ModelTypeStore::WriteBatch; | 46 using WriteBatch = ModelTypeStore::WriteBatch; |
| 35 | 47 |
| 36 DeviceInfoService::DeviceInfoService( | 48 DeviceInfoService::DeviceInfoService( |
| 37 LocalDeviceInfoProvider* local_device_info_provider, | 49 sync_driver::LocalDeviceInfoProvider* local_device_info_provider, |
| 38 const StoreFactoryFunction& callback, | 50 const StoreFactoryFunction& callback, |
| 39 const ChangeProcessorFactory& change_processor_factory) | 51 const ChangeProcessorFactory& change_processor_factory) |
| 40 : ModelTypeService(change_processor_factory, DEVICE_INFO), | 52 : ModelTypeService(change_processor_factory, syncer::DEVICE_INFO), |
| 41 local_device_info_provider_(local_device_info_provider) { | 53 local_device_info_provider_(local_device_info_provider) { |
| 42 DCHECK(local_device_info_provider); | 54 DCHECK(local_device_info_provider); |
| 43 | 55 |
| 44 // This is not threadsafe, but presuably the provider initializes on the same | 56 // This is not threadsafe, but presuably the provider initializes on the same |
| 45 // thread as us so we're okay. | 57 // thread as us so we're okay. |
| 46 if (local_device_info_provider->GetLocalDeviceInfo()) { | 58 if (local_device_info_provider->GetLocalDeviceInfo()) { |
| 47 OnProviderInitialized(); | 59 OnProviderInitialized(); |
| 48 } else { | 60 } else { |
| 49 subscription_ = | 61 subscription_ = |
| 50 local_device_info_provider->RegisterOnInitializedCallback(base::Bind( | 62 local_device_info_provider->RegisterOnInitializedCallback(base::Bind( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 kv.second.value().specifics.device_info(); | 101 kv.second.value().specifics.device_info(); |
| 90 DCHECK_EQ(kv.first, specifics.cache_guid()); | 102 DCHECK_EQ(kv.first, specifics.cache_guid()); |
| 91 if (specifics.cache_guid() == local_guid) { | 103 if (specifics.cache_guid() == local_guid) { |
| 92 // Don't Put local data if it's the same as the remote copy. | 104 // Don't Put local data if it's the same as the remote copy. |
| 93 if (local_info->Equals(*CopyToModel(specifics))) { | 105 if (local_info->Equals(*CopyToModel(specifics))) { |
| 94 local_guids_to_put.erase(local_guid); | 106 local_guids_to_put.erase(local_guid); |
| 95 } else { | 107 } else { |
| 96 // This device is valid right now and this entry is about to be | 108 // This device is valid right now and this entry is about to be |
| 97 // committed, use this as an opportunity to refresh the timestamp. | 109 // committed, use this as an opportunity to refresh the timestamp. |
| 98 all_data_[local_guid]->set_last_updated_timestamp( | 110 all_data_[local_guid]->set_last_updated_timestamp( |
| 99 TimeToProtoTime(Time::Now())); | 111 syncer::TimeToProtoTime(Time::Now())); |
| 100 } | 112 } |
| 101 } else { | 113 } else { |
| 102 // Remote data wins conflicts. | 114 // Remote data wins conflicts. |
| 103 local_guids_to_put.erase(specifics.cache_guid()); | 115 local_guids_to_put.erase(specifics.cache_guid()); |
| 104 has_changes = true; | 116 has_changes = true; |
| 105 StoreSpecifics(base::MakeUnique<DeviceInfoSpecifics>(specifics), | 117 StoreSpecifics(base::MakeUnique<DeviceInfoSpecifics>(specifics), |
| 106 batch.get()); | 118 batch.get()); |
| 107 } | 119 } |
| 108 } | 120 } |
| 109 | 121 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 } | 187 } |
| 176 | 188 |
| 177 callback.Run(SyncError(), std::move(batch)); | 189 callback.Run(SyncError(), std::move(batch)); |
| 178 } | 190 } |
| 179 | 191 |
| 180 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) { | 192 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) { |
| 181 DCHECK(entity_data.specifics.has_device_info()); | 193 DCHECK(entity_data.specifics.has_device_info()); |
| 182 return DeviceInfoUtil::SpecificsToTag(entity_data.specifics.device_info()); | 194 return DeviceInfoUtil::SpecificsToTag(entity_data.specifics.device_info()); |
| 183 } | 195 } |
| 184 | 196 |
| 185 std::string DeviceInfoService::GetStorageKey(const EntityData& entity_data) { | 197 std::string DeviceInfoService::GetStorageKey( |
| 198 const syncer_v2::EntityData& entity_data) { |
| 186 DCHECK(entity_data.specifics.has_device_info()); | 199 DCHECK(entity_data.specifics.has_device_info()); |
| 187 return entity_data.specifics.device_info().cache_guid(); | 200 return entity_data.specifics.device_info().cache_guid(); |
| 188 } | 201 } |
| 189 | 202 |
| 190 void DeviceInfoService::OnChangeProcessorSet() { | 203 void DeviceInfoService::OnChangeProcessorSet() { |
| 191 // We've recieved a new processor that needs metadata. If we're still in the | 204 // We've recieved a new processor that needs metadata. If we're still in the |
| 192 // process of loading data and/or metadata, then |has_metadata_loaded_| is | 205 // process of loading data and/or metadata, then |has_metadata_loaded_| is |
| 193 // false and we'll wait for those async reads to happen. If we've already | 206 // false and we'll wait for those async reads to happen. If we've already |
| 194 // loaded metadata and then subsequently we get a new processor, we must not | 207 // loaded metadata and then subsequently we get a new processor, we must not |
| 195 // have created the processor ourselves because we had no metadata. So there | 208 // have created the processor ourselves because we had no metadata. So there |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 void DeviceInfoService::SendLocalData() { | 466 void DeviceInfoService::SendLocalData() { |
| 454 DCHECK(has_provider_initialized_); | 467 DCHECK(has_provider_initialized_); |
| 455 // TODO(skym): Handle disconnecting and reconnecting, this will currently halt | 468 // TODO(skym): Handle disconnecting and reconnecting, this will currently halt |
| 456 // the pulse timer and never restart it. | 469 // the pulse timer and never restart it. |
| 457 if (!change_processor()) { | 470 if (!change_processor()) { |
| 458 return; | 471 return; |
| 459 } | 472 } |
| 460 | 473 |
| 461 std::unique_ptr<DeviceInfoSpecifics> specifics = | 474 std::unique_ptr<DeviceInfoSpecifics> specifics = |
| 462 CopyToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo()); | 475 CopyToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo()); |
| 463 specifics->set_last_updated_timestamp(TimeToProtoTime(Time::Now())); | 476 specifics->set_last_updated_timestamp(syncer::TimeToProtoTime(Time::Now())); |
| 464 | 477 |
| 465 std::unique_ptr<MetadataChangeList> metadata_change_list = | 478 std::unique_ptr<MetadataChangeList> metadata_change_list = |
| 466 CreateMetadataChangeList(); | 479 CreateMetadataChangeList(); |
| 467 change_processor()->Put(specifics->cache_guid(), CopyToEntityData(*specifics), | 480 change_processor()->Put(specifics->cache_guid(), CopyToEntityData(*specifics), |
| 468 metadata_change_list.get()); | 481 metadata_change_list.get()); |
| 469 | 482 |
| 470 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); | 483 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 471 StoreSpecifics(std::move(specifics), batch.get()); | 484 StoreSpecifics(std::move(specifics), batch.get()); |
| 472 | 485 |
| 473 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true); | 486 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 CreateChangeProcessor(); | 520 CreateChangeProcessor(); |
| 508 } | 521 } |
| 509 change_processor()->OnMetadataLoaded( | 522 change_processor()->OnMetadataLoaded( |
| 510 change_processor()->CreateAndUploadError(FROM_HERE, msg), nullptr); | 523 change_processor()->CreateAndUploadError(FROM_HERE, msg), nullptr); |
| 511 } | 524 } |
| 512 | 525 |
| 513 // static | 526 // static |
| 514 Time DeviceInfoService::GetLastUpdateTime( | 527 Time DeviceInfoService::GetLastUpdateTime( |
| 515 const DeviceInfoSpecifics& specifics) { | 528 const DeviceInfoSpecifics& specifics) { |
| 516 if (specifics.has_last_updated_timestamp()) { | 529 if (specifics.has_last_updated_timestamp()) { |
| 517 return ProtoTimeToTime(specifics.last_updated_timestamp()); | 530 return syncer::ProtoTimeToTime(specifics.last_updated_timestamp()); |
| 518 } else { | 531 } else { |
| 519 return Time(); | 532 return Time(); |
| 520 } | 533 } |
| 521 } | 534 } |
| 522 | 535 |
| 523 } // namespace syncer | 536 } // namespace sync_driver_v2 |
| OLD | NEW |