Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / prefs / pref_change_registrar.cc
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
14 PrefChangeRegistrar::PrefChangeRegistrar() : service_(nullptr) {}
15
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
28 void PrefChangeRegistrar::Init(PrefService* service) {
29   DCHECK(IsEmpty() || service_ == service);
30   service_ = service;
31 }
32
33 void PrefChangeRegistrar::Add(const std::string& path,
34                               const base::RepeatingClosure& obs) {
35   Add(path,
36       base::BindRepeating(&PrefChangeRegistrar::InvokeUnnamedCallback, obs));
37 }
38
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
52 void PrefChangeRegistrar::Remove(const std::string& path) {
53   DCHECK(IsObserved(path));
54
55   observers_.erase(path);
56   service_->RemovePrefObserver(path, this);
57 }
58
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
68 bool PrefChangeRegistrar::IsEmpty() const {
69   return observers_.empty();
70 }
71
72 bool PrefChangeRegistrar::IsObserved(const std::string& pref) {
73   return observers_.find(pref) != observers_.end();
74 }
75
76 void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service,
77                                               const std::string& pref) {
78   if (IsObserved(pref))
79     observers_[pref].Run(pref);
80 }
81
82 void PrefChangeRegistrar::InvokeUnnamedCallback(
83     const base::RepeatingClosure& callback,
84     const std::string& pref_name) {
85   callback.Run();
86 }
87
88 PrefService* PrefChangeRegistrar::prefs() {
89   return service_;
90 }
91
92 const PrefService* PrefChangeRegistrar::prefs() const {
93   return service_;
94 }