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

Side by Side Diff: components/sync/device_info/device_info_sync_bridge.cc

Issue 2460903003: [Sync] Rename DeviceInfoService to DeviceInfoSyncBridge. (Closed)
Patch Set: Updating nullptr to null in comment. Created 4 years, 1 month 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/device_info/device_info_service.h" 5 #include "components/sync/device_info/device_info_sync_bridge.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <set> 10 #include <set>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 specifics->set_chrome_version(info.chrome_version()); 77 specifics->set_chrome_version(info.chrome_version());
78 specifics->set_sync_user_agent(info.sync_user_agent()); 78 specifics->set_sync_user_agent(info.sync_user_agent());
79 specifics->set_device_type(info.device_type()); 79 specifics->set_device_type(info.device_type());
80 specifics->set_signin_scoped_device_id(info.signin_scoped_device_id()); 80 specifics->set_signin_scoped_device_id(info.signin_scoped_device_id());
81 specifics->set_last_updated_timestamp(last_updated_timestamp); 81 specifics->set_last_updated_timestamp(last_updated_timestamp);
82 return specifics; 82 return specifics;
83 } 83 }
84 84
85 } // namespace 85 } // namespace
86 86
87 DeviceInfoService::DeviceInfoService( 87 DeviceInfoSyncBridge::DeviceInfoSyncBridge(
88 LocalDeviceInfoProvider* local_device_info_provider, 88 LocalDeviceInfoProvider* local_device_info_provider,
89 const StoreFactoryFunction& callback, 89 const StoreFactoryFunction& callback,
90 const ChangeProcessorFactory& change_processor_factory) 90 const ChangeProcessorFactory& change_processor_factory)
91 : ModelTypeSyncBridge(change_processor_factory, DEVICE_INFO), 91 : ModelTypeSyncBridge(change_processor_factory, DEVICE_INFO),
92 local_device_info_provider_(local_device_info_provider) { 92 local_device_info_provider_(local_device_info_provider) {
93 DCHECK(local_device_info_provider); 93 DCHECK(local_device_info_provider);
94 94
95 // This is not threadsafe, but presuably the provider initializes on the same 95 // This is not threadsafe, but presuably the provider initializes on the same
96 // thread as us so we're okay. 96 // thread as us so we're okay.
97 if (local_device_info_provider->GetLocalDeviceInfo()) { 97 if (local_device_info_provider->GetLocalDeviceInfo()) {
98 OnProviderInitialized(); 98 OnProviderInitialized();
99 } else { 99 } else {
100 subscription_ = 100 subscription_ = local_device_info_provider->RegisterOnInitializedCallback(
101 local_device_info_provider->RegisterOnInitializedCallback(base::Bind( 101 base::Bind(&DeviceInfoSyncBridge::OnProviderInitialized,
102 &DeviceInfoService::OnProviderInitialized, base::Unretained(this))); 102 base::Unretained(this)));
103 } 103 }
104 104
105 callback.Run( 105 callback.Run(
106 base::Bind(&DeviceInfoService::OnStoreCreated, base::AsWeakPtr(this))); 106 base::Bind(&DeviceInfoSyncBridge::OnStoreCreated, base::AsWeakPtr(this)));
107 } 107 }
108 108
109 DeviceInfoService::~DeviceInfoService() {} 109 DeviceInfoSyncBridge::~DeviceInfoSyncBridge() {}
110 110
111 std::unique_ptr<MetadataChangeList> 111 std::unique_ptr<MetadataChangeList>
112 DeviceInfoService::CreateMetadataChangeList() { 112 DeviceInfoSyncBridge::CreateMetadataChangeList() {
113 return base::MakeUnique<SimpleMetadataChangeList>(); 113 return base::MakeUnique<SimpleMetadataChangeList>();
114 } 114 }
115 115
116 SyncError DeviceInfoService::MergeSyncData( 116 SyncError DeviceInfoSyncBridge::MergeSyncData(
117 std::unique_ptr<MetadataChangeList> metadata_change_list, 117 std::unique_ptr<MetadataChangeList> metadata_change_list,
118 EntityDataMap entity_data_map) { 118 EntityDataMap entity_data_map) {
119 DCHECK(has_provider_initialized_); 119 DCHECK(has_provider_initialized_);
120 DCHECK(change_processor()->IsTrackingMetadata()); 120 DCHECK(change_processor()->IsTrackingMetadata());
121 121
122 // Local data should typically be near empty, with the only possible value 122 // Local data should typically be near empty, with the only possible value
123 // corresponding to this device. This is because on signout all device info 123 // corresponding to this device. This is because on signout all device info
124 // data is blown away. However, this simplification is being ignored here and 124 // data is blown away. However, this simplification is being ignored here and
125 // a full difference is going to be calculated to explore what other service 125 // a full difference is going to be calculated to explore what other bridge
126 // implementations may look like. 126 // implementations may look like.
127 std::set<std::string> local_guids_to_put; 127 std::set<std::string> local_guids_to_put;
128 for (const auto& kv : all_data_) { 128 for (const auto& kv : all_data_) {
129 local_guids_to_put.insert(kv.first); 129 local_guids_to_put.insert(kv.first);
130 } 130 }
131 131
132 bool has_changes = false; 132 bool has_changes = false;
133 const DeviceInfo* local_info = 133 const DeviceInfo* local_info =
134 local_device_info_provider_->GetLocalDeviceInfo(); 134 local_device_info_provider_->GetLocalDeviceInfo();
135 std::string local_guid = local_info->guid(); 135 std::string local_guid = local_info->guid();
(...skipping 24 matching lines...) Expand all
160 for (const std::string& guid : local_guids_to_put) { 160 for (const std::string& guid : local_guids_to_put) {
161 change_processor()->Put(guid, CopyToEntityData(*all_data_[guid]), 161 change_processor()->Put(guid, CopyToEntityData(*all_data_[guid]),
162 metadata_change_list.get()); 162 metadata_change_list.get());
163 } 163 }
164 164
165 CommitAndNotify(std::move(batch), std::move(metadata_change_list), 165 CommitAndNotify(std::move(batch), std::move(metadata_change_list),
166 has_changes); 166 has_changes);
167 return SyncError(); 167 return SyncError();
168 } 168 }
169 169
170 SyncError DeviceInfoService::ApplySyncChanges( 170 SyncError DeviceInfoSyncBridge::ApplySyncChanges(
171 std::unique_ptr<MetadataChangeList> metadata_change_list, 171 std::unique_ptr<MetadataChangeList> metadata_change_list,
172 EntityChangeList entity_changes) { 172 EntityChangeList entity_changes) {
173 DCHECK(has_provider_initialized_); 173 DCHECK(has_provider_initialized_);
174 174
175 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); 175 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch();
176 bool has_changes = false; 176 bool has_changes = false;
177 for (EntityChange& change : entity_changes) { 177 for (EntityChange& change : entity_changes) {
178 const std::string guid = change.storage_key(); 178 const std::string guid = change.storage_key();
179 // Each device is the authoritative source for itself, ignore any remote 179 // Each device is the authoritative source for itself, ignore any remote
180 // changes that have our local cache guid. 180 // changes that have our local cache guid.
(...skipping 11 matching lines...) Expand all
192 batch.get()); 192 batch.get());
193 has_changes = true; 193 has_changes = true;
194 } 194 }
195 } 195 }
196 196
197 CommitAndNotify(std::move(batch), std::move(metadata_change_list), 197 CommitAndNotify(std::move(batch), std::move(metadata_change_list),
198 has_changes); 198 has_changes);
199 return SyncError(); 199 return SyncError();
200 } 200 }
201 201
202 void DeviceInfoService::GetData(StorageKeyList storage_keys, 202 void DeviceInfoSyncBridge::GetData(StorageKeyList storage_keys,
203 DataCallback callback) { 203 DataCallback callback) {
204 auto batch = base::MakeUnique<MutableDataBatch>(); 204 auto batch = base::MakeUnique<MutableDataBatch>();
205 for (const auto& key : storage_keys) { 205 for (const auto& key : storage_keys) {
206 const auto& iter = all_data_.find(key); 206 const auto& iter = all_data_.find(key);
207 if (iter != all_data_.end()) { 207 if (iter != all_data_.end()) {
208 DCHECK_EQ(key, iter->second->cache_guid()); 208 DCHECK_EQ(key, iter->second->cache_guid());
209 batch->Put(key, CopyToEntityData(*iter->second)); 209 batch->Put(key, CopyToEntityData(*iter->second));
210 } 210 }
211 } 211 }
212 callback.Run(SyncError(), std::move(batch)); 212 callback.Run(SyncError(), std::move(batch));
213 } 213 }
214 214
215 void DeviceInfoService::GetAllData(DataCallback callback) { 215 void DeviceInfoSyncBridge::GetAllData(DataCallback callback) {
216 auto batch = base::MakeUnique<MutableDataBatch>(); 216 auto batch = base::MakeUnique<MutableDataBatch>();
217 for (const auto& kv : all_data_) { 217 for (const auto& kv : all_data_) {
218 batch->Put(kv.first, CopyToEntityData(*kv.second)); 218 batch->Put(kv.first, CopyToEntityData(*kv.second));
219 } 219 }
220 callback.Run(SyncError(), std::move(batch)); 220 callback.Run(SyncError(), std::move(batch));
221 } 221 }
222 222
223 std::string DeviceInfoService::GetClientTag(const EntityData& entity_data) { 223 std::string DeviceInfoSyncBridge::GetClientTag(const EntityData& entity_data) {
224 DCHECK(entity_data.specifics.has_device_info()); 224 DCHECK(entity_data.specifics.has_device_info());
225 return DeviceInfoUtil::SpecificsToTag(entity_data.specifics.device_info()); 225 return DeviceInfoUtil::SpecificsToTag(entity_data.specifics.device_info());
226 } 226 }
227 227
228 std::string DeviceInfoService::GetStorageKey(const EntityData& entity_data) { 228 std::string DeviceInfoSyncBridge::GetStorageKey(const EntityData& entity_data) {
229 DCHECK(entity_data.specifics.has_device_info()); 229 DCHECK(entity_data.specifics.has_device_info());
230 return entity_data.specifics.device_info().cache_guid(); 230 return entity_data.specifics.device_info().cache_guid();
231 } 231 }
232 232
233 void DeviceInfoService::DisableSync() { 233 void DeviceInfoSyncBridge::DisableSync() {
234 // TODO(skym, crbug.com/659263): Would it be reasonable to pulse_timer_.Stop() 234 // TODO(skym, crbug.com/659263): Would it be reasonable to pulse_timer_.Stop()
235 // or subscription_.reset() here? 235 // or subscription_.reset() here?
236 236
237 // Allow deletion of metadata to happen before the deletion of data below. If 237 // Allow deletion of metadata to happen before the deletion of data below. If
238 // we crash after removing metadata but not regular data, then merge can 238 // we crash after removing metadata but not regular data, then merge can
239 // handle pairing everything back up. 239 // handle pairing everything back up.
240 ModelTypeSyncBridge::DisableSync(); 240 ModelTypeSyncBridge::DisableSync();
241 241
242 // Remove all local data, if sync is being disabled, the user has expressed 242 // Remove all local data, if sync is being disabled, the user has expressed
243 // their desire to not have knowledge about other devices. 243 // their desire to not have knowledge about other devices.
244 if (!all_data_.empty()) { 244 if (!all_data_.empty()) {
245 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); 245 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch();
246 for (const auto& kv : all_data_) { 246 for (const auto& kv : all_data_) {
247 store_->DeleteData(batch.get(), kv.first); 247 store_->DeleteData(batch.get(), kv.first);
248 } 248 }
249 store_->CommitWriteBatch( 249 store_->CommitWriteBatch(
250 std::move(batch), 250 std::move(batch),
251 base::Bind(&DeviceInfoService::OnCommit, base::AsWeakPtr(this))); 251 base::Bind(&DeviceInfoSyncBridge::OnCommit, base::AsWeakPtr(this)));
252 252
253 all_data_.clear(); 253 all_data_.clear();
254 NotifyObservers(); 254 NotifyObservers();
255 } 255 }
256 } 256 }
257 257
258 bool DeviceInfoService::IsSyncing() const { 258 bool DeviceInfoSyncBridge::IsSyncing() const {
259 return !all_data_.empty(); 259 return !all_data_.empty();
260 } 260 }
261 261
262 std::unique_ptr<DeviceInfo> DeviceInfoService::GetDeviceInfo( 262 std::unique_ptr<DeviceInfo> DeviceInfoSyncBridge::GetDeviceInfo(
263 const std::string& client_id) const { 263 const std::string& client_id) const {
264 const ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id); 264 const ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id);
265 if (iter == all_data_.end()) { 265 if (iter == all_data_.end()) {
266 return std::unique_ptr<DeviceInfo>(); 266 return std::unique_ptr<DeviceInfo>();
267 } 267 }
268 return SpecificsToModel(*iter->second); 268 return SpecificsToModel(*iter->second);
269 } 269 }
270 270
271 std::vector<std::unique_ptr<DeviceInfo>> DeviceInfoService::GetAllDeviceInfo() 271 std::vector<std::unique_ptr<DeviceInfo>>
272 const { 272 DeviceInfoSyncBridge::GetAllDeviceInfo() const {
273 std::vector<std::unique_ptr<DeviceInfo>> list; 273 std::vector<std::unique_ptr<DeviceInfo>> list;
274 for (ClientIdToSpecifics::const_iterator iter = all_data_.begin(); 274 for (ClientIdToSpecifics::const_iterator iter = all_data_.begin();
275 iter != all_data_.end(); ++iter) { 275 iter != all_data_.end(); ++iter) {
276 list.push_back(SpecificsToModel(*iter->second)); 276 list.push_back(SpecificsToModel(*iter->second));
277 } 277 }
278 return list; 278 return list;
279 } 279 }
280 280
281 void DeviceInfoService::AddObserver(Observer* observer) { 281 void DeviceInfoSyncBridge::AddObserver(Observer* observer) {
282 observers_.AddObserver(observer); 282 observers_.AddObserver(observer);
283 } 283 }
284 284
285 void DeviceInfoService::RemoveObserver(Observer* observer) { 285 void DeviceInfoSyncBridge::RemoveObserver(Observer* observer) {
286 observers_.RemoveObserver(observer); 286 observers_.RemoveObserver(observer);
287 } 287 }
288 288
289 int DeviceInfoService::CountActiveDevices() const { 289 int DeviceInfoSyncBridge::CountActiveDevices() const {
290 return CountActiveDevices(Time::Now()); 290 return CountActiveDevices(Time::Now());
291 } 291 }
292 292
293 void DeviceInfoService::NotifyObservers() { 293 void DeviceInfoSyncBridge::NotifyObservers() {
294 for (auto& observer : observers_) 294 for (auto& observer : observers_)
295 observer.OnDeviceInfoChange(); 295 observer.OnDeviceInfoChange();
296 } 296 }
297 297
298 void DeviceInfoService::StoreSpecifics( 298 void DeviceInfoSyncBridge::StoreSpecifics(
299 std::unique_ptr<DeviceInfoSpecifics> specifics, 299 std::unique_ptr<DeviceInfoSpecifics> specifics,
300 WriteBatch* batch) { 300 WriteBatch* batch) {
301 const std::string guid = specifics->cache_guid(); 301 const std::string guid = specifics->cache_guid();
302 store_->WriteData(batch, guid, specifics->SerializeAsString()); 302 store_->WriteData(batch, guid, specifics->SerializeAsString());
303 all_data_[guid] = std::move(specifics); 303 all_data_[guid] = std::move(specifics);
304 } 304 }
305 305
306 bool DeviceInfoService::DeleteSpecifics(const std::string& guid, 306 bool DeviceInfoSyncBridge::DeleteSpecifics(const std::string& guid,
307 WriteBatch* batch) { 307 WriteBatch* batch) {
308 ClientIdToSpecifics::const_iterator iter = all_data_.find(guid); 308 ClientIdToSpecifics::const_iterator iter = all_data_.find(guid);
309 if (iter != all_data_.end()) { 309 if (iter != all_data_.end()) {
310 store_->DeleteData(batch, guid); 310 store_->DeleteData(batch, guid);
311 all_data_.erase(iter); 311 all_data_.erase(iter);
312 return true; 312 return true;
313 } else { 313 } else {
314 return false; 314 return false;
315 } 315 }
316 } 316 }
317 317
318 void DeviceInfoService::OnProviderInitialized() { 318 void DeviceInfoSyncBridge::OnProviderInitialized() {
319 // Now that the provider has initialized, remove the subscription. The service 319 // Now that the provider has initialized, remove the subscription. The bridge
320 // should only need to give the processor metadata upon initialization. If 320 // should only need to give the processor metadata upon initialization. If
321 // sync is disabled and enabled, our provider will try to retrigger this 321 // sync is disabled and enabled, our provider will try to retrigger this
322 // event, but we do not want to send any more metadata to the processor. 322 // event, but we do not want to send any more metadata to the processor.
323 subscription_.reset(); 323 subscription_.reset();
324 324
325 has_provider_initialized_ = true; 325 has_provider_initialized_ = true;
326 LoadMetadataIfReady(); 326 LoadMetadataIfReady();
327 } 327 }
328 328
329 void DeviceInfoService::OnStoreCreated(Result result, 329 void DeviceInfoSyncBridge::OnStoreCreated(
330 std::unique_ptr<ModelTypeStore> store) { 330 Result result,
331 std::unique_ptr<ModelTypeStore> store) {
331 if (result == Result::SUCCESS) { 332 if (result == Result::SUCCESS) {
332 std::swap(store_, store); 333 std::swap(store_, store);
333 store_->ReadAllData( 334 store_->ReadAllData(base::Bind(&DeviceInfoSyncBridge::OnReadAllData,
334 base::Bind(&DeviceInfoService::OnReadAllData, base::AsWeakPtr(this))); 335 base::AsWeakPtr(this)));
335 } else { 336 } else {
336 ReportStartupErrorToSync("ModelTypeStore creation failed."); 337 ReportStartupErrorToSync("ModelTypeStore creation failed.");
337 } 338 }
338 } 339 }
339 340
340 void DeviceInfoService::OnReadAllData(Result result, 341 void DeviceInfoSyncBridge::OnReadAllData(
341 std::unique_ptr<RecordList> record_list) { 342 Result result,
343 std::unique_ptr<RecordList> record_list) {
342 if (result != Result::SUCCESS) { 344 if (result != Result::SUCCESS) {
343 ReportStartupErrorToSync("Initial load of data failed."); 345 ReportStartupErrorToSync("Initial load of data failed.");
344 return; 346 return;
345 } 347 }
346 348
347 for (const Record& r : *record_list.get()) { 349 for (const Record& r : *record_list.get()) {
348 std::unique_ptr<DeviceInfoSpecifics> specifics = 350 std::unique_ptr<DeviceInfoSpecifics> specifics =
349 base::MakeUnique<DeviceInfoSpecifics>(); 351 base::MakeUnique<DeviceInfoSpecifics>();
350 if (specifics->ParseFromString(r.value)) { 352 if (specifics->ParseFromString(r.value)) {
351 all_data_[specifics->cache_guid()] = std::move(specifics); 353 all_data_[specifics->cache_guid()] = std::move(specifics);
352 } else { 354 } else {
353 ReportStartupErrorToSync("Failed to deserialize specifics."); 355 ReportStartupErrorToSync("Failed to deserialize specifics.");
354 } 356 }
355 } 357 }
356 358
357 has_data_loaded_ = true; 359 has_data_loaded_ = true;
358 LoadMetadataIfReady(); 360 LoadMetadataIfReady();
359 } 361 }
360 362
361 void DeviceInfoService::LoadMetadataIfReady() { 363 void DeviceInfoSyncBridge::LoadMetadataIfReady() {
362 if (has_data_loaded_ && has_provider_initialized_) { 364 if (has_data_loaded_ && has_provider_initialized_) {
363 store_->ReadAllMetadata(base::Bind(&DeviceInfoService::OnReadAllMetadata, 365 store_->ReadAllMetadata(base::Bind(&DeviceInfoSyncBridge::OnReadAllMetadata,
364 base::AsWeakPtr(this))); 366 base::AsWeakPtr(this)));
365 } 367 }
366 } 368 }
367 369
368 void DeviceInfoService::OnReadAllMetadata( 370 void DeviceInfoSyncBridge::OnReadAllMetadata(
369 Result result, 371 Result result,
370 std::unique_ptr<RecordList> metadata_records, 372 std::unique_ptr<RecordList> metadata_records,
371 const std::string& global_metadata) { 373 const std::string& global_metadata) {
372 if (result != Result::SUCCESS) { 374 if (result != Result::SUCCESS) {
373 ReportStartupErrorToSync("Load of metadata completely failed."); 375 ReportStartupErrorToSync("Load of metadata completely failed.");
374 return; 376 return;
375 } 377 }
376 378
377 auto batch = base::MakeUnique<MetadataBatch>(); 379 auto batch = base::MakeUnique<MetadataBatch>();
378 ModelTypeState state; 380 ModelTypeState state;
(...skipping 10 matching lines...) Expand all
389 batch->AddMetadata(r.id, entity_metadata); 391 batch->AddMetadata(r.id, entity_metadata);
390 } else { 392 } else {
391 ReportStartupErrorToSync("Failed to deserialize entity metadata."); 393 ReportStartupErrorToSync("Failed to deserialize entity metadata.");
392 } 394 }
393 } 395 }
394 396
395 change_processor()->OnMetadataLoaded(SyncError(), std::move(batch)); 397 change_processor()->OnMetadataLoaded(SyncError(), std::move(batch));
396 ReconcileLocalAndStored(); 398 ReconcileLocalAndStored();
397 } 399 }
398 400
399 void DeviceInfoService::OnCommit(Result result) { 401 void DeviceInfoSyncBridge::OnCommit(Result result) {
400 if (result != Result::SUCCESS) { 402 if (result != Result::SUCCESS) {
401 change_processor()->CreateAndUploadError(FROM_HERE, 403 change_processor()->CreateAndUploadError(FROM_HERE,
402 "Failed a write to store."); 404 "Failed a write to store.");
403 } 405 }
404 } 406 }
405 407
406 void DeviceInfoService::ReconcileLocalAndStored() { 408 void DeviceInfoSyncBridge::ReconcileLocalAndStored() {
407 // On initial syncing we will have a change processor here, but it will not be 409 // On initial syncing we will have a change processor here, but it will not be
408 // tracking changes. We need to persist a copy of our local device info to 410 // tracking changes. We need to persist a copy of our local device info to
409 // disk, but the Put call to the processor will be ignored. That should be 411 // disk, but the Put call to the processor will be ignored. That should be
410 // fine however, as the discrepancy will be picked up later in merge. We don't 412 // fine however, as the discrepancy will be picked up later in merge. We don't
411 // bother trying to track this case and act intelligently because simply not 413 // bother trying to track this case and act intelligently because simply not
412 // much of a benefit in doing so. 414 // much of a benefit in doing so.
413 DCHECK(has_provider_initialized_); 415 DCHECK(has_provider_initialized_);
414 416
415 const DeviceInfo* current_info = 417 const DeviceInfo* current_info =
416 local_device_info_provider_->GetLocalDeviceInfo(); 418 local_device_info_provider_->GetLocalDeviceInfo();
417 auto iter = all_data_.find(current_info->guid()); 419 auto iter = all_data_.find(current_info->guid());
418 420
419 // Convert to DeviceInfo for Equals function. 421 // Convert to DeviceInfo for Equals function.
420 if (iter != all_data_.end() && 422 if (iter != all_data_.end() &&
421 current_info->Equals(*SpecificsToModel(*iter->second))) { 423 current_info->Equals(*SpecificsToModel(*iter->second))) {
422 const TimeDelta pulse_delay(DeviceInfoUtil::CalculatePulseDelay( 424 const TimeDelta pulse_delay(DeviceInfoUtil::CalculatePulseDelay(
423 GetLastUpdateTime(*iter->second), Time::Now())); 425 GetLastUpdateTime(*iter->second), Time::Now()));
424 if (!pulse_delay.is_zero()) { 426 if (!pulse_delay.is_zero()) {
425 pulse_timer_.Start(FROM_HERE, pulse_delay, 427 pulse_timer_.Start(FROM_HERE, pulse_delay,
426 base::Bind(&DeviceInfoService::SendLocalData, 428 base::Bind(&DeviceInfoSyncBridge::SendLocalData,
427 base::Unretained(this))); 429 base::Unretained(this)));
428 return; 430 return;
429 } 431 }
430 } 432 }
431 SendLocalData(); 433 SendLocalData();
432 } 434 }
433 435
434 void DeviceInfoService::SendLocalData() { 436 void DeviceInfoSyncBridge::SendLocalData() {
435 DCHECK(has_provider_initialized_); 437 DCHECK(has_provider_initialized_);
436 438
437 // It is possible that the provider no longer has data for us, such as when 439 // It is possible that the provider no longer has data for us, such as when
438 // the user signs out. No-op this pulse, but keep the timer going in case sync 440 // the user signs out. No-op this pulse, but keep the timer going in case sync
439 // is enabled later. 441 // is enabled later.
440 if (local_device_info_provider_->GetLocalDeviceInfo() != nullptr) { 442 if (local_device_info_provider_->GetLocalDeviceInfo() != nullptr) {
441 std::unique_ptr<DeviceInfoSpecifics> specifics = 443 std::unique_ptr<DeviceInfoSpecifics> specifics =
442 ModelToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo(), 444 ModelToSpecifics(*local_device_info_provider_->GetLocalDeviceInfo(),
443 TimeToProtoTime(Time::Now())); 445 TimeToProtoTime(Time::Now()));
444 std::unique_ptr<MetadataChangeList> metadata_change_list = 446 std::unique_ptr<MetadataChangeList> metadata_change_list =
445 CreateMetadataChangeList(); 447 CreateMetadataChangeList();
446 if (change_processor()->IsTrackingMetadata()) { 448 if (change_processor()->IsTrackingMetadata()) {
447 change_processor()->Put(specifics->cache_guid(), 449 change_processor()->Put(specifics->cache_guid(),
448 CopyToEntityData(*specifics), 450 CopyToEntityData(*specifics),
449 metadata_change_list.get()); 451 metadata_change_list.get());
450 } 452 }
451 453
452 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch(); 454 std::unique_ptr<WriteBatch> batch = store_->CreateWriteBatch();
453 StoreSpecifics(std::move(specifics), batch.get()); 455 StoreSpecifics(std::move(specifics), batch.get());
454 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true); 456 CommitAndNotify(std::move(batch), std::move(metadata_change_list), true);
455 } 457 }
456 458
457 pulse_timer_.Start( 459 pulse_timer_.Start(
458 FROM_HERE, DeviceInfoUtil::kPulseInterval, 460 FROM_HERE, DeviceInfoUtil::kPulseInterval,
459 base::Bind(&DeviceInfoService::SendLocalData, base::Unretained(this))); 461 base::Bind(&DeviceInfoSyncBridge::SendLocalData, base::Unretained(this)));
460 } 462 }
461 463
462 void DeviceInfoService::CommitAndNotify( 464 void DeviceInfoSyncBridge::CommitAndNotify(
463 std::unique_ptr<WriteBatch> batch, 465 std::unique_ptr<WriteBatch> batch,
464 std::unique_ptr<MetadataChangeList> metadata_change_list, 466 std::unique_ptr<MetadataChangeList> metadata_change_list,
465 bool should_notify) { 467 bool should_notify) {
466 static_cast<SimpleMetadataChangeList*>(metadata_change_list.get()) 468 static_cast<SimpleMetadataChangeList*>(metadata_change_list.get())
467 ->TransferChanges(store_.get(), batch.get()); 469 ->TransferChanges(store_.get(), batch.get());
468 store_->CommitWriteBatch( 470 store_->CommitWriteBatch(
469 std::move(batch), 471 std::move(batch),
470 base::Bind(&DeviceInfoService::OnCommit, base::AsWeakPtr(this))); 472 base::Bind(&DeviceInfoSyncBridge::OnCommit, base::AsWeakPtr(this)));
471 if (should_notify) { 473 if (should_notify) {
472 NotifyObservers(); 474 NotifyObservers();
473 } 475 }
474 } 476 }
475 477
476 int DeviceInfoService::CountActiveDevices(const Time now) const { 478 int DeviceInfoSyncBridge::CountActiveDevices(const Time now) const {
477 return std::count_if(all_data_.begin(), all_data_.end(), 479 return std::count_if(all_data_.begin(), all_data_.end(),
478 [now](ClientIdToSpecifics::const_reference pair) { 480 [now](ClientIdToSpecifics::const_reference pair) {
479 return DeviceInfoUtil::IsActive( 481 return DeviceInfoUtil::IsActive(
480 GetLastUpdateTime(*pair.second), now); 482 GetLastUpdateTime(*pair.second), now);
481 }); 483 });
482 } 484 }
483 485
484 void DeviceInfoService::ReportStartupErrorToSync(const std::string& msg) { 486 void DeviceInfoSyncBridge::ReportStartupErrorToSync(const std::string& msg) {
485 // TODO(skym): Shouldn't need to log this here, reporting should always log. 487 // TODO(skym): Shouldn't need to log this here, reporting should always log.
486 LOG(WARNING) << msg; 488 LOG(WARNING) << msg;
487 change_processor()->OnMetadataLoaded( 489 change_processor()->OnMetadataLoaded(
488 change_processor()->CreateAndUploadError(FROM_HERE, msg), nullptr); 490 change_processor()->CreateAndUploadError(FROM_HERE, msg), nullptr);
489 } 491 }
490 492
491 } // namespace syncer 493 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/device_info/device_info_sync_bridge.h ('k') | components/sync/device_info/device_info_sync_bridge_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698