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/device_info/device_info_sync_service.h" | 5 #include "components/sync/device_info/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/device_info/device_info_util.h" | 16 #include "components/sync/device_info/device_info_util.h" |
17 #include "components/sync/device_info/local_device_info_provider.h" | 17 #include "components/sync/device_info/local_device_info_provider.h" |
18 #include "components/sync/protocol/sync.pb.h" | 18 #include "components/sync/protocol/sync.pb.h" |
19 | 19 |
20 namespace sync_driver { | 20 namespace syncer { |
21 | 21 |
22 using base::Time; | 22 using base::Time; |
23 using base::TimeDelta; | 23 using base::TimeDelta; |
24 using syncer::ModelType; | |
25 using syncer::SyncChange; | |
26 using syncer::SyncChangeList; | |
27 using syncer::SyncChangeProcessor; | |
28 using syncer::SyncData; | |
29 using syncer::SyncDataList; | |
30 using syncer::SyncErrorFactory; | |
31 using syncer::SyncMergeResult; | |
32 | 24 |
33 DeviceInfoSyncService::DeviceInfoSyncService( | 25 DeviceInfoSyncService::DeviceInfoSyncService( |
34 LocalDeviceInfoProvider* local_device_info_provider) | 26 LocalDeviceInfoProvider* local_device_info_provider) |
35 : local_device_info_provider_(local_device_info_provider) { | 27 : local_device_info_provider_(local_device_info_provider) { |
36 DCHECK(local_device_info_provider); | 28 DCHECK(local_device_info_provider); |
37 } | 29 } |
38 | 30 |
39 DeviceInfoSyncService::~DeviceInfoSyncService() {} | 31 DeviceInfoSyncService::~DeviceInfoSyncService() {} |
40 | 32 |
41 SyncMergeResult DeviceInfoSyncService::MergeDataAndStartSyncing( | 33 SyncMergeResult DeviceInfoSyncService::MergeDataAndStartSyncing( |
42 ModelType type, | 34 ModelType type, |
43 const SyncDataList& initial_sync_data, | 35 const SyncDataList& initial_sync_data, |
44 std::unique_ptr<SyncChangeProcessor> sync_processor, | 36 std::unique_ptr<SyncChangeProcessor> sync_processor, |
45 std::unique_ptr<SyncErrorFactory> error_handler) { | 37 std::unique_ptr<SyncErrorFactory> error_handler) { |
46 DCHECK(sync_processor.get()); | 38 DCHECK(sync_processor.get()); |
47 DCHECK(error_handler.get()); | 39 DCHECK(error_handler.get()); |
48 DCHECK_EQ(type, syncer::DEVICE_INFO); | 40 DCHECK_EQ(type, DEVICE_INFO); |
49 | 41 |
50 DCHECK(!IsSyncing()); | 42 DCHECK(!IsSyncing()); |
51 | 43 |
52 sync_processor_ = std::move(sync_processor); | 44 sync_processor_ = std::move(sync_processor); |
53 error_handler_ = std::move(error_handler); | 45 error_handler_ = std::move(error_handler); |
54 | 46 |
55 // Initialization should be completed before this type is enabled | 47 // Initialization should be completed before this type is enabled |
56 // and local device info must be available. | 48 // and local device info must be available. |
57 const DeviceInfo* local_device_info = | 49 const DeviceInfo* local_device_info = |
58 local_device_info_provider_->GetLocalDeviceInfo(); | 50 local_device_info_provider_->GetLocalDeviceInfo(); |
59 DCHECK(local_device_info != NULL); | 51 DCHECK(local_device_info != NULL); |
60 | 52 |
61 // Indicates whether a local device has been added or updated. | 53 // Indicates whether a local device has been added or updated. |
62 // |change_type| defaults to ADD and might be changed to | 54 // |change_type| defaults to ADD and might be changed to |
63 // UPDATE to INVALID down below if the initial data contains | 55 // UPDATE to INVALID down below if the initial data contains |
64 // data matching the local device ID. | 56 // data matching the local device ID. |
65 SyncChange::SyncChangeType change_type = SyncChange::ACTION_ADD; | 57 SyncChange::SyncChangeType change_type = SyncChange::ACTION_ADD; |
66 TimeDelta pulse_delay; | 58 TimeDelta pulse_delay; |
67 size_t num_items_new = 0; | 59 size_t num_items_new = 0; |
68 size_t num_items_updated = 0; | 60 size_t num_items_updated = 0; |
69 | 61 |
70 // Iterate over all initial sync data and copy it to the cache. | 62 // Iterate over all initial sync data and copy it to the cache. |
71 for (SyncDataList::const_iterator iter = initial_sync_data.begin(); | 63 for (SyncDataList::const_iterator iter = initial_sync_data.begin(); |
72 iter != initial_sync_data.end(); ++iter) { | 64 iter != initial_sync_data.end(); ++iter) { |
73 DCHECK_EQ(syncer::DEVICE_INFO, iter->GetDataType()); | 65 DCHECK_EQ(DEVICE_INFO, iter->GetDataType()); |
74 | 66 |
75 const std::string& id = iter->GetSpecifics().device_info().cache_guid(); | 67 const std::string& id = iter->GetSpecifics().device_info().cache_guid(); |
76 | 68 |
77 if (id == local_device_info->guid()) { | 69 if (id == local_device_info->guid()) { |
78 // |initial_sync_data| contains data matching the local device. | 70 // |initial_sync_data| contains data matching the local device. |
79 std::unique_ptr<DeviceInfo> synced_local_device_info = | 71 std::unique_ptr<DeviceInfo> synced_local_device_info = |
80 base::WrapUnique(CreateDeviceInfo(*iter)); | 72 base::WrapUnique(CreateDeviceInfo(*iter)); |
81 | 73 |
82 pulse_delay = DeviceInfoUtil::CalculatePulseDelay( | 74 pulse_delay = DeviceInfoUtil::CalculatePulseDelay( |
83 GetLastUpdateTime(*iter), Time::Now()); | 75 GetLastUpdateTime(*iter), Time::Now()); |
(...skipping 10 matching lines...) Expand all Loading... |
94 continue; | 86 continue; |
95 } | 87 } |
96 } else { | 88 } else { |
97 // A new device that doesn't match the local device. | 89 // A new device that doesn't match the local device. |
98 num_items_new++; | 90 num_items_new++; |
99 } | 91 } |
100 | 92 |
101 StoreSyncData(id, *iter); | 93 StoreSyncData(id, *iter); |
102 } | 94 } |
103 | 95 |
104 syncer::SyncMergeResult result(type); | 96 SyncMergeResult result(type); |
105 | 97 |
106 // If the SyncData for the local device is new or different then send it | 98 // If the SyncData for the local device is new or different then send it |
107 // immediately, otherwise wait until the pulse interval has elapsed from the | 99 // immediately, otherwise wait until the pulse interval has elapsed from the |
108 // previous update. Regardless of the branch here we setup a timer loop with | 100 // previous update. Regardless of the branch here we setup a timer loop with |
109 // SendLocalData such that we continue pulsing every interval. | 101 // SendLocalData such that we continue pulsing every interval. |
110 if (change_type == SyncChange::ACTION_INVALID) { | 102 if (change_type == SyncChange::ACTION_INVALID) { |
111 pulse_timer_.Start( | 103 pulse_timer_.Start( |
112 FROM_HERE, pulse_delay, | 104 FROM_HERE, pulse_delay, |
113 base::Bind(&DeviceInfoSyncService::SendLocalData, | 105 base::Bind(&DeviceInfoSyncService::SendLocalData, |
114 base::Unretained(this), SyncChange::ACTION_UPDATE)); | 106 base::Unretained(this), SyncChange::ACTION_UPDATE)); |
115 } else { | 107 } else { |
116 SendLocalData(change_type); | 108 SendLocalData(change_type); |
117 } | 109 } |
118 | 110 |
119 result.set_num_items_before_association(1); | 111 result.set_num_items_before_association(1); |
120 result.set_num_items_after_association(all_data_.size()); | 112 result.set_num_items_after_association(all_data_.size()); |
121 result.set_num_items_added(num_items_new); | 113 result.set_num_items_added(num_items_new); |
122 result.set_num_items_modified(num_items_updated); | 114 result.set_num_items_modified(num_items_updated); |
123 result.set_num_items_deleted(0); | 115 result.set_num_items_deleted(0); |
124 | 116 |
125 NotifyObservers(); | 117 NotifyObservers(); |
126 | 118 |
127 return result; | 119 return result; |
128 } | 120 } |
129 | 121 |
130 bool DeviceInfoSyncService::IsSyncing() const { | 122 bool DeviceInfoSyncService::IsSyncing() const { |
131 return !all_data_.empty(); | 123 return !all_data_.empty(); |
132 } | 124 } |
133 | 125 |
134 void DeviceInfoSyncService::StopSyncing(syncer::ModelType type) { | 126 void DeviceInfoSyncService::StopSyncing(ModelType type) { |
135 bool was_syncing = IsSyncing(); | 127 bool was_syncing = IsSyncing(); |
136 | 128 |
137 pulse_timer_.Stop(); | 129 pulse_timer_.Stop(); |
138 all_data_.clear(); | 130 all_data_.clear(); |
139 sync_processor_.reset(); | 131 sync_processor_.reset(); |
140 error_handler_.reset(); | 132 error_handler_.reset(); |
141 | 133 |
142 if (was_syncing) { | 134 if (was_syncing) { |
143 NotifyObservers(); | 135 NotifyObservers(); |
144 } | 136 } |
145 } | 137 } |
146 | 138 |
147 SyncDataList DeviceInfoSyncService::GetAllSyncData( | 139 SyncDataList DeviceInfoSyncService::GetAllSyncData(ModelType type) const { |
148 syncer::ModelType type) const { | |
149 SyncDataList list; | 140 SyncDataList list; |
150 | 141 |
151 for (SyncDataMap::const_iterator iter = all_data_.begin(); | 142 for (SyncDataMap::const_iterator iter = all_data_.begin(); |
152 iter != all_data_.end(); ++iter) { | 143 iter != all_data_.end(); ++iter) { |
153 list.push_back(iter->second); | 144 list.push_back(iter->second); |
154 } | 145 } |
155 | 146 |
156 return list; | 147 return list; |
157 } | 148 } |
158 | 149 |
159 syncer::SyncError DeviceInfoSyncService::ProcessSyncChanges( | 150 SyncError DeviceInfoSyncService::ProcessSyncChanges( |
160 const tracked_objects::Location& from_here, | 151 const tracked_objects::Location& from_here, |
161 const SyncChangeList& change_list) { | 152 const SyncChangeList& change_list) { |
162 syncer::SyncError error; | 153 SyncError error; |
163 | 154 |
164 DCHECK(local_device_info_provider_->GetLocalDeviceInfo()); | 155 DCHECK(local_device_info_provider_->GetLocalDeviceInfo()); |
165 const std::string& local_device_id = | 156 const std::string& local_device_id = |
166 local_device_info_provider_->GetLocalDeviceInfo()->guid(); | 157 local_device_info_provider_->GetLocalDeviceInfo()->guid(); |
167 | 158 |
168 bool has_changes = false; | 159 bool has_changes = false; |
169 | 160 |
170 // Iterate over all chanages and merge entries. | 161 // Iterate over all chanages and merge entries. |
171 for (SyncChangeList::const_iterator iter = change_list.begin(); | 162 for (SyncChangeList::const_iterator iter = change_list.begin(); |
172 iter != change_list.end(); ++iter) { | 163 iter != change_list.end(); ++iter) { |
173 const SyncData& sync_data = iter->sync_data(); | 164 const SyncData& sync_data = iter->sync_data(); |
174 DCHECK_EQ(syncer::DEVICE_INFO, sync_data.GetDataType()); | 165 DCHECK_EQ(DEVICE_INFO, sync_data.GetDataType()); |
175 | 166 |
176 const std::string& client_id = | 167 const std::string& client_id = |
177 sync_data.GetSpecifics().device_info().cache_guid(); | 168 sync_data.GetSpecifics().device_info().cache_guid(); |
178 // Ignore device info matching the local device. | 169 // Ignore device info matching the local device. |
179 if (local_device_id == client_id) { | 170 if (local_device_id == client_id) { |
180 DVLOG(1) << "Ignoring sync changes for the local DEVICE_INFO"; | 171 DVLOG(1) << "Ignoring sync changes for the local DEVICE_INFO"; |
181 continue; | 172 continue; |
182 } | 173 } |
183 | 174 |
184 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) { | 175 if (iter->change_type() == SyncChange::ACTION_DELETE) { |
185 has_changes = true; | 176 has_changes = true; |
186 DeleteSyncData(client_id); | 177 DeleteSyncData(client_id); |
187 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE || | 178 } else if (iter->change_type() == SyncChange::ACTION_UPDATE || |
188 iter->change_type() == syncer::SyncChange::ACTION_ADD) { | 179 iter->change_type() == SyncChange::ACTION_ADD) { |
189 has_changes = true; | 180 has_changes = true; |
190 StoreSyncData(client_id, sync_data); | 181 StoreSyncData(client_id, sync_data); |
191 } else { | 182 } else { |
192 error.Reset(FROM_HERE, "Invalid action received.", syncer::DEVICE_INFO); | 183 error.Reset(FROM_HERE, "Invalid action received.", DEVICE_INFO); |
193 } | 184 } |
194 } | 185 } |
195 | 186 |
196 if (has_changes) { | 187 if (has_changes) { |
197 NotifyObservers(); | 188 NotifyObservers(); |
198 } | 189 } |
199 | 190 |
200 return error; | 191 return error; |
201 } | 192 } |
202 | 193 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 SyncData DeviceInfoSyncService::CreateLocalData(const DeviceInfo* info) { | 232 SyncData DeviceInfoSyncService::CreateLocalData(const DeviceInfo* info) { |
242 sync_pb::EntitySpecifics entity; | 233 sync_pb::EntitySpecifics entity; |
243 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); | 234 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); |
244 | 235 |
245 specifics.set_cache_guid(info->guid()); | 236 specifics.set_cache_guid(info->guid()); |
246 specifics.set_client_name(info->client_name()); | 237 specifics.set_client_name(info->client_name()); |
247 specifics.set_chrome_version(info->chrome_version()); | 238 specifics.set_chrome_version(info->chrome_version()); |
248 specifics.set_sync_user_agent(info->sync_user_agent()); | 239 specifics.set_sync_user_agent(info->sync_user_agent()); |
249 specifics.set_device_type(info->device_type()); | 240 specifics.set_device_type(info->device_type()); |
250 specifics.set_signin_scoped_device_id(info->signin_scoped_device_id()); | 241 specifics.set_signin_scoped_device_id(info->signin_scoped_device_id()); |
251 specifics.set_last_updated_timestamp(syncer::TimeToProtoTime(Time::Now())); | 242 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now())); |
252 | 243 |
253 return CreateLocalData(entity); | 244 return CreateLocalData(entity); |
254 } | 245 } |
255 | 246 |
256 SyncData DeviceInfoSyncService::CreateLocalData( | 247 SyncData DeviceInfoSyncService::CreateLocalData( |
257 const sync_pb::EntitySpecifics& entity) { | 248 const sync_pb::EntitySpecifics& entity) { |
258 const sync_pb::DeviceInfoSpecifics& specifics = entity.device_info(); | 249 const sync_pb::DeviceInfoSpecifics& specifics = entity.device_info(); |
259 return SyncData::CreateLocalData(DeviceInfoUtil::SpecificsToTag(specifics), | 250 return SyncData::CreateLocalData(DeviceInfoUtil::SpecificsToTag(specifics), |
260 specifics.client_name(), entity); | 251 specifics.client_name(), entity); |
261 } | 252 } |
262 | 253 |
263 DeviceInfo* DeviceInfoSyncService::CreateDeviceInfo( | 254 DeviceInfo* DeviceInfoSyncService::CreateDeviceInfo(const SyncData& sync_data) { |
264 const syncer::SyncData& sync_data) { | |
265 const sync_pb::DeviceInfoSpecifics& specifics = | 255 const sync_pb::DeviceInfoSpecifics& specifics = |
266 sync_data.GetSpecifics().device_info(); | 256 sync_data.GetSpecifics().device_info(); |
267 | 257 |
268 return new DeviceInfo(specifics.cache_guid(), specifics.client_name(), | 258 return new DeviceInfo(specifics.cache_guid(), specifics.client_name(), |
269 specifics.chrome_version(), specifics.sync_user_agent(), | 259 specifics.chrome_version(), specifics.sync_user_agent(), |
270 specifics.device_type(), | 260 specifics.device_type(), |
271 specifics.signin_scoped_device_id()); | 261 specifics.signin_scoped_device_id()); |
272 } | 262 } |
273 | 263 |
274 void DeviceInfoSyncService::StoreSyncData(const std::string& client_id, | 264 void DeviceInfoSyncService::StoreSyncData(const std::string& client_id, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 return std::count_if(all_data_.begin(), all_data_.end(), | 303 return std::count_if(all_data_.begin(), all_data_.end(), |
314 [now](SyncDataMap::const_reference pair) { | 304 [now](SyncDataMap::const_reference pair) { |
315 return DeviceInfoUtil::IsActive( | 305 return DeviceInfoUtil::IsActive( |
316 GetLastUpdateTime(pair.second), now); | 306 GetLastUpdateTime(pair.second), now); |
317 }); | 307 }); |
318 } | 308 } |
319 | 309 |
320 // static | 310 // static |
321 Time DeviceInfoSyncService::GetLastUpdateTime(const SyncData& device_info) { | 311 Time DeviceInfoSyncService::GetLastUpdateTime(const SyncData& device_info) { |
322 if (device_info.GetSpecifics().device_info().has_last_updated_timestamp()) { | 312 if (device_info.GetSpecifics().device_info().has_last_updated_timestamp()) { |
323 return syncer::ProtoTimeToTime( | 313 return ProtoTimeToTime( |
324 device_info.GetSpecifics().device_info().last_updated_timestamp()); | 314 device_info.GetSpecifics().device_info().last_updated_timestamp()); |
325 } else if (!device_info.IsLocal()) { | 315 } else if (!device_info.IsLocal()) { |
326 // If there is no |last_updated_timestamp| present, fallback to mod time. | 316 // If there is no |last_updated_timestamp| present, fallback to mod time. |
327 return syncer::SyncDataRemote(device_info).GetModifiedTime(); | 317 return SyncDataRemote(device_info).GetModifiedTime(); |
328 } else { | 318 } else { |
329 // We shouldn't reach this point for remote data, so this means we're likely | 319 // We shouldn't reach this point for remote data, so this means we're likely |
330 // looking at the local device info. Using a long ago time is perfect, since | 320 // looking at the local device info. Using a long ago time is perfect, since |
331 // the desired behavior is to update/pulse our data as soon as possible. | 321 // the desired behavior is to update/pulse our data as soon as possible. |
332 return Time(); | 322 return Time(); |
333 } | 323 } |
334 } | 324 } |
335 | 325 |
336 } // namespace sync_driver | 326 } // namespace syncer |
OLD | NEW |