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