Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(612)

Side by Side Diff: components/prefs/pref_notifier_impl.cc

Issue 2352703003: Remove stl_util's STLDeleteContainerPairSecondPointers from prefs. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/prefs/pref_notifier_impl.h ('k') | components/prefs/pref_notifier_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/prefs/pref_notifier_impl.h" 5 #include "components/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/memory/ptr_util.h"
9 #include "components/prefs/pref_service.h" 9 #include "components/prefs/pref_service.h"
10 10
11 PrefNotifierImpl::PrefNotifierImpl() 11 PrefNotifierImpl::PrefNotifierImpl() : pref_service_(nullptr) {}
12 : pref_service_(NULL) {
13 }
14 12
15 PrefNotifierImpl::PrefNotifierImpl(PrefService* service) 13 PrefNotifierImpl::PrefNotifierImpl(PrefService* service)
16 : pref_service_(service) { 14 : pref_service_(service) {
17 } 15 }
18 16
19 PrefNotifierImpl::~PrefNotifierImpl() { 17 PrefNotifierImpl::~PrefNotifierImpl() {
20 DCHECK(thread_checker_.CalledOnValidThread()); 18 DCHECK(thread_checker_.CalledOnValidThread());
21 19
22 // Verify that there are no pref observers when we shut down. 20 // Verify that there are no pref observers when we shut down.
23 for (PrefObserverMap::iterator it = pref_observers_.begin(); 21 for (const auto& observer_list : pref_observers_) {
24 it != pref_observers_.end(); ++it) { 22 PrefObserverList::Iterator obs_iterator(observer_list.second.get());
25 PrefObserverList::Iterator obs_iterator(it->second);
26 if (obs_iterator.GetNext()) { 23 if (obs_iterator.GetNext()) {
27 LOG(WARNING) << "pref observer found at shutdown " << it->first; 24 LOG(WARNING) << "Pref observer found at shutdown.";
28 } 25 }
29 } 26 }
30 27
31 // Same for initialization observers. 28 // Same for initialization observers.
32 if (!init_observers_.empty()) 29 if (!init_observers_.empty())
33 LOG(WARNING) << "Init observer found at shutdown."; 30 LOG(WARNING) << "Init observer found at shutdown.";
34 31
35 base::STLDeleteContainerPairSecondPointers(pref_observers_.begin(),
36 pref_observers_.end());
37 pref_observers_.clear(); 32 pref_observers_.clear();
38 init_observers_.clear(); 33 init_observers_.clear();
39 } 34 }
40 35
41 void PrefNotifierImpl::AddPrefObserver(const std::string& path, 36 void PrefNotifierImpl::AddPrefObserver(const std::string& path,
42 PrefObserver* obs) { 37 PrefObserver* obs) {
43 // Get the pref observer list associated with the path. 38 // Get the pref observer list associated with the path.
44 PrefObserverList* observer_list = NULL; 39 PrefObserverList* observer_list = nullptr;
45 const PrefObserverMap::iterator observer_iterator = 40 auto observer_iterator = pref_observers_.find(path);
46 pref_observers_.find(path);
47 if (observer_iterator == pref_observers_.end()) { 41 if (observer_iterator == pref_observers_.end()) {
48 observer_list = new PrefObserverList; 42 observer_list = new PrefObserverList;
49 pref_observers_[path] = observer_list; 43 pref_observers_[path] = base::WrapUnique(observer_list);
50 } else { 44 } else {
51 observer_list = observer_iterator->second; 45 observer_list = observer_iterator->second.get();
52 } 46 }
53 47
54 // Add the pref observer. ObserverList will DCHECK if it already is 48 // Add the pref observer. ObserverList will DCHECK if it already is
55 // in the list. 49 // in the list.
56 observer_list->AddObserver(obs); 50 observer_list->AddObserver(obs);
57 } 51 }
58 52
59 void PrefNotifierImpl::RemovePrefObserver(const std::string& path, 53 void PrefNotifierImpl::RemovePrefObserver(const std::string& path,
60 PrefObserver* obs) { 54 PrefObserver* obs) {
61 DCHECK(thread_checker_.CalledOnValidThread()); 55 DCHECK(thread_checker_.CalledOnValidThread());
62 56
63 const PrefObserverMap::iterator observer_iterator = 57 auto observer_iterator = pref_observers_.find(path);
64 pref_observers_.find(path);
65 if (observer_iterator == pref_observers_.end()) { 58 if (observer_iterator == pref_observers_.end()) {
66 return; 59 return;
67 } 60 }
68 61
69 PrefObserverList* observer_list = observer_iterator->second; 62 PrefObserverList* observer_list = observer_iterator->second.get();
70 observer_list->RemoveObserver(obs); 63 observer_list->RemoveObserver(obs);
71 } 64 }
72 65
73 void PrefNotifierImpl::AddInitObserver(base::Callback<void(bool)> obs) { 66 void PrefNotifierImpl::AddInitObserver(base::Callback<void(bool)> obs) {
74 init_observers_.push_back(obs); 67 init_observers_.push_back(obs);
75 } 68 }
76 69
77 void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) { 70 void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) {
78 FireObservers(path); 71 FireObservers(path);
79 } 72 }
80 73
81 void PrefNotifierImpl::OnInitializationCompleted(bool succeeded) { 74 void PrefNotifierImpl::OnInitializationCompleted(bool succeeded) {
82 DCHECK(thread_checker_.CalledOnValidThread()); 75 DCHECK(thread_checker_.CalledOnValidThread());
83 76
84 // We must make a copy of init_observers_ and clear it before we run 77 // We must make a copy of init_observers_ and clear it before we run
85 // observers, or we can end up in this method re-entrantly before 78 // observers, or we can end up in this method re-entrantly before
86 // clearing the observers list. 79 // clearing the observers list.
87 PrefInitObserverList observers(init_observers_); 80 PrefInitObserverList observers(init_observers_);
88 init_observers_.clear(); 81 init_observers_.clear();
89 82
90 for (PrefInitObserverList::iterator it = observers.begin(); 83 for (auto& observer : observers)
91 it != observers.end(); 84 observer.Run(succeeded);
92 ++it) {
93 it->Run(succeeded);
94 }
95 } 85 }
96 86
97 void PrefNotifierImpl::FireObservers(const std::string& path) { 87 void PrefNotifierImpl::FireObservers(const std::string& path) {
98 DCHECK(thread_checker_.CalledOnValidThread()); 88 DCHECK(thread_checker_.CalledOnValidThread());
99 89
100 // Only send notifications for registered preferences. 90 // Only send notifications for registered preferences.
101 if (!pref_service_->FindPreference(path)) 91 if (!pref_service_->FindPreference(path))
102 return; 92 return;
103 93
104 const PrefObserverMap::iterator observer_iterator = 94 auto observer_iterator = pref_observers_.find(path);
105 pref_observers_.find(path);
106 if (observer_iterator == pref_observers_.end()) 95 if (observer_iterator == pref_observers_.end())
107 return; 96 return;
108 97
109 FOR_EACH_OBSERVER(PrefObserver, 98 FOR_EACH_OBSERVER(PrefObserver,
110 *(observer_iterator->second), 99 *(observer_iterator->second),
111 OnPreferenceChanged(pref_service_, path)); 100 OnPreferenceChanged(pref_service_, path));
112 } 101 }
113 102
114 void PrefNotifierImpl::SetPrefService(PrefService* pref_service) { 103 void PrefNotifierImpl::SetPrefService(PrefService* pref_service) {
115 DCHECK(pref_service_ == NULL); 104 DCHECK(pref_service_ == nullptr);
116 pref_service_ = pref_service; 105 pref_service_ = pref_service;
117 } 106 }
OLDNEW
« no previous file with comments | « components/prefs/pref_notifier_impl.h ('k') | components/prefs/pref_notifier_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698