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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/common/chrome_notification_types.h" | |
11 #include "content/public/browser/notification_observer.h" | |
12 #include "content/public/browser/notification_service.h" | |
13 | 10 |
14 PrefNotifierImpl::PrefNotifierImpl() | 11 PrefNotifierImpl::PrefNotifierImpl() |
15 : pref_service_(NULL) { | 12 : pref_service_(NULL) { |
16 } | 13 } |
17 | 14 |
18 PrefNotifierImpl::PrefNotifierImpl(PrefService* service) | 15 PrefNotifierImpl::PrefNotifierImpl(PrefService* service) |
19 : pref_service_(service) { | 16 : pref_service_(service) { |
20 } | 17 } |
21 | 18 |
22 PrefNotifierImpl::~PrefNotifierImpl() { | 19 PrefNotifierImpl::~PrefNotifierImpl() { |
23 DCHECK(CalledOnValidThread()); | 20 DCHECK(CalledOnValidThread()); |
24 | 21 |
25 // Verify that there are no pref observers when we shut down. | 22 // Verify that there are no pref observers when we shut down. |
26 for (PrefObserverMap::iterator it = pref_observers_.begin(); | 23 for (PrefObserverMap::iterator it = pref_observers_.begin(); |
27 it != pref_observers_.end(); ++it) { | 24 it != pref_observers_.end(); ++it) { |
28 NotificationObserverList::Iterator obs_iterator(*(it->second)); | 25 PrefObserverList::Iterator obs_iterator(*(it->second)); |
29 if (obs_iterator.GetNext()) { | 26 if (obs_iterator.GetNext()) { |
30 LOG(WARNING) << "pref observer found at shutdown " << it->first; | 27 LOG(WARNING) << "pref observer found at shutdown " << it->first; |
31 } | 28 } |
32 } | 29 } |
33 | 30 |
| 31 // Same for initialization observers. |
| 32 PrefInitObserverList::Iterator init_it(init_observers_); |
| 33 if (init_it.GetNext()) { |
| 34 LOG(WARNING) << "Init observer found at shutdown."; |
| 35 } |
| 36 |
34 STLDeleteContainerPairSecondPointers(pref_observers_.begin(), | 37 STLDeleteContainerPairSecondPointers(pref_observers_.begin(), |
35 pref_observers_.end()); | 38 pref_observers_.end()); |
36 pref_observers_.clear(); | 39 pref_observers_.clear(); |
| 40 init_observers_.Clear(); |
37 } | 41 } |
38 | 42 |
39 void PrefNotifierImpl::AddPrefObserver(const char* path, | 43 void PrefNotifierImpl::AddPrefObserver(const char* path, |
40 content::NotificationObserver* obs) { | 44 PrefObserver* obs) { |
41 // Get the pref observer list associated with the path. | 45 // Get the pref observer list associated with the path. |
42 NotificationObserverList* observer_list = NULL; | 46 PrefObserverList* observer_list = NULL; |
43 const PrefObserverMap::iterator observer_iterator = | 47 const PrefObserverMap::iterator observer_iterator = |
44 pref_observers_.find(path); | 48 pref_observers_.find(path); |
45 if (observer_iterator == pref_observers_.end()) { | 49 if (observer_iterator == pref_observers_.end()) { |
46 observer_list = new NotificationObserverList; | 50 observer_list = new PrefObserverList; |
47 pref_observers_[path] = observer_list; | 51 pref_observers_[path] = observer_list; |
48 } else { | 52 } else { |
49 observer_list = observer_iterator->second; | 53 observer_list = observer_iterator->second; |
50 } | 54 } |
51 | 55 |
52 // Verify that this observer doesn't already exist. | 56 // Add the pref observer. ObserverList will DCHECK if it already is |
53 NotificationObserverList::Iterator it(*observer_list); | 57 // in the list. |
54 content::NotificationObserver* existing_obs; | |
55 while ((existing_obs = it.GetNext()) != NULL) { | |
56 DCHECK(existing_obs != obs) << path << " observer already registered"; | |
57 if (existing_obs == obs) | |
58 return; | |
59 } | |
60 | |
61 // Ok, safe to add the pref observer. | |
62 observer_list->AddObserver(obs); | 58 observer_list->AddObserver(obs); |
63 } | 59 } |
64 | 60 |
65 void PrefNotifierImpl::RemovePrefObserver(const char* path, | 61 void PrefNotifierImpl::RemovePrefObserver(const char* path, |
66 content::NotificationObserver* obs) { | 62 PrefObserver* obs) { |
67 DCHECK(CalledOnValidThread()); | 63 DCHECK(CalledOnValidThread()); |
68 | 64 |
69 const PrefObserverMap::iterator observer_iterator = | 65 const PrefObserverMap::iterator observer_iterator = |
70 pref_observers_.find(path); | 66 pref_observers_.find(path); |
71 if (observer_iterator == pref_observers_.end()) { | 67 if (observer_iterator == pref_observers_.end()) { |
72 return; | 68 return; |
73 } | 69 } |
74 | 70 |
75 NotificationObserverList* observer_list = observer_iterator->second; | 71 PrefObserverList* observer_list = observer_iterator->second; |
76 observer_list->RemoveObserver(obs); | 72 observer_list->RemoveObserver(obs); |
77 } | 73 } |
78 | 74 |
| 75 void PrefNotifierImpl::AddInitObserver(PrefInitObserver* obs) { |
| 76 init_observers_.AddObserver(obs); |
| 77 } |
| 78 |
| 79 void PrefNotifierImpl::RemoveInitObserver(PrefInitObserver* obs) { |
| 80 init_observers_.RemoveObserver(obs); |
| 81 } |
| 82 |
79 void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) { | 83 void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) { |
80 FireObservers(path); | 84 FireObservers(path); |
81 } | 85 } |
82 | 86 |
83 void PrefNotifierImpl::OnInitializationCompleted(bool succeeded) { | 87 void PrefNotifierImpl::OnInitializationCompleted(bool succeeded) { |
84 DCHECK(CalledOnValidThread()); | 88 DCHECK(CalledOnValidThread()); |
85 | 89 |
86 content::NotificationService::current()->Notify( | 90 FOR_EACH_OBSERVER(PrefInitObserver, |
87 chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED, | 91 init_observers_, |
88 content::Source<PrefService>(pref_service_), | 92 OnInitializationCompleted(pref_service_, succeeded)); |
89 content::Details<bool>(&succeeded)); | |
90 } | 93 } |
91 | 94 |
92 void PrefNotifierImpl::FireObservers(const std::string& path) { | 95 void PrefNotifierImpl::FireObservers(const std::string& path) { |
93 DCHECK(CalledOnValidThread()); | 96 DCHECK(CalledOnValidThread()); |
94 | 97 |
95 // Only send notifications for registered preferences. | 98 // Only send notifications for registered preferences. |
96 if (!pref_service_->FindPreference(path.c_str())) | 99 if (!pref_service_->FindPreference(path.c_str())) |
97 return; | 100 return; |
98 | 101 |
99 const PrefObserverMap::iterator observer_iterator = | 102 const PrefObserverMap::iterator observer_iterator = |
100 pref_observers_.find(path); | 103 pref_observers_.find(path); |
101 if (observer_iterator == pref_observers_.end()) | 104 if (observer_iterator == pref_observers_.end()) |
102 return; | 105 return; |
103 | 106 |
104 NotificationObserverList::Iterator it(*(observer_iterator->second)); | 107 FOR_EACH_OBSERVER(PrefObserver, |
105 content::NotificationObserver* observer; | 108 *(observer_iterator->second), |
106 while ((observer = it.GetNext()) != NULL) { | 109 OnPreferenceChanged(pref_service_, path)); |
107 observer->Observe(chrome::NOTIFICATION_PREF_CHANGED, | |
108 content::Source<PrefService>(pref_service_), | |
109 content::Details<const std::string>(&path)); | |
110 } | |
111 } | 110 } |
112 | 111 |
113 void PrefNotifierImpl::SetPrefService(PrefService* pref_service) { | 112 void PrefNotifierImpl::SetPrefService(PrefService* pref_service) { |
114 DCHECK(pref_service_ == NULL); | 113 DCHECK(pref_service_ == NULL); |
115 pref_service_ = pref_service; | 114 pref_service_ = pref_service; |
116 } | 115 } |
OLD | NEW |