OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/pref_notifier_impl.h" | 5 #include "chrome/browser/prefs/pref_notifier_impl.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
10 | 11 |
11 PrefNotifierImpl::PrefNotifierImpl() | 12 PrefNotifierImpl::PrefNotifierImpl() |
12 : pref_service_(NULL) { | 13 : pref_service_(NULL) { |
13 } | 14 } |
14 | 15 |
15 PrefNotifierImpl::PrefNotifierImpl(PrefService* service) | 16 PrefNotifierImpl::PrefNotifierImpl(PrefService* service) |
16 : pref_service_(service) { | 17 : pref_service_(service) { |
17 } | 18 } |
18 | 19 |
19 PrefNotifierImpl::~PrefNotifierImpl() { | 20 PrefNotifierImpl::~PrefNotifierImpl() { |
20 DCHECK(CalledOnValidThread()); | 21 DCHECK(CalledOnValidThread()); |
21 | 22 |
22 // Verify that there are no pref observers when we shut down. | 23 // Verify that there are no pref observers when we shut down. |
23 for (PrefObserverMap::iterator it = pref_observers_.begin(); | 24 for (PrefObserverMap::iterator it = pref_observers_.begin(); |
24 it != pref_observers_.end(); ++it) { | 25 it != pref_observers_.end(); ++it) { |
25 PrefObserverList::Iterator obs_iterator(*(it->second)); | 26 if (!it->second->empty()) { |
26 if (obs_iterator.GetNext()) { | |
27 LOG(WARNING) << "pref observer found at shutdown " << it->first; | 27 LOG(WARNING) << "pref observer found at shutdown " << it->first; |
28 } | 28 } |
29 } | 29 } |
30 | 30 |
31 // Same for initialization observers. | 31 // Same for initialization observers. |
32 if (!init_observers_.empty()) | 32 if (!init_observers_.empty()) |
33 LOG(WARNING) << "Init observer found at shutdown."; | 33 LOG(WARNING) << "Init observer found at shutdown."; |
34 | 34 |
35 STLDeleteContainerPairSecondPointers(pref_observers_.begin(), | 35 STLDeleteContainerPairSecondPointers(pref_observers_.begin(), |
36 pref_observers_.end()); | 36 pref_observers_.end()); |
37 pref_observers_.clear(); | 37 pref_observers_.clear(); |
38 init_observers_.clear(); | 38 init_observers_.clear(); |
39 } | 39 } |
40 | 40 |
41 void PrefNotifierImpl::AddPrefObserver(const char* path, | 41 void PrefNotifierImpl::AddPrefObserver(const char* path, |
42 PrefObserver* obs) { | 42 const base::Closure& obs) { |
43 // Get the pref observer list associated with the path. | 43 // Get the pref observer list associated with the path. |
44 PrefObserverList* observer_list = NULL; | 44 PrefObserverList* observer_list = NULL; |
45 const PrefObserverMap::iterator observer_iterator = | 45 const PrefObserverMap::iterator observer_iterator = |
46 pref_observers_.find(path); | 46 pref_observers_.find(path); |
47 if (observer_iterator == pref_observers_.end()) { | 47 if (observer_iterator == pref_observers_.end()) { |
48 observer_list = new PrefObserverList; | 48 observer_list = new PrefObserverList; |
49 pref_observers_[path] = observer_list; | 49 pref_observers_[path] = observer_list; |
50 } else { | 50 } else { |
51 observer_list = observer_iterator->second; | 51 observer_list = observer_iterator->second; |
52 } | 52 } |
53 | 53 |
54 // Add the pref observer. ObserverList will DCHECK if it already is | 54 // Add the pref observer. |
55 // in the list. | 55 // TODO(joi): DO NOT COMMIT must DCHECK if already in list |
56 observer_list->AddObserver(obs); | 56 observer_list->push_back(obs); |
57 } | 57 } |
58 | 58 |
| 59 class SameObserverPredicate { |
| 60 public: |
| 61 SameObserverPredicate(const base::Closure& observer) : obs_(observer) {} |
| 62 |
| 63 bool operator ()(const base::Closure& other) { |
| 64 return obs_.Equals(other); |
| 65 } |
| 66 |
| 67 private: |
| 68 const base::Closure& obs_; |
| 69 }; |
| 70 |
59 void PrefNotifierImpl::RemovePrefObserver(const char* path, | 71 void PrefNotifierImpl::RemovePrefObserver(const char* path, |
60 PrefObserver* obs) { | 72 const base::Closure& obs) { |
61 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
62 | 74 |
63 const PrefObserverMap::iterator observer_iterator = | 75 const PrefObserverMap::iterator observer_iterator = |
64 pref_observers_.find(path); | 76 pref_observers_.find(path); |
65 if (observer_iterator == pref_observers_.end()) { | 77 if (observer_iterator == pref_observers_.end()) { |
66 return; | 78 return; |
67 } | 79 } |
68 | 80 |
69 PrefObserverList* observer_list = observer_iterator->second; | 81 PrefObserverList* observer_list = observer_iterator->second; |
70 observer_list->RemoveObserver(obs); | 82 // TODO(joi): DO NOT COMMIT, DCHECK if not found. |
| 83 observer_list->erase( |
| 84 std::find_if(observer_list->begin(), observer_list->end(), |
| 85 SameObserverPredicate(obs))); |
71 } | 86 } |
72 | 87 |
73 void PrefNotifierImpl::AddInitObserver(base::Callback<void(bool)> obs) { | 88 void PrefNotifierImpl::AddInitObserver(base::Callback<void(bool)> obs) { |
74 init_observers_.push_back(obs); | 89 init_observers_.push_back(obs); |
75 } | 90 } |
76 | 91 |
77 void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) { | 92 void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) { |
78 FireObservers(path); | 93 FireObservers(path); |
79 } | 94 } |
80 | 95 |
(...skipping 18 matching lines...) Expand all Loading... |
99 | 114 |
100 // Only send notifications for registered preferences. | 115 // Only send notifications for registered preferences. |
101 if (!pref_service_->FindPreference(path.c_str())) | 116 if (!pref_service_->FindPreference(path.c_str())) |
102 return; | 117 return; |
103 | 118 |
104 const PrefObserverMap::iterator observer_iterator = | 119 const PrefObserverMap::iterator observer_iterator = |
105 pref_observers_.find(path); | 120 pref_observers_.find(path); |
106 if (observer_iterator == pref_observers_.end()) | 121 if (observer_iterator == pref_observers_.end()) |
107 return; | 122 return; |
108 | 123 |
109 FOR_EACH_OBSERVER(PrefObserver, | 124 for (PrefObserverList::iterator it = observer_iterator->second->begin(); |
110 *(observer_iterator->second), | 125 it != observer_iterator->second->end(); |
111 OnPreferenceChanged(pref_service_, path)); | 126 ++it) { |
| 127 it->Run(); |
| 128 } |
112 } | 129 } |
113 | 130 |
114 void PrefNotifierImpl::SetPrefService(PrefService* pref_service) { | 131 void PrefNotifierImpl::SetPrefService(PrefService* pref_service) { |
115 DCHECK(pref_service_ == NULL); | 132 DCHECK(pref_service_ == NULL); |
116 pref_service_ = pref_service; | 133 pref_service_ = pref_service; |
117 } | 134 } |
OLD | NEW |