Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: components/sync_driver/device_info_service.cc

Issue 1907683003: Convert //components/sync_driver from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Build fix, address feedback Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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/strings/string_util.h" 14 #include "base/strings/string_util.h"
14 #include "sync/api/entity_change.h" 15 #include "sync/api/entity_change.h"
15 #include "sync/api/metadata_batch.h" 16 #include "sync/api/metadata_batch.h"
16 #include "sync/api/sync_error.h" 17 #include "sync/api/sync_error.h"
17 #include "sync/internal_api/public/data_batch_impl.h" 18 #include "sync/internal_api/public/data_batch_impl.h"
18 #include "sync/internal_api/public/simple_metadata_change_list.h" 19 #include "sync/internal_api/public/simple_metadata_change_list.h"
19 #include "sync/protocol/data_type_state.pb.h" 20 #include "sync/protocol/data_type_state.pb.h"
20 #include "sync/protocol/sync.pb.h" 21 #include "sync/protocol/sync.pb.h"
21 22
22 namespace sync_driver_v2 { 23 namespace sync_driver_v2 {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 local_device_info_provider->RegisterOnInitializedCallback(base::Bind( 66 local_device_info_provider->RegisterOnInitializedCallback(base::Bind(
66 &DeviceInfoService::OnProviderInitialized, base::Unretained(this))); 67 &DeviceInfoService::OnProviderInitialized, base::Unretained(this)));
67 } 68 }
68 69
69 callback.Run(base::Bind(&DeviceInfoService::OnStoreCreated, 70 callback.Run(base::Bind(&DeviceInfoService::OnStoreCreated,
70 weak_factory_.GetWeakPtr())); 71 weak_factory_.GetWeakPtr()));
71 } 72 }
72 73
73 DeviceInfoService::~DeviceInfoService() {} 74 DeviceInfoService::~DeviceInfoService() {}
74 75
75 scoped_ptr<MetadataChangeList> DeviceInfoService::CreateMetadataChangeList() { 76 std::unique_ptr<MetadataChangeList>
76 return make_scoped_ptr(new SimpleMetadataChangeList()); 77 DeviceInfoService::CreateMetadataChangeList() {
78 return base::WrapUnique(new SimpleMetadataChangeList());
77 } 79 }
78 80
79 SyncError DeviceInfoService::MergeSyncData( 81 SyncError DeviceInfoService::MergeSyncData(
80 scoped_ptr<MetadataChangeList> metadata_change_list, 82 std::unique_ptr<MetadataChangeList> metadata_change_list,
81 EntityDataMap entity_data_map) { 83 EntityDataMap entity_data_map) {
82 if (!has_provider_initialized_ || !has_metadata_loaded_ || 84 if (!has_provider_initialized_ || !has_metadata_loaded_ ||
83 !change_processor()) { 85 !change_processor()) {
84 return SyncError( 86 return SyncError(
85 FROM_HERE, SyncError::DATATYPE_ERROR, 87 FROM_HERE, SyncError::DATATYPE_ERROR,
86 "Cannot call MergeSyncData without provider, data, and processor.", 88 "Cannot call MergeSyncData without provider, data, and processor.",
87 syncer::DEVICE_INFO); 89 syncer::DEVICE_INFO);
88 } 90 }
89 91
90 // Local data should typically be near empty, with the only possible value 92 // Local data should typically be near empty, with the only possible value
91 // corresponding to this device. This is because on signout all device info 93 // corresponding to this device. This is because on signout all device info
92 // data is blown away. However, this simplification is being ignored here and 94 // data is blown away. However, this simplification is being ignored here and
93 // a full difference is going to be calculated to explore what other service 95 // a full difference is going to be calculated to explore what other service
94 // implementations may look like. 96 // implementations may look like.
95 std::set<std::string> local_guids_to_put; 97 std::set<std::string> local_guids_to_put;
96 for (const auto& kv : all_data_) { 98 for (const auto& kv : all_data_) {
97 local_guids_to_put.insert(kv.first); 99 local_guids_to_put.insert(kv.first);
98 } 100 }
99 101
100 bool has_changes = false; 102 bool has_changes = false;
101 const DeviceInfo* local_info = 103 const DeviceInfo* local_info =
102 local_device_info_provider_->GetLocalDeviceInfo(); 104 local_device_info_provider_->GetLocalDeviceInfo();
103 std::string local_guid = local_info->guid(); 105 std::string local_guid = local_info->guid();
104 scoped_ptr<WriteBatch> batch = store_->CreateWriteBatch(); 106 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch();
105 for (const auto& kv : entity_data_map) { 107 for (const auto& kv : entity_data_map) {
106 const DeviceInfoSpecifics& specifics = 108 const DeviceInfoSpecifics& specifics =
107 kv.second.value().specifics.device_info(); 109 kv.second.value().specifics.device_info();
108 DCHECK_EQ(kv.first, SpecificsToTag(specifics)); 110 DCHECK_EQ(kv.first, SpecificsToTag(specifics));
109 if (specifics.cache_guid() == local_guid) { 111 if (specifics.cache_guid() == local_guid) {
110 // Don't Put local data if it's the same as the remote copy. 112 // Don't Put local data if it's the same as the remote copy.
111 if (local_info->Equals(*CopyToModel(specifics))) { 113 if (local_info->Equals(*CopyToModel(specifics))) {
112 local_guids_to_put.erase(local_guid); 114 local_guids_to_put.erase(local_guid);
113 } 115 }
114 } else { 116 } else {
115 // Remote data wins conflicts. 117 // Remote data wins conflicts.
116 local_guids_to_put.erase(specifics.cache_guid()); 118 local_guids_to_put.erase(specifics.cache_guid());
117 has_changes = true; 119 has_changes = true;
118 StoreSpecifics(make_scoped_ptr(new DeviceInfoSpecifics(specifics)), 120 StoreSpecifics(base::WrapUnique(new DeviceInfoSpecifics(specifics)),
119 batch.get()); 121 batch.get());
120 } 122 }
121 } 123 }
122 124
123 for (const std::string& guid : local_guids_to_put) { 125 for (const std::string& guid : local_guids_to_put) {
124 change_processor()->Put(SpecificsToTag(*all_data_[guid]), 126 change_processor()->Put(SpecificsToTag(*all_data_[guid]),
125 CopyToEntityData(*all_data_[guid]), 127 CopyToEntityData(*all_data_[guid]),
126 metadata_change_list.get()); 128 metadata_change_list.get());
127 } 129 }
128 130
129 CommitAndNotify(std::move(batch), std::move(metadata_change_list), 131 CommitAndNotify(std::move(batch), std::move(metadata_change_list),
130 has_changes); 132 has_changes);
131 return syncer::SyncError(); 133 return syncer::SyncError();
132 } 134 }
133 135
134 SyncError DeviceInfoService::ApplySyncChanges( 136 SyncError DeviceInfoService::ApplySyncChanges(
135 scoped_ptr<MetadataChangeList> metadata_change_list, 137 std::unique_ptr<MetadataChangeList> metadata_change_list,
136 EntityChangeList entity_changes) { 138 EntityChangeList entity_changes) {
137 if (!has_provider_initialized_ || !has_metadata_loaded_) { 139 if (!has_provider_initialized_ || !has_metadata_loaded_) {
138 return SyncError( 140 return SyncError(
139 FROM_HERE, SyncError::DATATYPE_ERROR, 141 FROM_HERE, SyncError::DATATYPE_ERROR,
140 "Cannot call ApplySyncChanges before provider and data have loaded.", 142 "Cannot call ApplySyncChanges before provider and data have loaded.",
141 syncer::DEVICE_INFO); 143 syncer::DEVICE_INFO);
142 } 144 }
143 145
144 scoped_ptr<WriteBatch> batch = store_->CreateWriteBatch(); 146 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch();
145 bool has_changes = false; 147 bool has_changes = false;
146 for (EntityChange& change : entity_changes) { 148 for (EntityChange& change : entity_changes) {
147 const std::string guid = TagToCacheGuid(change.client_tag()); 149 const std::string guid = TagToCacheGuid(change.client_tag());
148 // Each device is the authoritative source for itself, ignore any remote 150 // Each device is the authoritative source for itself, ignore any remote
149 // changes that have our local cache guid. 151 // changes that have our local cache guid.
150 if (guid == local_device_info_provider_->GetLocalDeviceInfo()->guid()) { 152 if (guid == local_device_info_provider_->GetLocalDeviceInfo()->guid()) {
151 continue; 153 continue;
152 } 154 }
153 155
154 if (change.type() == EntityChange::ACTION_DELETE) { 156 if (change.type() == EntityChange::ACTION_DELETE) {
155 has_changes |= DeleteSpecifics(guid, batch.get()); 157 has_changes |= DeleteSpecifics(guid, batch.get());
156 } else { 158 } else {
157 const DeviceInfoSpecifics& specifics = 159 const DeviceInfoSpecifics& specifics =
158 change.data().specifics.device_info(); 160 change.data().specifics.device_info();
159 DCHECK(guid == specifics.cache_guid()); 161 DCHECK(guid == specifics.cache_guid());
160 StoreSpecifics(make_scoped_ptr(new DeviceInfoSpecifics(specifics)), 162 StoreSpecifics(base::WrapUnique(new DeviceInfoSpecifics(specifics)),
161 batch.get()); 163 batch.get());
162 has_changes = true; 164 has_changes = true;
163 } 165 }
164 } 166 }
165 167
166 CommitAndNotify(std::move(batch), std::move(metadata_change_list), 168 CommitAndNotify(std::move(batch), std::move(metadata_change_list),
167 has_changes); 169 has_changes);
168 return SyncError(); 170 return SyncError();
169 } 171 }
170 172
171 void DeviceInfoService::GetData(ClientTagList client_tags, 173 void DeviceInfoService::GetData(ClientTagList client_tags,
172 DataCallback callback) { 174 DataCallback callback) {
173 if (!has_metadata_loaded_) { 175 if (!has_metadata_loaded_) {
174 callback.Run( 176 callback.Run(
175 SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, 177 SyncError(FROM_HERE, SyncError::DATATYPE_ERROR,
176 "Should not call GetData before metadata has loaded.", 178 "Should not call GetData before metadata has loaded.",
177 syncer::DEVICE_INFO), 179 syncer::DEVICE_INFO),
178 scoped_ptr<DataBatchImpl>()); 180 std::unique_ptr<DataBatchImpl>());
179 return; 181 return;
180 } 182 }
181 183
182 scoped_ptr<DataBatchImpl> batch(new DataBatchImpl()); 184 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl());
183 for (const auto& tag : client_tags) { 185 for (const auto& tag : client_tags) {
184 const auto& iter = all_data_.find(TagToCacheGuid(tag)); 186 const auto& iter = all_data_.find(TagToCacheGuid(tag));
185 if (iter != all_data_.end()) { 187 if (iter != all_data_.end()) {
186 DCHECK_EQ(tag, SpecificsToTag(*iter->second)); 188 DCHECK_EQ(tag, SpecificsToTag(*iter->second));
187 batch->Put(tag, CopyToEntityData(*iter->second)); 189 batch->Put(tag, CopyToEntityData(*iter->second));
188 } 190 }
189 } 191 }
190 callback.Run(syncer::SyncError(), std::move(batch)); 192 callback.Run(syncer::SyncError(), std::move(batch));
191 } 193 }
192 194
193 void DeviceInfoService::GetAllData(DataCallback callback) { 195 void DeviceInfoService::GetAllData(DataCallback callback) {
194 if (!has_metadata_loaded_) { 196 if (!has_metadata_loaded_) {
195 callback.Run( 197 callback.Run(
196 SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, 198 SyncError(FROM_HERE, SyncError::DATATYPE_ERROR,
197 "Should not call GetAllData before metadata has loaded.", 199 "Should not call GetAllData before metadata has loaded.",
198 syncer::DEVICE_INFO), 200 syncer::DEVICE_INFO),
199 scoped_ptr<DataBatchImpl>()); 201 std::unique_ptr<DataBatchImpl>());
200 return; 202 return;
201 } 203 }
202 204
203 scoped_ptr<DataBatchImpl> batch(new DataBatchImpl()); 205 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl());
204 for (const auto& kv : all_data_) { 206 for (const auto& kv : all_data_) {
205 batch->Put(SpecificsToTag(*kv.second), CopyToEntityData(*kv.second)); 207 batch->Put(SpecificsToTag(*kv.second), CopyToEntityData(*kv.second));
206 } 208 }
207 callback.Run(syncer::SyncError(), std::move(batch)); 209 callback.Run(syncer::SyncError(), std::move(batch));
208 } 210 }
209 211
210 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) { 212 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) {
211 DCHECK(entity_data.specifics.has_device_info()); 213 DCHECK(entity_data.specifics.has_device_info());
212 return SpecificsToTag(entity_data.specifics.device_info()); 214 return SpecificsToTag(entity_data.specifics.device_info());
213 } 215 }
214 216
215 void DeviceInfoService::OnChangeProcessorSet() { 217 void DeviceInfoService::OnChangeProcessorSet() {
216 // We've recieved a new processor that needs metadata. If we're still in the 218 // We've recieved a new processor that needs metadata. If we're still in the
217 // process of loading data and/or metadata, then |has_metadata_loaded_| is 219 // process of loading data and/or metadata, then |has_metadata_loaded_| is
218 // false and we'll wait for those async reads to happen. If we've already 220 // false and we'll wait for those async reads to happen. If we've already
219 // loaded metadata and then subsequently we get a new processor, we must not 221 // loaded metadata and then subsequently we get a new processor, we must not
220 // have created the processor ourselves because we had no metadata. So there 222 // have created the processor ourselves because we had no metadata. So there
221 // must not be any metadata on disk. 223 // must not be any metadata on disk.
222 if (has_metadata_loaded_) { 224 if (has_metadata_loaded_) {
223 change_processor()->OnMetadataLoaded(make_scoped_ptr(new MetadataBatch())); 225 change_processor()->OnMetadataLoaded(base::WrapUnique(new MetadataBatch()));
224 TryReconcileLocalAndStored(); 226 TryReconcileLocalAndStored();
225 } 227 }
226 } 228 }
227 229
228 bool DeviceInfoService::IsSyncing() const { 230 bool DeviceInfoService::IsSyncing() const {
229 return !all_data_.empty(); 231 return !all_data_.empty();
230 } 232 }
231 233
232 scoped_ptr<DeviceInfo> DeviceInfoService::GetDeviceInfo( 234 std::unique_ptr<DeviceInfo> DeviceInfoService::GetDeviceInfo(
233 const std::string& client_id) const { 235 const std::string& client_id) const {
234 const ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id); 236 const ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id);
235 if (iter == all_data_.end()) { 237 if (iter == all_data_.end()) {
236 return scoped_ptr<DeviceInfo>(); 238 return std::unique_ptr<DeviceInfo>();
237 } 239 }
238 240
239 return CopyToModel(*iter->second); 241 return CopyToModel(*iter->second);
240 } 242 }
241 243
242 ScopedVector<DeviceInfo> DeviceInfoService::GetAllDeviceInfo() const { 244 ScopedVector<DeviceInfo> DeviceInfoService::GetAllDeviceInfo() const {
243 ScopedVector<DeviceInfo> list; 245 ScopedVector<DeviceInfo> list;
244 246
245 for (ClientIdToSpecifics::const_iterator iter = all_data_.begin(); 247 for (ClientIdToSpecifics::const_iterator iter = all_data_.begin();
246 iter != all_data_.end(); ++iter) { 248 iter != all_data_.end(); ++iter) {
(...skipping 26 matching lines...) Expand all
273 } 275 }
274 276
275 std::string DeviceInfoService::TagToCacheGuid(const std::string& tag) { 277 std::string DeviceInfoService::TagToCacheGuid(const std::string& tag) {
276 DCHECK(base::StartsWith(tag, kClientTagPrefix, base::CompareCase::SENSITIVE)); 278 DCHECK(base::StartsWith(tag, kClientTagPrefix, base::CompareCase::SENSITIVE));
277 return tag.substr(strlen(kClientTagPrefix)); 279 return tag.substr(strlen(kClientTagPrefix));
278 } 280 }
279 281
280 // TODO(skym): crbug.com/543406: It might not make sense for this to be a 282 // TODO(skym): crbug.com/543406: It might not make sense for this to be a
281 // scoped_ptr. 283 // scoped_ptr.
282 // Static. 284 // Static.
283 scoped_ptr<DeviceInfoSpecifics> DeviceInfoService::CopyToSpecifics( 285 std::unique_ptr<DeviceInfoSpecifics> DeviceInfoService::CopyToSpecifics(
284 const DeviceInfo& info) { 286 const DeviceInfo& info) {
285 scoped_ptr<DeviceInfoSpecifics> specifics = 287 std::unique_ptr<DeviceInfoSpecifics> specifics =
286 make_scoped_ptr(new DeviceInfoSpecifics); 288 base::WrapUnique(new DeviceInfoSpecifics);
287 specifics->set_cache_guid(info.guid()); 289 specifics->set_cache_guid(info.guid());
288 specifics->set_client_name(info.client_name()); 290 specifics->set_client_name(info.client_name());
289 specifics->set_chrome_version(info.chrome_version()); 291 specifics->set_chrome_version(info.chrome_version());
290 specifics->set_sync_user_agent(info.sync_user_agent()); 292 specifics->set_sync_user_agent(info.sync_user_agent());
291 specifics->set_device_type(info.device_type()); 293 specifics->set_device_type(info.device_type());
292 specifics->set_signin_scoped_device_id(info.signin_scoped_device_id()); 294 specifics->set_signin_scoped_device_id(info.signin_scoped_device_id());
293 return specifics; 295 return specifics;
294 } 296 }
295 297
296 // Static. 298 // Static.
297 scoped_ptr<DeviceInfo> DeviceInfoService::CopyToModel( 299 std::unique_ptr<DeviceInfo> DeviceInfoService::CopyToModel(
298 const DeviceInfoSpecifics& specifics) { 300 const DeviceInfoSpecifics& specifics) {
299 return make_scoped_ptr(new DeviceInfo( 301 return base::WrapUnique(new DeviceInfo(
300 specifics.cache_guid(), specifics.client_name(), 302 specifics.cache_guid(), specifics.client_name(),
301 specifics.chrome_version(), specifics.sync_user_agent(), 303 specifics.chrome_version(), specifics.sync_user_agent(),
302 specifics.device_type(), specifics.signin_scoped_device_id())); 304 specifics.device_type(), specifics.signin_scoped_device_id()));
303 } 305 }
304 306
305 // Static. 307 // Static.
306 scoped_ptr<EntityData> DeviceInfoService::CopyToEntityData( 308 std::unique_ptr<EntityData> DeviceInfoService::CopyToEntityData(
307 const DeviceInfoSpecifics& specifics) { 309 const DeviceInfoSpecifics& specifics) {
308 scoped_ptr<EntityData> entity_data(new EntityData()); 310 std::unique_ptr<EntityData> entity_data(new EntityData());
309 *entity_data->specifics.mutable_device_info() = specifics; 311 *entity_data->specifics.mutable_device_info() = specifics;
310 entity_data->non_unique_name = specifics.client_name(); 312 entity_data->non_unique_name = specifics.client_name();
311 return entity_data; 313 return entity_data;
312 } 314 }
313 315
314 void DeviceInfoService::StoreSpecifics( 316 void DeviceInfoService::StoreSpecifics(
315 scoped_ptr<DeviceInfoSpecifics> specifics, 317 std::unique_ptr<DeviceInfoSpecifics> specifics,
316 WriteBatch* batch) { 318 WriteBatch* batch) {
317 const std::string guid = specifics->cache_guid(); 319 const std::string guid = specifics->cache_guid();
318 DVLOG(1) << "Storing DEVICE_INFO for " << specifics->client_name() 320 DVLOG(1) << "Storing DEVICE_INFO for " << specifics->client_name()
319 << " with ID " << guid; 321 << " with ID " << guid;
320 store_->WriteData(batch, guid, specifics->SerializeAsString()); 322 store_->WriteData(batch, guid, specifics->SerializeAsString());
321 all_data_[guid] = std::move(specifics); 323 all_data_[guid] = std::move(specifics);
322 } 324 }
323 325
324 bool DeviceInfoService::DeleteSpecifics(const std::string& guid, 326 bool DeviceInfoService::DeleteSpecifics(const std::string& guid,
325 WriteBatch* batch) { 327 WriteBatch* batch) {
326 ClientIdToSpecifics::const_iterator iter = all_data_.find(guid); 328 ClientIdToSpecifics::const_iterator iter = all_data_.find(guid);
327 if (iter != all_data_.end()) { 329 if (iter != all_data_.end()) {
328 DVLOG(1) << "Deleting DEVICE_INFO for " << iter->second->client_name() 330 DVLOG(1) << "Deleting DEVICE_INFO for " << iter->second->client_name()
329 << " with ID " << guid; 331 << " with ID " << guid;
330 store_->DeleteData(batch, guid); 332 store_->DeleteData(batch, guid);
331 all_data_.erase(iter); 333 all_data_.erase(iter);
332 return true; 334 return true;
333 } else { 335 } else {
334 return false; 336 return false;
335 } 337 }
336 } 338 }
337 339
338 void DeviceInfoService::OnProviderInitialized() { 340 void DeviceInfoService::OnProviderInitialized() {
339 has_provider_initialized_ = true; 341 has_provider_initialized_ = true;
340 TryReconcileLocalAndStored(); 342 TryReconcileLocalAndStored();
341 } 343 }
342 344
343 void DeviceInfoService::OnStoreCreated(Result result, 345 void DeviceInfoService::OnStoreCreated(Result result,
344 scoped_ptr<ModelTypeStore> store) { 346 std::unique_ptr<ModelTypeStore> store) {
345 if (result == Result::SUCCESS) { 347 if (result == Result::SUCCESS) {
346 std::swap(store_, store); 348 std::swap(store_, store);
347 store_->ReadAllData(base::Bind(&DeviceInfoService::OnReadAllData, 349 store_->ReadAllData(base::Bind(&DeviceInfoService::OnReadAllData,
348 weak_factory_.GetWeakPtr())); 350 weak_factory_.GetWeakPtr()));
349 } else { 351 } else {
350 LOG(WARNING) << "ModelTypeStore creation failed."; 352 LOG(WARNING) << "ModelTypeStore creation failed.";
351 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization 353 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization
352 // failure. 354 // failure.
353 } 355 }
354 } 356 }
355 357
356 void DeviceInfoService::OnReadAllData(Result result, 358 void DeviceInfoService::OnReadAllData(Result result,
357 scoped_ptr<RecordList> record_list) { 359 std::unique_ptr<RecordList> record_list) {
358 if (result != Result::SUCCESS) { 360 if (result != Result::SUCCESS) {
359 LOG(WARNING) << "Initial load of data failed."; 361 LOG(WARNING) << "Initial load of data failed.";
360 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization 362 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization
361 // failure. 363 // failure.
362 return; 364 return;
363 } 365 }
364 366
365 for (const Record& r : *record_list.get()) { 367 for (const Record& r : *record_list.get()) {
366 scoped_ptr<DeviceInfoSpecifics> specifics( 368 std::unique_ptr<DeviceInfoSpecifics> specifics(
367 make_scoped_ptr(new DeviceInfoSpecifics())); 369 base::WrapUnique(new DeviceInfoSpecifics()));
368 if (specifics->ParseFromString(r.value)) { 370 if (specifics->ParseFromString(r.value)) {
369 all_data_[specifics->cache_guid()] = std::move(specifics); 371 all_data_[specifics->cache_guid()] = std::move(specifics);
370 } else { 372 } else {
371 LOG(WARNING) << "Failed to deserialize specifics."; 373 LOG(WARNING) << "Failed to deserialize specifics.";
372 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization 374 // TODO(skym, crbug.com/582460): Handle unrecoverable initialization
373 // failure. 375 // failure.
374 } 376 }
375 } 377 }
376 store_->ReadAllMetadata(base::Bind(&DeviceInfoService::OnReadAllMetadata, 378 store_->ReadAllMetadata(base::Bind(&DeviceInfoService::OnReadAllMetadata,
377 weak_factory_.GetWeakPtr())); 379 weak_factory_.GetWeakPtr()));
378 } 380 }
379 381
380 void DeviceInfoService::OnReadAllMetadata( 382 void DeviceInfoService::OnReadAllMetadata(
381 Result result, 383 Result result,
382 scoped_ptr<RecordList> metadata_records, 384 std::unique_ptr<RecordList> metadata_records,
383 const std::string& global_metadata) { 385 const std::string& global_metadata) {
384 if (result != Result::SUCCESS) { 386 if (result != Result::SUCCESS) {
385 // Store has encountered some serious error. We should still be able to 387 // Store has encountered some serious error. We should still be able to
386 // continue as a read only service, since if we got this far we must have 388 // continue as a read only service, since if we got this far we must have
387 // loaded all data out succesfully. TODO(skym): Should we communicate this 389 // loaded all data out succesfully. TODO(skym): Should we communicate this
388 // to sync somehow? 390 // to sync somehow?
389 LOG(WARNING) << "Load of metadata completely failed."; 391 LOG(WARNING) << "Load of metadata completely failed.";
390 return; 392 return;
391 } 393 }
392 394
(...skipping 11 matching lines...) Expand all
404 // at this point so that we'll know to give a processor empty metadata if it 406 // at this point so that we'll know to give a processor empty metadata if it
405 // is created later. 407 // is created later.
406 has_metadata_loaded_ = true; 408 has_metadata_loaded_ = true;
407 409
408 if (!change_processor()) { 410 if (!change_processor()) {
409 // This means we haven't been told to start sycning and we don't have any 411 // This means we haven't been told to start sycning and we don't have any
410 // local metadata 412 // local metadata
411 return; 413 return;
412 } 414 }
413 415
414 scoped_ptr<MetadataBatch> batch(new MetadataBatch()); 416 std::unique_ptr<MetadataBatch> batch(new MetadataBatch());
415 DataTypeState state; 417 DataTypeState state;
416 if (state.ParseFromString(global_metadata)) { 418 if (state.ParseFromString(global_metadata)) {
417 batch->SetDataTypeState(state); 419 batch->SetDataTypeState(state);
418 } else { 420 } else {
419 // TODO(skym): How bad is this scenario? We may be able to just give an 421 // TODO(skym): How bad is this scenario? We may be able to just give an
420 // empty batch to the processor and we'll treat corrupted data type state 422 // empty batch to the processor and we'll treat corrupted data type state
421 // as no data type state at all. The question is do we want to add any of 423 // as no data type state at all. The question is do we want to add any of
422 // the entity metadata to the batch or completely skip that step? We're 424 // the entity metadata to the batch or completely skip that step? We're
423 // going to have to perform a merge shortly. Does this decision/logic even 425 // going to have to perform a merge shortly. Does this decision/logic even
424 // belong in this service? 426 // belong in this service?
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 auto iter = all_data_.find(current_info->guid()); 460 auto iter = all_data_.find(current_info->guid());
459 // Convert to DeviceInfo for Equals function. 461 // Convert to DeviceInfo for Equals function.
460 if (iter == all_data_.end() || 462 if (iter == all_data_.end() ||
461 !current_info->Equals(*CopyToModel(*iter->second))) { 463 !current_info->Equals(*CopyToModel(*iter->second))) {
462 PutAndStore(*current_info); 464 PutAndStore(*current_info);
463 } 465 }
464 } 466 }
465 } 467 }
466 468
467 void DeviceInfoService::PutAndStore(const DeviceInfo& device_info) { 469 void DeviceInfoService::PutAndStore(const DeviceInfo& device_info) {
468 scoped_ptr<DeviceInfoSpecifics> specifics = CopyToSpecifics(device_info); 470 std::unique_ptr<DeviceInfoSpecifics> specifics = CopyToSpecifics(device_info);
469 471
470 scoped_ptr<MetadataChangeList> metadata_change_list = 472 std::unique_ptr<MetadataChangeList> metadata_change_list =
471 CreateMetadataChangeList(); 473 CreateMetadataChangeList();
472 change_processor()->Put(SpecificsToTag(*specifics), 474 change_processor()->Put(SpecificsToTag(*specifics),
473 CopyToEntityData(*specifics), 475 CopyToEntityData(*specifics),
474 metadata_change_list.get()); 476 metadata_change_list.get());
475 477
476 scoped_ptr<WriteBatch> batch = store_->CreateWriteBatch(); 478 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch();
477 StoreSpecifics(std::move(specifics), batch.get()); 479 StoreSpecifics(std::move(specifics), batch.get());
478 480
479 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true); 481 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true);
480 } 482 }
481 483
482 void DeviceInfoService::CommitAndNotify( 484 void DeviceInfoService::CommitAndNotify(
483 scoped_ptr<WriteBatch> batch, 485 std::unique_ptr<WriteBatch> batch,
484 scoped_ptr<MetadataChangeList> metadata_change_list, 486 std::unique_ptr<MetadataChangeList> metadata_change_list,
485 bool should_notify) { 487 bool should_notify) {
486 static_cast<SimpleMetadataChangeList*>(metadata_change_list.get()) 488 static_cast<SimpleMetadataChangeList*>(metadata_change_list.get())
487 ->TransferChanges(store_.get(), batch.get()); 489 ->TransferChanges(store_.get(), batch.get());
488 store_->CommitWriteBatch( 490 store_->CommitWriteBatch(
489 std::move(batch), 491 std::move(batch),
490 base::Bind(&DeviceInfoService::OnCommit, weak_factory_.GetWeakPtr())); 492 base::Bind(&DeviceInfoService::OnCommit, weak_factory_.GetWeakPtr()));
491 if (should_notify) { 493 if (should_notify) {
492 NotifyObservers(); 494 NotifyObservers();
493 } 495 }
494 } 496 }
495 497
496 } // namespace sync_driver_v2 498 } // namespace sync_driver_v2
OLDNEW
« no previous file with comments | « components/sync_driver/device_info_service.h ('k') | components/sync_driver/device_info_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698