Chromium Code Reviews| Index: chrome/browser/prefs/tracked/protected_pref_store.cc |
| diff --git a/chrome/browser/prefs/tracked/protected_pref_store.cc b/chrome/browser/prefs/tracked/protected_pref_store.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..508d5ee278241d4d135972510e3478efbaa33ecd |
| --- /dev/null |
| +++ b/chrome/browser/prefs/tracked/protected_pref_store.cc |
| @@ -0,0 +1,181 @@ |
| +// Copyright (c) 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 "chrome/browser/prefs/tracked/protected_pref_store.h" |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/prefs/pref_hash_store.h" |
| +#include "chrome/browser/prefs/pref_hash_store_transaction.h" |
| + |
| +ProtectedPrefStore::TeeObserver::TeeObserver(ProtectedPrefStore* outer) |
| + : outer_(outer), |
| + failed_sub_initializations_(0), |
| + successful_sub_initializations_(0) {} |
| + |
| +void ProtectedPrefStore::TeeObserver::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) { |
|
Bernhard Bauer
2014/03/26 15:05:50
You could negate the condition and early-return. T
erikwright (departed)
2014/03/26 21:08:12
Done.
|
| + FOR_EACH_OBSERVER( |
| + PrefStore::Observer, outer_->observers_, OnPrefValueChanged(key)); |
| + } |
| +} |
| + |
| +void ProtectedPrefStore::TeeObserver::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 (!outer_->on_initialization_.is_null()) |
| + outer_->on_initialization_.Run(); |
| + |
| + 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_EACH_OBSERVER( |
| + PrefStore::Observer, |
| + outer_->observers_, |
| + OnInitializationCompleted(successful_sub_initializations_ == 2)); |
| + } |
| +} |
| + |
| +ProtectedPrefStore::ProtectedPrefStore( |
| + const scoped_refptr<PersistentPrefStore>& unprotected_pref_store, |
| + const scoped_refptr<PersistentPrefStore>& protected_pref_store, |
| + const std::set<std::string>& protected_pref_names, |
| + const base::Closure& on_initialization) |
| + : unprotected_pref_store_(unprotected_pref_store), |
| + protected_pref_store_(protected_pref_store), |
| + protected_preference_names_(protected_pref_names), |
| + on_initialization_(on_initialization), |
| + tee_observer_(this) { |
| + |
| + unprotected_pref_store_->AddObserver(&tee_observer_); |
| + protected_pref_store_->AddObserver(&tee_observer_); |
| +} |
| + |
| +void ProtectedPrefStore::AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void ProtectedPrefStore::RemoveObserver(Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +bool ProtectedPrefStore::HasObservers() const { |
| + return observers_.might_have_observers(); |
| +} |
| + |
| +bool ProtectedPrefStore::IsInitializationComplete() const { |
| + return unprotected_pref_store_->IsInitializationComplete() && |
| + protected_pref_store_->IsInitializationComplete(); |
| +} |
| + |
| +bool ProtectedPrefStore::GetValue(const std::string& key, |
| + const base::Value** result) const { |
| + return StoreForKey(key)->GetValue(key, result); |
| +} |
| + |
| +void ProtectedPrefStore::SetValue(const std::string& key, base::Value* value) { |
| + StoreForKey(key)->SetValue(key, value); |
| +} |
| + |
| +void ProtectedPrefStore::RemoveValue(const std::string& key) { |
| + StoreForKey(key)->RemoveValue(key); |
| +} |
| + |
| +bool ProtectedPrefStore::GetMutableValue(const std::string& key, |
| + base::Value** result) { |
| + return StoreForKey(key)->GetMutableValue(key, result); |
| +} |
| + |
| +void ProtectedPrefStore::ReportValueChanged(const std::string& key) { |
| + StoreForKey(key)->ReportValueChanged(key); |
| +} |
| + |
| +void ProtectedPrefStore::SetValueSilently(const std::string& key, |
| + base::Value* value) { |
| + StoreForKey(key)->SetValueSilently(key, value); |
| +} |
| + |
| +bool ProtectedPrefStore::ReadOnly() const { |
| + return protected_pref_store_->ReadOnly() || |
| + unprotected_pref_store_->ReadOnly(); |
| +} |
| + |
| +PersistentPrefStore::PrefReadError ProtectedPrefStore::GetReadError() const { |
| + PersistentPrefStore::PrefReadError read_error = |
| + unprotected_pref_store_->GetReadError(); |
| + return read_error != PersistentPrefStore::PREF_READ_ERROR_NONE |
| + ? read_error |
| + : protected_pref_store_->GetReadError(); |
| +} |
| + |
| +PersistentPrefStore::PrefReadError ProtectedPrefStore::ReadPrefs() { |
| + PersistentPrefStore::PrefReadError unprotected_read_error = |
| + unprotected_pref_store_->ReadPrefs(); |
| + PersistentPrefStore::PrefReadError protected_read_error = |
| + protected_pref_store_->ReadPrefs(); |
| + |
| + return unprotected_read_error != PersistentPrefStore::PREF_READ_ERROR_NONE |
| + ? unprotected_read_error |
| + : protected_read_error; |
| +} |
| + |
| +void ProtectedPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { |
| + read_error_delegate_.reset(error_delegate); |
| + unprotected_pref_store_->ReadPrefsAsync(NULL); |
| + protected_pref_store_->ReadPrefsAsync(NULL); |
| +} |
| + |
| +void ProtectedPrefStore::CommitPendingWrite() { |
| + unprotected_pref_store_->CommitPendingWrite(); |
| + protected_pref_store_->CommitPendingWrite(); |
| +} |
| + |
| +ProtectedPrefStore::~ProtectedPrefStore() { |
| + unprotected_pref_store_->RemoveObserver(&tee_observer_); |
| + protected_pref_store_->RemoveObserver(&tee_observer_); |
| +} |
| + |
| +const PersistentPrefStore* |
| +ProtectedPrefStore::StoreForKey(const std::string& key) const { |
| + if (ContainsKey(protected_preference_names_, key) || |
| + protected_pref_store_->GetValue(key, NULL)) { |
| + return protected_pref_store_.get(); |
| + } |
| + return unprotected_pref_store_.get(); |
| +} |
| + |
| +PersistentPrefStore* ProtectedPrefStore::StoreForKey(const std::string& key) { |
| + if (ContainsKey(protected_preference_names_, key)) |
| + return protected_pref_store_.get(); |
| + |
| + // Check if this unprotected value was previously protected. If so, migrate it |
| + // back to the unprotected store. |
| + // It's hard to do this in a single pass at startup because PrefStore does not |
| + // permit us to enumerate its contents. |
| + const base::Value* value = NULL; |
| + if (protected_pref_store_->GetValue(key, &value)) { |
| + scoped_ptr<base::Value> migrated_value(value->DeepCopy()); |
| + value = NULL; |
| + unprotected_pref_store_->SetValue(key, migrated_value.release()); |
| + unprotected_pref_store_->CommitPendingWrite(); |
| + protected_pref_store_->RemoveValue(key); |
| + protected_pref_store_->CommitPendingWrite(); |
| + } |
| + |
| + return unprotected_pref_store_.get(); |
| +} |