OLD | NEW |
---|---|
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 "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_ service.h" | 5 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_ service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 const syncer::SyncDataList& initial_sync_data, | 194 const syncer::SyncDataList& initial_sync_data, |
195 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 195 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
196 scoped_ptr<syncer::SyncErrorFactory> error_handler) { | 196 scoped_ptr<syncer::SyncErrorFactory> error_handler) { |
197 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type); | 197 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type); |
198 sync_processor_ = std::move(sync_processor); | 198 sync_processor_ = std::move(sync_processor); |
199 error_handler_ = std::move(error_handler); | 199 error_handler_ = std::move(error_handler); |
200 | 200 |
201 // We keep a map from MU ID to the set of keys that we have seen in the | 201 // We keep a map from MU ID to the set of keys that we have seen in the |
202 // initial sync data. | 202 // initial sync data. |
203 std::map<std::string, std::set<std::string> > seen_keys; | 203 std::map<std::string, std::set<std::string> > seen_keys; |
204 SyncMergeResult result(SUPERVISED_USER_SHARED_SETTINGS); | |
205 int num_added = 0; | |
206 int num_modified = 0; | |
207 | |
208 DictionaryPrefUpdate update_prefs(prefs_, | |
Bernhard Bauer
2016/01/06 16:41:50
If you don't need to modify anything, you can just
Deepak
2016/01/07 09:52:31
Done.
| |
209 prefs::kSupervisedUserSharedSettings); | |
210 base::DictionaryValue* pref_dict = update_prefs.Get(); | |
211 result.set_num_items_before_association(pref_dict->size()); | |
Bernhard Bauer
2016/01/06 16:41:50
Can you put this into a local variable like |num_a
Deepak
2016/01/07 09:52:31
Done.
| |
204 | 212 |
205 // Iterate over all initial sync data, and update it locally. This means that | 213 // Iterate over all initial sync data, and update it locally. This means that |
206 // the value from the server always wins over a local value. | 214 // the value from the server always wins over a local value. |
207 for (const SyncData& sync_data : initial_sync_data) { | 215 for (const SyncData& sync_data : initial_sync_data) { |
208 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, sync_data.GetDataType()); | 216 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, sync_data.GetDataType()); |
209 const ::sync_pb::ManagedUserSharedSettingSpecifics& | 217 const ::sync_pb::ManagedUserSharedSettingSpecifics& |
210 supervised_user_shared_setting = | 218 supervised_user_shared_setting = |
211 sync_data.GetSpecifics().managed_user_shared_setting(); | 219 sync_data.GetSpecifics().managed_user_shared_setting(); |
212 scoped_ptr<Value> value = | 220 scoped_ptr<Value> value = |
213 base::JSONReader::Read(supervised_user_shared_setting.value()); | 221 base::JSONReader::Read(supervised_user_shared_setting.value()); |
214 const std::string& su_id = supervised_user_shared_setting.mu_id(); | 222 const std::string& su_id = supervised_user_shared_setting.mu_id(); |
215 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id); | 223 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id); |
216 const std::string& key = supervised_user_shared_setting.key(); | 224 const std::string& key = supervised_user_shared_setting.key(); |
217 DictionaryValue* dict = FindOrCreateDictionary(update.Get(), key); | 225 DictionaryValue* dict = FindOrCreateDictionary(update.Get(), key); |
218 dict->SetWithoutPathExpansion(kValue, value.release()); | 226 dict->SetWithoutPathExpansion(kValue, value.release()); |
219 | 227 |
220 // Every setting we get from the server should have the acknowledged flag | 228 // Every setting we get from the server should have the acknowledged flag |
221 // set. | 229 // set. |
222 DCHECK(supervised_user_shared_setting.acknowledged()); | 230 DCHECK(supervised_user_shared_setting.acknowledged()); |
223 dict->SetBooleanWithoutPathExpansion( | 231 dict->SetBooleanWithoutPathExpansion( |
224 kAcknowledged, supervised_user_shared_setting.acknowledged()); | 232 kAcknowledged, supervised_user_shared_setting.acknowledged()); |
225 callbacks_.Notify(su_id, key); | 233 callbacks_.Notify(su_id, key); |
226 | 234 |
235 if (seen_keys.find(su_id) == seen_keys.end()) | |
236 num_added++; | |
237 else | |
238 num_modified++; | |
227 seen_keys[su_id].insert(key); | 239 seen_keys[su_id].insert(key); |
228 } | 240 } |
229 | 241 |
230 // Iterate over all settings that we have locally, which includes settings | 242 // Iterate over all settings that we have locally, which includes settings |
231 // that were just synced down. We filter those out using |seen_keys|. | 243 // that were just synced down. We filter those out using |seen_keys|. |
232 SyncChangeList change_list; | 244 SyncChangeList change_list; |
233 const DictionaryValue* all_settings = | 245 const DictionaryValue* all_settings = |
234 prefs_->GetDictionary(prefs::kSupervisedUserSharedSettings); | 246 prefs_->GetDictionary(prefs::kSupervisedUserSharedSettings); |
235 for (DictionaryValue::Iterator it(*all_settings); !it.IsAtEnd(); | 247 for (DictionaryValue::Iterator it(*all_settings); !it.IsAtEnd(); |
236 it.Advance()) { | 248 it.Advance()) { |
237 const DictionaryValue* dict = NULL; | 249 const DictionaryValue* dict = NULL; |
238 bool success = it.value().GetAsDictionary(&dict); | 250 bool success = it.value().GetAsDictionary(&dict); |
239 DCHECK(success); | 251 DCHECK(success); |
240 | 252 |
241 const std::set<std::string>& seen = seen_keys[it.key()]; | 253 const std::set<std::string>& seen = seen_keys[it.key()]; |
242 for (DictionaryValue::Iterator jt(*dict); !jt.IsAtEnd(); jt.Advance()) { | 254 for (DictionaryValue::Iterator jt(*dict); !jt.IsAtEnd(); jt.Advance()) { |
243 // We only need to upload settings that we haven't seen in the initial | 255 // We only need to upload settings that we haven't seen in the initial |
244 // sync data (which means they were added locally). | 256 // sync data (which means they were added locally). |
245 if (seen.count(jt.key()) > 0) | 257 if (seen.count(jt.key()) > 0) |
246 continue; | 258 continue; |
247 | 259 |
248 SyncData data = CreateSyncDataForValue(it.key(), jt.key(), jt.value()); | 260 SyncData data = CreateSyncDataForValue(it.key(), jt.key(), jt.value()); |
249 DCHECK(data.IsValid()); | 261 DCHECK(data.IsValid()); |
250 change_list.push_back( | 262 change_list.push_back( |
251 SyncChange(FROM_HERE, SyncChange::ACTION_ADD, data)); | 263 SyncChange(FROM_HERE, SyncChange::ACTION_ADD, data)); |
252 } | 264 } |
253 } | 265 } |
254 | 266 |
255 SyncMergeResult result(SUPERVISED_USER_SHARED_SETTINGS); | |
256 // Process all the accumulated changes. | 267 // Process all the accumulated changes. |
257 if (change_list.size() > 0) { | 268 if (change_list.size() > 0) { |
258 result.set_error( | 269 result.set_error( |
259 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); | 270 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); |
260 } | 271 } |
261 | 272 |
262 // TODO(bauerb): Statistics? | 273 result.set_num_items_added(num_added); |
274 result.set_num_items_modified(num_modified); | |
275 result.set_num_items_deleted(0); | |
Bernhard Bauer
2016/01/06 16:41:50
It's fine not to set this value (0 is the default
Deepak
2016/01/07 09:52:31
Done.
| |
276 result.set_num_items_after_association(pref_dict->size()); | |
263 return result; | 277 return result; |
264 } | 278 } |
265 | 279 |
266 void SupervisedUserSharedSettingsService::StopSyncing(syncer::ModelType type) { | 280 void SupervisedUserSharedSettingsService::StopSyncing(syncer::ModelType type) { |
267 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type); | 281 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, type); |
268 sync_processor_.reset(); | 282 sync_processor_.reset(); |
269 error_handler_.reset(); | 283 error_handler_.reset(); |
270 } | 284 } |
271 | 285 |
272 syncer::SyncDataList SupervisedUserSharedSettingsService::GetAllSyncData( | 286 syncer::SyncDataList SupervisedUserSharedSettingsService::GetAllSyncData( |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
337 NOTREACHED(); | 351 NOTREACHED(); |
338 break; | 352 break; |
339 } | 353 } |
340 } | 354 } |
341 callbacks_.Notify(su_id, key); | 355 callbacks_.Notify(su_id, key); |
342 } | 356 } |
343 | 357 |
344 SyncError error; | 358 SyncError error; |
345 return error; | 359 return error; |
346 } | 360 } |
OLD | NEW |