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

Side by Side Diff: base/prefs/public/pref_change_registrar.cc

Issue 11368098: Draft change to use base::Closure instead of PrefObserver in PrefChangeRegistrar. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to review comments. Created 8 years, 1 month 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 | Annotate | Revision Log
OLDNEW
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 "base/prefs/public/pref_change_registrar.h" 5 #include "base/prefs/public/pref_change_registrar.h"
6 6
7 #include "base/bind.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/prefs/public/pref_service_base.h" 9 #include "base/prefs/public/pref_service_base.h"
9 10
10 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} 11 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {}
11 12
12 PrefChangeRegistrar::~PrefChangeRegistrar() { 13 PrefChangeRegistrar::~PrefChangeRegistrar() {
13 // If you see an invalid memory access in this destructor, this 14 // If you see an invalid memory access in this destructor, this
14 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that 15 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that
15 // has been destroyed. This should not happen any more but be warned. 16 // has been destroyed. This should not happen any more but be warned.
16 // Feel free to contact battre@chromium.org in case this happens. 17 // Feel free to contact battre@chromium.org in case this happens.
17 RemoveAll(); 18 RemoveAll();
18 } 19 }
19 20
20 void PrefChangeRegistrar::Init(PrefServiceBase* service) { 21 void PrefChangeRegistrar::Init(PrefServiceBase* service) {
21 DCHECK(IsEmpty() || service_ == service); 22 DCHECK(IsEmpty() || service_ == service);
22 service_ = service; 23 service_ = service;
23 } 24 }
24 25
25 void PrefChangeRegistrar::Add(const char* path, PrefObserver* obs) { 26 void PrefChangeRegistrar::Add(const char* path, PrefObserver* obs) {
27 DCHECK(obs);
28 return Add(path, base::Bind(&PrefObserver::OnPreferenceChanged,
29 base::Unretained(obs),
30 service_, std::string(path)));
31 }
32
33 void PrefChangeRegistrar::Add(const char* path, const base::Closure& obs) {
26 if (!service_) { 34 if (!service_) {
27 NOTREACHED(); 35 NOTREACHED();
28 return; 36 return;
29 } 37 }
30 ObserverRegistration registration(path, obs); 38 DCHECK(!IsObserved(path)) << "Already had this pref registered.";
31 if (observers_.find(registration) != observers_.end()) { 39
32 NOTREACHED(); 40 service_->AddPrefObserver(path, this);
33 return; 41 observers_[path] = obs;
34 }
35 observers_.insert(registration);
36 service_->AddPrefObserver(path, obs);
37 } 42 }
38 43
39 void PrefChangeRegistrar::Remove(const char* path, PrefObserver* obs) { 44 void PrefChangeRegistrar::Remove(const char* path) {
40 if (!service_) { 45 DCHECK(IsObserved(path));
41 NOTREACHED(); 46
42 return; 47 observers_.erase(path);
43 } 48 service_->RemovePrefObserver(path, this);
44 ObserverRegistration registration(path, obs);
45 std::set<ObserverRegistration>::iterator it =
46 observers_.find(registration);
47 if (it == observers_.end()) {
48 NOTREACHED();
49 return;
50 }
51 service_->RemovePrefObserver(it->first.c_str(), it->second);
52 observers_.erase(it);
53 } 49 }
54 50
55 void PrefChangeRegistrar::RemoveAll() { 51 void PrefChangeRegistrar::RemoveAll() {
56 if (service_) { 52 for (ObserverMap::const_iterator it = observers_.begin();
57 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); 53 it != observers_.end(); ++it) {
58 it != observers_.end(); ++it) { 54 service_->RemovePrefObserver(it->first.c_str(), this);
59 service_->RemovePrefObserver(it->first.c_str(), it->second);
60 }
61 observers_.clear();
62 } 55 }
56
57 observers_.clear();
63 } 58 }
64 59
65 bool PrefChangeRegistrar::IsEmpty() const { 60 bool PrefChangeRegistrar::IsEmpty() const {
66 return observers_.empty(); 61 return observers_.empty();
67 } 62 }
68 63
69 bool PrefChangeRegistrar::IsObserved(const std::string& pref) { 64 bool PrefChangeRegistrar::IsObserved(const std::string& pref) {
70 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); 65 return observers_.find(pref) != observers_.end();
71 it != observers_.end(); ++it) {
72 if (it->first == pref)
73 return true;
74 }
75 return false;
76 } 66 }
77 67
78 bool PrefChangeRegistrar::IsManaged() { 68 bool PrefChangeRegistrar::IsManaged() {
79 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); 69 for (ObserverMap::const_iterator it = observers_.begin();
80 it != observers_.end(); ++it) { 70 it != observers_.end(); ++it) {
81 const PrefServiceBase::Preference* pref = 71 const PrefServiceBase::Preference* pref =
82 service_->FindPreference(it->first.c_str()); 72 service_->FindPreference(it->first.c_str());
83 if (pref && pref->IsManaged()) 73 if (pref && pref->IsManaged())
84 return true; 74 return true;
85 } 75 }
86 return false; 76 return false;
87 } 77 }
78
79 void PrefChangeRegistrar::OnPreferenceChanged(PrefServiceBase* service,
80 const std::string& pref) {
81 if (IsObserved(pref))
82 observers_[pref].Run();
83 }
OLDNEW
« no previous file with comments | « base/prefs/public/pref_change_registrar.h ('k') | base/prefs/public/pref_change_registrar_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698