forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ChromecastConfig to manage cast_shell's PrefService.
R=lcwu@chromium.org,jam@chromium.org BUG=336640 Review URL: https://codereview.chromium.org/443833002 Cr-Commit-Position: refs/heads/master@{#288292} git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288292 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
gunsch@chromium.org
committed
Aug 8, 2014
1 parent
5378371
commit a15f8d8
Showing
6 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2014 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chromecast/common/chromecast_config.h" | ||
|
||
#include <string> | ||
|
||
#include "base/command_line.h" | ||
#include "base/file_util.h" | ||
#include "base/logging.h" | ||
#include "base/path_service.h" | ||
#include "base/prefs/json_pref_store.h" | ||
#include "base/prefs/pref_registry_simple.h" | ||
#include "base/prefs/pref_service_factory.h" | ||
#include "base/prefs/pref_store.h" | ||
#include "base/strings/string_number_conversions.h" | ||
#include "chromecast/common/cast_paths.h" | ||
|
||
namespace chromecast { | ||
|
||
namespace { | ||
|
||
// Config file IO worker constants. | ||
const int kNumOfConfigFileIOWorkers = 1; | ||
const char kNameOfConfigFileIOWorkers[] = "ConfigFileIO"; | ||
|
||
void UserPrefsLoadError( | ||
PersistentPrefStore::PrefReadError* error_val, | ||
PersistentPrefStore::PrefReadError error) { | ||
DCHECK(error_val); | ||
*error_val = error; | ||
} | ||
|
||
base::FilePath GetConfigPath() { | ||
base::FilePath config_path; | ||
CHECK(PathService::Get(FILE_CAST_CONFIG, &config_path)); | ||
return config_path; | ||
} | ||
|
||
} // namespace | ||
|
||
// static | ||
ChromecastConfig* ChromecastConfig::g_instance_ = NULL; | ||
|
||
// static | ||
void ChromecastConfig::Create(PrefRegistrySimple* registry) { | ||
DCHECK(g_instance_ == NULL); | ||
g_instance_ = new ChromecastConfig(); | ||
g_instance_->Load(registry); | ||
} | ||
|
||
// static | ||
ChromecastConfig* ChromecastConfig::GetInstance() { | ||
DCHECK(g_instance_ != NULL); | ||
return g_instance_; | ||
} | ||
|
||
ChromecastConfig::ChromecastConfig() | ||
: config_path_(GetConfigPath()), | ||
worker_pool_(new base::SequencedWorkerPool(kNumOfConfigFileIOWorkers, | ||
kNameOfConfigFileIOWorkers)) { | ||
} | ||
|
||
ChromecastConfig::~ChromecastConfig() { | ||
// Explict writing before worker_pool shutdown. | ||
pref_service_->CommitPendingWrite(); | ||
worker_pool_->Shutdown(); | ||
} | ||
|
||
bool ChromecastConfig::Load(PrefRegistrySimple* registry) { | ||
DCHECK(thread_checker_.CalledOnValidThread()); | ||
VLOG(1) << "Loading config from " << config_path_.value(); | ||
|
||
RegisterPlatformPrefs(registry); | ||
|
||
PersistentPrefStore::PrefReadError prefs_read_error = | ||
PersistentPrefStore::PREF_READ_ERROR_NONE; | ||
base::PrefServiceFactory prefServiceFactory; | ||
prefServiceFactory.SetUserPrefsFile(config_path_, | ||
JsonPrefStore::GetTaskRunnerForFile(config_path_, worker_pool_)); | ||
prefServiceFactory.set_async(false); | ||
prefServiceFactory.set_read_error_callback( | ||
base::Bind(&UserPrefsLoadError, &prefs_read_error)); | ||
pref_service_ = prefServiceFactory.Create(registry); | ||
|
||
if (prefs_read_error == PersistentPrefStore::PREF_READ_ERROR_NONE) { | ||
return true; | ||
} else { | ||
LOG(ERROR) << "Cannot initialize chromecast config: " | ||
<< config_path_.value() | ||
<< ", pref_error=" << prefs_read_error; | ||
return false; | ||
} | ||
} | ||
|
||
void ChromecastConfig::Save() const { | ||
DCHECK(thread_checker_.CalledOnValidThread()); | ||
VLOG(1) << "Saving config to: " << config_path_.value(); | ||
pref_service_->CommitPendingWrite(); | ||
} | ||
|
||
const std::string ChromecastConfig::GetValue(const std::string& key) const { | ||
DCHECK(thread_checker_.CalledOnValidThread()); | ||
return pref_service_->GetString(key.c_str()); | ||
} | ||
|
||
const int ChromecastConfig::GetIntValue(const std::string& key) const { | ||
return pref_service_->GetInteger(key.c_str()); | ||
} | ||
|
||
void ChromecastConfig::SetValue( | ||
const std::string& key, | ||
const std::string& value) const { | ||
DCHECK(thread_checker_.CalledOnValidThread()); | ||
if (pref_service_->IsUserModifiablePreference(key.c_str())) { | ||
VLOG(1) << "Set config: key=" << key << ", value=" << value; | ||
pref_service_->SetString(key.c_str(), value); | ||
} else { | ||
LOG(ERROR) << "Cannot set read-only config: key=" << key | ||
<< ", value=" << value; | ||
} | ||
} | ||
|
||
void ChromecastConfig::SetIntValue(const std::string& key, int value) const { | ||
DCHECK(thread_checker_.CalledOnValidThread()); | ||
if (pref_service_->IsUserModifiablePreference(key.c_str())) { | ||
VLOG(1) << "Set config: key=" << key << ", value=" << value; | ||
pref_service_->SetInteger(key.c_str(), value); | ||
} else { | ||
LOG(ERROR) << "Cannot set read-only config: key=" << key | ||
<< ", value=" << value; | ||
} | ||
} | ||
|
||
} // namespace chromecast |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2014 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// | ||
// Chromecast-specific configurations retrieved from and stored into a given | ||
// configuration file. | ||
|
||
#ifndef CHROMECAST_COMMON_CHROMECAST_CONFIG_H_ | ||
#define CHROMECAST_COMMON_CHROMECAST_CONFIG_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/files/file_path.h" | ||
#include "base/memory/ref_counted.h" | ||
#include "base/memory/scoped_ptr.h" | ||
#include "base/prefs/pref_service.h" | ||
#include "base/threading/sequenced_worker_pool.h" | ||
#include "base/threading/thread_checker.h" | ||
|
||
class PrefRegistrySimple; | ||
|
||
namespace chromecast { | ||
|
||
// Manages configuration for Chromecast via PrefService. | ||
// It uses JsonPrefStore internally and/so the format of config file is same to | ||
// that of JsonPrefStore. | ||
// It is NOT thread-safe; all functions must be run on the same thread as | ||
// the object is created. | ||
class ChromecastConfig { | ||
public: | ||
// Creates new singleton instance of ChromecastConfig. | ||
static void Create(PrefRegistrySimple* registry); | ||
|
||
// Returns the singleton instance of ChromecastConfig. | ||
static ChromecastConfig* GetInstance(); | ||
|
||
// Saves configs into configuration file. | ||
void Save() const; | ||
|
||
// Returns string value for key, if present. | ||
const std::string GetValue(const std::string& key) const; | ||
|
||
// Returns integer value for key, if present. | ||
const int GetIntValue(const std::string& key) const; | ||
|
||
// Sets new string value for key. | ||
void SetValue(const std::string& key, const std::string& value) const; | ||
|
||
// Sets new int value for key. | ||
void SetIntValue(const std::string& key, int value) const; | ||
|
||
scoped_refptr<base::SequencedWorkerPool> worker_pool() const { | ||
return worker_pool_; | ||
} | ||
|
||
PrefService* pref_service() const { return pref_service_.get(); } | ||
|
||
private: | ||
ChromecastConfig(); | ||
~ChromecastConfig(); | ||
|
||
// Loads configs from config file. Returns true if successful. | ||
bool Load(PrefRegistrySimple* registry); | ||
|
||
// Registers any needed preferences for the current platform. | ||
void RegisterPlatformPrefs(PrefRegistrySimple* registry); | ||
|
||
// Global singleton instance. | ||
static ChromecastConfig* g_instance_; | ||
|
||
const base::FilePath config_path_; | ||
const scoped_refptr<base::SequencedWorkerPool> worker_pool_; | ||
scoped_ptr<PrefService> pref_service_; | ||
|
||
base::ThreadChecker thread_checker_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(ChromecastConfig); | ||
}; | ||
|
||
} // namespace chromecast | ||
|
||
#endif // CHROMECAST_COMMON_CHROMECAST_CONFIG_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2014 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chromecast/common/chromecast_config.h" | ||
|
||
namespace chromecast { | ||
|
||
void ChromecastConfig::RegisterPlatformPrefs(PrefRegistrySimple* registry) { | ||
} | ||
|
||
} // namespace chromecast |