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/api/prefs/pref_change_registrar.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "chrome/browser/api/prefs/pref_service_base.h" | |
9 | |
10 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} | |
11 | |
12 PrefChangeRegistrar::~PrefChangeRegistrar() { | |
13 // If you see an invalid memory access in this destructor, this | |
14 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that | |
15 // 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 RemoveAll(); | |
18 } | |
19 | |
20 void PrefChangeRegistrar::Init(PrefServiceBase* service) { | |
21 DCHECK(IsEmpty() || service_ == service); | |
22 service_ = service; | |
23 } | |
24 | |
25 void PrefChangeRegistrar::Add(const char* path, | |
26 content::NotificationObserver* obs) { | |
27 if (!service_) { | |
28 NOTREACHED(); | |
29 return; | |
30 } | |
31 ObserverRegistration registration(path, obs); | |
32 if (observers_.find(registration) != observers_.end()) { | |
33 NOTREACHED(); | |
34 return; | |
35 } | |
36 observers_.insert(registration); | |
37 service_->AddPrefObserver(path, obs); | |
38 } | |
39 | |
40 void PrefChangeRegistrar::Remove(const char* path, | |
41 content::NotificationObserver* obs) { | |
42 if (!service_) { | |
43 NOTREACHED(); | |
44 return; | |
45 } | |
46 ObserverRegistration registration(path, obs); | |
47 std::set<ObserverRegistration>::iterator it = | |
48 observers_.find(registration); | |
49 if (it == observers_.end()) { | |
50 NOTREACHED(); | |
51 return; | |
52 } | |
53 service_->RemovePrefObserver(it->first.c_str(), it->second); | |
54 observers_.erase(it); | |
55 } | |
56 | |
57 void PrefChangeRegistrar::RemoveAll() { | |
58 if (service_) { | |
59 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); | |
60 it != observers_.end(); ++it) { | |
61 service_->RemovePrefObserver(it->first.c_str(), it->second); | |
62 } | |
63 observers_.clear(); | |
64 } | |
65 } | |
66 | |
67 bool PrefChangeRegistrar::IsEmpty() const { | |
68 return observers_.empty(); | |
69 } | |
70 | |
71 bool PrefChangeRegistrar::IsObserved(const std::string& pref) { | |
72 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); | |
73 it != observers_.end(); ++it) { | |
74 if (it->first == pref) | |
75 return true; | |
76 } | |
77 return false; | |
78 } | |
79 | |
80 bool PrefChangeRegistrar::IsManaged() { | |
81 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); | |
82 it != observers_.end(); ++it) { | |
83 const PrefServiceBase::Preference* pref = | |
84 service_->FindPreference(it->first.c_str()); | |
85 if (pref && pref->IsManaged()) | |
86 return true; | |
87 } | |
88 return false; | |
89 } | |
OLD | NEW |