Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/prefs/pref_notifier_impl.h" | |
| 6 | |
| 7 #include "base/stl_util-inl.h" | |
| 8 #include "chrome/browser/prefs/pref_service.h" | |
| 9 #include "chrome/common/notification_observer.h" | |
| 10 #include "chrome/common/notification_service.h" | |
| 11 | |
| 12 PrefNotifierImpl::PrefNotifierImpl(PrefService* service) | |
| 13 : pref_service_(service) { | |
| 14 } | |
| 15 | |
| 16 PrefNotifierImpl::~PrefNotifierImpl() { | |
| 17 DCHECK(CalledOnValidThread()); | |
| 18 | |
| 19 // Verify that there are no pref observers when we shut down. | |
| 20 for (PrefObserverMap::iterator it = pref_observers_.begin(); | |
| 21 it != pref_observers_.end(); ++it) { | |
| 22 NotificationObserverList::Iterator obs_iterator(*(it->second)); | |
| 23 if (obs_iterator.GetNext()) { | |
| 24 LOG(WARNING) << "pref observer found at shutdown " << it->first; | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 STLDeleteContainerPairSecondPointers(pref_observers_.begin(), | |
| 29 pref_observers_.end()); | |
| 30 pref_observers_.clear(); | |
| 31 } | |
| 32 | |
| 33 void PrefNotifierImpl::AddPrefObserver(const char* path, | |
| 34 NotificationObserver* obs) { | |
| 35 // Get the pref observer list associated with the path. | |
| 36 NotificationObserverList* observer_list = NULL; | |
| 37 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); | |
|
danno
2010/12/06 09:20:14
const
Mattias Nissler (ping if slow)
2010/12/06 10:58:12
Done.
| |
| 38 if (observer_iterator == pref_observers_.end()) { | |
| 39 observer_list = new NotificationObserverList; | |
| 40 pref_observers_[path] = observer_list; | |
| 41 } else { | |
| 42 observer_list = observer_iterator->second; | |
| 43 } | |
| 44 | |
| 45 // Verify that this observer doesn't already exist. | |
| 46 NotificationObserverList::Iterator it(*observer_list); | |
| 47 NotificationObserver* existing_obs; | |
| 48 while ((existing_obs = it.GetNext()) != NULL) { | |
| 49 DCHECK(existing_obs != obs) << path << " observer already registered"; | |
| 50 if (existing_obs == obs) | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 // Ok, safe to add the pref observer. | |
| 55 observer_list->AddObserver(obs); | |
| 56 } | |
| 57 | |
| 58 void PrefNotifierImpl::RemovePrefObserver(const char* path, | |
| 59 NotificationObserver* obs) { | |
| 60 DCHECK(CalledOnValidThread()); | |
| 61 | |
| 62 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); | |
|
danno
2010/12/06 09:20:14
const
Mattias Nissler (ping if slow)
2010/12/06 10:58:12
Done.
| |
| 63 if (observer_iterator == pref_observers_.end()) { | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 NotificationObserverList* observer_list = observer_iterator->second; | |
| 68 observer_list->RemoveObserver(obs); | |
| 69 } | |
| 70 | |
| 71 void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) { | |
| 72 FireObservers(path); | |
| 73 } | |
| 74 | |
| 75 void PrefNotifierImpl::OnInitializationCompleted() { | |
| 76 DCHECK(CalledOnValidThread()); | |
| 77 | |
| 78 NotificationService::current()->Notify( | |
| 79 NotificationType::PREF_INITIALIZATION_COMPLETED, | |
| 80 Source<PrefService>(pref_service_), | |
| 81 NotificationService::NoDetails()); | |
| 82 } | |
| 83 | |
| 84 void PrefNotifierImpl::FireObservers(const std::string& path) { | |
| 85 DCHECK(CalledOnValidThread()); | |
| 86 | |
| 87 // Only send notifications for registered preferences. | |
| 88 if (!pref_service_->FindPreference(path.c_str())) | |
| 89 return; | |
| 90 | |
| 91 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); | |
|
danno
2010/12/06 09:20:14
const
Mattias Nissler (ping if slow)
2010/12/06 10:58:12
Done.
| |
| 92 if (observer_iterator == pref_observers_.end()) | |
| 93 return; | |
| 94 | |
| 95 NotificationObserverList::Iterator it(*(observer_iterator->second)); | |
| 96 NotificationObserver* observer; | |
| 97 while ((observer = it.GetNext()) != NULL) { | |
| 98 observer->Observe(NotificationType::PREF_CHANGED, | |
| 99 Source<PrefService>(pref_service_), | |
| 100 Details<const std::string>(&path)); | |
| 101 } | |
| 102 } | |
| OLD | NEW |