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