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 } |
| 81 } else { |
| 82 // Local device info doesn't yet exist in the cache and |
| 83 // will be added further below. |
| 84 // |num_items_new| and |num_items_updated| are already correct. |
| 85 change_type = SyncChange::ACTION_ADD; |
| 86 } |
| 87 |
| 88 syncer::SyncMergeResult result(type); |
| 89 |
| 90 // Update SyncData from device info if it is new or different than |
| 91 // the synced one, and also add it to the |change_list|. |
| 92 if (change_type != SyncChange::ACTION_INVALID) { |
| 93 SyncData local_data = CreateLocalData(local_device_info); |
| 94 StoreSyncData(local_device_info->guid(), local_data); |
| 95 |
| 96 SyncChangeList change_list; |
| 97 change_list.push_back(SyncChange(FROM_HERE, change_type, local_data)); |
| 98 result.set_error( |
| 99 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); |
| 100 } |
| 101 |
| 102 result.set_num_items_before_association(1); |
| 103 result.set_num_items_after_association(all_data_.size()); |
| 104 result.set_num_items_added(num_items_new); |
| 105 result.set_num_items_modified(num_items_updated); |
| 106 result.set_num_items_deleted(0); |
| 107 |
| 108 NotifyObservers(); |
| 109 |
| 110 return result; |
| 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(); |
| 125 ++iter) { |
| 126 list.push_back(iter->second); |
| 127 } |
| 128 |
| 129 return list; |
| 130 } |
| 131 |
| 132 syncer::SyncError DeviceInfoSyncService::ProcessSyncChanges( |
| 133 const tracked_objects::Location& from_here, |
| 134 const SyncChangeList& change_list) { |
| 135 syncer::SyncError error; |
| 136 |
| 137 DCHECK(local_device_info_provider_->GetLocalDeviceInfo()); |
| 138 const std::string& local_device_id = |
| 139 local_device_info_provider_->GetLocalDeviceInfo()->guid(); |
| 140 |
| 141 bool has_changes = false; |
| 142 |
| 143 // Iterate over all chanages and merge entries. |
| 144 for (SyncChangeList::const_iterator iter = change_list.begin(); |
| 145 iter != change_list.end(); |
| 146 ++iter) { |
| 147 const SyncData& sync_data = iter->sync_data(); |
| 148 DCHECK_EQ(syncer::DEVICE_INFO, sync_data.GetDataType()); |
| 149 |
| 150 const std::string& client_id = |
| 151 sync_data.GetSpecifics().device_info().cache_guid(); |
| 152 // Ignore device info matching the local device. |
| 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 NotifyObservers(); |
| 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(); |
| 192 ++iter) { |
| 193 list.push_back(CreateDeviceInfo(iter->second)); |
| 194 } |
| 195 |
| 196 return list.Pass(); |
| 197 } |
| 198 |
| 199 void DeviceInfoSyncService::AddObserver(Observer* observer) { |
| 200 observers_.AddObserver(observer); |
| 201 } |
| 202 |
| 203 void DeviceInfoSyncService::RemoveObserver(Observer* observer) { |
| 204 observers_.RemoveObserver(observer); |
| 205 } |
| 206 |
| 207 void DeviceInfoSyncService::NotifyObservers() { |
| 208 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceInfoChange()); |
| 209 } |
| 210 |
| 211 SyncData DeviceInfoSyncService::CreateLocalData(const DeviceInfo* info) { |
| 212 sync_pb::EntitySpecifics entity; |
| 213 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); |
| 214 |
| 215 specifics.set_cache_guid(info->guid()); |
| 216 specifics.set_client_name(info->client_name()); |
| 217 specifics.set_chrome_version(info->chrome_version()); |
| 218 specifics.set_sync_user_agent(info->sync_user_agent()); |
| 219 specifics.set_device_type(info->device_type()); |
| 220 specifics.set_signin_scoped_device_id(info->signin_scoped_device_id()); |
| 221 |
| 222 std::string local_device_tag = |
| 223 base::StringPrintf("DeviceInfo_%s", info->guid().c_str()); |
| 224 |
| 225 return SyncData::CreateLocalData( |
| 226 local_device_tag, info->client_name(), entity); |
| 227 } |
| 228 |
| 229 DeviceInfo* DeviceInfoSyncService::CreateDeviceInfo( |
| 230 const syncer::SyncData sync_data) { |
| 231 const sync_pb::DeviceInfoSpecifics& specifics = |
| 232 sync_data.GetSpecifics().device_info(); |
| 233 |
| 234 return new DeviceInfo(specifics.cache_guid(), |
| 235 specifics.client_name(), |
| 236 specifics.chrome_version(), |
| 237 specifics.sync_user_agent(), |
| 238 specifics.device_type(), |
| 239 specifics.signin_scoped_device_id()); |
| 240 } |
| 241 |
| 242 void DeviceInfoSyncService::StoreSyncData(const std::string& client_id, |
| 243 const SyncData& sync_data) { |
| 244 DVLOG(1) << "Storing DEVICE_INFO for " |
| 245 << sync_data.GetSpecifics().device_info().client_name() |
| 246 << " with ID " << client_id; |
| 247 all_data_[client_id] = sync_data; |
| 248 } |
| 249 |
| 250 void DeviceInfoSyncService::DeleteSyncData(const std::string& client_id) { |
| 251 SyncDataMap::iterator iter = all_data_.find(client_id); |
| 252 if (iter != all_data_.end()) { |
| 253 DVLOG(1) << "Deleting DEVICE_INFO for " |
| 254 << iter->second.GetSpecifics().device_info().client_name() |
| 255 << " with ID " << client_id; |
| 256 all_data_.erase(iter); |
| 257 } |
| 258 } |
| 259 |
| 260 } // namespace browser_sync |
OLD | NEW |