OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "chromecast/common/chromecast_config.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 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 |
| 20 namespace chromecast { |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Config file IO worker constants. |
| 25 const int kNumOfConfigFileIOWorkers = 1; |
| 26 const char kNameOfConfigFileIOWorkers[] = "ConfigFileIO"; |
| 27 |
| 28 void UserPrefsLoadError( |
| 29 PersistentPrefStore::PrefReadError* error_val, |
| 30 PersistentPrefStore::PrefReadError error) { |
| 31 DCHECK(error_val); |
| 32 *error_val = error; |
| 33 } |
| 34 |
| 35 base::FilePath GetConfigPath() { |
| 36 base::FilePath config_path; |
| 37 CHECK(PathService::Get(FILE_CAST_CONFIG, &config_path)); |
| 38 return config_path; |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 // static |
| 44 ChromecastConfig* ChromecastConfig::g_instance_ = NULL; |
| 45 |
| 46 // static |
| 47 void ChromecastConfig::Create(PrefRegistrySimple* registry) { |
| 48 DCHECK(g_instance_ == NULL); |
| 49 g_instance_ = new ChromecastConfig(); |
| 50 g_instance_->Load(registry); |
| 51 } |
| 52 |
| 53 // static |
| 54 ChromecastConfig* ChromecastConfig::GetInstance() { |
| 55 DCHECK(g_instance_ != NULL); |
| 56 return g_instance_; |
| 57 } |
| 58 |
| 59 ChromecastConfig::ChromecastConfig() |
| 60 : config_path_(GetConfigPath()), |
| 61 worker_pool_(new base::SequencedWorkerPool(kNumOfConfigFileIOWorkers, |
| 62 kNameOfConfigFileIOWorkers)) { |
| 63 } |
| 64 |
| 65 ChromecastConfig::~ChromecastConfig() { |
| 66 // Explict writing before worker_pool shutdown. |
| 67 pref_service_->CommitPendingWrite(); |
| 68 worker_pool_->Shutdown(); |
| 69 } |
| 70 |
| 71 bool ChromecastConfig::Load(PrefRegistrySimple* registry) { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 VLOG(1) << "Loading config from " << config_path_.value(); |
| 74 |
| 75 RegisterPlatformPrefs(registry); |
| 76 |
| 77 PersistentPrefStore::PrefReadError prefs_read_error = |
| 78 PersistentPrefStore::PREF_READ_ERROR_NONE; |
| 79 base::PrefServiceFactory prefServiceFactory; |
| 80 prefServiceFactory.SetUserPrefsFile(config_path_, |
| 81 JsonPrefStore::GetTaskRunnerForFile(config_path_, worker_pool_)); |
| 82 prefServiceFactory.set_async(false); |
| 83 prefServiceFactory.set_read_error_callback( |
| 84 base::Bind(&UserPrefsLoadError, &prefs_read_error)); |
| 85 pref_service_ = prefServiceFactory.Create(registry); |
| 86 |
| 87 if (prefs_read_error == PersistentPrefStore::PREF_READ_ERROR_NONE) { |
| 88 return true; |
| 89 } else { |
| 90 LOG(ERROR) << "Cannot initialize chromecast config: " |
| 91 << config_path_.value() |
| 92 << ", pref_error=" << prefs_read_error; |
| 93 return false; |
| 94 } |
| 95 } |
| 96 |
| 97 void ChromecastConfig::Save() const { |
| 98 DCHECK(thread_checker_.CalledOnValidThread()); |
| 99 VLOG(1) << "Saving config to: " << config_path_.value(); |
| 100 pref_service_->CommitPendingWrite(); |
| 101 } |
| 102 |
| 103 const std::string ChromecastConfig::GetValue(const std::string& key) const { |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); |
| 105 return pref_service_->GetString(key.c_str()); |
| 106 } |
| 107 |
| 108 const int ChromecastConfig::GetIntValue(const std::string& key) const { |
| 109 return pref_service_->GetInteger(key.c_str()); |
| 110 } |
| 111 |
| 112 void ChromecastConfig::SetValue( |
| 113 const std::string& key, |
| 114 const std::string& value) const { |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 if (pref_service_->IsUserModifiablePreference(key.c_str())) { |
| 117 VLOG(1) << "Set config: key=" << key << ", value=" << value; |
| 118 pref_service_->SetString(key.c_str(), value); |
| 119 } else { |
| 120 LOG(ERROR) << "Cannot set read-only config: key=" << key |
| 121 << ", value=" << value; |
| 122 } |
| 123 } |
| 124 |
| 125 void ChromecastConfig::SetIntValue(const std::string& key, int value) const { |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); |
| 127 if (pref_service_->IsUserModifiablePreference(key.c_str())) { |
| 128 VLOG(1) << "Set config: key=" << key << ", value=" << value; |
| 129 pref_service_->SetInteger(key.c_str(), value); |
| 130 } else { |
| 131 LOG(ERROR) << "Cannot set read-only config: key=" << key |
| 132 << ", value=" << value; |
| 133 } |
| 134 } |
| 135 |
| 136 } // namespace chromecast |
OLD | NEW |