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

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

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

Powered by Google App Engine
This is Rietveld 408576698