This source file includes following definitions.
- GetRlzStorePath
- GetRlzStoreLockPath
- GetKeyName
- GetKeyName
- read_only_
- HasAccess
- WritePingTime
- ReadPingTime
- ClearPingTime
- WriteAccessPointRlz
- ReadAccessPointRlz
- ClearAccessPointRlz
- AddProductEvent
- ReadProductEvents
- ClearProductEvent
- ClearAllProductEvents
- AddStatefulEvent
- IsStatefulEvent
- ClearAllStatefulEvents
- CollectGarbage
- ReadStore
- WriteStore
- AddValueToList
- RemoveValueFromList
- GetStore
- SetRlzStoreDirectory
- RlzStoreFilenameStr
#include "rlz/chromeos/lib/rlz_value_store_chromeos.h"
#include "base/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "rlz/lib/lib_values.h"
#include "rlz/lib/recursive_cross_process_lock_posix.h"
#include "rlz/lib/rlz_lib.h"
namespace rlz_lib {
namespace {
const char kPingTimeKey[] = "ping_time";
const char kAccessPointKey[] = "access_points";
const char kProductEventKey[] = "product_events";
const char kStatefulEventKey[] = "stateful_events";
const char kNoSupplementaryBrand[] = "_";
const base::FilePath::CharType kRLZDataFileName[] =
FILE_PATH_LITERAL("RLZ Data");
const base::FilePath::CharType kRLZLockFileName[] =
FILE_PATH_LITERAL("RLZ Data.lock");
base::FilePath g_testing_rlz_store_path_;
base::FilePath GetRlzStorePath() {
return g_testing_rlz_store_path_.empty() ?
base::GetHomeDir().Append(kRLZDataFileName) :
g_testing_rlz_store_path_.Append(kRLZDataFileName);
}
base::FilePath GetRlzStoreLockPath() {
return g_testing_rlz_store_path_.empty() ?
base::GetHomeDir().Append(kRLZLockFileName) :
g_testing_rlz_store_path_.Append(kRLZLockFileName);
}
std::string GetKeyName(std::string key, AccessPoint access_point) {
std::string brand = SupplementaryBranding::GetBrand();
if (brand.empty())
brand = kNoSupplementaryBrand;
return key + "." + GetAccessPointName(access_point) + "." + brand;
}
std::string GetKeyName(std::string key, Product product) {
std::string brand = SupplementaryBranding::GetBrand();
if (brand.empty())
brand = kNoSupplementaryBrand;
return key + "." + GetProductName(product) + "." + brand;
}
}
RlzValueStoreChromeOS::RlzValueStoreChromeOS(const base::FilePath& store_path)
: rlz_store_(new base::DictionaryValue),
store_path_(store_path),
read_only_(true) {
ReadStore();
}
RlzValueStoreChromeOS::~RlzValueStoreChromeOS() {
WriteStore();
}
bool RlzValueStoreChromeOS::HasAccess(AccessType type) {
DCHECK(CalledOnValidThread());
return type == kReadAccess || !read_only_;
}
bool RlzValueStoreChromeOS::WritePingTime(Product product, int64 time) {
DCHECK(CalledOnValidThread());
rlz_store_->SetString(GetKeyName(kPingTimeKey, product),
base::Int64ToString(time));
return true;
}
bool RlzValueStoreChromeOS::ReadPingTime(Product product, int64* time) {
DCHECK(CalledOnValidThread());
std::string ping_time;
return rlz_store_->GetString(GetKeyName(kPingTimeKey, product), &ping_time) &&
base::StringToInt64(ping_time, time);
}
bool RlzValueStoreChromeOS::ClearPingTime(Product product) {
DCHECK(CalledOnValidThread());
rlz_store_->Remove(GetKeyName(kPingTimeKey, product), NULL);
return true;
}
bool RlzValueStoreChromeOS::WriteAccessPointRlz(AccessPoint access_point,
const char* new_rlz) {
DCHECK(CalledOnValidThread());
rlz_store_->SetString(
GetKeyName(kAccessPointKey, access_point), new_rlz);
return true;
}
bool RlzValueStoreChromeOS::ReadAccessPointRlz(AccessPoint access_point,
char* rlz,
size_t rlz_size) {
DCHECK(CalledOnValidThread());
std::string rlz_value;
rlz_store_->GetString(GetKeyName(kAccessPointKey, access_point), &rlz_value);
if (rlz_value.size() < rlz_size) {
strncpy(rlz, rlz_value.c_str(), rlz_size);
return true;
}
if (rlz_size > 0)
*rlz = '\0';
return false;
}
bool RlzValueStoreChromeOS::ClearAccessPointRlz(AccessPoint access_point) {
DCHECK(CalledOnValidThread());
rlz_store_->Remove(GetKeyName(kAccessPointKey, access_point), NULL);
return true;
}
bool RlzValueStoreChromeOS::AddProductEvent(Product product,
const char* event_rlz) {
DCHECK(CalledOnValidThread());
return AddValueToList(GetKeyName(kProductEventKey, product),
new base::StringValue(event_rlz));
}
bool RlzValueStoreChromeOS::ReadProductEvents(
Product product,
std::vector<std::string>* events) {
DCHECK(CalledOnValidThread());
base::ListValue* events_list = NULL; ;
if (!rlz_store_->GetList(GetKeyName(kProductEventKey, product), &events_list))
return false;
events->clear();
for (size_t i = 0; i < events_list->GetSize(); ++i) {
std::string event;
if (events_list->GetString(i, &event))
events->push_back(event);
}
return true;
}
bool RlzValueStoreChromeOS::ClearProductEvent(Product product,
const char* event_rlz) {
DCHECK(CalledOnValidThread());
base::StringValue event_value(event_rlz);
return RemoveValueFromList(GetKeyName(kProductEventKey, product),
event_value);
}
bool RlzValueStoreChromeOS::ClearAllProductEvents(Product product) {
DCHECK(CalledOnValidThread());
rlz_store_->Remove(GetKeyName(kProductEventKey, product), NULL);
return true;
}
bool RlzValueStoreChromeOS::AddStatefulEvent(Product product,
const char* event_rlz) {
DCHECK(CalledOnValidThread());
return AddValueToList(GetKeyName(kStatefulEventKey, product),
new base::StringValue(event_rlz));
}
bool RlzValueStoreChromeOS::IsStatefulEvent(Product product,
const char* event_rlz) {
DCHECK(CalledOnValidThread());
base::StringValue event_value(event_rlz);
base::ListValue* events_list = NULL;
return rlz_store_->GetList(GetKeyName(kStatefulEventKey, product),
&events_list) &&
events_list->Find(event_value) != events_list->end();
}
bool RlzValueStoreChromeOS::ClearAllStatefulEvents(Product product) {
DCHECK(CalledOnValidThread());
rlz_store_->Remove(GetKeyName(kStatefulEventKey, product), NULL);
return true;
}
void RlzValueStoreChromeOS::CollectGarbage() {
DCHECK(CalledOnValidThread());
NOTIMPLEMENTED();
}
void RlzValueStoreChromeOS::ReadStore() {
int error_code = 0;
std::string error_msg;
JSONFileValueSerializer serializer(store_path_);
scoped_ptr<base::Value> value(
serializer.Deserialize(&error_code, &error_msg));
switch (error_code) {
case JSONFileValueSerializer::JSON_NO_SUCH_FILE:
read_only_ = false;
break;
case JSONFileValueSerializer::JSON_NO_ERROR:
read_only_ = false;
rlz_store_.reset(static_cast<base::DictionaryValue*>(value.release()));
break;
default:
LOG(ERROR) << "Error reading RLZ store: " << error_msg;
}
}
void RlzValueStoreChromeOS::WriteStore() {
std::string json_data;
JSONStringValueSerializer serializer(&json_data);
serializer.set_pretty_print(true);
scoped_ptr<base::DictionaryValue> copy(
rlz_store_->DeepCopyWithoutEmptyChildren());
if (!serializer.Serialize(*copy.get())) {
LOG(ERROR) << "Failed to serialize RLZ data";
NOTREACHED();
return;
}
if (!base::ImportantFileWriter::WriteFileAtomically(store_path_, json_data))
LOG(ERROR) << "Error writing RLZ store";
}
bool RlzValueStoreChromeOS::AddValueToList(std::string list_name,
base::Value* value) {
base::ListValue* list_value = NULL;
if (!rlz_store_->GetList(list_name, &list_value)) {
list_value = new base::ListValue;
rlz_store_->Set(list_name, list_value);
}
list_value->AppendIfNotPresent(value);
return true;
}
bool RlzValueStoreChromeOS::RemoveValueFromList(std::string list_name,
const base::Value& value) {
base::ListValue* list_value = NULL;
if (!rlz_store_->GetList(list_name, &list_value))
return false;
size_t index;
list_value->Remove(value, &index);
return true;
}
namespace {
RecursiveCrossProcessLock g_recursive_lock =
RECURSIVE_CROSS_PROCESS_LOCK_INITIALIZER;
int g_lock_depth = 0;
RlzValueStoreChromeOS* g_store = NULL;
}
ScopedRlzValueStoreLock::ScopedRlzValueStoreLock() {
bool got_cross_process_lock =
g_recursive_lock.TryGetCrossProcessLock(GetRlzStoreLockPath());
++g_lock_depth;
if (!got_cross_process_lock) {
DCHECK(!g_store);
return;
}
if (g_lock_depth > 1) {
DCHECK(g_store);
store_.reset(g_store);
return;
}
DCHECK(!g_store);
g_store = new RlzValueStoreChromeOS(GetRlzStorePath());
store_.reset(g_store);
}
ScopedRlzValueStoreLock::~ScopedRlzValueStoreLock() {
--g_lock_depth;
DCHECK(g_lock_depth >= 0);
if (g_lock_depth > 0) {
ignore_result(store_.release());
return;
}
g_store = NULL;
g_recursive_lock.ReleaseLock();
}
RlzValueStore* ScopedRlzValueStoreLock::GetStore() {
return store_.get();
}
namespace testing {
void SetRlzStoreDirectory(const base::FilePath& directory) {
g_testing_rlz_store_path_ = directory;
}
std::string RlzStoreFilenameStr() {
return GetRlzStorePath().value();
}
}
}