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