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"
20 #include "chromecast/metrics/cast_metrics_prefs.h"
22 namespace chromecast
{
26 // Config file IO worker constants.
27 const int kNumOfConfigFileIOWorkers
= 1;
28 const char kNameOfConfigFileIOWorkers
[] = "ConfigFileIO";
30 void UserPrefsLoadError(
31 PersistentPrefStore::PrefReadError
* error_val
,
32 PersistentPrefStore::PrefReadError error
) {
37 base::FilePath
GetConfigPath() {
38 base::FilePath config_path
;
39 CHECK(PathService::Get(FILE_CAST_CONFIG
, &config_path
));
46 ChromecastConfig
* ChromecastConfig::g_instance_
= NULL
;
49 void ChromecastConfig::Create(PrefRegistrySimple
* registry
) {
50 DCHECK(g_instance_
== NULL
);
51 g_instance_
= new ChromecastConfig();
52 g_instance_
->Load(registry
);
56 ChromecastConfig
* ChromecastConfig::GetInstance() {
57 DCHECK(g_instance_
!= NULL
);
61 ChromecastConfig::ChromecastConfig()
62 : config_path_(GetConfigPath()),
63 worker_pool_(new base::SequencedWorkerPool(kNumOfConfigFileIOWorkers
,
64 kNameOfConfigFileIOWorkers
)) {
67 ChromecastConfig::~ChromecastConfig() {
68 // Explict writing before worker_pool shutdown.
69 pref_service_
->CommitPendingWrite();
70 worker_pool_
->Shutdown();
73 bool ChromecastConfig::Load(PrefRegistrySimple
* registry
) {
74 DCHECK(thread_checker_
.CalledOnValidThread());
75 VLOG(1) << "Loading config from " << config_path_
.value();
76 registry
->RegisterIntegerPref(prefs::kRemoteDebuggingPort
, 0);
78 metrics::RegisterPrefs(registry
);
79 RegisterPlatformPrefs(registry
);
81 PersistentPrefStore::PrefReadError prefs_read_error
=
82 PersistentPrefStore::PREF_READ_ERROR_NONE
;
83 base::PrefServiceFactory prefServiceFactory
;
84 prefServiceFactory
.SetUserPrefsFile(config_path_
,
85 JsonPrefStore::GetTaskRunnerForFile(config_path_
, worker_pool_
));
86 prefServiceFactory
.set_async(false);
87 prefServiceFactory
.set_read_error_callback(
88 base::Bind(&UserPrefsLoadError
, &prefs_read_error
));
89 pref_service_
= prefServiceFactory
.Create(registry
);
91 if (prefs_read_error
== PersistentPrefStore::PREF_READ_ERROR_NONE
) {
94 LOG(ERROR
) << "Cannot initialize chromecast config: "
95 << config_path_
.value()
96 << ", pref_error=" << prefs_read_error
;
101 void ChromecastConfig::Save() const {
102 DCHECK(thread_checker_
.CalledOnValidThread());
103 VLOG(1) << "Saving config to: " << config_path_
.value();
104 pref_service_
->CommitPendingWrite();
107 const std::string
ChromecastConfig::GetValue(const std::string
& key
) const {
108 DCHECK(thread_checker_
.CalledOnValidThread());
109 return pref_service_
->GetString(key
.c_str());
112 const int ChromecastConfig::GetIntValue(const std::string
& key
) const {
113 return pref_service_
->GetInteger(key
.c_str());
116 void ChromecastConfig::SetValue(
117 const std::string
& key
,
118 const std::string
& value
) const {
119 DCHECK(thread_checker_
.CalledOnValidThread());
120 if (pref_service_
->IsUserModifiablePreference(key
.c_str())) {
121 VLOG(1) << "Set config: key=" << key
<< ", value=" << value
;
122 pref_service_
->SetString(key
.c_str(), value
);
124 LOG(ERROR
) << "Cannot set read-only config: key=" << key
125 << ", value=" << value
;
129 void ChromecastConfig::SetIntValue(const std::string
& key
, int value
) const {
130 DCHECK(thread_checker_
.CalledOnValidThread());
131 if (pref_service_
->IsUserModifiablePreference(key
.c_str())) {
132 VLOG(1) << "Set config: key=" << key
<< ", value=" << value
;
133 pref_service_
->SetInteger(key
.c_str(), value
);
135 LOG(ERROR
) << "Cannot set read-only config: key=" << key
136 << ", value=" << value
;
140 } // namespace chromecast