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.
5 #include "chromecast/common/chromecast_config.h"
9 #include "base/command_line.h"
10 #include "base/file_util.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/prefs/json_pref_store.h"
14 #include "base/prefs/pref_registry_simple.h"
15 #include "base/prefs/pref_service_factory.h"
16 #include "base/prefs/pref_store.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "chromecast/common/cast_paths.h"
19 #include "chromecast/common/pref_names.h"
21 namespace chromecast {
25 // Config file IO worker constants.
26 const int kNumOfConfigFileIOWorkers = 1;
27 const char kNameOfConfigFileIOWorkers[] = "ConfigFileIO";
29 void UserPrefsLoadError(
30 PersistentPrefStore::PrefReadError* error_val,
31 PersistentPrefStore::PrefReadError error) {
36 base::FilePath GetConfigPath() {
37 base::FilePath config_path;
38 CHECK(PathService::Get(FILE_CAST_CONFIG, &config_path));
45 ChromecastConfig* ChromecastConfig::g_instance_ = NULL;
48 void ChromecastConfig::Create(PrefRegistrySimple* registry) {
49 DCHECK(g_instance_ == NULL);
50 g_instance_ = new ChromecastConfig();
51 g_instance_->Load(registry);
55 ChromecastConfig* ChromecastConfig::GetInstance() {
56 DCHECK(g_instance_ != NULL);
60 ChromecastConfig::ChromecastConfig()
61 : config_path_(GetConfigPath()),
62 worker_pool_(new base::SequencedWorkerPool(kNumOfConfigFileIOWorkers,
63 kNameOfConfigFileIOWorkers)) {
66 ChromecastConfig::~ChromecastConfig() {
67 // Explict writing before worker_pool shutdown.
68 pref_service_->CommitPendingWrite();
69 worker_pool_->Shutdown();
72 bool ChromecastConfig::Load(PrefRegistrySimple* registry) {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 VLOG(1) << "Loading config from " << config_path_.value();
75 registry->RegisterIntegerPref(prefs::kRemoteDebuggingPort, 0);
77 RegisterPlatformPrefs(registry);
79 PersistentPrefStore::PrefReadError prefs_read_error =
80 PersistentPrefStore::PREF_READ_ERROR_NONE;
81 base::PrefServiceFactory prefServiceFactory;
82 prefServiceFactory.SetUserPrefsFile(config_path_,
83 JsonPrefStore::GetTaskRunnerForFile(config_path_, worker_pool_));
84 prefServiceFactory.set_async(false);
85 prefServiceFactory.set_read_error_callback(
86 base::Bind(&UserPrefsLoadError, &prefs_read_error));
87 pref_service_ = prefServiceFactory.Create(registry);
89 if (prefs_read_error == PersistentPrefStore::PREF_READ_ERROR_NONE) {
92 LOG(ERROR) << "Cannot initialize chromecast config: "
93 << config_path_.value()
94 << ", pref_error=" << prefs_read_error;
99 void ChromecastConfig::Save() const {
100 DCHECK(thread_checker_.CalledOnValidThread());
101 VLOG(1) << "Saving config to: " << config_path_.value();
102 pref_service_->CommitPendingWrite();
105 const std::string ChromecastConfig::GetValue(const std::string& key) const {
106 DCHECK(thread_checker_.CalledOnValidThread());
107 return pref_service_->GetString(key.c_str());
110 const int ChromecastConfig::GetIntValue(const std::string& key) const {
111 return pref_service_->GetInteger(key.c_str());
114 void ChromecastConfig::SetValue(
115 const std::string& key,
116 const std::string& value) const {
117 DCHECK(thread_checker_.CalledOnValidThread());
118 if (pref_service_->IsUserModifiablePreference(key.c_str())) {
119 VLOG(1) << "Set config: key=" << key << ", value=" << value;
120 pref_service_->SetString(key.c_str(), value);
122 LOG(ERROR) << "Cannot set read-only config: key=" << key
123 << ", value=" << value;
127 void ChromecastConfig::SetIntValue(const std::string& key, int value) const {
128 DCHECK(thread_checker_.CalledOnValidThread());
129 if (pref_service_->IsUserModifiablePreference(key.c_str())) {
130 VLOG(1) << "Set config: key=" << key << ", value=" << value;
131 pref_service_->SetInteger(key.c_str(), value);
133 LOG(ERROR) << "Cannot set read-only config: key=" << key
134 << ", value=" << value;
138 } // namespace chromecast