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..b96d6e490f73190bb659e87edb28f6d349a373f6 |
| --- /dev/null |
| +++ b/chrome/browser/prefs/tracked/protected_pref_store.cc |
| @@ -0,0 +1,234 @@ |
| +// 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" |
| + |
| +namespace { |
| + |
| +// Observes ReadErrorDelegate events from the underlying stores and synthesizes |
| +// an event for reporting to the external ReadErrorDelegate if necessary. |
|
robertshield
2014/03/25 03:05:12
Please explicitly state that this class is not thr
erikwright (departed)
2014/03/25 20:28:26
Luckily the class is no longer needed.
|
| +class SharedReadErrorDelegate |
| + : public base::RefCounted<SharedReadErrorDelegate> { |
| + public: |
| + // Creates a SharedReadErrorDelegate that can observe multiple underlying |
| + // PersistentPrefStores and report the first error from any of them to |
| + // |external_delegate|. |
| + explicit SharedReadErrorDelegate( |
| + scoped_ptr<PersistentPrefStore::ReadErrorDelegate> external_delegate) |
| + : sent_error_(false), external_delegate_(external_delegate.Pass()) {} |
| + |
| + // Makes a delegate that can observe one underlying store. |
| + scoped_ptr<PersistentPrefStore::ReadErrorDelegate> MakeDelegate() { |
| + return scoped_ptr<PersistentPrefStore::ReadErrorDelegate>( |
| + new TeeErrorDelegate(this)); |
| + } |
| + |
| + private: |
| + // Observes the events from a single underlying store. |
| + class TeeErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| + public: |
| + explicit TeeErrorDelegate( |
| + const scoped_refptr<SharedReadErrorDelegate>& outer) |
| + : outer_(outer) {} |
| + |
| + // PersistentPrefStore::ReadErrorDelegate implementation |
| + virtual void OnError(PersistentPrefStore::PrefReadError read_error) |
| + OVERRIDE { |
| + // If this is the first error observed by this SharedReadErrorDelegate, |
| + // report it to the external delegate. |
| + if (outer_ && !outer_->sent_error_) { |
| + outer_->sent_error_ = true; |
| + outer_->external_delegate_->OnError(read_error); |
| + } |
| + } |
| + |
| + private: |
| + scoped_refptr<SharedReadErrorDelegate> outer_; |
| + }; |
| + |
| + friend class base::RefCounted<SharedReadErrorDelegate>; |
| + ~SharedReadErrorDelegate() {} |
| + |
| + bool sent_error_; |
| + scoped_ptr<PersistentPrefStore::ReadErrorDelegate> external_delegate_; |
| +}; |
| + |
| +} // namespace |
| + |
| +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) { |
| + 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 && |
| + !outer_->on_initialization_.is_null()) { |
| + outer_->on_initialization_.Run(); |
| + } |
| + |
| + if (failed_sub_initializations_ + successful_sub_initializations_ == 2) { |
| + FOR_EACH_OBSERVER( |
| + PrefStore::Observer, |
| + outer_->observers_, |
| + OnInitializationCompleted(successful_sub_initializations_ == 2)); |
| + } |
|
robertshield
2014/03/25 03:05:12
Can you replace lines 88-98 with:
if (failed_sub
erikwright (departed)
2014/03/25 20:28:26
Done.
|
| +} |
| + |
| +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(); |
|
robertshield
2014/03/25 03:05:12
nit: indent
erikwright (departed)
2014/03/25 20:28:26
Done.
|
| +} |
| + |
| +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) { |
| + scoped_ptr<ReadErrorDelegate> unprotected_delegate; |
| + scoped_ptr<ReadErrorDelegate> protected_delegate; |
| + if (error_delegate) { |
| + scoped_refptr<SharedReadErrorDelegate> shared_delegate( |
| + new SharedReadErrorDelegate(make_scoped_ptr(error_delegate))); |
| + unprotected_delegate.reset(shared_delegate->MakeDelegate().release()); |
| + protected_delegate.reset(shared_delegate->MakeDelegate().release()); |
| + } |
| + unprotected_pref_store_->ReadPrefsAsync(unprotected_delegate.release()); |
| + protected_pref_store_->ReadPrefsAsync(protected_delegate.release()); |
| +} |
| + |
| +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(); |
| +} |