sync_policy_handler.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2013 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. #include "components/sync/driver/sync_policy_handler.h"
  5. #include <string>
  6. #include "base/values.h"
  7. #include "build/chromeos_buildflags.h"
  8. #include "components/policy/core/common/policy_map.h"
  9. #include "components/policy/policy_constants.h"
  10. #include "components/prefs/pref_value_map.h"
  11. #include "components/sync/base/pref_names.h"
  12. #include "components/sync/base/sync_prefs.h"
  13. #include "components/sync/base/user_selectable_type.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. #if BUILDFLAG(IS_CHROMEOS_ASH)
  16. #include "ash/constants/ash_features.h"
  17. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  18. namespace syncer {
  19. namespace {
  20. void DisableSyncType(const std::string& type_name, PrefValueMap* prefs) {
  21. absl::optional<UserSelectableType> type =
  22. GetUserSelectableTypeFromString(type_name);
  23. if (type.has_value()) {
  24. const char* pref = SyncPrefs::GetPrefNameForType(*type);
  25. if (pref)
  26. prefs->SetValue(pref, base::Value(false));
  27. }
  28. #if BUILDFLAG(IS_CHROMEOS_ASH)
  29. if (chromeos::features::IsSyncSettingsCategorizationEnabled()) {
  30. // Check for OS types. This includes types that used to be browser types,
  31. // like "apps" and "preferences".
  32. absl::optional<UserSelectableOsType> os_type =
  33. GetUserSelectableOsTypeFromString(type_name);
  34. if (os_type.has_value()) {
  35. const char* os_pref = SyncPrefs::GetPrefNameForOsType(*os_type);
  36. if (os_pref)
  37. prefs->SetValue(os_pref, base::Value(false));
  38. }
  39. }
  40. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  41. }
  42. } // namespace
  43. SyncPolicyHandler::SyncPolicyHandler()
  44. : policy::TypeCheckingPolicyHandler(policy::key::kSyncDisabled,
  45. base::Value::Type::BOOLEAN) {}
  46. SyncPolicyHandler::~SyncPolicyHandler() = default;
  47. void SyncPolicyHandler::ApplyPolicySettings(const policy::PolicyMap& policies,
  48. PrefValueMap* prefs) {
  49. const base::Value* disable_sync_value =
  50. policies.GetValue(policy_name(), base::Value::Type::BOOLEAN);
  51. if (disable_sync_value && disable_sync_value->GetBool()) {
  52. prefs->SetValue(prefs::kSyncManaged, disable_sync_value->Clone());
  53. }
  54. const base::Value* disabled_sync_types_value = policies.GetValue(
  55. policy::key::kSyncTypesListDisabled, base::Value::Type::LIST);
  56. if (disabled_sync_types_value) {
  57. base::Value::ConstListView list =
  58. disabled_sync_types_value->GetListDeprecated();
  59. for (const base::Value& type_name : list) {
  60. if (!type_name.is_string())
  61. continue;
  62. DisableSyncType(type_name.GetString(), prefs);
  63. }
  64. }
  65. }
  66. } // namespace syncer