This source file includes following definitions.
- IsHashDictionaryTrusted
- has_pending_write_
- Reset
- BeginTransaction
- GetCurrentVersion
- CommitPendingWrite
- has_changed_
- PrefHashStoreTransactionImpl
- CheckValue
- StoreHash
- CheckSplitValue
- StoreSplitHash
- GetSplitMacs
#include "chrome/browser/prefs/pref_hash_store_impl.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_hash_store_transaction.h"
#include "chrome/browser/prefs/tracked/hash_store_contents.h"
namespace {
bool IsHashDictionaryTrusted(const PrefHashCalculator& calculator,
const HashStoreContents& contents) {
const base::DictionaryValue* store_contents = contents.GetContents();
std::string super_mac = contents.GetSuperMac();
return store_contents && !super_mac.empty() &&
calculator.Validate(contents.hash_store_id(),
store_contents,
super_mac) == PrefHashCalculator::VALID;
}
}
class PrefHashStoreImpl::PrefHashStoreTransactionImpl
: public PrefHashStoreTransaction {
public:
explicit PrefHashStoreTransactionImpl(PrefHashStoreImpl* outer);
virtual ~PrefHashStoreTransactionImpl();
virtual ValueState CheckValue(const std::string& path,
const base::Value* value) const OVERRIDE;
virtual void StoreHash(const std::string& path,
const base::Value* value) OVERRIDE;
virtual ValueState CheckSplitValue(
const std::string& path,
const base::DictionaryValue* initial_split_value,
std::vector<std::string>* invalid_keys) const OVERRIDE;
virtual void StoreSplitHash(
const std::string& path,
const base::DictionaryValue* split_value) OVERRIDE;
private:
bool GetSplitMacs(const std::string& path,
std::map<std::string, std::string>* split_macs) const;
PrefHashStoreImpl* outer_;
bool has_changed_;
DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl);
};
PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed,
const std::string& device_id,
scoped_ptr<HashStoreContents> contents)
: pref_hash_calculator_(seed, device_id),
contents_(contents.Pass()),
initial_hashes_dictionary_trusted_(
IsHashDictionaryTrusted(pref_hash_calculator_, *contents_)),
has_pending_write_(false) {
DCHECK(contents_);
UMA_HISTOGRAM_BOOLEAN("Settings.HashesDictionaryTrusted",
initial_hashes_dictionary_trusted_);
}
PrefHashStoreImpl::~PrefHashStoreImpl() {}
void PrefHashStoreImpl::Reset() {
contents_->Reset();
}
scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction() {
return scoped_ptr<PrefHashStoreTransaction>(
new PrefHashStoreTransactionImpl(this));
}
PrefHashStoreImpl::StoreVersion PrefHashStoreImpl::GetCurrentVersion() const {
if (!contents_->IsInitialized())
return VERSION_UNINITIALIZED;
int current_version;
if (!contents_->GetVersion(¤t_version)) {
return VERSION_PRE_MIGRATION;
}
DCHECK_GT(current_version, VERSION_PRE_MIGRATION);
return static_cast<StoreVersion>(current_version);
}
void PrefHashStoreImpl::CommitPendingWrite() {
if (has_pending_write_) {
contents_->CommitPendingWrite();
has_pending_write_ = false;
}
}
PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl(
PrefHashStoreImpl* outer) : outer_(outer), has_changed_(false) {
}
PrefHashStoreImpl::PrefHashStoreTransactionImpl::
~PrefHashStoreTransactionImpl() {
if (has_changed_) {
const base::DictionaryValue* hashes_dict = outer_->contents_->GetContents();
outer_->contents_->SetSuperMac(outer_->pref_hash_calculator_.Calculate(
outer_->contents_->hash_store_id(), hashes_dict));
outer_->has_pending_write_ = true;
}
int current_version;
if (!outer_->contents_->GetVersion(¤t_version) ||
current_version != VERSION_LATEST) {
outer_->contents_->SetVersion(VERSION_LATEST);
outer_->has_pending_write_ = true;
}
}
PrefHashStoreTransaction::ValueState
PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue(
const std::string& path, const base::Value* initial_value) const {
const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents();
std::string last_hash;
if (hashed_prefs)
hashed_prefs->GetString(path, &last_hash);
if (last_hash.empty()) {
return (!initial_value || outer_->initial_hashes_dictionary_trusted_) ?
TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE;
}
PrefHashCalculator::ValidationResult validation_result =
outer_->pref_hash_calculator_.Validate(path, initial_value, last_hash);
switch (validation_result) {
case PrefHashCalculator::VALID:
return UNCHANGED;
case PrefHashCalculator::VALID_WEAK_LEGACY:
return WEAK_LEGACY;
case PrefHashCalculator::VALID_SECURE_LEGACY:
return SECURE_LEGACY;
case PrefHashCalculator::INVALID:
return initial_value ? CHANGED : CLEARED;
}
NOTREACHED() << "Unexpected PrefHashCalculator::ValidationResult: "
<< validation_result;
return UNTRUSTED_UNKNOWN_VALUE;
}
void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreHash(
const std::string& path, const base::Value* new_value) {
const std::string mac =
outer_->pref_hash_calculator_.Calculate(path, new_value);
(*outer_->contents_->GetMutableContents())->SetString(path, mac);
has_changed_ = true;
}
PrefHashStoreTransaction::ValueState
PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckSplitValue(
const std::string& path,
const base::DictionaryValue* initial_split_value,
std::vector<std::string>* invalid_keys) const {
DCHECK(invalid_keys && invalid_keys->empty());
std::map<std::string, std::string> split_macs;
const bool has_hashes = GetSplitMacs(path, &split_macs);
if (!initial_split_value || initial_split_value->empty())
return has_hashes ? CLEARED : UNCHANGED;
if (!has_hashes) {
return outer_->initial_hashes_dictionary_trusted_ ?
TRUSTED_UNKNOWN_VALUE : UNTRUSTED_UNKNOWN_VALUE;
}
bool has_secure_legacy_id_hashes = false;
std::string keyed_path(path);
keyed_path.push_back('.');
const size_t common_part_length = keyed_path.length();
for (base::DictionaryValue::Iterator it(*initial_split_value); !it.IsAtEnd();
it.Advance()) {
std::map<std::string, std::string>::iterator entry =
split_macs.find(it.key());
if (entry == split_macs.end()) {
invalid_keys->push_back(it.key());
} else {
keyed_path.replace(common_part_length, std::string::npos, it.key());
switch (outer_->pref_hash_calculator_.Validate(
keyed_path, &it.value(), entry->second)) {
case PrefHashCalculator::VALID:
break;
case WEAK_LEGACY:
NOTREACHED();
invalid_keys->push_back(it.key());
break;
case SECURE_LEGACY:
has_secure_legacy_id_hashes = true;
break;
case PrefHashCalculator::INVALID:
invalid_keys->push_back(it.key());
break;
}
split_macs.erase(entry);
}
}
for (std::map<std::string, std::string>::const_iterator it =
split_macs.begin();
it != split_macs.end();
++it) {
invalid_keys->push_back(it->first);
}
return invalid_keys->empty()
? (has_secure_legacy_id_hashes ? SECURE_LEGACY : UNCHANGED)
: CHANGED;
}
void PrefHashStoreImpl::PrefHashStoreTransactionImpl::StoreSplitHash(
const std::string& path,
const base::DictionaryValue* split_value) {
scoped_ptr<HashStoreContents::MutableDictionary> mutable_dictionary =
outer_->contents_->GetMutableContents();
(*mutable_dictionary)->Remove(path, NULL);
if (split_value) {
std::string keyed_path(path);
keyed_path.push_back('.');
const size_t common_part_length = keyed_path.length();
for (base::DictionaryValue::Iterator it(*split_value); !it.IsAtEnd();
it.Advance()) {
keyed_path.replace(common_part_length, std::string::npos, it.key());
(*mutable_dictionary)->SetString(
keyed_path,
outer_->pref_hash_calculator_.Calculate(keyed_path, &it.value()));
}
}
has_changed_ = true;
}
bool PrefHashStoreImpl::PrefHashStoreTransactionImpl::GetSplitMacs(
const std::string& key,
std::map<std::string, std::string>* split_macs) const {
DCHECK(split_macs);
DCHECK(split_macs->empty());
const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents();
const base::DictionaryValue* split_mac_dictionary = NULL;
if (!hashed_prefs || !hashed_prefs->GetDictionary(key, &split_mac_dictionary))
return false;
for (base::DictionaryValue::Iterator it(*split_mac_dictionary); !it.IsAtEnd();
it.Advance()) {
std::string mac_string;
if (!it.value().GetAsString(&mac_string)) {
NOTREACHED();
continue;
}
split_macs->insert(make_pair(it.key(), mac_string));
}
return true;
}