Skip to content

Commit

Permalink
Adds ChromecastConfig to manage cast_shell's PrefService.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 0 deletions.
6 changes: 6 additions & 0 deletions chromecast/chromecast.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
'sources': [
'common/cast_paths.cc',
'common/cast_paths.h',
'common/chromecast_config.cc',
'common/chromecast_config.h',
],
'conditions': [
['chromecast_branding=="Chrome"', {
'dependencies': [
'internal/chromecast_internal.gyp:cast_common_internal',
],
}, {
'sources': [
'common/chromecast_config_simple.cc',
],
}],
],
},
Expand Down
11 changes: 11 additions & 0 deletions chromecast/common/cast_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ bool PathProvider(int key, base::FilePath* result) {
#endif
return true;
}
case FILE_CAST_CONFIG: {
base::FilePath data_dir;
#if defined(OS_ANDROID)
CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &data_dir);
*result = data_dir.Append("cast_shell.conf");
#else
CHECK(PathService::Get(DIR_CAST_HOME, &data_dir));
*result = data_dir.Append(".eureka.conf");
#endif // defined(OS_ANDROID)
return true;
}
}
return false;
}
Expand Down
1 change: 1 addition & 0 deletions chromecast/common/cast_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum {
DIR_CAST_HOME, // Return a modified $HOME which works for both
// development use and the actual device.

FILE_CAST_CONFIG, // Config/preferences file path.
PATH_END
};

Expand Down
136 changes: 136 additions & 0 deletions chromecast/common/chromecast_config.cc
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
82 changes: 82 additions & 0 deletions chromecast/common/chromecast_config.h
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_
12 changes: 12 additions & 0 deletions chromecast/common/chromecast_config_simple.cc
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

0 comments on commit a15f8d8

Please sign in to comment.