| 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 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/user_metrics.h" | 15 #include "base/metrics/user_metrics.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/threading/sequenced_worker_pool.h" | 17 #include "base/threading/sequenced_worker_pool.h" |
| 18 #include "base/values.h" |
| 18 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" | 19 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" |
| 19 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
| 20 #include "components/prefs/json_pref_store.h" | 21 #include "components/prefs/json_pref_store.h" |
| 21 #include "components/prefs/pref_filter.h" | 22 #include "components/prefs/pref_filter.h" |
| 22 #include "components/sync/model/sync_change.h" | 23 #include "components/sync/model/sync_change.h" |
| 23 #include "components/sync/model/sync_error_factory.h" | 24 #include "components/sync/model/sync_error_factory.h" |
| 24 #include "components/sync/protocol/sync.pb.h" | 25 #include "components/sync/protocol/sync.pb.h" |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 26 | 27 |
| 27 using base::DictionaryValue; | 28 using base::DictionaryValue; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 base::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Queued")); | 167 base::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Queued")); |
| 167 dict = GetQueuedItems(); | 168 dict = GetQueuedItems(); |
| 168 } | 169 } |
| 169 dict->SetWithoutPathExpansion(key_suffix, std::move(value)); | 170 dict->SetWithoutPathExpansion(key_suffix, std::move(value)); |
| 170 } | 171 } |
| 171 | 172 |
| 172 void SupervisedUserSettingsService::SetLocalSetting( | 173 void SupervisedUserSettingsService::SetLocalSetting( |
| 173 const std::string& key, | 174 const std::string& key, |
| 174 std::unique_ptr<base::Value> value) { | 175 std::unique_ptr<base::Value> value) { |
| 175 if (value) | 176 if (value) |
| 176 local_settings_->SetWithoutPathExpansion(key, value.release()); | 177 local_settings_->SetWithoutPathExpansion(key, std::move(value)); |
| 177 else | 178 else |
| 178 local_settings_->RemoveWithoutPathExpansion(key, nullptr); | 179 local_settings_->RemoveWithoutPathExpansion(key, nullptr); |
| 179 | 180 |
| 180 InformSubscribers(); | 181 InformSubscribers(); |
| 181 } | 182 } |
| 182 | 183 |
| 183 // static | 184 // static |
| 184 SyncData SupervisedUserSettingsService::CreateSyncDataForSetting( | 185 SyncData SupervisedUserSettingsService::CreateSyncDataForSetting( |
| 185 const std::string& name, | 186 const std::string& name, |
| 186 const base::Value& value) { | 187 const base::Value& value) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 // SetWithoutPathExpansion below requires non-null values. | 246 // SetWithoutPathExpansion below requires non-null values. |
| 246 if (!value) { | 247 if (!value) { |
| 247 DLOG(ERROR) << "Invalid managed user setting value: " | 248 DLOG(ERROR) << "Invalid managed user setting value: " |
| 248 << supervised_user_setting.value() | 249 << supervised_user_setting.value() |
| 249 << ". Values must be JSON values."; | 250 << ". Values must be JSON values."; |
| 250 continue; | 251 continue; |
| 251 } | 252 } |
| 252 std::string name_suffix = supervised_user_setting.name(); | 253 std::string name_suffix = supervised_user_setting.name(); |
| 253 std::string name_key = name_suffix; | 254 std::string name_key = name_suffix; |
| 254 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&name_suffix); | 255 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&name_suffix); |
| 255 dict->SetWithoutPathExpansion(name_suffix, value.release()); | 256 dict->SetWithoutPathExpansion(name_suffix, std::move(value)); |
| 256 if (seen_keys.find(name_key) == seen_keys.end()) { | 257 if (seen_keys.find(name_key) == seen_keys.end()) { |
| 257 added_sync_keys.insert(name_key); | 258 added_sync_keys.insert(name_key); |
| 258 num_added++; | 259 num_added++; |
| 259 } else { | 260 } else { |
| 260 num_modified++; | 261 num_modified++; |
| 261 } | 262 } |
| 262 } | 263 } |
| 263 | 264 |
| 264 num_deleted -= num_modified; | 265 num_deleted -= num_modified; |
| 265 | 266 |
| 266 store_->ReportValueChanged(kAtomicSettings, | 267 store_->ReportValueChanged(kAtomicSettings, |
| 267 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 268 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 268 store_->ReportValueChanged(kSplitSettings, | 269 store_->ReportValueChanged(kSplitSettings, |
| 269 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 270 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 270 InformSubscribers(); | 271 InformSubscribers(); |
| 271 | 272 |
| 272 // Upload all the queued up items (either with an ADD or an UPDATE action, | 273 // Upload all the queued up items (either with an ADD or an UPDATE action, |
| 273 // depending on whether they already exist) and move them to split settings. | 274 // depending on whether they already exist) and move them to split settings. |
| 274 SyncChangeList change_list; | 275 SyncChangeList change_list; |
| 275 for (base::DictionaryValue::Iterator it(*queued_items); !it.IsAtEnd(); | 276 for (base::DictionaryValue::Iterator it(*queued_items); !it.IsAtEnd(); |
| 276 it.Advance()) { | 277 it.Advance()) { |
| 277 std::string key_suffix = it.key(); | 278 std::string key_suffix = it.key(); |
| 278 std::string name_key = key_suffix; | 279 std::string name_key = key_suffix; |
| 279 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&key_suffix); | 280 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&key_suffix); |
| 280 SyncData data = CreateSyncDataForSetting(it.key(), it.value()); | 281 SyncData data = CreateSyncDataForSetting(it.key(), it.value()); |
| 281 SyncChange::SyncChangeType change_type = | 282 SyncChange::SyncChangeType change_type = |
| 282 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE | 283 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE |
| 283 : SyncChange::ACTION_ADD; | 284 : SyncChange::ACTION_ADD; |
| 284 change_list.push_back(SyncChange(FROM_HERE, change_type, data)); | 285 change_list.push_back(SyncChange(FROM_HERE, change_type, data)); |
| 285 dict->SetWithoutPathExpansion(key_suffix, it.value().DeepCopy()); | 286 dict->SetWithoutPathExpansion(key_suffix, |
| 287 base::MakeUnique<base::Value>(it.value())); |
| 286 if (added_sync_keys.find(name_key) != added_sync_keys.end()) { | 288 if (added_sync_keys.find(name_key) != added_sync_keys.end()) { |
| 287 num_added--; | 289 num_added--; |
| 288 } | 290 } |
| 289 } | 291 } |
| 290 queued_items->Clear(); | 292 queued_items->Clear(); |
| 291 | 293 |
| 292 SyncMergeResult result(SUPERVISED_USER_SETTINGS); | 294 SyncMergeResult result(SUPERVISED_USER_SETTINGS); |
| 293 // Process all the accumulated changes from the queued items. | 295 // Process all the accumulated changes from the queued items. |
| 294 if (change_list.size() > 0) { | 296 if (change_list.size() > 0) { |
| 295 store_->ReportValueChanged(kQueuedItems, | 297 store_->ReportValueChanged(kQueuedItems, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 case SyncChange::ACTION_UPDATE: { | 367 case SyncChange::ACTION_UPDATE: { |
| 366 std::unique_ptr<base::Value> value = | 368 std::unique_ptr<base::Value> value = |
| 367 JSONReader::Read(supervised_user_setting.value()); | 369 JSONReader::Read(supervised_user_setting.value()); |
| 368 if (dict->HasKey(key)) { | 370 if (dict->HasKey(key)) { |
| 369 DLOG_IF(WARNING, change_type == SyncChange::ACTION_ADD) | 371 DLOG_IF(WARNING, change_type == SyncChange::ACTION_ADD) |
| 370 << "Value for key " << key << " already exists"; | 372 << "Value for key " << key << " already exists"; |
| 371 } else { | 373 } else { |
| 372 DLOG_IF(WARNING, change_type == SyncChange::ACTION_UPDATE) | 374 DLOG_IF(WARNING, change_type == SyncChange::ACTION_UPDATE) |
| 373 << "Value for key " << key << " doesn't exist yet"; | 375 << "Value for key " << key << " doesn't exist yet"; |
| 374 } | 376 } |
| 375 dict->SetWithoutPathExpansion(key, value.release()); | 377 dict->SetWithoutPathExpansion(key, std::move(value)); |
| 376 break; | 378 break; |
| 377 } | 379 } |
| 378 case SyncChange::ACTION_DELETE: { | 380 case SyncChange::ACTION_DELETE: { |
| 379 DLOG_IF(WARNING, !dict->HasKey(key)) << "Trying to delete nonexistent " | 381 DLOG_IF(WARNING, !dict->HasKey(key)) << "Trying to delete nonexistent " |
| 380 << "key " << key; | 382 << "key " << key; |
| 381 dict->RemoveWithoutPathExpansion(key, nullptr); | 383 dict->RemoveWithoutPathExpansion(key, nullptr); |
| 382 break; | 384 break; |
| 383 } | 385 } |
| 384 case SyncChange::ACTION_INVALID: { | 386 case SyncChange::ACTION_INVALID: { |
| 385 NOTREACHED(); | 387 NOTREACHED(); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 return settings; | 491 return settings; |
| 490 } | 492 } |
| 491 | 493 |
| 492 void SupervisedUserSettingsService::InformSubscribers() { | 494 void SupervisedUserSettingsService::InformSubscribers() { |
| 493 if (!IsReady()) | 495 if (!IsReady()) |
| 494 return; | 496 return; |
| 495 | 497 |
| 496 std::unique_ptr<base::DictionaryValue> settings = GetSettings(); | 498 std::unique_ptr<base::DictionaryValue> settings = GetSettings(); |
| 497 callback_list_.Notify(settings.get()); | 499 callback_list_.Notify(settings.get()); |
| 498 } | 500 } |
| OLD | NEW |