Chromium Code Reviews| 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 |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/memory/ptr_util.h" | |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 15 #include "components/pref_registry/pref_registry_syncable.h" | 16 #include "components/pref_registry/pref_registry_syncable.h" |
| 16 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 17 #include "components/prefs/scoped_user_pref_update.h" | 18 #include "components/prefs/scoped_user_pref_update.h" |
| 18 #include "components/sync/model/sync_change.h" | 19 #include "components/sync/model/sync_change.h" |
| 19 #include "components/sync/model/sync_data.h" | 20 #include "components/sync/model/sync_data.h" |
| 20 #include "components/sync/model/sync_error.h" | 21 #include "components/sync/model/sync_error.h" |
| 21 #include "components/sync/model/sync_error_factory.h" | 22 #include "components/sync/model/sync_error_factory.h" |
| 22 #include "components/sync/model/sync_merge_result.h" | 23 #include "components/sync/model/sync_merge_result.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 const char kAcknowledged[] = "acknowledged"; | 41 const char kAcknowledged[] = "acknowledged"; |
| 41 const char kValue[] = "value"; | 42 const char kValue[] = "value"; |
| 42 | 43 |
| 43 DictionaryValue* FindOrCreateDictionary(DictionaryValue* parent, | 44 DictionaryValue* FindOrCreateDictionary(DictionaryValue* parent, |
| 44 const std::string& key) { | 45 const std::string& key) { |
| 45 DictionaryValue* dict = nullptr; | 46 DictionaryValue* dict = nullptr; |
| 46 if (!parent->GetDictionaryWithoutPathExpansion(key, &dict)) { | 47 if (!parent->GetDictionaryWithoutPathExpansion(key, &dict)) { |
| 47 dict = new DictionaryValue; | 48 parent->SetWithoutPathExpansion(key, |
| 48 parent->SetWithoutPathExpansion(key, dict); | 49 base::MakeUnique<base::DictionaryValue>()); |
| 50 parent->GetDictionaryWithoutPathExpansion(key, &dict); | |
| 49 } | 51 } |
| 50 return dict; | 52 return dict; |
| 51 } | 53 } |
| 52 | 54 |
| 53 class ScopedSupervisedUserSharedSettingsUpdate { | 55 class ScopedSupervisedUserSharedSettingsUpdate { |
| 54 public: | 56 public: |
| 55 ScopedSupervisedUserSharedSettingsUpdate(PrefService* prefs, | 57 ScopedSupervisedUserSharedSettingsUpdate(PrefService* prefs, |
| 56 const std::string& su_id) | 58 const std::string& su_id) |
| 57 : update_(prefs, prefs::kSupervisedUserSharedSettings), su_id_(su_id) { | 59 : update_(prefs, prefs::kSupervisedUserSharedSettings), su_id_(su_id) { |
| 58 DCHECK(!su_id.empty()); | 60 DCHECK(!su_id.empty()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 const std::string& su_id, | 105 const std::string& su_id, |
| 104 const std::string& key, | 106 const std::string& key, |
| 105 const Value& value, | 107 const Value& value, |
| 106 bool acknowledged) { | 108 bool acknowledged) { |
| 107 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id); | 109 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id); |
| 108 DictionaryValue* update_dict = update.Get(); | 110 DictionaryValue* update_dict = update.Get(); |
| 109 | 111 |
| 110 DictionaryValue* dict = nullptr; | 112 DictionaryValue* dict = nullptr; |
| 111 bool has_key = update_dict->GetDictionaryWithoutPathExpansion(key, &dict); | 113 bool has_key = update_dict->GetDictionaryWithoutPathExpansion(key, &dict); |
| 112 if (!has_key) { | 114 if (!has_key) { |
| 113 dict = new DictionaryValue; | 115 update_dict->SetWithoutPathExpansion( |
| 114 update_dict->SetWithoutPathExpansion(key, dict); | 116 key, base::MakeUnique<base::DictionaryValue>()); |
| 117 update_dict->GetDictionaryWithoutPathExpansion(key, &dict); | |
| 115 } | 118 } |
| 116 dict->SetWithoutPathExpansion(kValue, value.DeepCopy()); | 119 dict->SetWithoutPathExpansion(kValue, value.CreateDeepCopy()); |
|
vabr (Chromium)
2017/04/28 07:23:40
Ditto about using copy constructor instead.
jdoerrie
2017/05/02 18:08:06
Done.
| |
| 117 dict->SetBooleanWithoutPathExpansion(kAcknowledged, acknowledged); | 120 dict->SetBooleanWithoutPathExpansion(kAcknowledged, acknowledged); |
| 118 | 121 |
| 119 if (!sync_processor_) | 122 if (!sync_processor_) |
| 120 return; | 123 return; |
| 121 | 124 |
| 122 SyncData data = CreateSyncDataForSetting(su_id, key, value, acknowledged); | 125 SyncData data = CreateSyncDataForSetting(su_id, key, value, acknowledged); |
| 123 SyncChange::SyncChangeType change_type = | 126 SyncChange::SyncChangeType change_type = |
| 124 has_key ? SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD; | 127 has_key ? SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD; |
| 125 SyncChangeList changes; | 128 SyncChangeList changes; |
| 126 changes.push_back(SyncChange(FROM_HERE, change_type, data)); | 129 changes.push_back(SyncChange(FROM_HERE, change_type, data)); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, sync_data.GetDataType()); | 226 DCHECK_EQ(SUPERVISED_USER_SHARED_SETTINGS, sync_data.GetDataType()); |
| 224 const ::sync_pb::ManagedUserSharedSettingSpecifics& | 227 const ::sync_pb::ManagedUserSharedSettingSpecifics& |
| 225 supervised_user_shared_setting = | 228 supervised_user_shared_setting = |
| 226 sync_data.GetSpecifics().managed_user_shared_setting(); | 229 sync_data.GetSpecifics().managed_user_shared_setting(); |
| 227 std::unique_ptr<Value> value = | 230 std::unique_ptr<Value> value = |
| 228 base::JSONReader::Read(supervised_user_shared_setting.value()); | 231 base::JSONReader::Read(supervised_user_shared_setting.value()); |
| 229 const std::string& su_id = supervised_user_shared_setting.mu_id(); | 232 const std::string& su_id = supervised_user_shared_setting.mu_id(); |
| 230 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id); | 233 ScopedSupervisedUserSharedSettingsUpdate update(prefs_, su_id); |
| 231 const std::string& key = supervised_user_shared_setting.key(); | 234 const std::string& key = supervised_user_shared_setting.key(); |
| 232 DictionaryValue* dict = FindOrCreateDictionary(update.Get(), key); | 235 DictionaryValue* dict = FindOrCreateDictionary(update.Get(), key); |
| 233 dict->SetWithoutPathExpansion(kValue, value.release()); | 236 dict->SetWithoutPathExpansion(kValue, std::move(value)); |
| 234 | 237 |
| 235 // Every setting we get from the server should have the acknowledged flag | 238 // Every setting we get from the server should have the acknowledged flag |
| 236 // set. | 239 // set. |
| 237 DCHECK(supervised_user_shared_setting.acknowledged()); | 240 DCHECK(supervised_user_shared_setting.acknowledged()); |
| 238 dict->SetBooleanWithoutPathExpansion( | 241 dict->SetBooleanWithoutPathExpansion( |
| 239 kAcknowledged, supervised_user_shared_setting.acknowledged()); | 242 kAcknowledged, supervised_user_shared_setting.acknowledged()); |
| 240 callbacks_.Notify(su_id, key); | 243 callbacks_.Notify(su_id, key); |
| 241 | 244 |
| 242 if (pref_seen_keys.find(su_id) == pref_seen_keys.end()) | 245 if (pref_seen_keys.find(su_id) == pref_seen_keys.end()) |
| 243 num_added++; | 246 num_added++; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 // flag set. | 337 // flag set. |
| 335 DCHECK(supervised_user_shared_setting.acknowledged()); | 338 DCHECK(supervised_user_shared_setting.acknowledged()); |
| 336 | 339 |
| 337 if (has_key) { | 340 if (has_key) { |
| 338 // If the supervised user already exists, it should be an update | 341 // If the supervised user already exists, it should be an update |
| 339 // action. | 342 // action. |
| 340 DCHECK_EQ(SyncChange::ACTION_UPDATE, sync_change.change_type()); | 343 DCHECK_EQ(SyncChange::ACTION_UPDATE, sync_change.change_type()); |
| 341 } else { | 344 } else { |
| 342 // Otherwise, it should be an add action. | 345 // Otherwise, it should be an add action. |
| 343 DCHECK_EQ(SyncChange::ACTION_ADD, sync_change.change_type()); | 346 DCHECK_EQ(SyncChange::ACTION_ADD, sync_change.change_type()); |
| 344 dict = new DictionaryValue; | 347 update_dict->SetWithoutPathExpansion( |
| 345 update_dict->SetWithoutPathExpansion(key, dict); | 348 key, base::MakeUnique<base::DictionaryValue>()); |
| 349 update_dict->GetDictionaryWithoutPathExpansion(key, &dict); | |
| 346 } | 350 } |
| 347 std::unique_ptr<Value> value = | 351 std::unique_ptr<Value> value = |
| 348 base::JSONReader::Read(supervised_user_shared_setting.value()); | 352 base::JSONReader::Read(supervised_user_shared_setting.value()); |
| 349 dict->SetWithoutPathExpansion(kValue, value.release()); | 353 dict->SetWithoutPathExpansion(kValue, std::move(value)); |
| 350 dict->SetBooleanWithoutPathExpansion( | 354 dict->SetBooleanWithoutPathExpansion( |
| 351 kAcknowledged, supervised_user_shared_setting.acknowledged()); | 355 kAcknowledged, supervised_user_shared_setting.acknowledged()); |
| 352 break; | 356 break; |
| 353 } | 357 } |
| 354 case SyncChange::ACTION_DELETE: { | 358 case SyncChange::ACTION_DELETE: { |
| 355 if (has_key) | 359 if (has_key) |
| 356 update_dict->RemoveWithoutPathExpansion(key, nullptr); | 360 update_dict->RemoveWithoutPathExpansion(key, nullptr); |
| 357 else | 361 else |
| 358 NOTREACHED() << "Trying to delete nonexistent key " << key; | 362 NOTREACHED() << "Trying to delete nonexistent key " << key; |
| 359 break; | 363 break; |
| 360 } | 364 } |
| 361 case SyncChange::ACTION_INVALID: { | 365 case SyncChange::ACTION_INVALID: { |
| 362 NOTREACHED(); | 366 NOTREACHED(); |
| 363 break; | 367 break; |
| 364 } | 368 } |
| 365 } | 369 } |
| 366 callbacks_.Notify(su_id, key); | 370 callbacks_.Notify(su_id, key); |
| 367 } | 371 } |
| 368 | 372 |
| 369 SyncError error; | 373 SyncError error; |
| 370 return error; | 374 return error; |
| 371 } | 375 } |
| OLD | NEW |