Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/sync/glue/device_info_sync_service.h" | |
| 6 | |
| 7 #include "base/strings/stringprintf.h" | |
| 8 #include "chrome/browser/sync/glue/local_device_info_provider.h" | |
| 9 #include "sync/api/sync_change.h" | |
| 10 #include "sync/protocol/sync.pb.h" | |
| 11 | |
| 12 namespace browser_sync { | |
| 13 | |
| 14 using syncer::ModelType; | |
| 15 using syncer::SyncChange; | |
| 16 using syncer::SyncChangeList; | |
| 17 using syncer::SyncChangeProcessor; | |
| 18 using syncer::SyncData; | |
| 19 using syncer::SyncDataList; | |
| 20 using syncer::SyncErrorFactory; | |
| 21 using syncer::SyncMergeResult; | |
| 22 | |
| 23 DeviceInfoSyncService::DeviceInfoSyncService( | |
| 24 LocalDeviceInfoProvider* local_device_info_provider) | |
| 25 : local_device_info_provider_(local_device_info_provider) { | |
| 26 DCHECK(local_device_info_provider); | |
| 27 } | |
| 28 | |
| 29 DeviceInfoSyncService::~DeviceInfoSyncService() { | |
| 30 } | |
| 31 | |
| 32 SyncMergeResult DeviceInfoSyncService::MergeDataAndStartSyncing( | |
| 33 ModelType type, | |
| 34 const SyncDataList& initial_sync_data, | |
| 35 scoped_ptr<SyncChangeProcessor> sync_processor, | |
| 36 scoped_ptr<SyncErrorFactory> error_handler) { | |
| 37 DCHECK(sync_processor.get()); | |
| 38 DCHECK(error_handler.get()); | |
| 39 DCHECK_EQ(type, syncer::DEVICE_INFO); | |
| 40 | |
| 41 DCHECK(all_data_.empty()); | |
| 42 | |
| 43 sync_processor_ = sync_processor.Pass(); | |
| 44 error_handler_ = error_handler.Pass(); | |
| 45 | |
| 46 // Iterate over all initial sync data and copy it to the cache. | |
| 47 for (SyncDataList::const_iterator iter = initial_sync_data.begin(); | |
| 48 iter != initial_sync_data.end(); | |
| 49 ++iter) { | |
| 50 DCHECK_EQ(syncer::DEVICE_INFO, iter->GetDataType()); | |
| 51 StoreSyncData(iter->GetSpecifics().device_info().cache_guid(), *iter); | |
| 52 } | |
| 53 | |
| 54 size_t num_items_new = initial_sync_data.size(); | |
| 55 size_t num_items_updated = 0; | |
| 56 // Indicates whether a local device has been added or updated | |
| 57 SyncChange::SyncChangeType change_type = SyncChange::ACTION_INVALID; | |
| 58 | |
| 59 // Initialization should be completed before this type is enabled | |
| 60 // and local device info must be available. | |
| 61 const DeviceInfo* local_device_info = | |
| 62 local_device_info_provider_->GetLocalDeviceInfo(); | |
| 63 DCHECK(local_device_info != NULL); | |
| 64 // Before storing the local device info check if the data with | |
| 65 // the same guid has already been synced. This attempts to retrieve | |
| 66 // DeviceInfo from the cached data initialized above. | |
| 67 scoped_ptr<DeviceInfo> synced_local_device_info = | |
| 68 GetDeviceInfo(local_device_info->guid()); | |
| 69 | |
| 70 if (synced_local_device_info.get()) { | |
| 71 // Local device info has been synced and exists in the cache. | |
| 72 // |num_items_new| and |num_items_updated| need to be updated to | |
| 73 // reflect that. | |
| 74 num_items_new--; | |
| 75 // Overwrite the synced device info with the local data only if | |
| 76 // it is different. | |
| 77 if (!synced_local_device_info->Equals(*local_device_info)) { | |
| 78 num_items_updated++; | |
| 79 change_type = SyncChange::ACTION_UPDATE; | |
| 80 // TODO(stanisc): Should local device info be merged with the | |
| 81 // synced one? | |
|
pavely
2014/07/31 22:52:40
Today all changes to DeviceInfo originate on local
stanisc
2014/08/01 18:54:51
OK. I'll remove this comment and keep the current
| |
| 82 } | |
| 83 } else { | |
| 84 // Local device info doesn't yet exist in the cache and | |
| 85 // will be added further below. | |
| 86 // |num_items_new| and |num_items_updated| are already correct. | |
| 87 change_type = SyncChange::ACTION_ADD; | |
| 88 } | |
| 89 | |
| 90 syncer::SyncMergeResult result(type); | |
| 91 | |
| 92 // Update SyncData from device info if it new or different than | |
| 93 // the synced one, and also add it to the |change_list|. | |
| 94 if (change_type != SyncChange::ACTION_INVALID) { | |
| 95 SyncData local_data = CreateLocalData(local_device_info); | |
| 96 StoreSyncData(local_device_info->guid(), local_data); | |
| 97 | |
| 98 SyncChangeList change_list; | |
| 99 change_list.push_back(SyncChange(FROM_HERE, change_type, local_data)); | |
| 100 result.set_error( | |
| 101 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); | |
| 102 } | |
| 103 | |
| 104 result.set_num_items_before_association(1); | |
| 105 result.set_num_items_after_association(all_data_.size()); | |
| 106 result.set_num_items_added(num_items_new); | |
| 107 result.set_num_items_modified(num_items_updated); | |
| 108 result.set_num_items_deleted(0); | |
| 109 | |
| 110 return result; | |
|
pavely
2014/07/31 22:52:40
Do you need to notify callback list here?
stanisc
2014/08/01 18:54:51
The original implementation doesn't notify at this
| |
| 111 } | |
| 112 | |
| 113 void DeviceInfoSyncService::StopSyncing(syncer::ModelType type) { | |
| 114 all_data_.clear(); | |
| 115 sync_processor_.reset(); | |
| 116 error_handler_.reset(); | |
| 117 } | |
| 118 | |
| 119 SyncDataList DeviceInfoSyncService::GetAllSyncData( | |
| 120 syncer::ModelType type) const { | |
| 121 SyncDataList list; | |
| 122 | |
| 123 for (SyncDataMap::const_iterator iter = all_data_.begin(); | |
| 124 iter != all_data_.end(); ++iter) { | |
| 125 list.push_back(iter->second); | |
| 126 } | |
| 127 | |
| 128 return list; | |
| 129 } | |
| 130 | |
| 131 syncer::SyncError DeviceInfoSyncService::ProcessSyncChanges( | |
| 132 const tracked_objects::Location& from_here, | |
| 133 const SyncChangeList& change_list) { | |
| 134 syncer::SyncError error; | |
| 135 | |
| 136 DCHECK(local_device_info_provider_->GetLocalDeviceInfo()); | |
| 137 const std::string& local_device_id = | |
| 138 local_device_info_provider_->GetLocalDeviceInfo()->guid(); | |
| 139 | |
| 140 bool has_changes = false; | |
| 141 | |
| 142 // Iterate over all chanages and merge entries | |
|
stanisc
2014/07/30 18:57:50
Will add missing period at the end of the sentence
stanisc
2014/08/01 18:54:51
Done.
| |
| 143 for (SyncChangeList::const_iterator iter = change_list.begin(); | |
| 144 iter != change_list.end(); | |
| 145 ++iter) { | |
| 146 const SyncData& sync_data = iter->sync_data(); | |
| 147 DCHECK_EQ(syncer::DEVICE_INFO, sync_data.GetDataType()); | |
| 148 | |
| 149 const std::string& client_id = | |
| 150 sync_data.GetSpecifics().device_info().cache_guid(); | |
| 151 // TODO (stanisc) Should Device info matching the local device be | |
| 152 // ignored or merged? | |
| 153 if (local_device_id == client_id) { | |
| 154 DVLOG(1) << "Ignoring sync changes for the local DEVICE_INFO"; | |
| 155 continue; | |
| 156 } | |
| 157 | |
| 158 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) { | |
| 159 has_changes = true; | |
| 160 DeleteSyncData(client_id); | |
| 161 } else if (iter->change_type() == syncer::SyncChange::ACTION_UPDATE || | |
| 162 iter->change_type() == syncer::SyncChange::ACTION_ADD) { | |
| 163 has_changes = true; | |
| 164 StoreSyncData(client_id, sync_data); | |
| 165 } else { | |
| 166 error.Reset(FROM_HERE, "Invalid action received.", syncer::DEVICE_INFO); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 if (has_changes) { | |
| 171 callback_list_.Notify(); | |
| 172 } | |
| 173 | |
| 174 return error; | |
| 175 } | |
| 176 | |
| 177 scoped_ptr<DeviceInfo> DeviceInfoSyncService::GetDeviceInfo( | |
| 178 const std::string& client_id) const { | |
| 179 SyncDataMap::const_iterator iter = all_data_.find(client_id); | |
| 180 if (iter == all_data_.end()) { | |
| 181 return scoped_ptr<DeviceInfo>(); | |
| 182 } | |
| 183 | |
| 184 return make_scoped_ptr(CreateDeviceInfo(iter->second)); | |
| 185 } | |
| 186 | |
| 187 ScopedVector<DeviceInfo> DeviceInfoSyncService::GetAllDeviceInfo() const { | |
| 188 ScopedVector<DeviceInfo> list; | |
| 189 | |
| 190 for (SyncDataMap::const_iterator iter = all_data_.begin(); | |
| 191 iter != all_data_.end(); ++iter) { | |
| 192 list.push_back(CreateDeviceInfo(iter->second)); | |
| 193 } | |
| 194 | |
| 195 return list.Pass(); | |
| 196 } | |
| 197 | |
| 198 scoped_ptr<DeviceInfoSyncService::Subscription> | |
| 199 DeviceInfoSyncService::RegisterOnDeviceInfoChangedCallback( | |
| 200 const base::Closure& callback) { | |
| 201 return callback_list_.Add(callback); | |
| 202 } | |
| 203 | |
| 204 SyncData DeviceInfoSyncService::CreateLocalData(const DeviceInfo* info) { | |
| 205 sync_pb::EntitySpecifics entity; | |
| 206 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); | |
| 207 | |
| 208 specifics.set_cache_guid(info->guid()); | |
| 209 specifics.set_client_name(info->client_name()); | |
| 210 specifics.set_chrome_version(info->chrome_version()); | |
| 211 specifics.set_sync_user_agent(info->sync_user_agent()); | |
| 212 specifics.set_device_type(info->device_type()); | |
| 213 specifics.set_signin_scoped_device_id(info->signin_scoped_device_id()); | |
| 214 | |
| 215 std::string local_device_tag = base::StringPrintf( | |
| 216 "DeviceInfo_%s", info->guid().c_str()); | |
| 217 | |
| 218 return SyncData::CreateLocalData( | |
| 219 local_device_tag, info->client_name(), entity); | |
| 220 } | |
| 221 | |
| 222 DeviceInfo* DeviceInfoSyncService::CreateDeviceInfo( | |
| 223 const syncer::SyncData sync_data) { | |
| 224 const sync_pb::DeviceInfoSpecifics& specifics = | |
| 225 sync_data.GetSpecifics().device_info(); | |
| 226 | |
| 227 return new DeviceInfo(specifics.cache_guid(), | |
| 228 specifics.client_name(), | |
| 229 specifics.chrome_version(), | |
| 230 specifics.sync_user_agent(), | |
| 231 specifics.device_type(), | |
| 232 specifics.signin_scoped_device_id()); | |
|
pavely
2014/07/31 22:52:40
There are few formatting suggestions for this chan
| |
| 233 } | |
| 234 | |
| 235 void DeviceInfoSyncService::StoreSyncData( | |
| 236 const std::string& client_id, const SyncData& sync_data) { | |
| 237 DVLOG(1) << "Storing DEVICE_INFO for " | |
| 238 << sync_data.GetSpecifics().device_info().client_name() | |
| 239 << " with ID " | |
| 240 << client_id; | |
| 241 all_data_[client_id] = sync_data; | |
| 242 } | |
| 243 | |
| 244 void DeviceInfoSyncService::DeleteSyncData(const std::string& client_id) { | |
| 245 SyncDataMap::iterator iter = all_data_.find(client_id); | |
| 246 if (iter != all_data_.end()) { | |
| 247 DVLOG(1) << "Deleting DEVICE_INFO for " | |
| 248 << iter->second.GetSpecifics().device_info().client_name() | |
| 249 << " with ID " | |
| 250 << client_id; | |
| 251 all_data_.erase(iter); | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 } // namespace browser_sync | |
| OLD | NEW |