| 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.h" | |
| 6 | |
| 7 #include "base/stl_util-inl.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
| 10 #include "chrome/browser/prefs/pref_service.h" | |
| 11 #include "chrome/browser/prefs/pref_value_store.h" | |
| 12 #include "chrome/common/notification_service.h" | |
| 13 | |
| 14 | |
| 15 PrefNotifier::PrefNotifier(PrefService* service, PrefValueStore* value_store) | |
| 16 : pref_service_(service), | |
| 17 pref_value_store_(value_store) { | |
| 18 registrar_.Add(this, | |
| 19 NotificationType(NotificationType::POLICY_CHANGED), | |
| 20 NotificationService::AllSources()); | |
| 21 } | |
| 22 | |
| 23 PrefNotifier::~PrefNotifier() { | |
| 24 DCHECK(CalledOnValidThread()); | |
| 25 | |
| 26 // Verify that there are no pref observers when we shut down. | |
| 27 for (PrefObserverMap::iterator it = pref_observers_.begin(); | |
| 28 it != pref_observers_.end(); ++it) { | |
| 29 NotificationObserverList::Iterator obs_iterator(*(it->second)); | |
| 30 if (obs_iterator.GetNext()) { | |
| 31 LOG(WARNING) << "pref observer found at shutdown " << it->first; | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 STLDeleteContainerPairSecondPointers(pref_observers_.begin(), | |
| 36 pref_observers_.end()); | |
| 37 pref_observers_.clear(); | |
| 38 } | |
| 39 | |
| 40 void PrefNotifier::OnPreferenceSet(const char* pref_name, | |
| 41 PrefNotifier::PrefStoreType new_store) { | |
| 42 if (pref_value_store_->PrefHasChanged(pref_name, new_store)) | |
| 43 FireObservers(pref_name); | |
| 44 } | |
| 45 | |
| 46 void PrefNotifier::OnUserPreferenceSet(const char* pref_name) { | |
| 47 OnPreferenceSet(pref_name, PrefNotifier::USER_STORE); | |
| 48 } | |
| 49 | |
| 50 void PrefNotifier::FireObservers(const char* path) { | |
| 51 DCHECK(CalledOnValidThread()); | |
| 52 | |
| 53 // Convert path to a std::string because the Details constructor requires a | |
| 54 // class. | |
| 55 std::string path_str(path); | |
| 56 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path_str); | |
| 57 if (observer_iterator == pref_observers_.end()) | |
| 58 return; | |
| 59 | |
| 60 NotificationObserverList::Iterator it(*(observer_iterator->second)); | |
| 61 NotificationObserver* observer; | |
| 62 while ((observer = it.GetNext()) != NULL) { | |
| 63 observer->Observe(NotificationType::PREF_CHANGED, | |
| 64 Source<PrefService>(pref_service_), | |
| 65 Details<std::string>(&path_str)); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void PrefNotifier::AddPrefObserver(const char* path, | |
| 70 NotificationObserver* obs) { | |
| 71 // Get the pref observer list associated with the path. | |
| 72 NotificationObserverList* observer_list = NULL; | |
| 73 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); | |
| 74 if (observer_iterator == pref_observers_.end()) { | |
| 75 observer_list = new NotificationObserverList; | |
| 76 pref_observers_[path] = observer_list; | |
| 77 } else { | |
| 78 observer_list = observer_iterator->second; | |
| 79 } | |
| 80 | |
| 81 // Verify that this observer doesn't already exist. | |
| 82 NotificationObserverList::Iterator it(*observer_list); | |
| 83 NotificationObserver* existing_obs; | |
| 84 while ((existing_obs = it.GetNext()) != NULL) { | |
| 85 DCHECK(existing_obs != obs) << path << " observer already registered"; | |
| 86 if (existing_obs == obs) | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 // Ok, safe to add the pref observer. | |
| 91 observer_list->AddObserver(obs); | |
| 92 } | |
| 93 | |
| 94 void PrefNotifier::RemovePrefObserver(const char* path, | |
| 95 NotificationObserver* obs) { | |
| 96 DCHECK(CalledOnValidThread()); | |
| 97 | |
| 98 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); | |
| 99 if (observer_iterator == pref_observers_.end()) { | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 NotificationObserverList* observer_list = observer_iterator->second; | |
| 104 observer_list->RemoveObserver(obs); | |
| 105 } | |
| 106 | |
| 107 void PrefNotifier::FireObserversForRefreshedManagedPrefs( | |
| 108 std::vector<std::string> changed_prefs_paths) { | |
| 109 DCHECK(CalledOnValidThread()); | |
| 110 std::vector<std::string>::const_iterator current; | |
| 111 for (current = changed_prefs_paths.begin(); | |
| 112 current != changed_prefs_paths.end(); | |
| 113 ++current) { | |
| 114 FireObservers(current->c_str()); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 void PrefNotifier::Observe(NotificationType type, | |
| 119 const NotificationSource& source, | |
| 120 const NotificationDetails& details) { | |
| 121 using policy::ConfigurationPolicyPrefStore; | |
| 122 | |
| 123 if (type == NotificationType::POLICY_CHANGED) { | |
| 124 PrefValueStore::AfterRefreshCallback* callback = | |
| 125 NewCallback(this, | |
| 126 &PrefNotifier::FireObserversForRefreshedManagedPrefs); | |
| 127 // The notification of the policy refresh can come from any thread, | |
| 128 // but the manipulation of the PrefValueStore must happen on the UI | |
| 129 // thread, thus the policy refresh must be explicitly started on it. | |
| 130 BrowserThread::PostTask( | |
| 131 BrowserThread::UI, FROM_HERE, | |
| 132 NewRunnableMethod( | |
| 133 pref_value_store_, | |
| 134 &PrefValueStore::RefreshPolicyPrefs, | |
| 135 callback)); | |
| 136 } | |
| 137 } | |
| OLD | NEW |