Chromium Code Reviews| 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" |
| 9 #include "base/prefs/public/pref_observer.h" | |
| 8 #include "base/prefs/public/pref_service_base.h" | 10 #include "base/prefs/public/pref_service_base.h" |
| 9 | 11 |
| 10 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} | 12 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} |
| 11 | 13 |
| 12 PrefChangeRegistrar::~PrefChangeRegistrar() { | 14 PrefChangeRegistrar::~PrefChangeRegistrar() { |
| 13 // If you see an invalid memory access in this destructor, this | 15 // If you see an invalid memory access in this destructor, this |
| 14 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that | 16 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that |
| 15 // has been destroyed. This should not happen any more but be warned. | 17 // has been destroyed. This should not happen any more but be warned. |
| 16 // Feel free to contact battre@chromium.org in case this happens. | 18 // Feel free to contact battre@chromium.org in case this happens. |
| 17 RemoveAll(); | 19 RemoveAll(); |
| 18 } | 20 } |
| 19 | 21 |
| 20 void PrefChangeRegistrar::Init(PrefServiceBase* service) { | 22 void PrefChangeRegistrar::Init(PrefServiceBase* service) { |
| 21 DCHECK(IsEmpty() || service_ == service); | 23 DCHECK(IsEmpty() || service_ == service); |
| 22 service_ = service; | 24 service_ = service; |
| 23 } | 25 } |
| 24 | 26 |
| 25 void PrefChangeRegistrar::Add(const char* path, PrefObserver* obs) { | 27 void PrefChangeRegistrar::Add(const char* path, PrefObserver* 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 ObserverRegistration registration(path, obs); |
| 31 if (observers_.find(registration) != observers_.end()) { | 38 if (observers_.find(registration) != observers_.end()) { |
| 32 NOTREACHED(); | 39 NOTREACHED(); |
| 33 return; | 40 return; |
| 34 } | 41 } |
| 35 observers_.insert(registration); | 42 observers_.insert(registration); |
| 36 service_->AddPrefObserver(path, obs); | 43 service_->AddPrefObserver(path, obs); |
| 37 } | 44 } |
| 38 | 45 |
| 39 void PrefChangeRegistrar::Remove(const char* path, PrefObserver* obs) { | 46 void PrefChangeRegistrar::Remove(const char* path, PrefObserver* obs) { |
| 47 // Need to bind the callback the exact same way, although it will | |
| 48 // never be called, so that it matches the callback bound via | |
| 49 // AddPrefObserver. | |
| 50 return Remove(path, base::Bind(&PrefObserver::OnPreferenceChanged, | |
| 51 base::Unretained(obs), service_, path)); | |
|
Mattias Nissler (ping if slow)
2012/11/06 13:26:48
Hm, comparing callbacks... not sure whether that's
| |
| 52 } | |
| 53 | |
| 54 void PrefChangeRegistrar::Remove(const char* path, const base::Closure& obs) { | |
| 40 if (!service_) { | 55 if (!service_) { |
| 41 NOTREACHED(); | 56 NOTREACHED(); |
| 42 return; | 57 return; |
| 43 } | 58 } |
| 44 ObserverRegistration registration(path, obs); | 59 ObserverRegistration registration(path, obs); |
| 45 std::set<ObserverRegistration>::iterator it = | 60 std::set<ObserverRegistration>::iterator it = |
| 46 observers_.find(registration); | 61 observers_.find(registration); |
| 47 if (it == observers_.end()) { | 62 if (it == observers_.end()) { |
| 48 NOTREACHED(); | 63 NOTREACHED(); |
| 49 return; | 64 return; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 78 bool PrefChangeRegistrar::IsManaged() { | 93 bool PrefChangeRegistrar::IsManaged() { |
| 79 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); | 94 for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); |
| 80 it != observers_.end(); ++it) { | 95 it != observers_.end(); ++it) { |
| 81 const PrefServiceBase::Preference* pref = | 96 const PrefServiceBase::Preference* pref = |
| 82 service_->FindPreference(it->first.c_str()); | 97 service_->FindPreference(it->first.c_str()); |
| 83 if (pref && pref->IsManaged()) | 98 if (pref && pref->IsManaged()) |
| 84 return true; | 99 return true; |
| 85 } | 100 } |
| 86 return false; | 101 return false; |
| 87 } | 102 } |
| OLD | NEW |