| 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/supervised_user_settings_service.h" | 5 #include "chrome/browser/supervised_user/supervised_user_settings_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 14 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 14 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 15 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" | 17 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" |
| 16 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
| 17 #include "components/prefs/json_pref_store.h" | 19 #include "components/prefs/json_pref_store.h" |
| 18 #include "components/prefs/pref_filter.h" | 20 #include "components/prefs/pref_filter.h" |
| 19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/user_metrics.h" | 22 #include "content/public/browser/user_metrics.h" |
| 21 #include "sync/api/sync_change.h" | 23 #include "sync/api/sync_change.h" |
| 22 #include "sync/api/sync_error_factory.h" | 24 #include "sync/api/sync_error_factory.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } | 63 } |
| 62 | 64 |
| 63 SupervisedUserSettingsService::~SupervisedUserSettingsService() {} | 65 SupervisedUserSettingsService::~SupervisedUserSettingsService() {} |
| 64 | 66 |
| 65 void SupervisedUserSettingsService::Init( | 67 void SupervisedUserSettingsService::Init( |
| 66 base::FilePath profile_path, | 68 base::FilePath profile_path, |
| 67 base::SequencedTaskRunner* sequenced_task_runner, | 69 base::SequencedTaskRunner* sequenced_task_runner, |
| 68 bool load_synchronously) { | 70 bool load_synchronously) { |
| 69 base::FilePath path = | 71 base::FilePath path = |
| 70 profile_path.Append(chrome::kSupervisedUserSettingsFilename); | 72 profile_path.Append(chrome::kSupervisedUserSettingsFilename); |
| 71 PersistentPrefStore* store = new JsonPrefStore( | 73 PersistentPrefStore* store = new JsonPrefStore(path, sequenced_task_runner, |
| 72 path, sequenced_task_runner, scoped_ptr<PrefFilter>()); | 74 std::unique_ptr<PrefFilter>()); |
| 73 Init(store); | 75 Init(store); |
| 74 if (load_synchronously) { | 76 if (load_synchronously) { |
| 75 store_->ReadPrefs(); | 77 store_->ReadPrefs(); |
| 76 // TODO(bauerb): Temporary CHECK while investigating | 78 // TODO(bauerb): Temporary CHECK while investigating |
| 77 // https://crbug.com/425785. Remove (or change to DCHECK) once the bug | 79 // https://crbug.com/425785. Remove (or change to DCHECK) once the bug |
| 78 // is fixed. | 80 // is fixed. |
| 79 CHECK(store_->IsInitializationComplete()); | 81 CHECK(store_->IsInitializationComplete()); |
| 80 } else { | 82 } else { |
| 81 store_->ReadPrefsAsync(nullptr); | 83 store_->ReadPrefsAsync(nullptr); |
| 82 } | 84 } |
| 83 } | 85 } |
| 84 | 86 |
| 85 void SupervisedUserSettingsService::Init( | 87 void SupervisedUserSettingsService::Init( |
| 86 scoped_refptr<PersistentPrefStore> store) { | 88 scoped_refptr<PersistentPrefStore> store) { |
| 87 DCHECK(!store_.get()); | 89 DCHECK(!store_.get()); |
| 88 store_ = store; | 90 store_ = store; |
| 89 store_->AddObserver(this); | 91 store_->AddObserver(this); |
| 90 } | 92 } |
| 91 | 93 |
| 92 scoped_ptr<SupervisedUserSettingsService::SettingsCallbackList::Subscription> | 94 std::unique_ptr< |
| 93 SupervisedUserSettingsService::Subscribe( | 95 SupervisedUserSettingsService::SettingsCallbackList::Subscription> |
| 94 const SettingsCallback& callback) { | 96 SupervisedUserSettingsService::Subscribe(const SettingsCallback& callback) { |
| 95 if (IsReady()) { | 97 if (IsReady()) { |
| 96 scoped_ptr<base::DictionaryValue> settings = GetSettings(); | 98 std::unique_ptr<base::DictionaryValue> settings = GetSettings(); |
| 97 callback.Run(settings.get()); | 99 callback.Run(settings.get()); |
| 98 } | 100 } |
| 99 | 101 |
| 100 return callback_list_.Add(callback); | 102 return callback_list_.Add(callback); |
| 101 } | 103 } |
| 102 | 104 |
| 103 Profile* SupervisedUserSettingsService::GetProfile(){ | 105 Profile* SupervisedUserSettingsService::GetProfile(){ |
| 104 return profile_; | 106 return profile_; |
| 105 } | 107 } |
| 106 | 108 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 122 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 124 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 123 } | 125 } |
| 124 | 126 |
| 125 // static | 127 // static |
| 126 std::string SupervisedUserSettingsService::MakeSplitSettingKey( | 128 std::string SupervisedUserSettingsService::MakeSplitSettingKey( |
| 127 const std::string& prefix, | 129 const std::string& prefix, |
| 128 const std::string& key) { | 130 const std::string& key) { |
| 129 return prefix + kSplitSettingKeySeparator + key; | 131 return prefix + kSplitSettingKeySeparator + key; |
| 130 } | 132 } |
| 131 | 133 |
| 132 void SupervisedUserSettingsService::UploadItem(const std::string& key, | 134 void SupervisedUserSettingsService::UploadItem( |
| 133 scoped_ptr<base::Value> value) { | 135 const std::string& key, |
| 136 std::unique_ptr<base::Value> value) { |
| 134 DCHECK(!SettingShouldApplyToPrefs(key)); | 137 DCHECK(!SettingShouldApplyToPrefs(key)); |
| 135 | 138 |
| 136 std::string key_suffix = key; | 139 std::string key_suffix = key; |
| 137 base::DictionaryValue* dict = nullptr; | 140 base::DictionaryValue* dict = nullptr; |
| 138 if (sync_processor_) { | 141 if (sync_processor_) { |
| 139 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Syncing")); | 142 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Syncing")); |
| 140 dict = GetDictionaryAndSplitKey(&key_suffix); | 143 dict = GetDictionaryAndSplitKey(&key_suffix); |
| 141 DCHECK(GetQueuedItems()->empty()); | 144 DCHECK(GetQueuedItems()->empty()); |
| 142 SyncChangeList change_list; | 145 SyncChangeList change_list; |
| 143 SyncData data = CreateSyncDataForSetting(key, *value); | 146 SyncData data = CreateSyncDataForSetting(key, *value); |
| 144 SyncChange::SyncChangeType change_type = | 147 SyncChange::SyncChangeType change_type = |
| 145 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE | 148 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE |
| 146 : SyncChange::ACTION_ADD; | 149 : SyncChange::ACTION_ADD; |
| 147 change_list.push_back(SyncChange(FROM_HERE, change_type, data)); | 150 change_list.push_back(SyncChange(FROM_HERE, change_type, data)); |
| 148 SyncError error = | 151 SyncError error = |
| 149 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); | 152 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); |
| 150 DCHECK(!error.IsSet()) << error.ToString(); | 153 DCHECK(!error.IsSet()) << error.ToString(); |
| 151 } else { | 154 } else { |
| 152 // Queue the item up to be uploaded when we start syncing | 155 // Queue the item up to be uploaded when we start syncing |
| 153 // (in MergeDataAndStartSyncing()). | 156 // (in MergeDataAndStartSyncing()). |
| 154 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Queued")); | 157 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Queued")); |
| 155 dict = GetQueuedItems(); | 158 dict = GetQueuedItems(); |
| 156 } | 159 } |
| 157 dict->SetWithoutPathExpansion(key_suffix, value.release()); | 160 dict->SetWithoutPathExpansion(key_suffix, value.release()); |
| 158 } | 161 } |
| 159 | 162 |
| 160 void SupervisedUserSettingsService::SetLocalSetting( | 163 void SupervisedUserSettingsService::SetLocalSetting( |
| 161 const std::string& key, | 164 const std::string& key, |
| 162 scoped_ptr<base::Value> value) { | 165 std::unique_ptr<base::Value> value) { |
| 163 if (value) | 166 if (value) |
| 164 local_settings_->SetWithoutPathExpansion(key, value.release()); | 167 local_settings_->SetWithoutPathExpansion(key, value.release()); |
| 165 else | 168 else |
| 166 local_settings_->RemoveWithoutPathExpansion(key, nullptr); | 169 local_settings_->RemoveWithoutPathExpansion(key, nullptr); |
| 167 | 170 |
| 168 InformSubscribers(); | 171 InformSubscribers(); |
| 169 } | 172 } |
| 170 | 173 |
| 171 // static | 174 // static |
| 172 SyncData SupervisedUserSettingsService::CreateSyncDataForSetting( | 175 SyncData SupervisedUserSettingsService::CreateSyncDataForSetting( |
| 173 const std::string& name, | 176 const std::string& name, |
| 174 const base::Value& value) { | 177 const base::Value& value) { |
| 175 std::string json_value; | 178 std::string json_value; |
| 176 base::JSONWriter::Write(value, &json_value); | 179 base::JSONWriter::Write(value, &json_value); |
| 177 ::sync_pb::EntitySpecifics specifics; | 180 ::sync_pb::EntitySpecifics specifics; |
| 178 specifics.mutable_managed_user_setting()->set_name(name); | 181 specifics.mutable_managed_user_setting()->set_name(name); |
| 179 specifics.mutable_managed_user_setting()->set_value(json_value); | 182 specifics.mutable_managed_user_setting()->set_value(json_value); |
| 180 return SyncData::CreateLocalData(name, name, specifics); | 183 return SyncData::CreateLocalData(name, name, specifics); |
| 181 } | 184 } |
| 182 | 185 |
| 183 void SupervisedUserSettingsService::Shutdown() { | 186 void SupervisedUserSettingsService::Shutdown() { |
| 184 store_->RemoveObserver(this); | 187 store_->RemoveObserver(this); |
| 185 } | 188 } |
| 186 | 189 |
| 187 SyncMergeResult SupervisedUserSettingsService::MergeDataAndStartSyncing( | 190 SyncMergeResult SupervisedUserSettingsService::MergeDataAndStartSyncing( |
| 188 ModelType type, | 191 ModelType type, |
| 189 const SyncDataList& initial_sync_data, | 192 const SyncDataList& initial_sync_data, |
| 190 scoped_ptr<SyncChangeProcessor> sync_processor, | 193 std::unique_ptr<SyncChangeProcessor> sync_processor, |
| 191 scoped_ptr<SyncErrorFactory> error_handler) { | 194 std::unique_ptr<SyncErrorFactory> error_handler) { |
| 192 DCHECK_EQ(SUPERVISED_USER_SETTINGS, type); | 195 DCHECK_EQ(SUPERVISED_USER_SETTINGS, type); |
| 193 sync_processor_ = std::move(sync_processor); | 196 sync_processor_ = std::move(sync_processor); |
| 194 error_handler_ = std::move(error_handler); | 197 error_handler_ = std::move(error_handler); |
| 195 | 198 |
| 196 std::set<std::string> seen_keys; | 199 std::set<std::string> seen_keys; |
| 197 int num_before_association = 0; | 200 int num_before_association = 0; |
| 198 // Getting number of atomic setting items. | 201 // Getting number of atomic setting items. |
| 199 num_before_association = GetAtomicSettings()->size(); | 202 num_before_association = GetAtomicSettings()->size(); |
| 200 for (base::DictionaryValue::Iterator it(*GetAtomicSettings()); !it.IsAtEnd(); | 203 for (base::DictionaryValue::Iterator it(*GetAtomicSettings()); !it.IsAtEnd(); |
| 201 it.Advance()) { | 204 it.Advance()) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 220 | 223 |
| 221 // Clear all atomic and split settings, then recreate them from Sync data. | 224 // Clear all atomic and split settings, then recreate them from Sync data. |
| 222 Clear(); | 225 Clear(); |
| 223 int num_added = 0; | 226 int num_added = 0; |
| 224 int num_modified = 0; | 227 int num_modified = 0; |
| 225 std::set<std::string> added_sync_keys; | 228 std::set<std::string> added_sync_keys; |
| 226 for (const SyncData& sync_data : initial_sync_data) { | 229 for (const SyncData& sync_data : initial_sync_data) { |
| 227 DCHECK_EQ(SUPERVISED_USER_SETTINGS, sync_data.GetDataType()); | 230 DCHECK_EQ(SUPERVISED_USER_SETTINGS, sync_data.GetDataType()); |
| 228 const ::sync_pb::ManagedUserSettingSpecifics& supervised_user_setting = | 231 const ::sync_pb::ManagedUserSettingSpecifics& supervised_user_setting = |
| 229 sync_data.GetSpecifics().managed_user_setting(); | 232 sync_data.GetSpecifics().managed_user_setting(); |
| 230 scoped_ptr<base::Value> value = | 233 std::unique_ptr<base::Value> value = |
| 231 JSONReader::Read(supervised_user_setting.value()); | 234 JSONReader::Read(supervised_user_setting.value()); |
| 232 std::string name_suffix = supervised_user_setting.name(); | 235 std::string name_suffix = supervised_user_setting.name(); |
| 233 std::string name_key = name_suffix; | 236 std::string name_key = name_suffix; |
| 234 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&name_suffix); | 237 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&name_suffix); |
| 235 dict->SetWithoutPathExpansion(name_suffix, value.release()); | 238 dict->SetWithoutPathExpansion(name_suffix, value.release()); |
| 236 if (seen_keys.find(name_key) == seen_keys.end()) { | 239 if (seen_keys.find(name_key) == seen_keys.end()) { |
| 237 added_sync_keys.insert(name_key); | 240 added_sync_keys.insert(name_key); |
| 238 num_added++; | 241 num_added++; |
| 239 } else { | 242 } else { |
| 240 num_modified++; | 243 num_modified++; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 SyncData data = sync_change.sync_data(); | 339 SyncData data = sync_change.sync_data(); |
| 337 DCHECK_EQ(SUPERVISED_USER_SETTINGS, data.GetDataType()); | 340 DCHECK_EQ(SUPERVISED_USER_SETTINGS, data.GetDataType()); |
| 338 const ::sync_pb::ManagedUserSettingSpecifics& supervised_user_setting = | 341 const ::sync_pb::ManagedUserSettingSpecifics& supervised_user_setting = |
| 339 data.GetSpecifics().managed_user_setting(); | 342 data.GetSpecifics().managed_user_setting(); |
| 340 std::string key = supervised_user_setting.name(); | 343 std::string key = supervised_user_setting.name(); |
| 341 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&key); | 344 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&key); |
| 342 SyncChange::SyncChangeType change_type = sync_change.change_type(); | 345 SyncChange::SyncChangeType change_type = sync_change.change_type(); |
| 343 switch (change_type) { | 346 switch (change_type) { |
| 344 case SyncChange::ACTION_ADD: | 347 case SyncChange::ACTION_ADD: |
| 345 case SyncChange::ACTION_UPDATE: { | 348 case SyncChange::ACTION_UPDATE: { |
| 346 scoped_ptr<base::Value> value = | 349 std::unique_ptr<base::Value> value = |
| 347 JSONReader::Read(supervised_user_setting.value()); | 350 JSONReader::Read(supervised_user_setting.value()); |
| 348 if (dict->HasKey(key)) { | 351 if (dict->HasKey(key)) { |
| 349 DLOG_IF(WARNING, change_type == SyncChange::ACTION_ADD) | 352 DLOG_IF(WARNING, change_type == SyncChange::ACTION_ADD) |
| 350 << "Value for key " << key << " already exists"; | 353 << "Value for key " << key << " already exists"; |
| 351 } else { | 354 } else { |
| 352 DLOG_IF(WARNING, change_type == SyncChange::ACTION_UPDATE) | 355 DLOG_IF(WARNING, change_type == SyncChange::ACTION_UPDATE) |
| 353 << "Value for key " << key << " doesn't exist yet"; | 356 << "Value for key " << key << " doesn't exist yet"; |
| 354 } | 357 } |
| 355 dict->SetWithoutPathExpansion(key, value.release()); | 358 dict->SetWithoutPathExpansion(key, value.release()); |
| 356 break; | 359 break; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 | 400 |
| 398 base::DictionaryValue* SupervisedUserSettingsService::GetOrCreateDictionary( | 401 base::DictionaryValue* SupervisedUserSettingsService::GetOrCreateDictionary( |
| 399 const std::string& key) const { | 402 const std::string& key) const { |
| 400 base::Value* value = nullptr; | 403 base::Value* value = nullptr; |
| 401 base::DictionaryValue* dict = nullptr; | 404 base::DictionaryValue* dict = nullptr; |
| 402 if (store_->GetMutableValue(key, &value)) { | 405 if (store_->GetMutableValue(key, &value)) { |
| 403 bool success = value->GetAsDictionary(&dict); | 406 bool success = value->GetAsDictionary(&dict); |
| 404 DCHECK(success); | 407 DCHECK(success); |
| 405 } else { | 408 } else { |
| 406 dict = new base::DictionaryValue; | 409 dict = new base::DictionaryValue; |
| 407 store_->SetValue(key, make_scoped_ptr(dict), | 410 store_->SetValue(key, base::WrapUnique(dict), |
| 408 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 411 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 409 } | 412 } |
| 410 | 413 |
| 411 return dict; | 414 return dict; |
| 412 } | 415 } |
| 413 | 416 |
| 414 base::DictionaryValue* | 417 base::DictionaryValue* |
| 415 SupervisedUserSettingsService::GetAtomicSettings() const { | 418 SupervisedUserSettingsService::GetAtomicSettings() const { |
| 416 return GetOrCreateDictionary(kAtomicSettings); | 419 return GetOrCreateDictionary(kAtomicSettings); |
| 417 } | 420 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 435 base::DictionaryValue* dict = nullptr; | 438 base::DictionaryValue* dict = nullptr; |
| 436 if (!split_settings->GetDictionary(prefix, &dict)) { | 439 if (!split_settings->GetDictionary(prefix, &dict)) { |
| 437 dict = new base::DictionaryValue; | 440 dict = new base::DictionaryValue; |
| 438 DCHECK(!split_settings->HasKey(prefix)); | 441 DCHECK(!split_settings->HasKey(prefix)); |
| 439 split_settings->Set(prefix, dict); | 442 split_settings->Set(prefix, dict); |
| 440 } | 443 } |
| 441 key->erase(0, pos + 1); | 444 key->erase(0, pos + 1); |
| 442 return dict; | 445 return dict; |
| 443 } | 446 } |
| 444 | 447 |
| 445 scoped_ptr<base::DictionaryValue> SupervisedUserSettingsService::GetSettings() { | 448 std::unique_ptr<base::DictionaryValue> |
| 449 SupervisedUserSettingsService::GetSettings() { |
| 446 DCHECK(IsReady()); | 450 DCHECK(IsReady()); |
| 447 if (!active_ || initialization_failed_) | 451 if (!active_ || initialization_failed_) |
| 448 return scoped_ptr<base::DictionaryValue>(); | 452 return std::unique_ptr<base::DictionaryValue>(); |
| 449 | 453 |
| 450 scoped_ptr<base::DictionaryValue> settings(local_settings_->DeepCopy()); | 454 std::unique_ptr<base::DictionaryValue> settings(local_settings_->DeepCopy()); |
| 451 | 455 |
| 452 base::DictionaryValue* atomic_settings = GetAtomicSettings(); | 456 base::DictionaryValue* atomic_settings = GetAtomicSettings(); |
| 453 for (base::DictionaryValue::Iterator it(*atomic_settings); !it.IsAtEnd(); | 457 for (base::DictionaryValue::Iterator it(*atomic_settings); !it.IsAtEnd(); |
| 454 it.Advance()) { | 458 it.Advance()) { |
| 455 if (!SettingShouldApplyToPrefs(it.key())) | 459 if (!SettingShouldApplyToPrefs(it.key())) |
| 456 continue; | 460 continue; |
| 457 | 461 |
| 458 settings->Set(it.key(), it.value().DeepCopy()); | 462 settings->Set(it.key(), it.value().DeepCopy()); |
| 459 } | 463 } |
| 460 | 464 |
| 461 base::DictionaryValue* split_settings = GetSplitSettings(); | 465 base::DictionaryValue* split_settings = GetSplitSettings(); |
| 462 for (base::DictionaryValue::Iterator it(*split_settings); !it.IsAtEnd(); | 466 for (base::DictionaryValue::Iterator it(*split_settings); !it.IsAtEnd(); |
| 463 it.Advance()) { | 467 it.Advance()) { |
| 464 if (!SettingShouldApplyToPrefs(it.key())) | 468 if (!SettingShouldApplyToPrefs(it.key())) |
| 465 continue; | 469 continue; |
| 466 | 470 |
| 467 settings->Set(it.key(), it.value().DeepCopy()); | 471 settings->Set(it.key(), it.value().DeepCopy()); |
| 468 } | 472 } |
| 469 | 473 |
| 470 return settings; | 474 return settings; |
| 471 } | 475 } |
| 472 | 476 |
| 473 void SupervisedUserSettingsService::InformSubscribers() { | 477 void SupervisedUserSettingsService::InformSubscribers() { |
| 474 if (!IsReady()) | 478 if (!IsReady()) |
| 475 return; | 479 return; |
| 476 | 480 |
| 477 scoped_ptr<base::DictionaryValue> settings = GetSettings(); | 481 std::unique_ptr<base::DictionaryValue> settings = GetSettings(); |
| 478 callback_list_.Notify(settings.get()); | 482 callback_list_.Notify(settings.get()); |
| 479 } | 483 } |
| OLD | NEW |