• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Reset()33 void PrefChangeRegistrar::Reset() {
34   RemoveAll();
35   service_ = nullptr;
36 }
37 
Add(std::string_view path,const base::RepeatingClosure & obs)38 void PrefChangeRegistrar::Add(std::string_view path,
39                               const base::RepeatingClosure& obs) {
40   Add(path,
41       base::BindRepeating(&PrefChangeRegistrar::InvokeUnnamedCallback, obs));
42 }
43 
Add(std::string_view path,const NamedChangeCallback & obs)44 void PrefChangeRegistrar::Add(std::string_view path,
45                               const NamedChangeCallback& obs) {
46   if (!service_) {
47     NOTREACHED();
48   }
49   DCHECK(!IsObserved(path)) << "Already had pref, \"" << path
50                             << "\", registered.";
51 
52   service_->AddPrefObserver(path, this);
53   observers_.insert_or_assign(std::string(path), obs);
54 }
55 
Remove(std::string_view path)56 void PrefChangeRegistrar::Remove(std::string_view path) {
57   DCHECK(IsObserved(path));
58 
59   // Use std::map::erase directly once C++23 is supported.
60   auto it = observers_.find(path);
61   observers_.erase(it);
62   service_->RemovePrefObserver(path, this);
63 }
64 
RemoveAll()65 void PrefChangeRegistrar::RemoveAll() {
66   for (ObserverMap::const_iterator it = observers_.begin();
67        it != observers_.end(); ++it) {
68     service_->RemovePrefObserver(it->first, this);
69   }
70 
71   observers_.clear();
72 }
73 
IsEmpty() const74 bool PrefChangeRegistrar::IsEmpty() const {
75   return observers_.empty();
76 }
77 
IsObserved(std::string_view pref)78 bool PrefChangeRegistrar::IsObserved(std::string_view pref) {
79   return observers_.find(pref) != observers_.end();
80 }
81 
OnPreferenceChanged(PrefService * service,std::string_view pref)82 void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service,
83                                               std::string_view pref) {
84   if (auto it = observers_.find(pref); it != observers_.end()) {
85     // TODO: crbug.com/349741884 - Consider changing the callback to accept a
86     // string_view.
87     it->second.Run(std::string(pref));
88   }
89 }
90 
InvokeUnnamedCallback(const base::RepeatingClosure & callback,const std::string & pref_name)91 void PrefChangeRegistrar::InvokeUnnamedCallback(
92     const base::RepeatingClosure& callback,
93     const std::string& pref_name) {
94   callback.Run();
95 }
96 
prefs()97 PrefService* PrefChangeRegistrar::prefs() {
98   return service_;
99 }
100 
prefs() const101 const PrefService* PrefChangeRegistrar::prefs() const {
102   return service_;
103 }
104