| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_sync_service.h" | 5 #include "components/sync/driver/device_info_sync_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "components/sync/api/sync_change.h" | 14 #include "components/sync/api/sync_change.h" |
| 15 #include "components/sync/base/time.h" | 15 #include "components/sync/base/time.h" |
| 16 #include "components/sync/driver/device_info_util.h" |
| 17 #include "components/sync/driver/local_device_info_provider.h" |
| 16 #include "components/sync/protocol/sync.pb.h" | 18 #include "components/sync/protocol/sync.pb.h" |
| 17 #include "components/sync_driver/device_info_util.h" | |
| 18 #include "components/sync_driver/local_device_info_provider.h" | |
| 19 | 19 |
| 20 namespace sync_driver { | 20 namespace sync_driver { |
| 21 | 21 |
| 22 using base::Time; | 22 using base::Time; |
| 23 using base::TimeDelta; | 23 using base::TimeDelta; |
| 24 using syncer::ModelType; | 24 using syncer::ModelType; |
| 25 using syncer::SyncChange; | 25 using syncer::SyncChange; |
| 26 using syncer::SyncChangeList; | 26 using syncer::SyncChangeList; |
| 27 using syncer::SyncChangeProcessor; | 27 using syncer::SyncChangeProcessor; |
| 28 using syncer::SyncData; | 28 using syncer::SyncData; |
| 29 using syncer::SyncDataList; | 29 using syncer::SyncDataList; |
| 30 using syncer::SyncErrorFactory; | 30 using syncer::SyncErrorFactory; |
| 31 using syncer::SyncMergeResult; | 31 using syncer::SyncMergeResult; |
| 32 | 32 |
| 33 DeviceInfoSyncService::DeviceInfoSyncService( | 33 DeviceInfoSyncService::DeviceInfoSyncService( |
| 34 LocalDeviceInfoProvider* local_device_info_provider) | 34 LocalDeviceInfoProvider* local_device_info_provider) |
| 35 : local_device_info_provider_(local_device_info_provider) { | 35 : local_device_info_provider_(local_device_info_provider) { |
| 36 DCHECK(local_device_info_provider); | 36 DCHECK(local_device_info_provider); |
| 37 } | 37 } |
| 38 | 38 |
| 39 DeviceInfoSyncService::~DeviceInfoSyncService() { | 39 DeviceInfoSyncService::~DeviceInfoSyncService() {} |
| 40 } | |
| 41 | 40 |
| 42 SyncMergeResult DeviceInfoSyncService::MergeDataAndStartSyncing( | 41 SyncMergeResult DeviceInfoSyncService::MergeDataAndStartSyncing( |
| 43 ModelType type, | 42 ModelType type, |
| 44 const SyncDataList& initial_sync_data, | 43 const SyncDataList& initial_sync_data, |
| 45 std::unique_ptr<SyncChangeProcessor> sync_processor, | 44 std::unique_ptr<SyncChangeProcessor> sync_processor, |
| 46 std::unique_ptr<SyncErrorFactory> error_handler) { | 45 std::unique_ptr<SyncErrorFactory> error_handler) { |
| 47 DCHECK(sync_processor.get()); | 46 DCHECK(sync_processor.get()); |
| 48 DCHECK(error_handler.get()); | 47 DCHECK(error_handler.get()); |
| 49 DCHECK_EQ(type, syncer::DEVICE_INFO); | 48 DCHECK_EQ(type, syncer::DEVICE_INFO); |
| 50 | 49 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 63 // |change_type| defaults to ADD and might be changed to | 62 // |change_type| defaults to ADD and might be changed to |
| 64 // UPDATE to INVALID down below if the initial data contains | 63 // UPDATE to INVALID down below if the initial data contains |
| 65 // data matching the local device ID. | 64 // data matching the local device ID. |
| 66 SyncChange::SyncChangeType change_type = SyncChange::ACTION_ADD; | 65 SyncChange::SyncChangeType change_type = SyncChange::ACTION_ADD; |
| 67 TimeDelta pulse_delay; | 66 TimeDelta pulse_delay; |
| 68 size_t num_items_new = 0; | 67 size_t num_items_new = 0; |
| 69 size_t num_items_updated = 0; | 68 size_t num_items_updated = 0; |
| 70 | 69 |
| 71 // Iterate over all initial sync data and copy it to the cache. | 70 // Iterate over all initial sync data and copy it to the cache. |
| 72 for (SyncDataList::const_iterator iter = initial_sync_data.begin(); | 71 for (SyncDataList::const_iterator iter = initial_sync_data.begin(); |
| 73 iter != initial_sync_data.end(); | 72 iter != initial_sync_data.end(); ++iter) { |
| 74 ++iter) { | |
| 75 DCHECK_EQ(syncer::DEVICE_INFO, iter->GetDataType()); | 73 DCHECK_EQ(syncer::DEVICE_INFO, iter->GetDataType()); |
| 76 | 74 |
| 77 const std::string& id = iter->GetSpecifics().device_info().cache_guid(); | 75 const std::string& id = iter->GetSpecifics().device_info().cache_guid(); |
| 78 | 76 |
| 79 if (id == local_device_info->guid()) { | 77 if (id == local_device_info->guid()) { |
| 80 // |initial_sync_data| contains data matching the local device. | 78 // |initial_sync_data| contains data matching the local device. |
| 81 std::unique_ptr<DeviceInfo> synced_local_device_info = | 79 std::unique_ptr<DeviceInfo> synced_local_device_info = |
| 82 base::WrapUnique(CreateDeviceInfo(*iter)); | 80 base::WrapUnique(CreateDeviceInfo(*iter)); |
| 83 | 81 |
| 84 pulse_delay = DeviceInfoUtil::CalculatePulseDelay( | 82 pulse_delay = DeviceInfoUtil::CalculatePulseDelay( |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 if (was_syncing) { | 142 if (was_syncing) { |
| 145 NotifyObservers(); | 143 NotifyObservers(); |
| 146 } | 144 } |
| 147 } | 145 } |
| 148 | 146 |
| 149 SyncDataList DeviceInfoSyncService::GetAllSyncData( | 147 SyncDataList DeviceInfoSyncService::GetAllSyncData( |
| 150 syncer::ModelType type) const { | 148 syncer::ModelType type) const { |
| 151 SyncDataList list; | 149 SyncDataList list; |
| 152 | 150 |
| 153 for (SyncDataMap::const_iterator iter = all_data_.begin(); | 151 for (SyncDataMap::const_iterator iter = all_data_.begin(); |
| 154 iter != all_data_.end(); | 152 iter != all_data_.end(); ++iter) { |
| 155 ++iter) { | |
| 156 list.push_back(iter->second); | 153 list.push_back(iter->second); |
| 157 } | 154 } |
| 158 | 155 |
| 159 return list; | 156 return list; |
| 160 } | 157 } |
| 161 | 158 |
| 162 syncer::SyncError DeviceInfoSyncService::ProcessSyncChanges( | 159 syncer::SyncError DeviceInfoSyncService::ProcessSyncChanges( |
| 163 const tracked_objects::Location& from_here, | 160 const tracked_objects::Location& from_here, |
| 164 const SyncChangeList& change_list) { | 161 const SyncChangeList& change_list) { |
| 165 syncer::SyncError error; | 162 syncer::SyncError error; |
| 166 | 163 |
| 167 DCHECK(local_device_info_provider_->GetLocalDeviceInfo()); | 164 DCHECK(local_device_info_provider_->GetLocalDeviceInfo()); |
| 168 const std::string& local_device_id = | 165 const std::string& local_device_id = |
| 169 local_device_info_provider_->GetLocalDeviceInfo()->guid(); | 166 local_device_info_provider_->GetLocalDeviceInfo()->guid(); |
| 170 | 167 |
| 171 bool has_changes = false; | 168 bool has_changes = false; |
| 172 | 169 |
| 173 // Iterate over all chanages and merge entries. | 170 // Iterate over all chanages and merge entries. |
| 174 for (SyncChangeList::const_iterator iter = change_list.begin(); | 171 for (SyncChangeList::const_iterator iter = change_list.begin(); |
| 175 iter != change_list.end(); | 172 iter != change_list.end(); ++iter) { |
| 176 ++iter) { | |
| 177 const SyncData& sync_data = iter->sync_data(); | 173 const SyncData& sync_data = iter->sync_data(); |
| 178 DCHECK_EQ(syncer::DEVICE_INFO, sync_data.GetDataType()); | 174 DCHECK_EQ(syncer::DEVICE_INFO, sync_data.GetDataType()); |
| 179 | 175 |
| 180 const std::string& client_id = | 176 const std::string& client_id = |
| 181 sync_data.GetSpecifics().device_info().cache_guid(); | 177 sync_data.GetSpecifics().device_info().cache_guid(); |
| 182 // Ignore device info matching the local device. | 178 // Ignore device info matching the local device. |
| 183 if (local_device_id == client_id) { | 179 if (local_device_id == client_id) { |
| 184 DVLOG(1) << "Ignoring sync changes for the local DEVICE_INFO"; | 180 DVLOG(1) << "Ignoring sync changes for the local DEVICE_INFO"; |
| 185 continue; | 181 continue; |
| 186 } | 182 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 211 return std::unique_ptr<DeviceInfo>(); | 207 return std::unique_ptr<DeviceInfo>(); |
| 212 } | 208 } |
| 213 | 209 |
| 214 return base::WrapUnique(CreateDeviceInfo(iter->second)); | 210 return base::WrapUnique(CreateDeviceInfo(iter->second)); |
| 215 } | 211 } |
| 216 | 212 |
| 217 ScopedVector<DeviceInfo> DeviceInfoSyncService::GetAllDeviceInfo() const { | 213 ScopedVector<DeviceInfo> DeviceInfoSyncService::GetAllDeviceInfo() const { |
| 218 ScopedVector<DeviceInfo> list; | 214 ScopedVector<DeviceInfo> list; |
| 219 | 215 |
| 220 for (SyncDataMap::const_iterator iter = all_data_.begin(); | 216 for (SyncDataMap::const_iterator iter = all_data_.begin(); |
| 221 iter != all_data_.end(); | 217 iter != all_data_.end(); ++iter) { |
| 222 ++iter) { | |
| 223 list.push_back(CreateDeviceInfo(iter->second)); | 218 list.push_back(CreateDeviceInfo(iter->second)); |
| 224 } | 219 } |
| 225 | 220 |
| 226 return list; | 221 return list; |
| 227 } | 222 } |
| 228 | 223 |
| 229 void DeviceInfoSyncService::AddObserver(Observer* observer) { | 224 void DeviceInfoSyncService::AddObserver(Observer* observer) { |
| 230 observers_.AddObserver(observer); | 225 observers_.AddObserver(observer); |
| 231 } | 226 } |
| 232 | 227 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 262 const sync_pb::DeviceInfoSpecifics& specifics = entity.device_info(); | 257 const sync_pb::DeviceInfoSpecifics& specifics = entity.device_info(); |
| 263 return SyncData::CreateLocalData(DeviceInfoUtil::SpecificsToTag(specifics), | 258 return SyncData::CreateLocalData(DeviceInfoUtil::SpecificsToTag(specifics), |
| 264 specifics.client_name(), entity); | 259 specifics.client_name(), entity); |
| 265 } | 260 } |
| 266 | 261 |
| 267 DeviceInfo* DeviceInfoSyncService::CreateDeviceInfo( | 262 DeviceInfo* DeviceInfoSyncService::CreateDeviceInfo( |
| 268 const syncer::SyncData& sync_data) { | 263 const syncer::SyncData& sync_data) { |
| 269 const sync_pb::DeviceInfoSpecifics& specifics = | 264 const sync_pb::DeviceInfoSpecifics& specifics = |
| 270 sync_data.GetSpecifics().device_info(); | 265 sync_data.GetSpecifics().device_info(); |
| 271 | 266 |
| 272 return new DeviceInfo(specifics.cache_guid(), | 267 return new DeviceInfo(specifics.cache_guid(), specifics.client_name(), |
| 273 specifics.client_name(), | 268 specifics.chrome_version(), specifics.sync_user_agent(), |
| 274 specifics.chrome_version(), | |
| 275 specifics.sync_user_agent(), | |
| 276 specifics.device_type(), | 269 specifics.device_type(), |
| 277 specifics.signin_scoped_device_id()); | 270 specifics.signin_scoped_device_id()); |
| 278 } | 271 } |
| 279 | 272 |
| 280 void DeviceInfoSyncService::StoreSyncData(const std::string& client_id, | 273 void DeviceInfoSyncService::StoreSyncData(const std::string& client_id, |
| 281 const SyncData& sync_data) { | 274 const SyncData& sync_data) { |
| 282 DVLOG(1) << "Storing DEVICE_INFO for " | 275 DVLOG(1) << "Storing DEVICE_INFO for " |
| 283 << sync_data.GetSpecifics().device_info().client_name() | 276 << sync_data.GetSpecifics().device_info().client_name() |
| 284 << " with ID " << client_id; | 277 << " with ID " << client_id; |
| 285 all_data_[client_id] = sync_data; | 278 all_data_[client_id] = sync_data; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 return syncer::SyncDataRemote(device_info).GetModifiedTime(); | 326 return syncer::SyncDataRemote(device_info).GetModifiedTime(); |
| 334 } else { | 327 } else { |
| 335 // We shouldn't reach this point for remote data, so this means we're likely | 328 // We shouldn't reach this point for remote data, so this means we're likely |
| 336 // looking at the local device info. Using a long ago time is perfect, since | 329 // looking at the local device info. Using a long ago time is perfect, since |
| 337 // the desired behavior is to update/pulse our data as soon as possible. | 330 // the desired behavior is to update/pulse our data as soon as possible. |
| 338 return Time(); | 331 return Time(); |
| 339 } | 332 } |
| 340 } | 333 } |
| 341 | 334 |
| 342 } // namespace sync_driver | 335 } // namespace sync_driver |
| OLD | NEW |