Embedder API for accessing DOM3 code value based on dom code value
[chromium-blink-merge.git] / components / sync_driver / device_info_sync_service.cc
blob76ea831a70d94fd00dc3419635aeb668ff612411
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/strings/stringprintf.h"
8 #include "components/sync_driver/local_device_info_provider.h"
9 #include "sync/api/sync_change.h"
10 #include "sync/protocol/sync.pb.h"
11 #include "sync/util/time.h"
13 namespace sync_driver {
15 using syncer::ModelType;
16 using syncer::SyncChange;
17 using syncer::SyncChangeList;
18 using syncer::SyncChangeProcessor;
19 using syncer::SyncData;
20 using syncer::SyncDataList;
21 using syncer::SyncErrorFactory;
22 using syncer::SyncMergeResult;
24 DeviceInfoSyncService::DeviceInfoSyncService(
25 LocalDeviceInfoProvider* local_device_info_provider)
26 : local_device_backup_time_(-1),
27 local_device_info_provider_(local_device_info_provider) {
28 DCHECK(local_device_info_provider);
31 DeviceInfoSyncService::~DeviceInfoSyncService() {
34 SyncMergeResult DeviceInfoSyncService::MergeDataAndStartSyncing(
35 ModelType type,
36 const SyncDataList& initial_sync_data,
37 scoped_ptr<SyncChangeProcessor> sync_processor,
38 scoped_ptr<SyncErrorFactory> error_handler) {
39 DCHECK(sync_processor.get());
40 DCHECK(error_handler.get());
41 DCHECK_EQ(type, syncer::DEVICE_INFO);
43 DCHECK(!IsSyncing());
45 sync_processor_ = sync_processor.Pass();
46 error_handler_ = error_handler.Pass();
48 // Initialization should be completed before this type is enabled
49 // and local device info must be available.
50 const DeviceInfo* local_device_info =
51 local_device_info_provider_->GetLocalDeviceInfo();
52 DCHECK(local_device_info != NULL);
54 // Indicates whether a local device has been added or updated.
55 // |change_type| defaults to ADD and might be changed to
56 // UPDATE to INVALID down below if the initial data contains
57 // data matching the local device ID.
58 SyncChange::SyncChangeType change_type = SyncChange::ACTION_ADD;
59 size_t num_items_new = 0;
60 size_t num_items_updated = 0;
62 // Iterate over all initial sync data and copy it to the cache.
63 for (SyncDataList::const_iterator iter = initial_sync_data.begin();
64 iter != initial_sync_data.end();
65 ++iter) {
66 DCHECK_EQ(syncer::DEVICE_INFO, iter->GetDataType());
68 const std::string& id = iter->GetSpecifics().device_info().cache_guid();
70 if (id == local_device_info->guid()) {
71 // |initial_sync_data| contains data matching the local device.
72 scoped_ptr<DeviceInfo> synced_local_device_info =
73 make_scoped_ptr(CreateDeviceInfo(*iter));
75 // Retrieve local device backup timestamp value from the sync data.
76 bool has_synced_backup_time =
77 iter->GetSpecifics().device_info().has_backup_timestamp();
78 int64 synced_backup_time =
79 has_synced_backup_time
80 ? iter->GetSpecifics().device_info().backup_timestamp()
81 : -1;
83 // Overwrite |local_device_backup_time_| with this value if it
84 // hasn't been set yet.
85 if (!has_local_device_backup_time() && has_synced_backup_time) {
86 set_local_device_backup_time(synced_backup_time);
89 // Store the synced device info for the local device only
90 // it is the same as the local info. Otherwise store the local
91 // device info and issue a change further below after finishing
92 // processing the |initial_sync_data|.
93 if (synced_local_device_info->Equals(*local_device_info) &&
94 synced_backup_time == local_device_backup_time()) {
95 change_type = SyncChange::ACTION_INVALID;
96 } else {
97 num_items_updated++;
98 change_type = SyncChange::ACTION_UPDATE;
99 continue;
101 } else {
102 // A new device that doesn't match the local device.
103 num_items_new++;
106 StoreSyncData(id, *iter);
109 syncer::SyncMergeResult result(type);
111 // Add SyncData for the local device if it is new or different than
112 // the synced one, and also add it to the |change_list|.
113 if (change_type != SyncChange::ACTION_INVALID) {
114 SyncData local_data = CreateLocalData(local_device_info);
115 StoreSyncData(local_device_info->guid(), local_data);
117 SyncChangeList change_list;
118 change_list.push_back(SyncChange(FROM_HERE, change_type, local_data));
119 result.set_error(
120 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list));
123 result.set_num_items_before_association(1);
124 result.set_num_items_after_association(all_data_.size());
125 result.set_num_items_added(num_items_new);
126 result.set_num_items_modified(num_items_updated);
127 result.set_num_items_deleted(0);
129 NotifyObservers();
131 return result;
134 bool DeviceInfoSyncService::IsSyncing() const {
135 return !all_data_.empty();
138 void DeviceInfoSyncService::StopSyncing(syncer::ModelType type) {
139 bool was_syncing = IsSyncing();
141 all_data_.clear();
142 sync_processor_.reset();
143 error_handler_.reset();
144 clear_local_device_backup_time();
146 if (was_syncing) {
147 NotifyObservers();
151 SyncDataList DeviceInfoSyncService::GetAllSyncData(
152 syncer::ModelType type) const {
153 SyncDataList list;
155 for (SyncDataMap::const_iterator iter = all_data_.begin();
156 iter != all_data_.end();
157 ++iter) {
158 list.push_back(iter->second);
161 return list;
164 syncer::SyncError DeviceInfoSyncService::ProcessSyncChanges(
165 const tracked_objects::Location& from_here,
166 const SyncChangeList& change_list) {
167 syncer::SyncError error;
169 DCHECK(local_device_info_provider_->GetLocalDeviceInfo());
170 const std::string& local_device_id =
171 local_device_info_provider_->GetLocalDeviceInfo()->guid();
173 bool has_changes = false;
175 // Iterate over all chanages and merge entries.
176 for (SyncChangeList::const_iterator iter = change_list.begin();
177 iter != change_list.end();
178 ++iter) {
179 const SyncData& sync_data = iter->sync_data();
180 DCHECK_EQ(syncer::DEVICE_INFO, sync_data.GetDataType());
182 const std::string& client_id =
183 sync_data.GetSpecifics().device_info().cache_guid();
184 // Ignore device info matching the local device.
185 if (local_device_id == client_id) {
186 DVLOG(1) << "Ignoring sync changes for the local DEVICE_INFO";
187 continue;
190 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
191 has_changes = true;
192 DeleteSyncData(client_id);
193 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE ||
194 iter->change_type() == syncer::SyncChange::ACTION_ADD) {
195 has_changes = true;
196 StoreSyncData(client_id, sync_data);
197 } else {
198 error.Reset(FROM_HERE, "Invalid action received.", syncer::DEVICE_INFO);
202 if (has_changes) {
203 NotifyObservers();
206 return error;
209 scoped_ptr<DeviceInfo> DeviceInfoSyncService::GetDeviceInfo(
210 const std::string& client_id) const {
211 SyncDataMap::const_iterator iter = all_data_.find(client_id);
212 if (iter == all_data_.end()) {
213 return scoped_ptr<DeviceInfo>();
216 return make_scoped_ptr(CreateDeviceInfo(iter->second));
219 ScopedVector<DeviceInfo> DeviceInfoSyncService::GetAllDeviceInfo() const {
220 ScopedVector<DeviceInfo> list;
222 for (SyncDataMap::const_iterator iter = all_data_.begin();
223 iter != all_data_.end();
224 ++iter) {
225 list.push_back(CreateDeviceInfo(iter->second));
228 return list.Pass();
231 void DeviceInfoSyncService::AddObserver(Observer* observer) {
232 observers_.AddObserver(observer);
235 void DeviceInfoSyncService::RemoveObserver(Observer* observer) {
236 observers_.RemoveObserver(observer);
239 void DeviceInfoSyncService::NotifyObservers() {
240 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceInfoChange());
243 void DeviceInfoSyncService::UpdateLocalDeviceBackupTime(
244 base::Time backup_time) {
245 set_local_device_backup_time(syncer::TimeToProtoTime(backup_time));
247 if (sync_processor_.get()) {
248 // Local device info must be available in advance
249 DCHECK(local_device_info_provider_->GetLocalDeviceInfo());
250 const std::string& local_id =
251 local_device_info_provider_->GetLocalDeviceInfo()->guid();
253 SyncDataMap::iterator iter = all_data_.find(local_id);
254 DCHECK(iter != all_data_.end());
256 syncer::SyncData& data = iter->second;
257 if (UpdateBackupTime(&data)) {
258 // Local device backup time has changed.
259 // Push changes to the server via the |sync_processor_|.
260 SyncChangeList change_list;
261 change_list.push_back(SyncChange(
262 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, data));
263 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list);
268 bool DeviceInfoSyncService::UpdateBackupTime(syncer::SyncData* sync_data) {
269 DCHECK(has_local_device_backup_time());
270 DCHECK(sync_data->GetSpecifics().has_device_info());
271 const sync_pb::DeviceInfoSpecifics& source_specifics =
272 sync_data->GetSpecifics().device_info();
274 if (!source_specifics.has_backup_timestamp() ||
275 source_specifics.backup_timestamp() != local_device_backup_time()) {
276 sync_pb::EntitySpecifics entity(sync_data->GetSpecifics());
277 entity.mutable_device_info()->set_backup_timestamp(
278 local_device_backup_time());
279 *sync_data = CreateLocalData(entity);
281 return true;
284 return false;
287 base::Time DeviceInfoSyncService::GetLocalDeviceBackupTime() const {
288 return has_local_device_backup_time()
289 ? syncer::ProtoTimeToTime(local_device_backup_time())
290 : base::Time();
293 SyncData DeviceInfoSyncService::CreateLocalData(const DeviceInfo* info) {
294 sync_pb::EntitySpecifics entity;
295 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info();
297 specifics.set_cache_guid(info->guid());
298 specifics.set_client_name(info->client_name());
299 specifics.set_chrome_version(info->chrome_version());
300 specifics.set_sync_user_agent(info->sync_user_agent());
301 specifics.set_device_type(info->device_type());
302 specifics.set_signin_scoped_device_id(info->signin_scoped_device_id());
304 if (has_local_device_backup_time()) {
305 specifics.set_backup_timestamp(local_device_backup_time());
308 return CreateLocalData(entity);
311 SyncData DeviceInfoSyncService::CreateLocalData(
312 const sync_pb::EntitySpecifics& entity) {
313 const sync_pb::DeviceInfoSpecifics& specifics = entity.device_info();
315 std::string local_device_tag =
316 base::StringPrintf("DeviceInfo_%s", specifics.cache_guid().c_str());
318 return SyncData::CreateLocalData(
319 local_device_tag, specifics.client_name(), entity);
322 DeviceInfo* DeviceInfoSyncService::CreateDeviceInfo(
323 const syncer::SyncData sync_data) {
324 const sync_pb::DeviceInfoSpecifics& specifics =
325 sync_data.GetSpecifics().device_info();
327 return new DeviceInfo(specifics.cache_guid(),
328 specifics.client_name(),
329 specifics.chrome_version(),
330 specifics.sync_user_agent(),
331 specifics.device_type(),
332 specifics.signin_scoped_device_id());
335 void DeviceInfoSyncService::StoreSyncData(const std::string& client_id,
336 const SyncData& sync_data) {
337 DVLOG(1) << "Storing DEVICE_INFO for "
338 << sync_data.GetSpecifics().device_info().client_name()
339 << " with ID " << client_id;
340 all_data_[client_id] = sync_data;
343 void DeviceInfoSyncService::DeleteSyncData(const std::string& client_id) {
344 SyncDataMap::iterator iter = all_data_.find(client_id);
345 if (iter != all_data_.end()) {
346 DVLOG(1) << "Deleting DEVICE_INFO for "
347 << iter->second.GetSpecifics().device_info().client_name()
348 << " with ID " << client_id;
349 all_data_.erase(iter);
353 } // namespace sync_driver