| 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/pref_change_registrar.h" | 5 #include "base/prefs/pref_change_registrar.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 | 10 |
| 11 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} | 11 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} |
| 12 | 12 |
| 13 PrefChangeRegistrar::~PrefChangeRegistrar() { | 13 PrefChangeRegistrar::~PrefChangeRegistrar() { |
| 14 // If you see an invalid memory access in this destructor, this | 14 // If you see an invalid memory access in this destructor, this |
| 15 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that | 15 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that |
| 16 // 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. |
| 17 // Feel free to contact battre@chromium.org in case this happens. | 17 // Feel free to contact battre@chromium.org in case this happens. |
| 18 RemoveAll(); | 18 RemoveAll(); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void PrefChangeRegistrar::Init(PrefService* service) { | 21 void PrefChangeRegistrar::Init(PrefService* service) { |
| 22 DCHECK(IsEmpty() || service_ == service); | 22 DCHECK(IsEmpty() || service_ == service); |
| 23 service_ = service; | 23 service_ = service; |
| 24 } | 24 } |
| 25 | 25 |
| 26 void PrefChangeRegistrar::Add(const char* path, | 26 void PrefChangeRegistrar::Add(const std::string& path, |
| 27 const base::Closure& obs) { | 27 const base::Closure& obs) { |
| 28 Add(path, base::Bind(&PrefChangeRegistrar::InvokeUnnamedCallback, obs)); | 28 Add(path, base::Bind(&PrefChangeRegistrar::InvokeUnnamedCallback, obs)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void PrefChangeRegistrar::Add(const char* path, | 31 void PrefChangeRegistrar::Add(const std::string& path, |
| 32 const NamedChangeCallback& obs) { | 32 const NamedChangeCallback& obs) { |
| 33 if (!service_) { | 33 if (!service_) { |
| 34 NOTREACHED(); | 34 NOTREACHED(); |
| 35 return; | 35 return; |
| 36 } | 36 } |
| 37 DCHECK(!IsObserved(path)) << "Already had this pref registered."; | 37 DCHECK(!IsObserved(path)) << "Already had this pref registered."; |
| 38 | 38 |
| 39 service_->AddPrefObserver(path, this); | 39 service_->AddPrefObserver(path, this); |
| 40 observers_[path] = obs; | 40 observers_[path] = obs; |
| 41 } | 41 } |
| 42 | 42 |
| 43 void PrefChangeRegistrar::Remove(const char* path) { | 43 void PrefChangeRegistrar::Remove(const std::string& path) { |
| 44 DCHECK(IsObserved(path)); | 44 DCHECK(IsObserved(path)); |
| 45 | 45 |
| 46 observers_.erase(path); | 46 observers_.erase(path); |
| 47 service_->RemovePrefObserver(path, this); | 47 service_->RemovePrefObserver(path, this); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void PrefChangeRegistrar::RemoveAll() { | 50 void PrefChangeRegistrar::RemoveAll() { |
| 51 for (ObserverMap::const_iterator it = observers_.begin(); | 51 for (ObserverMap::const_iterator it = observers_.begin(); |
| 52 it != observers_.end(); ++it) { | 52 it != observers_.end(); ++it) { |
| 53 service_->RemovePrefObserver(it->first.c_str(), this); | 53 service_->RemovePrefObserver(it->first, this); |
| 54 } | 54 } |
| 55 | 55 |
| 56 observers_.clear(); | 56 observers_.clear(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 bool PrefChangeRegistrar::IsEmpty() const { | 59 bool PrefChangeRegistrar::IsEmpty() const { |
| 60 return observers_.empty(); | 60 return observers_.empty(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 bool PrefChangeRegistrar::IsObserved(const std::string& pref) { | 63 bool PrefChangeRegistrar::IsObserved(const std::string& pref) { |
| 64 return observers_.find(pref) != observers_.end(); | 64 return observers_.find(pref) != observers_.end(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 bool PrefChangeRegistrar::IsManaged() { | 67 bool PrefChangeRegistrar::IsManaged() { |
| 68 for (ObserverMap::const_iterator it = observers_.begin(); | 68 for (ObserverMap::const_iterator it = observers_.begin(); |
| 69 it != observers_.end(); ++it) { | 69 it != observers_.end(); ++it) { |
| 70 const PrefService::Preference* pref = | 70 const PrefService::Preference* pref = service_->FindPreference(it->first); |
| 71 service_->FindPreference(it->first.c_str()); | |
| 72 if (pref && pref->IsManaged()) | 71 if (pref && pref->IsManaged()) |
| 73 return true; | 72 return true; |
| 74 } | 73 } |
| 75 return false; | 74 return false; |
| 76 } | 75 } |
| 77 | 76 |
| 78 void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service, | 77 void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service, |
| 79 const std::string& pref) { | 78 const std::string& pref) { |
| 80 if (IsObserved(pref)) | 79 if (IsObserved(pref)) |
| 81 observers_[pref].Run(pref); | 80 observers_[pref].Run(pref); |
| 82 } | 81 } |
| 83 | 82 |
| 84 void PrefChangeRegistrar::InvokeUnnamedCallback(const base::Closure& callback, | 83 void PrefChangeRegistrar::InvokeUnnamedCallback(const base::Closure& callback, |
| 85 const std::string& pref_name) { | 84 const std::string& pref_name) { |
| 86 callback.Run(); | 85 callback.Run(); |
| 87 } | 86 } |
| 88 | 87 |
| 89 PrefService* PrefChangeRegistrar::prefs() { | 88 PrefService* PrefChangeRegistrar::prefs() { |
| 90 return service_; | 89 return service_; |
| 91 } | 90 } |
| 92 | 91 |
| 93 const PrefService* PrefChangeRegistrar::prefs() const { | 92 const PrefService* PrefChangeRegistrar::prefs() const { |
| 94 return service_; | 93 return service_; |
| 95 } | 94 } |
| OLD | NEW |