| 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 "components/user_prefs/tracked/segregated_pref_store.h" | 5 #include "components/user_prefs/tracked/segregated_pref_store.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 | 12 |
| 13 SegregatedPrefStore::AggregatingObserver::AggregatingObserver( | 13 SegregatedPrefStore::AggregatingObserver::AggregatingObserver( |
| 14 SegregatedPrefStore* outer) | 14 SegregatedPrefStore* outer) |
| 15 : outer_(outer), | 15 : outer_(outer), |
| 16 failed_sub_initializations_(0), | 16 failed_sub_initializations_(0), |
| 17 successful_sub_initializations_(0) { | 17 successful_sub_initializations_(0) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 void SegregatedPrefStore::AggregatingObserver::OnPrefValueChanged( | 20 void SegregatedPrefStore::AggregatingObserver::OnPrefValueChanged( |
| 21 const std::string& key) { | 21 const std::string& key) { |
| 22 // There is no need to tell clients about changes if they have not yet been | 22 // There is no need to tell clients about changes if they have not yet been |
| 23 // told about initialization. | 23 // told about initialization. |
| 24 if (failed_sub_initializations_ + successful_sub_initializations_ < 2) | 24 if (failed_sub_initializations_ + successful_sub_initializations_ < 2) |
| 25 return; | 25 return; |
| 26 | 26 |
| 27 FOR_EACH_OBSERVER(PrefStore::Observer, outer_->observers_, | 27 for (auto& observer : outer_->observers_) |
| 28 OnPrefValueChanged(key)); | 28 observer.OnPrefValueChanged(key); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void SegregatedPrefStore::AggregatingObserver::OnInitializationCompleted( | 31 void SegregatedPrefStore::AggregatingObserver::OnInitializationCompleted( |
| 32 bool succeeded) { | 32 bool succeeded) { |
| 33 if (succeeded) | 33 if (succeeded) |
| 34 ++successful_sub_initializations_; | 34 ++successful_sub_initializations_; |
| 35 else | 35 else |
| 36 ++failed_sub_initializations_; | 36 ++failed_sub_initializations_; |
| 37 | 37 |
| 38 DCHECK_LE(failed_sub_initializations_ + successful_sub_initializations_, 2); | 38 DCHECK_LE(failed_sub_initializations_ + successful_sub_initializations_, 2); |
| 39 | 39 |
| 40 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) { | 40 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) { |
| 41 if (successful_sub_initializations_ == 2 && outer_->read_error_delegate_) { | 41 if (successful_sub_initializations_ == 2 && outer_->read_error_delegate_) { |
| 42 PersistentPrefStore::PrefReadError read_error = outer_->GetReadError(); | 42 PersistentPrefStore::PrefReadError read_error = outer_->GetReadError(); |
| 43 if (read_error != PersistentPrefStore::PREF_READ_ERROR_NONE) | 43 if (read_error != PersistentPrefStore::PREF_READ_ERROR_NONE) |
| 44 outer_->read_error_delegate_->OnError(read_error); | 44 outer_->read_error_delegate_->OnError(read_error); |
| 45 } | 45 } |
| 46 | 46 |
| 47 FOR_EACH_OBSERVER( | 47 for (auto& observer : outer_->observers_) |
| 48 PrefStore::Observer, outer_->observers_, | 48 observer.OnInitializationCompleted(successful_sub_initializations_ == 2); |
| 49 OnInitializationCompleted(successful_sub_initializations_ == 2)); | |
| 50 } | 49 } |
| 51 } | 50 } |
| 52 | 51 |
| 53 SegregatedPrefStore::SegregatedPrefStore( | 52 SegregatedPrefStore::SegregatedPrefStore( |
| 54 const scoped_refptr<PersistentPrefStore>& default_pref_store, | 53 const scoped_refptr<PersistentPrefStore>& default_pref_store, |
| 55 const scoped_refptr<PersistentPrefStore>& selected_pref_store, | 54 const scoped_refptr<PersistentPrefStore>& selected_pref_store, |
| 56 const std::set<std::string>& selected_pref_names) | 55 const std::set<std::string>& selected_pref_names) |
| 57 : default_pref_store_(default_pref_store), | 56 : default_pref_store_(default_pref_store), |
| 58 selected_pref_store_(selected_pref_store), | 57 selected_pref_store_(selected_pref_store), |
| 59 selected_preference_names_(selected_pref_names), | 58 selected_preference_names_(selected_pref_names), |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 .get(); | 171 .get(); |
| 173 } | 172 } |
| 174 | 173 |
| 175 const PersistentPrefStore* SegregatedPrefStore::StoreForKey( | 174 const PersistentPrefStore* SegregatedPrefStore::StoreForKey( |
| 176 const std::string& key) const { | 175 const std::string& key) const { |
| 177 return (base::ContainsKey(selected_preference_names_, key) | 176 return (base::ContainsKey(selected_preference_names_, key) |
| 178 ? selected_pref_store_ | 177 ? selected_pref_store_ |
| 179 : default_pref_store_) | 178 : default_pref_store_) |
| 180 .get(); | 179 .get(); |
| 181 } | 180 } |
| OLD | NEW |