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/files/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 scoped_refptr
<base::SequencedTaskRunner
> task_runner
=
83 JsonPrefStore::GetTaskRunnerForFile(config_path_
, worker_pool_
.get());
84 prefServiceFactory
.SetUserPrefsFile(config_path_
, task_runner
.get());
85 prefServiceFactory
.set_async(false);
86 prefServiceFactory
.set_read_error_callback(
87 base::Bind(&UserPrefsLoadError
, &prefs_read_error
));
88 pref_service_
= prefServiceFactory
.Create(registry
);
90 if (prefs_read_error
== PersistentPrefStore::PREF_READ_ERROR_NONE
) {
93 LOG(ERROR
) << "Cannot initialize chromecast config: "
94 << config_path_
.value()
95 << ", pref_error=" << prefs_read_error
;
100 void ChromecastConfig::Save() const {
101 DCHECK(thread_checker_
.CalledOnValidThread());
102 VLOG(1) << "Saving config to: " << config_path_
.value();
103 pref_service_
->CommitPendingWrite();
106 const std::string
ChromecastConfig::GetValue(const std::string
& key
) const {
107 DCHECK(thread_checker_
.CalledOnValidThread());
108 return pref_service_
->GetString(key
.c_str());
111 const int ChromecastConfig::GetIntValue(const std::string
& key
) const {
112 return pref_service_
->GetInteger(key
.c_str());
115 void ChromecastConfig::SetValue(
116 const std::string
& key
,
117 const std::string
& value
) const {
118 DCHECK(thread_checker_
.CalledOnValidThread());
119 if (pref_service_
->IsUserModifiablePreference(key
.c_str())) {
120 VLOG(1) << "Set config: key=" << key
<< ", value=" << value
;
121 pref_service_
->SetString(key
.c_str(), value
);
123 LOG(ERROR
) << "Cannot set read-only config: key=" << key
124 << ", value=" << value
;
128 void ChromecastConfig::SetIntValue(const std::string
& key
, int value
) const {
129 DCHECK(thread_checker_
.CalledOnValidThread());
130 if (pref_service_
->IsUserModifiablePreference(key
.c_str())) {
131 VLOG(1) << "Set config: key=" << key
<< ", value=" << value
;
132 pref_service_
->SetInteger(key
.c_str(), value
);
134 LOG(ERROR
) << "Cannot set read-only config: key=" << key
135 << ", value=" << value
;
139 bool ChromecastConfig::HasValue(const std::string
& key
) const {
140 DCHECK(thread_checker_
.CalledOnValidThread());
141 return pref_service_
->HasPrefPath(key
.c_str());
144 } // namespace chromecast