| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/prefs/tracked/segregated_pref_store.h" | 5 #include "components/user_prefs/tracked/segregated_pref_store.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 | 10 |
| 11 SegregatedPrefStore::AggregatingObserver::AggregatingObserver( | 11 SegregatedPrefStore::AggregatingObserver::AggregatingObserver( |
| 12 SegregatedPrefStore* outer) | 12 SegregatedPrefStore* outer) |
| 13 : outer_(outer), | 13 : outer_(outer), |
| 14 failed_sub_initializations_(0), | 14 failed_sub_initializations_(0), |
| 15 successful_sub_initializations_(0) {} | 15 successful_sub_initializations_(0) { |
| 16 } |
| 16 | 17 |
| 17 void SegregatedPrefStore::AggregatingObserver::OnPrefValueChanged( | 18 void SegregatedPrefStore::AggregatingObserver::OnPrefValueChanged( |
| 18 const std::string& key) { | 19 const std::string& key) { |
| 19 // There is no need to tell clients about changes if they have not yet been | 20 // There is no need to tell clients about changes if they have not yet been |
| 20 // told about initialization. | 21 // told about initialization. |
| 21 if (failed_sub_initializations_ + successful_sub_initializations_ < 2) | 22 if (failed_sub_initializations_ + successful_sub_initializations_ < 2) |
| 22 return; | 23 return; |
| 23 | 24 |
| 24 FOR_EACH_OBSERVER( | 25 FOR_EACH_OBSERVER(PrefStore::Observer, outer_->observers_, |
| 25 PrefStore::Observer, outer_->observers_, OnPrefValueChanged(key)); | 26 OnPrefValueChanged(key)); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void SegregatedPrefStore::AggregatingObserver::OnInitializationCompleted( | 29 void SegregatedPrefStore::AggregatingObserver::OnInitializationCompleted( |
| 29 bool succeeded) { | 30 bool succeeded) { |
| 30 if (succeeded) | 31 if (succeeded) |
| 31 ++successful_sub_initializations_; | 32 ++successful_sub_initializations_; |
| 32 else | 33 else |
| 33 ++failed_sub_initializations_; | 34 ++failed_sub_initializations_; |
| 34 | 35 |
| 35 DCHECK_LE(failed_sub_initializations_ + successful_sub_initializations_, 2); | 36 DCHECK_LE(failed_sub_initializations_ + successful_sub_initializations_, 2); |
| 36 | 37 |
| 37 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) { | 38 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) { |
| 38 if (successful_sub_initializations_ == 2 && outer_->read_error_delegate_) { | 39 if (successful_sub_initializations_ == 2 && outer_->read_error_delegate_) { |
| 39 PersistentPrefStore::PrefReadError read_error = outer_->GetReadError(); | 40 PersistentPrefStore::PrefReadError read_error = outer_->GetReadError(); |
| 40 if (read_error != PersistentPrefStore::PREF_READ_ERROR_NONE) | 41 if (read_error != PersistentPrefStore::PREF_READ_ERROR_NONE) |
| 41 outer_->read_error_delegate_->OnError(read_error); | 42 outer_->read_error_delegate_->OnError(read_error); |
| 42 } | 43 } |
| 43 | 44 |
| 44 FOR_EACH_OBSERVER( | 45 FOR_EACH_OBSERVER( |
| 45 PrefStore::Observer, | 46 PrefStore::Observer, outer_->observers_, |
| 46 outer_->observers_, | |
| 47 OnInitializationCompleted(successful_sub_initializations_ == 2)); | 47 OnInitializationCompleted(successful_sub_initializations_ == 2)); |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 SegregatedPrefStore::SegregatedPrefStore( | 51 SegregatedPrefStore::SegregatedPrefStore( |
| 52 const scoped_refptr<PersistentPrefStore>& default_pref_store, | 52 const scoped_refptr<PersistentPrefStore>& default_pref_store, |
| 53 const scoped_refptr<PersistentPrefStore>& selected_pref_store, | 53 const scoped_refptr<PersistentPrefStore>& selected_pref_store, |
| 54 const std::set<std::string>& selected_pref_names) | 54 const std::set<std::string>& selected_pref_names) |
| 55 : default_pref_store_(default_pref_store), | 55 : default_pref_store_(default_pref_store), |
| 56 selected_pref_store_(selected_pref_store), | 56 selected_pref_store_(selected_pref_store), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 StoreForKey(key)->ReportValueChanged(key, flags); | 102 StoreForKey(key)->ReportValueChanged(key, flags); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void SegregatedPrefStore::SetValueSilently(const std::string& key, | 105 void SegregatedPrefStore::SetValueSilently(const std::string& key, |
| 106 scoped_ptr<base::Value> value, | 106 scoped_ptr<base::Value> value, |
| 107 uint32 flags) { | 107 uint32 flags) { |
| 108 StoreForKey(key)->SetValueSilently(key, value.Pass(), flags); | 108 StoreForKey(key)->SetValueSilently(key, value.Pass(), flags); |
| 109 } | 109 } |
| 110 | 110 |
| 111 bool SegregatedPrefStore::ReadOnly() const { | 111 bool SegregatedPrefStore::ReadOnly() const { |
| 112 return selected_pref_store_->ReadOnly() || | 112 return selected_pref_store_->ReadOnly() || default_pref_store_->ReadOnly(); |
| 113 default_pref_store_->ReadOnly(); | |
| 114 } | 113 } |
| 115 | 114 |
| 116 PersistentPrefStore::PrefReadError SegregatedPrefStore::GetReadError() const { | 115 PersistentPrefStore::PrefReadError SegregatedPrefStore::GetReadError() const { |
| 117 PersistentPrefStore::PrefReadError read_error = | 116 PersistentPrefStore::PrefReadError read_error = |
| 118 default_pref_store_->GetReadError(); | 117 default_pref_store_->GetReadError(); |
| 119 if (read_error == PersistentPrefStore::PREF_READ_ERROR_NONE) { | 118 if (read_error == PersistentPrefStore::PREF_READ_ERROR_NONE) { |
| 120 read_error = selected_pref_store_->GetReadError(); | 119 read_error = selected_pref_store_->GetReadError(); |
| 121 // Ignore NO_FILE from selected_pref_store_. | 120 // Ignore NO_FILE from selected_pref_store_. |
| 122 if (read_error == PersistentPrefStore::PREF_READ_ERROR_NO_FILE) | 121 if (read_error == PersistentPrefStore::PREF_READ_ERROR_NO_FILE) |
| 123 read_error = PersistentPrefStore::PREF_READ_ERROR_NONE; | 122 read_error = PersistentPrefStore::PREF_READ_ERROR_NONE; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 ? selected_pref_store_ | 164 ? selected_pref_store_ |
| 166 : default_pref_store_).get(); | 165 : default_pref_store_).get(); |
| 167 } | 166 } |
| 168 | 167 |
| 169 const PersistentPrefStore* SegregatedPrefStore::StoreForKey( | 168 const PersistentPrefStore* SegregatedPrefStore::StoreForKey( |
| 170 const std::string& key) const { | 169 const std::string& key) const { |
| 171 return (ContainsKey(selected_preference_names_, key) | 170 return (ContainsKey(selected_preference_names_, key) |
| 172 ? selected_pref_store_ | 171 ? selected_pref_store_ |
| 173 : default_pref_store_).get(); | 172 : default_pref_store_).get(); |
| 174 } | 173 } |
| OLD | NEW |