Mandoline: Make TransferBufferManager RefCounted
[chromium-blink-merge.git] / components / sync_driver / device_info_sync_service.cc
blobfb441a44bcdf8acca2613582024d7b2db0943b5f
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
3 // found in the LICENSE file.
5 #include "components/sync_driver/device_info_sync_service.h"
7 #include "base/metrics/histogram_macros.h"
8 #include "base/strings/stringprintf.h"
9 #include "components/sync_driver/local_device_info_provider.h"
10 #include "sync/api/sync_change.h"
11 #include "sync/protocol/sync.pb.h"
12 #include "sync/util/time.h"
14 namespace sync_driver {
16 using syncer::ModelType;
17 using syncer::SyncChange;
18 using syncer::SyncChangeList;
19 using syncer::SyncChangeProcessor;
20 using syncer::SyncData;
21 using syncer::SyncDataList;
22 using syncer::SyncErrorFactory;
23 using syncer::SyncMergeResult;
25 namespace {
27 // TODO(pavely): Remove histogram once device_id mismatch is understood
28 // (crbug/481596).
29 // When signin_scoped_device_id from pref doesn't match the one in
30 // DeviceInfoSpecfics record histogram telling if sync or pref copy was empty.
31 // This will indicate how often such mismatch happens and what was the state
32 // before.
33 enum DeviceIdMismatchForHistogram {
34 DEVICE_ID_MISMATCH_BOTH_NONEMPTY = 0,
35 DEVICE_ID_MISMATCH_SYNC_EMPTY,
36 DEVICE_ID_MISMATCH_PREF_EMPTY,
37 DEVICE_ID_MISMATCH_COUNT,
40 void RecordDeviceIdChangedHistogram(const std::string& device_id_from_sync,
41 const std::string& device_id_from_pref) {
42 DCHECK(device_id_from_sync != device_id_from_pref);
43 DeviceIdMismatchForHistogram device_id_mismatch_for_histogram =
44 DEVICE_ID_MISMATCH_BOTH_NONEMPTY;
45 if (device_id_from_sync.empty()) {
46 device_id_mismatch_for_histogram = DEVICE_ID_MISMATCH_SYNC_EMPTY;
47 } else if (device_id_from_pref.empty()) {
48 device_id_mismatch_for_histogram = DEVICE_ID_MISMATCH_PREF_EMPTY;
50 UMA_HISTOGRAM_ENUMERATION("Sync.DeviceIdMismatchDetails",
51 device_id_mismatch_for_histogram,
52 DEVICE_ID_MISMATCH_COUNT);
55 } // namespace
57 DeviceInfoSyncService::DeviceInfoSyncService(
58 LocalDeviceInfoProvider* local_device_info_provider)
59 : local_device_backup_time_(-1),
60 local_device_info_provider_(local_device_info_provider) {
61 DCHECK(local_device_info_provider);
64 DeviceInfoSyncService::~DeviceInfoSyncService() {
67 SyncMergeResult DeviceInfoSyncService::MergeDataAndStartSyncing(
68 ModelType type,
69 const SyncDataList& initial_sync_data,
70 scoped_ptr<SyncChangeProcessor> sync_processor,
71 scoped_ptr<SyncErrorFactory> error_handler) {
72 DCHECK(sync_processor.get());
73 DCHECK(error_handler.get());
74 DCHECK_EQ(type, syncer::DEVICE_INFO);
76 DCHECK(!IsSyncing());
78 sync_processor_ = sync_processor.Pass();
79 error_handler_ = error_handler.Pass();
81 // Initialization should be completed before this type is enabled
82 // and local device info must be available.
83 const DeviceInfo* local_device_info =
84 local_device_info_provider_->GetLocalDeviceInfo();
85 DCHECK(local_device_info != NULL);
87 // Indicates whether a local device has been added or updated.
88 // |change_type| defaults to ADD and might be changed to
89 // UPDATE to INVALID down below if the initial data contains
90 // data matching the local device ID.
91 SyncChange::SyncChangeType change_type = SyncChange::ACTION_ADD;
92 size_t num_items_new = 0;
93 size_t num_items_updated = 0;
95 // Iterate over all initial sync data and copy it to the cache.
96 for (SyncDataList::const_iterator iter = initial_sync_data.begin();
97 iter != initial_sync_data.end();
98 ++iter) {
99 DCHECK_EQ(syncer::DEVICE_INFO, iter->GetDataType());
101 const std::string& id = iter->GetSpecifics().device_info().cache_guid();
103 if (id == local_device_info->guid()) {
104 // |initial_sync_data| contains data matching the local device.
105 scoped_ptr<DeviceInfo> synced_local_device_info =
106 make_scoped_ptr(CreateDeviceInfo(*iter));
108 // Retrieve local device backup timestamp value from the sync data.
109 bool has_synced_backup_time =
110 iter->GetSpecifics().device_info().has_backup_timestamp();
111 int64 synced_backup_time =
112 has_synced_backup_time
113 ? iter->GetSpecifics().device_info().backup_timestamp()
114 : -1;
116 // Overwrite |local_device_backup_time_| with this value if it
117 // hasn't been set yet.
118 if (!has_local_device_backup_time() && has_synced_backup_time) {
119 set_local_device_backup_time(synced_backup_time);
121 // TODO(pavely): Remove histogram once device_id mismatch is understood
122 // (crbug/481596).
123 if (synced_local_device_info->signin_scoped_device_id() !=
124 local_device_info->signin_scoped_device_id()) {
125 RecordDeviceIdChangedHistogram(
126 synced_local_device_info->signin_scoped_device_id(),
127 local_device_info->signin_scoped_device_id());
130 // Store the synced device info for the local device only
131 // it is the same as the local info. Otherwise store the local
132 // device info and issue a change further below after finishing
133 // processing the |initial_sync_data|.
134 if (synced_local_device_info->Equals(*local_device_info) &&
135 synced_backup_time == local_device_backup_time()) {
136 change_type = SyncChange::ACTION_INVALID;
137 } else {
138 num_items_updated++;
139 change_type = SyncChange::ACTION_UPDATE;
140 continue;
142 } else {
143 // A new device that doesn't match the local device.
144 num_items_new++;
147 StoreSyncData(id, *iter);
150 syncer::SyncMergeResult result(type);
152 // Add SyncData for the local device if it is new or different than
153 // the synced one, and also add it to the |change_list|.
154 if (change_type != SyncChange::ACTION_INVALID) {
155 SyncData local_data = CreateLocalData(local_device_info);
156 StoreSyncData(local_device_info->guid(), local_data);
158 SyncChangeList change_list;
159 change_list.push_back(SyncChange(FROM_HERE, change_type, local_data));
160 result.set_error(
161 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list));
164 result.set_num_items_before_association(1);
165 result.set_num_items_after_association(all_data_.size());
166 result.set_num_items_added(num_items_new);
167 result.set_num_items_modified(num_items_updated);
168 result.set_num_items_deleted(0);
170 NotifyObservers();
172 return result;
175 bool DeviceInfoSyncService::IsSyncing() const {
176 return !all_data_.empty();
179 void DeviceInfoSyncService::StopSyncing(syncer::ModelType type) {
180 bool was_syncing = IsSyncing();
182 all_data_.clear();
183 sync_processor_.reset();
184 error_handler_.reset();
185 clear_local_device_backup_time();
187 if (was_syncing) {
188 NotifyObservers();
192 SyncDataList DeviceInfoSyncService::GetAllSyncData(
193 syncer::ModelType type) const {
194 SyncDataList list;
196 for (SyncDataMap::const_iterator iter = all_data_.begin();
197 iter != all_data_.end();
198 ++iter) {
199 list.push_back(iter->second);
202 return list;
205 syncer::SyncError DeviceInfoSyncService::ProcessSyncChanges(
206 const tracked_objects::Location& from_here,
207 const SyncChangeList& change_list) {
208 syncer::SyncError error;
210 DCHECK(local_device_info_provider_->GetLocalDeviceInfo());
211 const std::string& local_device_id =
212 local_device_info_provider_->GetLocalDeviceInfo()->guid();
214 bool has_changes = false;
216 // Iterate over all chanages and merge entries.
217 for (SyncChangeList::const_iterator iter = change_list.begin();
218 iter != change_list.end();
219 ++iter) {
220 const SyncData& sync_data = iter->sync_data();
221 DCHECK_EQ(syncer::DEVICE_INFO, sync_data.GetDataType());
223 const std::string& client_id =
224 sync_data.GetSpecifics().device_info().cache_guid();
225 // Ignore device info matching the local device.
226 if (local_device_id == client_id) {
227 DVLOG(1) << "Ignoring sync changes for the local DEVICE_INFO";
228 continue;
231 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
232 has_changes = true;
233 DeleteSyncData(client_id);
234 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE ||
235 iter->change_type() == syncer::SyncChange::ACTION_ADD) {
236 has_changes = true;
237 StoreSyncData(client_id, sync_data);
238 } else {
239 error.Reset(FROM_HERE, "Invalid action received.", syncer::DEVICE_INFO);
243 if (has_changes) {
244 NotifyObservers();
247 return error;
250 scoped_ptr<DeviceInfo> DeviceInfoSyncService::GetDeviceInfo(
251 const std::string& client_id) const {
252 SyncDataMap::const_iterator iter = all_data_.find(client_id);
253 if (iter == all_data_.end()) {
254 return scoped_ptr<DeviceInfo>();
257 return make_scoped_ptr(CreateDeviceInfo(iter->second));
260 ScopedVector<DeviceInfo> DeviceInfoSyncService::GetAllDeviceInfo() const {
261 ScopedVector<DeviceInfo> list;
263 for (SyncDataMap::const_iterator iter = all_data_.begin();
264 iter != all_data_.end();
265 ++iter) {
266 list.push_back(CreateDeviceInfo(iter->second));
269 return list.Pass();
272 void DeviceInfoSyncService::AddObserver(Observer* observer) {
273 observers_.AddObserver(observer);
276 void DeviceInfoSyncService::RemoveObserver(Observer* observer) {
277 observers_.RemoveObserver(observer);
280 void DeviceInfoSyncService::NotifyObservers() {
281 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceInfoChange());
284 void DeviceInfoSyncService::UpdateLocalDeviceBackupTime(
285 base::Time backup_time) {
286 set_local_device_backup_time(syncer::TimeToProtoTime(backup_time));
288 if (sync_processor_.get()) {
289 // Local device info must be available in advance
290 DCHECK(local_device_info_provider_->GetLocalDeviceInfo());
291 const std::string& local_id =
292 local_device_info_provider_->GetLocalDeviceInfo()->guid();
294 SyncDataMap::iterator iter = all_data_.find(local_id);
295 DCHECK(iter != all_data_.end());
297 syncer::SyncData& data = iter->second;
298 if (UpdateBackupTime(&data)) {
299 // Local device backup time has changed.
300 // Push changes to the server via the |sync_processor_|.
301 SyncChangeList change_list;
302 change_list.push_back(SyncChange(
303 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data));
304 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list);
309 bool DeviceInfoSyncService::UpdateBackupTime(syncer::SyncData* sync_data) {
310 DCHECK(has_local_device_backup_time());
311 DCHECK(sync_data->GetSpecifics().has_device_info());
312 const sync_pb::DeviceInfoSpecifics& source_specifics =
313 sync_data->GetSpecifics().device_info();
315 if (!source_specifics.has_backup_timestamp() ||
316 source_specifics.backup_timestamp() != local_device_backup_time()) {
317 sync_pb::EntitySpecifics entity(sync_data->GetSpecifics());
318 entity.mutable_device_info()->set_backup_timestamp(
319 local_device_backup_time());
320 *sync_data = CreateLocalData(entity);
322 return true;
325 return false;
328 base::Time DeviceInfoSyncService::GetLocalDeviceBackupTime() const {
329 return has_local_device_backup_time()
330 ? syncer::ProtoTimeToTime(local_device_backup_time())
331 : base::Time();
334 SyncData DeviceInfoSyncService::CreateLocalData(const DeviceInfo* info) {
335 sync_pb::EntitySpecifics entity;
336 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info();
338 specifics.set_cache_guid(info->guid());
339 specifics.set_client_name(info->client_name());
340 specifics.set_chrome_version(info->chrome_version());
341 specifics.set_sync_user_agent(info->sync_user_agent());
342 specifics.set_device_type(info->device_type());
343 specifics.set_signin_scoped_device_id(info->signin_scoped_device_id());
345 if (has_local_device_backup_time()) {
346 specifics.set_backup_timestamp(local_device_backup_time());
349 return CreateLocalData(entity);
352 SyncData DeviceInfoSyncService::CreateLocalData(
353 const sync_pb::EntitySpecifics& entity) {
354 const sync_pb::DeviceInfoSpecifics& specifics = entity.device_info();
356 std::string local_device_tag =
357 base::StringPrintf("DeviceInfo_%s", specifics.cache_guid().c_str());
359 return SyncData::CreateLocalData(
360 local_device_tag, specifics.client_name(), entity);
363 DeviceInfo* DeviceInfoSyncService::CreateDeviceInfo(
364 const syncer::SyncData sync_data) {
365 const sync_pb::DeviceInfoSpecifics& specifics =
366 sync_data.GetSpecifics().device_info();
368 return new DeviceInfo(specifics.cache_guid(),
369 specifics.client_name(),
370 specifics.chrome_version(),
371 specifics.sync_user_agent(),
372 specifics.device_type(),
373 specifics.signin_scoped_device_id());
376 void DeviceInfoSyncService::StoreSyncData(const std::string& client_id,
377 const SyncData& sync_data) {
378 DVLOG(1) << "Storing DEVICE_INFO for "
379 << sync_data.GetSpecifics().device_info().client_name()
380 << " with ID " << client_id;
381 all_data_[client_id] = sync_data;
384 void DeviceInfoSyncService::DeleteSyncData(const std::string& client_id) {
385 SyncDataMap::iterator iter = all_data_.find(client_id);
386 if (iter != all_data_.end()) {
387 DVLOG(1) << "Deleting DEVICE_INFO for "
388 << iter->second.GetSpecifics().device_info().client_name()
389 << " with ID " << client_id;
390 all_data_.erase(iter);
394 } // namespace sync_driver