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 "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), service_, path)); | |
30 } | |
31 | |
32 void PrefChangeRegistrar::Add(const char* path, const base::Closure& obs) { | |
26 if (!service_) { | 33 if (!service_) { |
27 NOTREACHED(); | 34 NOTREACHED(); |
28 return; | 35 return; |
29 } | 36 } |
30 ObserverRegistration registration(path, obs); | 37 DCHECK(!IsObserved(path)) << "Already had this pref registered."; |
31 if (observers_.find(registration) != observers_.end()) { | 38 |
32 NOTREACHED(); | 39 service_->AddPrefObserver(path, this); |
33 return; | 40 observers_[path] = obs; |
34 } | |
35 observers_.insert(registration); | |
36 service_->AddPrefObserver(path, obs); | |
37 } | 41 } |
38 | 42 |
39 void PrefChangeRegistrar::Remove(const char* path, PrefObserver* obs) { | 43 void PrefChangeRegistrar::Remove(const char* path) { |
40 if (!service_) { | 44 DCHECK(IsObserved(path)); |
41 NOTREACHED(); | 45 |
42 return; | 46 observers_.erase(path); |
43 } | 47 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 } | 48 } |
54 | 49 |
55 void PrefChangeRegistrar::RemoveAll() { | 50 void PrefChangeRegistrar::RemoveAll() { |
56 if (service_) { | 51 for (ObserverMap::const_iterator it = observers_.begin(); |
57 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); | 52 it != observers_.end(); ++it) { |
58 it != observers_.end(); ++it) { | 53 service_->RemovePrefObserver(it->first.c_str(), this); |
59 service_->RemovePrefObserver(it->first.c_str(), it->second); | |
60 } | |
61 observers_.clear(); | |
62 } | 54 } |
55 | |
56 observers_.clear(); | |
63 } | 57 } |
64 | 58 |
65 bool PrefChangeRegistrar::IsEmpty() const { | 59 bool PrefChangeRegistrar::IsEmpty() const { |
66 return observers_.empty(); | 60 return observers_.empty(); |
67 } | 61 } |
68 | 62 |
69 bool PrefChangeRegistrar::IsObserved(const std::string& pref) { | 63 bool PrefChangeRegistrar::IsObserved(const std::string& pref) { |
70 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); | 64 return observers_.find(pref) != observers_.end(); |
71 it != observers_.end(); ++it) { | |
72 if (it->first == pref) | |
73 return true; | |
74 } | |
75 return false; | |
76 } | 65 } |
77 | 66 |
78 bool PrefChangeRegistrar::IsManaged() { | 67 bool PrefChangeRegistrar::IsManaged() { |
79 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); | 68 for (ObserverMap::const_iterator it = observers_.begin(); |
80 it != observers_.end(); ++it) { | 69 it != observers_.end(); ++it) { |
81 const PrefServiceBase::Preference* pref = | 70 const PrefServiceBase::Preference* pref = |
82 service_->FindPreference(it->first.c_str()); | 71 service_->FindPreference(it->first.c_str()); |
83 if (pref && pref->IsManaged()) | 72 if (pref && pref->IsManaged()) |
84 return true; | 73 return true; |
85 } | 74 } |
86 return false; | 75 return false; |
87 } | 76 } |
77 | |
78 void PrefChangeRegistrar::OnPreferenceChanged(PrefServiceBase* service, | |
79 const std::string& pref) { | |
80 if (IsObserved(pref)) { | |
Mattias Nissler (ping if slow)
2012/11/07 16:02:24
nit: no need for curlies.
Jói
2012/11/08 11:03:14
Done.
| |
81 observers_[pref].Run(); | |
82 } | |
83 } | |
OLD | NEW |