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 <utility> | 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "sync/api/entity_change.h" | 13 #include "sync/api/entity_change.h" |
13 #include "sync/api/metadata_batch.h" | 14 #include "sync/api/metadata_batch.h" |
14 #include "sync/api/sync_error.h" | 15 #include "sync/api/sync_error.h" |
15 #include "sync/internal_api/public/data_batch_impl.h" | 16 #include "sync/internal_api/public/data_batch_impl.h" |
16 #include "sync/internal_api/public/simple_metadata_change_list.h" | 17 #include "sync/internal_api/public/simple_metadata_change_list.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 63 |
63 DeviceInfoService::~DeviceInfoService() {} | 64 DeviceInfoService::~DeviceInfoService() {} |
64 | 65 |
65 scoped_ptr<MetadataChangeList> DeviceInfoService::CreateMetadataChangeList() { | 66 scoped_ptr<MetadataChangeList> DeviceInfoService::CreateMetadataChangeList() { |
66 return make_scoped_ptr(new SimpleMetadataChangeList()); | 67 return make_scoped_ptr(new SimpleMetadataChangeList()); |
67 } | 68 } |
68 | 69 |
69 SyncError DeviceInfoService::MergeSyncData( | 70 SyncError DeviceInfoService::MergeSyncData( |
70 scoped_ptr<MetadataChangeList> metadata_change_list, | 71 scoped_ptr<MetadataChangeList> metadata_change_list, |
71 EntityDataMap entity_data_map) { | 72 EntityDataMap entity_data_map) { |
72 // TODO(skym): crbug.com/543406: Implementation. | 73 if (!has_provider_initialized_ || !has_data_loaded_ || !change_processor()) { |
73 return SyncError(); | 74 return SyncError( |
| 75 FROM_HERE, SyncError::DATATYPE_ERROR, |
| 76 "Cannot call MergeSyncData without provider, data, and processor.", |
| 77 syncer::DEVICE_INFO); |
| 78 } |
| 79 |
| 80 // Local data should typically be near empty, with the only possible value |
| 81 // corresponding to this device. This is because on signout all device info |
| 82 // data is blown away. However, this simplification is being ignored here and |
| 83 // a full difference is going to be calculated to explore what other service |
| 84 // implementations may look like. |
| 85 std::set<std::string> local_only_tags; |
| 86 for (const auto& kv : all_data_) { |
| 87 local_only_tags.insert(kv.first); |
| 88 } |
| 89 |
| 90 bool has_changes = false; |
| 91 std::string local_tag = |
| 92 local_device_info_provider_->GetLocalDeviceInfo()->guid(); |
| 93 scoped_ptr<WriteBatch> batch = store_->CreateWriteBatch(); |
| 94 for (const auto& kv : entity_data_map) { |
| 95 const std::string tag = GetClientTag(kv.second.value()); |
| 96 const DeviceInfoSpecifics& specifics = |
| 97 kv.second.value().specifics.device_info(); |
| 98 |
| 99 // Ignore any remote changes that have our local cache guid. |
| 100 if (tag == local_tag) { |
| 101 continue; |
| 102 } |
| 103 |
| 104 // Remote data wins conflicts. |
| 105 local_only_tags.erase(tag); |
| 106 StoreSpecifics(make_scoped_ptr(new DeviceInfoSpecifics(specifics)), |
| 107 batch.get()); |
| 108 has_changes = true; |
| 109 } |
| 110 |
| 111 for (const std::string& tag : local_only_tags) { |
| 112 change_processor()->Put(tag, CopyIntoNewEntityData(*all_data_[tag]), |
| 113 metadata_change_list.get()); |
| 114 } |
| 115 |
| 116 // Transfer at the end because processor Put calls may update metadata. |
| 117 static_cast<SimpleMetadataChangeList*>(metadata_change_list.get()) |
| 118 ->TransferChanges(store_.get(), batch.get()); |
| 119 store_->CommitWriteBatch( |
| 120 std::move(batch), |
| 121 base::Bind(&DeviceInfoService::OnCommit, weak_factory_.GetWeakPtr())); |
| 122 if (has_changes) { |
| 123 NotifyObservers(); |
| 124 } |
| 125 return syncer::SyncError(); |
74 } | 126 } |
75 | 127 |
76 SyncError DeviceInfoService::ApplySyncChanges( | 128 SyncError DeviceInfoService::ApplySyncChanges( |
77 scoped_ptr<MetadataChangeList> metadata_change_list, | 129 scoped_ptr<MetadataChangeList> metadata_change_list, |
78 EntityChangeList entity_changes) { | 130 EntityChangeList entity_changes) { |
79 if (!has_provider_initialized_ || !has_data_loaded_) { | 131 if (!has_provider_initialized_ || !has_data_loaded_) { |
80 return SyncError( | 132 return SyncError( |
81 FROM_HERE, SyncError::DATATYPE_ERROR, | 133 FROM_HERE, SyncError::DATATYPE_ERROR, |
82 "Cannot call ApplySyncChanges before provider and data have loaded.", | 134 "Cannot call ApplySyncChanges before provider and data have loaded.", |
83 syncer::DEVICE_INFO); | 135 syncer::DEVICE_INFO); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 if (!has_data_loaded_) { | 173 if (!has_data_loaded_) { |
122 callback.Run(SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, | 174 callback.Run(SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, |
123 "Cannot call GetData before data has loaded.", | 175 "Cannot call GetData before data has loaded.", |
124 syncer::DEVICE_INFO), | 176 syncer::DEVICE_INFO), |
125 scoped_ptr<DataBatchImpl>()); | 177 scoped_ptr<DataBatchImpl>()); |
126 return; | 178 return; |
127 } | 179 } |
128 | 180 |
129 syncer::SyncError error; | 181 syncer::SyncError error; |
130 scoped_ptr<DataBatchImpl> batch(new DataBatchImpl()); | 182 scoped_ptr<DataBatchImpl> batch(new DataBatchImpl()); |
131 for (auto& tag : client_tags) { | 183 for (const auto& tag : client_tags) { |
132 auto iter = all_data_.find(tag); | 184 const auto iter = all_data_.find(tag); |
133 if (iter != all_data_.end()) { | 185 if (iter != all_data_.end()) { |
134 batch->Put(tag, CopyIntoNewEntityData(*iter->second)); | 186 batch->Put(tag, CopyIntoNewEntityData(*iter->second)); |
135 } | 187 } |
136 } | 188 } |
137 callback.Run(error, std::move(batch)); | 189 callback.Run(error, std::move(batch)); |
138 } | 190 } |
139 | 191 |
140 void DeviceInfoService::GetAllData(DataCallback callback) { | 192 void DeviceInfoService::GetAllData(DataCallback callback) { |
141 if (!has_data_loaded_) { | 193 if (!has_data_loaded_) { |
142 callback.Run(SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, | 194 callback.Run(SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, |
143 "Cannot call GetAllData before data has loaded.", | 195 "Cannot call GetAllData before data has loaded.", |
144 syncer::DEVICE_INFO), | 196 syncer::DEVICE_INFO), |
145 scoped_ptr<DataBatchImpl>()); | 197 scoped_ptr<DataBatchImpl>()); |
146 return; | 198 return; |
147 } | 199 } |
148 | 200 |
149 syncer::SyncError error; | 201 syncer::SyncError error; |
150 scoped_ptr<DataBatchImpl> batch(new DataBatchImpl()); | 202 scoped_ptr<DataBatchImpl> batch(new DataBatchImpl()); |
151 for (auto& kv : all_data_) { | 203 for (const auto& kv : all_data_) { |
152 batch->Put(kv.first, CopyIntoNewEntityData(*kv.second)); | 204 batch->Put(kv.first, CopyIntoNewEntityData(*kv.second)); |
153 } | 205 } |
154 callback.Run(error, std::move(batch)); | 206 callback.Run(error, std::move(batch)); |
155 } | 207 } |
156 | 208 |
157 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) { | 209 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) { |
158 DCHECK(entity_data.specifics.has_device_info()); | 210 DCHECK(entity_data.specifics.has_device_info()); |
159 return entity_data.specifics.device_info().cache_guid(); | 211 return entity_data.specifics.device_info().cache_guid(); |
160 } | 212 } |
161 | 213 |
162 void DeviceInfoService::OnChangeProcessorSet() { | 214 void DeviceInfoService::OnChangeProcessorSet() { |
163 TryLoadAllMetadata(); | 215 TryLoadAllMetadata(); |
164 } | 216 } |
165 | 217 |
166 bool DeviceInfoService::IsSyncing() const { | 218 bool DeviceInfoService::IsSyncing() const { |
167 return !all_data_.empty(); | 219 return !all_data_.empty(); |
168 } | 220 } |
169 | 221 |
170 scoped_ptr<DeviceInfo> DeviceInfoService::GetDeviceInfo( | 222 scoped_ptr<DeviceInfo> DeviceInfoService::GetDeviceInfo( |
171 const std::string& client_id) const { | 223 const std::string& client_id) const { |
172 ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id); | 224 const ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id); |
173 if (iter == all_data_.end()) { | 225 if (iter == all_data_.end()) { |
174 return scoped_ptr<DeviceInfo>(); | 226 return scoped_ptr<DeviceInfo>(); |
175 } | 227 } |
176 | 228 |
177 return CreateDeviceInfo(*iter->second); | 229 return CreateDeviceInfo(*iter->second); |
178 } | 230 } |
179 | 231 |
180 ScopedVector<DeviceInfo> DeviceInfoService::GetAllDeviceInfo() const { | 232 ScopedVector<DeviceInfo> DeviceInfoService::GetAllDeviceInfo() const { |
181 ScopedVector<DeviceInfo> list; | 233 ScopedVector<DeviceInfo> list; |
182 | 234 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 } | 416 } |
365 | 417 |
366 void DeviceInfoService::TryLoadAllMetadata() { | 418 void DeviceInfoService::TryLoadAllMetadata() { |
367 if (has_data_loaded_ && change_processor()) { | 419 if (has_data_loaded_ && change_processor()) { |
368 store_->ReadAllMetadata(base::Bind(&DeviceInfoService::OnReadAllMetadata, | 420 store_->ReadAllMetadata(base::Bind(&DeviceInfoService::OnReadAllMetadata, |
369 weak_factory_.GetWeakPtr())); | 421 weak_factory_.GetWeakPtr())); |
370 } | 422 } |
371 } | 423 } |
372 | 424 |
373 } // namespace sync_driver_v2 | 425 } // namespace sync_driver_v2 |
OLD | NEW |