This source file includes following definitions.
- RegisterProfilePrefs
- GetResetTime
- ClearResetTime
- MigrateValues
- Initialize
- FilterOnLoad
- FilterUpdate
- FilterSerializeData
#include "chrome/browser/prefs/pref_hash_filter.h"
#include <algorithm>
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/prefs/persistent_pref_store.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/pref_store.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_hash_store_transaction.h"
#include "chrome/browser/prefs/tracked/tracked_atomic_preference.h"
#include "chrome/browser/prefs/tracked/tracked_split_preference.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
PrefHashFilter::PrefHashFilter(
scoped_ptr<PrefHashStore> pref_hash_store,
const std::vector<TrackedPreferenceMetadata>& tracked_preferences,
size_t reporting_ids_count)
: pref_hash_store_(pref_hash_store.Pass()) {
DCHECK(pref_hash_store_);
DCHECK_GE(reporting_ids_count, tracked_preferences.size());
for (size_t i = 0; i < tracked_preferences.size(); ++i) {
const TrackedPreferenceMetadata& metadata = tracked_preferences[i];
scoped_ptr<TrackedPreference> tracked_preference;
switch (metadata.strategy) {
case TRACKING_STRATEGY_ATOMIC:
tracked_preference.reset(
new TrackedAtomicPreference(metadata.name, metadata.reporting_id,
reporting_ids_count,
metadata.enforcement_level));
break;
case TRACKING_STRATEGY_SPLIT:
tracked_preference.reset(
new TrackedSplitPreference(metadata.name, metadata.reporting_id,
reporting_ids_count,
metadata.enforcement_level));
break;
}
DCHECK(tracked_preference);
bool is_new = tracked_paths_.add(metadata.name,
tracked_preference.Pass()).second;
DCHECK(is_new);
}
}
PrefHashFilter::~PrefHashFilter() {
DCHECK(changed_paths_.empty());
}
void PrefHashFilter::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterStringPref(
prefs::kPreferenceResetTime,
base::Int64ToString(base::Time().ToInternalValue()),
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
base::Time PrefHashFilter::GetResetTime(PrefService* user_prefs) {
int64 internal_value = base::Time().ToInternalValue();
if (!base::StringToInt64(
user_prefs->GetString(prefs::kPreferenceResetTime),
&internal_value)) {
NOTREACHED();
return base::Time();
}
return base::Time::FromInternalValue(internal_value);
}
void PrefHashFilter::ClearResetTime(PrefService* user_prefs) {
user_prefs->ClearPref(prefs::kPreferenceResetTime);
}
void PrefHashFilter::MigrateValues(PersistentPrefStore* source,
PersistentPrefStore* destination) {
scoped_ptr<PrefHashStoreTransaction> transaction =
pref_hash_store_->BeginTransaction();
for (TrackedPreferencesMap::const_iterator it = tracked_paths_.begin();
it != tracked_paths_.end();
++it) {
const base::Value* source_value = NULL;
if (source->GetValue(it->first, &source_value) &&
!destination->GetValue(it->first, NULL)) {
base::DictionaryValue temp_dictionary;
temp_dictionary.Set(it->first, source_value->DeepCopy());
it->second->EnforceAndReport(&temp_dictionary, transaction.get());
scoped_ptr<base::Value> checked_value;
if (temp_dictionary.Remove(it->first, &checked_value))
destination->SetValue(it->first, checked_value.release());
}
source->RemoveValue(it->first);
}
destination->CommitPendingWrite();
transaction.reset();
source->CommitPendingWrite();
}
void PrefHashFilter::Initialize(const PrefStore& pref_store) {
scoped_ptr<PrefHashStoreTransaction> hash_store_transaction(
pref_hash_store_->BeginTransaction());
for (TrackedPreferencesMap::const_iterator it = tracked_paths_.begin();
it != tracked_paths_.end(); ++it) {
const std::string& initialized_path = it->first;
const TrackedPreference* initialized_preference = it->second;
const base::Value* value = NULL;
pref_store.GetValue(initialized_path, &value);
initialized_preference->OnNewValue(value, hash_store_transaction.get());
}
}
void PrefHashFilter::FilterOnLoad(base::DictionaryValue* pref_store_contents) {
DCHECK(pref_store_contents);
base::TimeTicks checkpoint = base::TimeTicks::Now();
bool did_reset = false;
{
scoped_ptr<PrefHashStoreTransaction> hash_store_transaction(
pref_hash_store_->BeginTransaction());
for (TrackedPreferencesMap::const_iterator it = tracked_paths_.begin();
it != tracked_paths_.end(); ++it) {
if (it->second->EnforceAndReport(pref_store_contents,
hash_store_transaction.get())) {
did_reset = true;
}
}
}
if (did_reset) {
pref_store_contents->Set(prefs::kPreferenceResetTime,
new base::StringValue(base::Int64ToString(
base::Time::Now().ToInternalValue())));
}
UMA_HISTOGRAM_TIMES("Settings.FilterOnLoadTime",
base::TimeTicks::Now() - checkpoint);
}
void PrefHashFilter::FilterUpdate(const std::string& path) {
TrackedPreferencesMap::const_iterator it = tracked_paths_.find(path);
if (it != tracked_paths_.end())
changed_paths_.insert(std::make_pair(path, it->second));
}
void PrefHashFilter::FilterSerializeData(
const base::DictionaryValue* pref_store_contents) {
if (!changed_paths_.empty()) {
base::TimeTicks checkpoint = base::TimeTicks::Now();
{
scoped_ptr<PrefHashStoreTransaction> hash_store_transaction(
pref_hash_store_->BeginTransaction());
for (ChangedPathsMap::const_iterator it = changed_paths_.begin();
it != changed_paths_.end(); ++it) {
const std::string& changed_path = it->first;
const TrackedPreference* changed_preference = it->second;
const base::Value* value = NULL;
pref_store_contents->Get(changed_path, &value);
changed_preference->OnNewValue(value, hash_store_transaction.get());
}
changed_paths_.clear();
}
UMA_HISTOGRAM_TIMES("Settings.FilterSerializeDataTime",
base::TimeTicks::Now() - checkpoint);
}
pref_hash_store_->CommitPendingWrite();
}