xref: /aosp_15_r20/external/cronet/components/prefs/pref_change_registrar.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 [email protected] 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   // [email protected].
25   RemoveAll();
26 }
27 
Init(PrefService * service)28 void PrefChangeRegistrar::Init(PrefService* service) {
29   DCHECK(IsEmpty() || service_ == service);
30   service_ = service;
31 }
32 
Reset()33 void PrefChangeRegistrar::Reset() {
34   RemoveAll();
35   service_ = nullptr;
36 }
37 
Add(const std::string & path,const base::RepeatingClosure & obs)38 void PrefChangeRegistrar::Add(const std::string& path,
39                               const base::RepeatingClosure& obs) {
40   Add(path,
41       base::BindRepeating(&PrefChangeRegistrar::InvokeUnnamedCallback, obs));
42 }
43 
Add(const std::string & path,const NamedChangeCallback & obs)44 void PrefChangeRegistrar::Add(const std::string& path,
45                               const NamedChangeCallback& obs) {
46   if (!service_) {
47     NOTREACHED();
48     return;
49   }
50   DCHECK(!IsObserved(path)) << "Already had pref, \"" << path
51                             << "\", registered.";
52 
53   service_->AddPrefObserver(path, this);
54   observers_[path] = obs;
55 }
56 
Remove(const std::string & path)57 void PrefChangeRegistrar::Remove(const std::string& path) {
58   DCHECK(IsObserved(path));
59 
60   observers_.erase(path);
61   service_->RemovePrefObserver(path, this);
62 }
63 
RemoveAll()64 void PrefChangeRegistrar::RemoveAll() {
65   for (ObserverMap::const_iterator it = observers_.begin();
66        it != observers_.end(); ++it) {
67     service_->RemovePrefObserver(it->first, this);
68   }
69 
70   observers_.clear();
71 }
72 
IsEmpty() const73 bool PrefChangeRegistrar::IsEmpty() const {
74   return observers_.empty();
75 }
76 
IsObserved(const std::string & pref)77 bool PrefChangeRegistrar::IsObserved(const std::string& pref) {
78   return observers_.find(pref) != observers_.end();
79 }
80 
OnPreferenceChanged(PrefService * service,const std::string & pref)81 void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service,
82                                               const std::string& pref) {
83   if (IsObserved(pref))
84     observers_[pref].Run(pref);
85 }
86 
InvokeUnnamedCallback(const base::RepeatingClosure & callback,const std::string & pref_name)87 void PrefChangeRegistrar::InvokeUnnamedCallback(
88     const base::RepeatingClosure& callback,
89     const std::string& pref_name) {
90   callback.Run();
91 }
92 
prefs()93 PrefService* PrefChangeRegistrar::prefs() {
94   return service_;
95 }
96 
prefs() const97 const PrefService* PrefChangeRegistrar::prefs() const {
98   return service_;
99 }
100