pref_service_helper.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  3. // found in the LICENSE file.
  4. #include "chromecast/browser/pref_service_helper.h"
  5. #include <string>
  6. #include "base/bind.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/logging.h"
  10. #include "base/path_service.h"
  11. #include "build/build_config.h"
  12. #include "chromecast/base/cast_paths.h"
  13. #include "chromecast/base/pref_names.h"
  14. #include "chromecast/chromecast_buildflags.h"
  15. #include "components/cdm/browser/media_drm_storage_impl.h"
  16. #include "components/prefs/json_pref_store.h"
  17. #include "components/prefs/pref_registry_simple.h"
  18. #include "components/prefs/pref_service_factory.h"
  19. #include "components/prefs/pref_store.h"
  20. #include "components/prefs/segregated_pref_store.h"
  21. namespace chromecast {
  22. namespace shell {
  23. namespace {
  24. void UserPrefsLoadError(PersistentPrefStore::PrefReadError* error_val,
  25. PersistentPrefStore::PrefReadError error) {
  26. DCHECK(error_val);
  27. *error_val = error;
  28. }
  29. base::FilePath GetConfigPath(ProcessType process_type) {
  30. base::FilePath config_path;
  31. switch (process_type) {
  32. case ProcessType::kCastBrowser:
  33. CHECK(base::PathService::Get(FILE_CAST_BROWSER_CONFIG, &config_path));
  34. break;
  35. case ProcessType::kCastService:
  36. CHECK(base::PathService::Get(FILE_CAST_CONFIG, &config_path));
  37. // No default. All possible cases are handled above.
  38. }
  39. CHECK(!config_path.empty());
  40. return config_path;
  41. }
  42. base::FilePath GetLargeConfigPath(ProcessType process_type) {
  43. return GetConfigPath(process_type).AddExtension(".large");
  44. }
  45. scoped_refptr<PersistentPrefStore> MakePrefStore(ProcessType process_type) {
  46. auto default_pref_store =
  47. base::MakeRefCounted<JsonPrefStore>(GetConfigPath(process_type));
  48. std::set<std::string> selected_pref_names;
  49. if (PrefServiceHelper::LargePrefNames) {
  50. selected_pref_names = PrefServiceHelper::LargePrefNames();
  51. }
  52. if (selected_pref_names.empty()) {
  53. return default_pref_store;
  54. }
  55. auto large_pref_store =
  56. base::MakeRefCounted<JsonPrefStore>(GetLargeConfigPath(process_type));
  57. // Move large prefs out of the default pref store, if necessary.
  58. if (default_pref_store->ReadPrefs() ==
  59. PersistentPrefStore::PREF_READ_ERROR_NONE) {
  60. large_pref_store->ReadPrefs();
  61. for (const std::string& pref_name : selected_pref_names) {
  62. const base::Value* pref_value = nullptr;
  63. if (!large_pref_store->GetValue(pref_name, &pref_value)) {
  64. // Copy from default prefs, if possible.
  65. if (default_pref_store->GetValue(pref_name, &pref_value)) {
  66. large_pref_store->SetValue(
  67. pref_name, std::make_unique<base::Value>(pref_value->Clone()), 0);
  68. }
  69. }
  70. default_pref_store->RemoveValue(pref_name, 0);
  71. }
  72. }
  73. return base::MakeRefCounted<SegregatedPrefStore>(
  74. std::move(default_pref_store), std::move(large_pref_store),
  75. selected_pref_names);
  76. }
  77. } // namespace
  78. // static
  79. std::unique_ptr<PrefService> PrefServiceHelper::CreatePrefService(
  80. PrefRegistrySimple* registry,
  81. ProcessType process_type) {
  82. const base::FilePath config_path(GetConfigPath(process_type));
  83. DVLOG(1) << "Loading config from " << config_path.value();
  84. registry->RegisterBooleanPref(prefs::kMetricsIsNewClientID, false);
  85. // Opt-in stats default to true to handle two different cases:
  86. // 1) Any crashes or UMA logs are recorded prior to setup completing
  87. // successfully (even though we can't send them yet). Unless the user
  88. // ends up actually opting out, we don't want to lose this data once
  89. // we get network connectivity and are able to send it. If the user
  90. // opts out, nothing further will be sent (honoring the user's setting).
  91. // 2) Dogfood users (see dogfood agreement).
  92. registry->RegisterBooleanPref(prefs::kOptInStats, true);
  93. registry->RegisterListPref(prefs::kActiveDCSExperiments);
  94. registry->RegisterDictionaryPref(prefs::kLatestDCSFeatures);
  95. registry->RegisterIntegerPref(prefs::kWebColorScheme, 0);
  96. cdm::MediaDrmStorageImpl::RegisterProfilePrefs(registry);
  97. RegisterPlatformPrefs(registry);
  98. PrefServiceFactory pref_service_factory;
  99. pref_service_factory.set_user_prefs(MakePrefStore(process_type));
  100. pref_service_factory.set_async(false);
  101. PersistentPrefStore::PrefReadError prefs_read_error =
  102. PersistentPrefStore::PREF_READ_ERROR_NONE;
  103. pref_service_factory.set_read_error_callback(
  104. base::BindRepeating(&UserPrefsLoadError, &prefs_read_error));
  105. std::unique_ptr<PrefService> pref_service(
  106. pref_service_factory.Create(registry));
  107. if (prefs_read_error != PersistentPrefStore::PREF_READ_ERROR_NONE) {
  108. LOG(ERROR) << "Cannot initialize chromecast config: "
  109. << config_path.value()
  110. << ", pref_error=" << prefs_read_error;
  111. }
  112. OnPrefsLoaded(pref_service.get());
  113. return pref_service;
  114. }
  115. } // namespace shell
  116. } // namespace chromecast