1 // Copyright 2010 The Chromium Authors
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 "components/prefs/pref_change_registrar.h"
6
7 #include <ostream>
8
9 #include "base/check.h"
10 #include "base/functional/bind.h"
11 #include "base/notreached.h"
12 #include "components/prefs/pref_service.h"
13
PrefChangeRegistrar()14 PrefChangeRegistrar::PrefChangeRegistrar() : service_(nullptr) {}
15
~PrefChangeRegistrar()16 PrefChangeRegistrar::~PrefChangeRegistrar() {
17 // If you see an invalid memory access in this destructor, this
18 // PrefChangeRegistrar might be subscribed to an OffTheRecordProfileImpl that
19 // has been destroyed. This should not happen any more but be warned.
20 // Feel free to contact battre@chromium.org in case this happens.
21 //
22 // This can also happen for non-OTR profiles, when the
23 // DestroyProfileOnBrowserClose flag is enabled. In that case, contact
24 // nicolaso@chromium.org.
25 RemoveAll();
26 }
27
Init(PrefService * service)28 void PrefChangeRegistrar::Init(PrefService* service) {
29 DCHECK(IsEmpty() || service_ == service);
30 service_ = service;
31 }
32
Add(const std::string & path,const base::RepeatingClosure & obs)33 void PrefChangeRegistrar::Add(const std::string& path,
34 const base::RepeatingClosure& obs) {
35 Add(path,
36 base::BindRepeating(&PrefChangeRegistrar::InvokeUnnamedCallback, obs));
37 }
38
Add(const std::string & path,const NamedChangeCallback & obs)39 void PrefChangeRegistrar::Add(const std::string& path,
40 const NamedChangeCallback& obs) {
41 if (!service_) {
42 NOTREACHED();
43 return;
44 }
45 DCHECK(!IsObserved(path)) << "Already had pref, \"" << path
46 << "\", registered.";
47
48 service_->AddPrefObserver(path, this);
49 observers_[path] = obs;
50 }
51
Remove(const std::string & path)52 void PrefChangeRegistrar::Remove(const std::string& path) {
53 DCHECK(IsObserved(path));
54
55 observers_.erase(path);
56 service_->RemovePrefObserver(path, this);
57 }
58
RemoveAll()59 void PrefChangeRegistrar::RemoveAll() {
60 for (ObserverMap::const_iterator it = observers_.begin();
61 it != observers_.end(); ++it) {
62 service_->RemovePrefObserver(it->first, this);
63 }
64
65 observers_.clear();
66 }
67
IsEmpty() const68 bool PrefChangeRegistrar::IsEmpty() const {
69 return observers_.empty();
70 }
71
IsObserved(const std::string & pref)72 bool PrefChangeRegistrar::IsObserved(const std::string& pref) {
73 return observers_.find(pref) != observers_.end();
74 }
75
OnPreferenceChanged(PrefService * service,const std::string & pref)76 void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service,
77 const std::string& pref) {
78 if (IsObserved(pref))
79 observers_[pref].Run(pref);
80 }
81
InvokeUnnamedCallback(const base::RepeatingClosure & callback,const std::string & pref_name)82 void PrefChangeRegistrar::InvokeUnnamedCallback(
83 const base::RepeatingClosure& callback,
84 const std::string& pref_name) {
85 callback.Run();
86 }
87
prefs()88 PrefService* PrefChangeRegistrar::prefs() {
89 return service_;
90 }
91
prefs() const92 const PrefService* PrefChangeRegistrar::prefs() const {
93 return service_;
94 }
95