1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// 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 "components/user_prefs/tracked/segregated_pref_store.h"
#include <utility>
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/values.h"
SegregatedPrefStore::AggregatingObserver::AggregatingObserver(
SegregatedPrefStore* outer)
: outer_(outer),
failed_sub_initializations_(0),
successful_sub_initializations_(0) {
}
void SegregatedPrefStore::AggregatingObserver::OnPrefValueChanged(
const std::string& key) {
// There is no need to tell clients about changes if they have not yet been
// told about initialization.
if (failed_sub_initializations_ + successful_sub_initializations_ < 2)
return;
for (auto& observer : outer_->observers_)
observer.OnPrefValueChanged(key);
}
void SegregatedPrefStore::AggregatingObserver::OnInitializationCompleted(
bool succeeded) {
if (succeeded)
++successful_sub_initializations_;
else
++failed_sub_initializations_;
DCHECK_LE(failed_sub_initializations_ + successful_sub_initializations_, 2);
if (failed_sub_initializations_ + successful_sub_initializations_ == 2) {
if (successful_sub_initializations_ == 2 && outer_->read_error_delegate_) {
PersistentPrefStore::PrefReadError read_error = outer_->GetReadError();
if (read_error != PersistentPrefStore::PREF_READ_ERROR_NONE)
outer_->read_error_delegate_->OnError(read_error);
}
for (auto& observer : outer_->observers_)
observer.OnInitializationCompleted(successful_sub_initializations_ == 2);
}
}
SegregatedPrefStore::SegregatedPrefStore(
const scoped_refptr<PersistentPrefStore>& default_pref_store,
const scoped_refptr<PersistentPrefStore>& selected_pref_store,
const std::set<std::string>& selected_pref_names)
: default_pref_store_(default_pref_store),
selected_pref_store_(selected_pref_store),
selected_preference_names_(selected_pref_names),
aggregating_observer_(this) {
default_pref_store_->AddObserver(&aggregating_observer_);
selected_pref_store_->AddObserver(&aggregating_observer_);
}
void SegregatedPrefStore::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void SegregatedPrefStore::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
bool SegregatedPrefStore::HasObservers() const {
return observers_.might_have_observers();
}
bool SegregatedPrefStore::IsInitializationComplete() const {
return default_pref_store_->IsInitializationComplete() &&
selected_pref_store_->IsInitializationComplete();
}
bool SegregatedPrefStore::GetValue(const std::string& key,
const base::Value** result) const {
return StoreForKey(key)->GetValue(key, result);
}
void SegregatedPrefStore::SetValue(const std::string& key,
std::unique_ptr<base::Value> value,
uint32_t flags) {
StoreForKey(key)->SetValue(key, std::move(value), flags);
}
void SegregatedPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
StoreForKey(key)->RemoveValue(key, flags);
}
bool SegregatedPrefStore::GetMutableValue(const std::string& key,
base::Value** result) {
return StoreForKey(key)->GetMutableValue(key, result);
}
void SegregatedPrefStore::ReportValueChanged(const std::string& key,
uint32_t flags) {
StoreForKey(key)->ReportValueChanged(key, flags);
}
void SegregatedPrefStore::SetValueSilently(const std::string& key,
std::unique_ptr<base::Value> value,
uint32_t flags) {
StoreForKey(key)->SetValueSilently(key, std::move(value), flags);
}
bool SegregatedPrefStore::ReadOnly() const {
return selected_pref_store_->ReadOnly() || default_pref_store_->ReadOnly();
}
PersistentPrefStore::PrefReadError SegregatedPrefStore::GetReadError() const {
PersistentPrefStore::PrefReadError read_error =
default_pref_store_->GetReadError();
if (read_error == PersistentPrefStore::PREF_READ_ERROR_NONE) {
read_error = selected_pref_store_->GetReadError();
// Ignore NO_FILE from selected_pref_store_.
if (read_error == PersistentPrefStore::PREF_READ_ERROR_NO_FILE)
read_error = PersistentPrefStore::PREF_READ_ERROR_NONE;
}
return read_error;
}
PersistentPrefStore::PrefReadError SegregatedPrefStore::ReadPrefs() {
// Note: Both of these stores own PrefFilters which makes ReadPrefs
// asynchronous. This is okay in this case as only the first call will be
// truly asynchronous, the second call will then unblock the migration in
// TrackedPreferencesMigrator and complete synchronously.
default_pref_store_->ReadPrefs();
PersistentPrefStore::PrefReadError selected_store_read_error =
selected_pref_store_->ReadPrefs();
DCHECK_NE(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE,
selected_store_read_error);
return GetReadError();
}
void SegregatedPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
read_error_delegate_.reset(error_delegate);
default_pref_store_->ReadPrefsAsync(NULL);
selected_pref_store_->ReadPrefsAsync(NULL);
}
void SegregatedPrefStore::CommitPendingWrite() {
default_pref_store_->CommitPendingWrite();
selected_pref_store_->CommitPendingWrite();
}
void SegregatedPrefStore::SchedulePendingLossyWrites() {
default_pref_store_->SchedulePendingLossyWrites();
selected_pref_store_->SchedulePendingLossyWrites();
}
void SegregatedPrefStore::ClearMutableValues() {
NOTIMPLEMENTED();
}
SegregatedPrefStore::~SegregatedPrefStore() {
default_pref_store_->RemoveObserver(&aggregating_observer_);
selected_pref_store_->RemoveObserver(&aggregating_observer_);
}
PersistentPrefStore* SegregatedPrefStore::StoreForKey(const std::string& key) {
return (base::ContainsKey(selected_preference_names_, key)
? selected_pref_store_
: default_pref_store_)
.get();
}
const PersistentPrefStore* SegregatedPrefStore::StoreForKey(
const std::string& key) const {
return (base::ContainsKey(selected_preference_names_, key)
? selected_pref_store_
: default_pref_store_)
.get();
}
|