Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / base / prefs / pref_change_registrar.cc
blob13193484ff449c348be51f94d35474968ae7efe7
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.
5 #include "base/prefs/pref_change_registrar.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
11 PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {}
13 PrefChangeRegistrar::~PrefChangeRegistrar() {
14 // If you see an invalid memory access in this destructor, this
15 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that
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.
18 RemoveAll();
21 void PrefChangeRegistrar::Init(PrefService* service) {
22 DCHECK(IsEmpty() || service_ == service);
23 service_ = service;
26 void PrefChangeRegistrar::Add(const std::string& path,
27 const base::Closure& obs) {
28 Add(path, base::Bind(&PrefChangeRegistrar::InvokeUnnamedCallback, obs));
31 void PrefChangeRegistrar::Add(const std::string& path,
32 const NamedChangeCallback& obs) {
33 if (!service_) {
34 NOTREACHED();
35 return;
37 DCHECK(!IsObserved(path)) << "Already had this pref registered.";
39 service_->AddPrefObserver(path, this);
40 observers_[path] = obs;
43 void PrefChangeRegistrar::Remove(const std::string& path) {
44 DCHECK(IsObserved(path));
46 observers_.erase(path);
47 service_->RemovePrefObserver(path, this);
50 void PrefChangeRegistrar::RemoveAll() {
51 for (ObserverMap::const_iterator it = observers_.begin();
52 it != observers_.end(); ++it) {
53 service_->RemovePrefObserver(it->first, this);
56 observers_.clear();
59 bool PrefChangeRegistrar::IsEmpty() const {
60 return observers_.empty();
63 bool PrefChangeRegistrar::IsObserved(const std::string& pref) {
64 return observers_.find(pref) != observers_.end();
67 bool PrefChangeRegistrar::IsManaged() {
68 for (ObserverMap::const_iterator it = observers_.begin();
69 it != observers_.end(); ++it) {
70 const PrefService::Preference* pref = service_->FindPreference(it->first);
71 if (pref && pref->IsManaged())
72 return true;
74 return false;
77 void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service,
78 const std::string& pref) {
79 if (IsObserved(pref))
80 observers_[pref].Run(pref);
83 void PrefChangeRegistrar::InvokeUnnamedCallback(const base::Closure& callback,
84 const std::string& pref_name) {
85 callback.Run();
88 PrefService* PrefChangeRegistrar::prefs() {
89 return service_;
92 const PrefService* PrefChangeRegistrar::prefs() const {
93 return service_;