OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "chrome/browser/prefs/pref_notifier.h" |
6 | 6 |
7 #include "base/stl_util-inl.h" | 7 #include "base/stl_util-inl.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 9 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
11 #include "chrome/browser/prefs/pref_value_store.h" | 11 #include "chrome/browser/prefs/pref_value_store.h" |
12 #include "chrome/common/notification_service.h" | 12 #include "chrome/common/notification_service.h" |
13 | 13 |
14 | 14 PrefNotifier::PrefNotifier(PrefService* service) |
15 PrefNotifier::PrefNotifier(PrefService* service, PrefValueStore* value_store) | 15 : pref_service_(service) { |
16 : pref_service_(service), | |
17 pref_value_store_(value_store) { | |
18 registrar_.Add(this, | |
19 NotificationType(NotificationType::POLICY_CHANGED), | |
20 NotificationService::AllSources()); | |
21 } | 16 } |
22 | 17 |
23 PrefNotifier::~PrefNotifier() { | 18 PrefNotifier::~PrefNotifier() { |
24 DCHECK(CalledOnValidThread()); | 19 DCHECK(CalledOnValidThread()); |
25 | 20 |
26 // Verify that there are no pref observers when we shut down. | 21 // Verify that there are no pref observers when we shut down. |
27 for (PrefObserverMap::iterator it = pref_observers_.begin(); | 22 for (PrefObserverMap::iterator it = pref_observers_.begin(); |
28 it != pref_observers_.end(); ++it) { | 23 it != pref_observers_.end(); ++it) { |
29 NotificationObserverList::Iterator obs_iterator(*(it->second)); | 24 NotificationObserverList::Iterator obs_iterator(*(it->second)); |
30 if (obs_iterator.GetNext()) { | 25 if (obs_iterator.GetNext()) { |
31 LOG(WARNING) << "pref observer found at shutdown " << it->first; | 26 LOG(WARNING) << "pref observer found at shutdown " << it->first; |
32 } | 27 } |
33 } | 28 } |
34 | 29 |
35 STLDeleteContainerPairSecondPointers(pref_observers_.begin(), | 30 STLDeleteContainerPairSecondPointers(pref_observers_.begin(), |
36 pref_observers_.end()); | 31 pref_observers_.end()); |
37 pref_observers_.clear(); | 32 pref_observers_.clear(); |
38 } | 33 } |
39 | 34 |
40 void PrefNotifier::OnPreferenceSet(const char* pref_name, | 35 void PrefNotifier::OnPreferenceChanged(const std::string& pref_name) { |
41 PrefNotifier::PrefStoreType new_store) { | 36 if (pref_service_->FindPreference(pref_name.c_str())) |
42 if (pref_value_store_->PrefHasChanged(pref_name, new_store)) | |
43 FireObservers(pref_name); | 37 FireObservers(pref_name); |
44 } | 38 } |
45 | 39 |
46 void PrefNotifier::OnUserPreferenceSet(const char* pref_name) { | 40 void PrefNotifier::OnInitializationCompleted() { |
47 OnPreferenceSet(pref_name, PrefNotifier::USER_STORE); | 41 NotificationService::current()->Notify( |
42 NotificationType::PREF_INITIALIZATION_COMPLETED, | |
43 Source<PrefService>(pref_service_), | |
44 NotificationService::NoDetails()); | |
48 } | 45 } |
49 | 46 |
50 void PrefNotifier::FireObservers(const char* path) { | 47 void PrefNotifier::FireObservers(const std::string& path) { |
51 DCHECK(CalledOnValidThread()); | 48 DCHECK(CalledOnValidThread()); |
52 | 49 |
53 // Convert path to a std::string because the Details constructor requires a | 50 // Convert path to a std::string because the Details constructor requires a |
54 // class. | 51 // class. |
battre (please use the other)
2010/12/02 10:41:19
This comment became obsolete.
Mattias Nissler (ping if slow)
2010/12/02 16:38:24
Done.
| |
55 std::string path_str(path); | 52 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); |
56 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path_str); | |
57 if (observer_iterator == pref_observers_.end()) | 53 if (observer_iterator == pref_observers_.end()) |
58 return; | 54 return; |
59 | 55 |
60 NotificationObserverList::Iterator it(*(observer_iterator->second)); | 56 NotificationObserverList::Iterator it(*(observer_iterator->second)); |
61 NotificationObserver* observer; | 57 NotificationObserver* observer; |
62 while ((observer = it.GetNext()) != NULL) { | 58 while ((observer = it.GetNext()) != NULL) { |
63 observer->Observe(NotificationType::PREF_CHANGED, | 59 observer->Observe(NotificationType::PREF_CHANGED, |
64 Source<PrefService>(pref_service_), | 60 Source<PrefService>(pref_service_), |
65 Details<std::string>(&path_str)); | 61 Details<const std::string>(&path)); |
66 } | 62 } |
67 } | 63 } |
68 | 64 |
69 void PrefNotifier::AddPrefObserver(const char* path, | 65 void PrefNotifier::AddPrefObserver(const char* path, |
70 NotificationObserver* obs) { | 66 NotificationObserver* obs) { |
71 // Get the pref observer list associated with the path. | 67 // Get the pref observer list associated with the path. |
72 NotificationObserverList* observer_list = NULL; | 68 NotificationObserverList* observer_list = NULL; |
73 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); | 69 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); |
74 if (observer_iterator == pref_observers_.end()) { | 70 if (observer_iterator == pref_observers_.end()) { |
75 observer_list = new NotificationObserverList; | 71 observer_list = new NotificationObserverList; |
(...skipping 20 matching lines...) Expand all Loading... | |
96 DCHECK(CalledOnValidThread()); | 92 DCHECK(CalledOnValidThread()); |
97 | 93 |
98 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); | 94 PrefObserverMap::iterator observer_iterator = pref_observers_.find(path); |
99 if (observer_iterator == pref_observers_.end()) { | 95 if (observer_iterator == pref_observers_.end()) { |
100 return; | 96 return; |
101 } | 97 } |
102 | 98 |
103 NotificationObserverList* observer_list = observer_iterator->second; | 99 NotificationObserverList* observer_list = observer_iterator->second; |
104 observer_list->RemoveObserver(obs); | 100 observer_list->RemoveObserver(obs); |
105 } | 101 } |
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 |