| 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 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 using Record = ModelTypeStore::Record; | 45 using Record = ModelTypeStore::Record; |
| 46 using RecordList = ModelTypeStore::RecordList; | 46 using RecordList = ModelTypeStore::RecordList; |
| 47 using Result = ModelTypeStore::Result; | 47 using Result = ModelTypeStore::Result; |
| 48 using WriteBatch = ModelTypeStore::WriteBatch; | 48 using WriteBatch = ModelTypeStore::WriteBatch; |
| 49 | 49 |
| 50 DeviceInfoService::DeviceInfoService( | 50 DeviceInfoService::DeviceInfoService( |
| 51 sync_driver::LocalDeviceInfoProvider* local_device_info_provider, | 51 sync_driver::LocalDeviceInfoProvider* local_device_info_provider, |
| 52 const StoreFactoryFunction& callback, | 52 const StoreFactoryFunction& callback, |
| 53 const ChangeProcessorFactory& change_processor_factory) | 53 const ChangeProcessorFactory& change_processor_factory) |
| 54 : ModelTypeService(change_processor_factory, syncer::DEVICE_INFO), | 54 : ModelTypeService(change_processor_factory, syncer::DEVICE_INFO), |
| 55 local_device_info_provider_(local_device_info_provider), | 55 local_device_info_provider_(local_device_info_provider) { |
| 56 weak_factory_(this) { | |
| 57 DCHECK(local_device_info_provider); | 56 DCHECK(local_device_info_provider); |
| 58 | 57 |
| 59 // This is not threadsafe, but presuably the provider initializes on the same | 58 // This is not threadsafe, but presuably the provider initializes on the same |
| 60 // thread as us so we're okay. | 59 // thread as us so we're okay. |
| 61 if (local_device_info_provider->GetLocalDeviceInfo()) { | 60 if (local_device_info_provider->GetLocalDeviceInfo()) { |
| 62 OnProviderInitialized(); | 61 OnProviderInitialized(); |
| 63 } else { | 62 } else { |
| 64 subscription_ = | 63 subscription_ = |
| 65 local_device_info_provider->RegisterOnInitializedCallback(base::Bind( | 64 local_device_info_provider->RegisterOnInitializedCallback(base::Bind( |
| 66 &DeviceInfoService::OnProviderInitialized, base::Unretained(this))); | 65 &DeviceInfoService::OnProviderInitialized, base::Unretained(this))); |
| 67 } | 66 } |
| 68 | 67 |
| 69 callback.Run(base::Bind(&DeviceInfoService::OnStoreCreated, | 68 callback.Run( |
| 70 weak_factory_.GetWeakPtr())); | 69 base::Bind(&DeviceInfoService::OnStoreCreated, base::AsWeakPtr(this))); |
| 71 } | 70 } |
| 72 | 71 |
| 73 DeviceInfoService::~DeviceInfoService() {} | 72 DeviceInfoService::~DeviceInfoService() {} |
| 74 | 73 |
| 75 std::unique_ptr<MetadataChangeList> | 74 std::unique_ptr<MetadataChangeList> |
| 76 DeviceInfoService::CreateMetadataChangeList() { | 75 DeviceInfoService::CreateMetadataChangeList() { |
| 77 return base::WrapUnique(new SimpleMetadataChangeList()); | 76 return base::WrapUnique(new SimpleMetadataChangeList()); |
| 78 } | 77 } |
| 79 | 78 |
| 80 SyncError DeviceInfoService::MergeSyncData( | 79 SyncError DeviceInfoService::MergeSyncData( |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 | 315 |
| 317 void DeviceInfoService::OnProviderInitialized() { | 316 void DeviceInfoService::OnProviderInitialized() { |
| 318 has_provider_initialized_ = true; | 317 has_provider_initialized_ = true; |
| 319 LoadMetadataIfReady(); | 318 LoadMetadataIfReady(); |
| 320 } | 319 } |
| 321 | 320 |
| 322 void DeviceInfoService::OnStoreCreated(Result result, | 321 void DeviceInfoService::OnStoreCreated(Result result, |
| 323 std::unique_ptr<ModelTypeStore> store) { | 322 std::unique_ptr<ModelTypeStore> store) { |
| 324 if (result == Result::SUCCESS) { | 323 if (result == Result::SUCCESS) { |
| 325 std::swap(store_, store); | 324 std::swap(store_, store); |
| 326 store_->ReadAllData(base::Bind(&DeviceInfoService::OnReadAllData, | 325 store_->ReadAllData( |
| 327 weak_factory_.GetWeakPtr())); | 326 base::Bind(&DeviceInfoService::OnReadAllData, base::AsWeakPtr(this))); |
| 328 } else { | 327 } else { |
| 329 ReportStartupErrorToSync("ModelTypeStore creation failed."); | 328 ReportStartupErrorToSync("ModelTypeStore creation failed."); |
| 330 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization | 329 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization |
| 331 // failure. | 330 // failure. |
| 332 } | 331 } |
| 333 } | 332 } |
| 334 | 333 |
| 335 void DeviceInfoService::OnReadAllData(Result result, | 334 void DeviceInfoService::OnReadAllData(Result result, |
| 336 std::unique_ptr<RecordList> record_list) { | 335 std::unique_ptr<RecordList> record_list) { |
| 337 if (result != Result::SUCCESS) { | 336 if (result != Result::SUCCESS) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 353 } | 352 } |
| 354 } | 353 } |
| 355 | 354 |
| 356 has_data_loaded_ = true; | 355 has_data_loaded_ = true; |
| 357 LoadMetadataIfReady(); | 356 LoadMetadataIfReady(); |
| 358 } | 357 } |
| 359 | 358 |
| 360 void DeviceInfoService::LoadMetadataIfReady() { | 359 void DeviceInfoService::LoadMetadataIfReady() { |
| 361 if (has_data_loaded_ && has_provider_initialized_) { | 360 if (has_data_loaded_ && has_provider_initialized_) { |
| 362 store_->ReadAllMetadata(base::Bind(&DeviceInfoService::OnReadAllMetadata, | 361 store_->ReadAllMetadata(base::Bind(&DeviceInfoService::OnReadAllMetadata, |
| 363 weak_factory_.GetWeakPtr())); | 362 base::AsWeakPtr(this))); |
| 364 } | 363 } |
| 365 } | 364 } |
| 366 | 365 |
| 367 void DeviceInfoService::OnReadAllMetadata( | 366 void DeviceInfoService::OnReadAllMetadata( |
| 368 Result result, | 367 Result result, |
| 369 std::unique_ptr<RecordList> metadata_records, | 368 std::unique_ptr<RecordList> metadata_records, |
| 370 const std::string& global_metadata) { | 369 const std::string& global_metadata) { |
| 371 DCHECK(!has_metadata_loaded_); | 370 DCHECK(!has_metadata_loaded_); |
| 372 | 371 |
| 373 if (result != Result::SUCCESS) { | 372 if (result != Result::SUCCESS) { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 } | 491 } |
| 493 | 492 |
| 494 void DeviceInfoService::CommitAndNotify( | 493 void DeviceInfoService::CommitAndNotify( |
| 495 std::unique_ptr<WriteBatch> batch, | 494 std::unique_ptr<WriteBatch> batch, |
| 496 std::unique_ptr<MetadataChangeList> metadata_change_list, | 495 std::unique_ptr<MetadataChangeList> metadata_change_list, |
| 497 bool should_notify) { | 496 bool should_notify) { |
| 498 static_cast<SimpleMetadataChangeList*>(metadata_change_list.get()) | 497 static_cast<SimpleMetadataChangeList*>(metadata_change_list.get()) |
| 499 ->TransferChanges(store_.get(), batch.get()); | 498 ->TransferChanges(store_.get(), batch.get()); |
| 500 store_->CommitWriteBatch( | 499 store_->CommitWriteBatch( |
| 501 std::move(batch), | 500 std::move(batch), |
| 502 base::Bind(&DeviceInfoService::OnCommit, weak_factory_.GetWeakPtr())); | 501 base::Bind(&DeviceInfoService::OnCommit, base::AsWeakPtr(this))); |
| 503 if (should_notify) { | 502 if (should_notify) { |
| 504 NotifyObservers(); | 503 NotifyObservers(); |
| 505 } | 504 } |
| 506 } | 505 } |
| 507 | 506 |
| 508 int DeviceInfoService::CountActiveDevices(const Time now) const { | 507 int DeviceInfoService::CountActiveDevices(const Time now) const { |
| 509 return std::count_if(all_data_.begin(), all_data_.end(), | 508 return std::count_if(all_data_.begin(), all_data_.end(), |
| 510 [now](ClientIdToSpecifics::const_reference pair) { | 509 [now](ClientIdToSpecifics::const_reference pair) { |
| 511 return DeviceInfoUtil::IsActive( | 510 return DeviceInfoUtil::IsActive( |
| 512 GetLastUpdateTime(*pair.second), now); | 511 GetLastUpdateTime(*pair.second), now); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 529 Time DeviceInfoService::GetLastUpdateTime( | 528 Time DeviceInfoService::GetLastUpdateTime( |
| 530 const DeviceInfoSpecifics& specifics) { | 529 const DeviceInfoSpecifics& specifics) { |
| 531 if (specifics.has_last_updated_timestamp()) { | 530 if (specifics.has_last_updated_timestamp()) { |
| 532 return syncer::ProtoTimeToTime(specifics.last_updated_timestamp()); | 531 return syncer::ProtoTimeToTime(specifics.last_updated_timestamp()); |
| 533 } else { | 532 } else { |
| 534 return Time(); | 533 return Time(); |
| 535 } | 534 } |
| 536 } | 535 } |
| 537 | 536 |
| 538 } // namespace sync_driver_v2 | 537 } // namespace sync_driver_v2 |
| OLD | NEW |