1 // Copyright 2012 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_notifier_impl.h"
6
7 #include "base/debug/alias.h"
8 #include "base/debug/dump_without_crashing.h"
9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/observer_list.h"
12 #include "base/strings/strcat.h"
13 #include "components/prefs/pref_service.h"
14
PrefNotifierImpl()15 PrefNotifierImpl::PrefNotifierImpl() : pref_service_(nullptr) {}
16
PrefNotifierImpl(PrefService * service)17 PrefNotifierImpl::PrefNotifierImpl(PrefService* service)
18 : pref_service_(service) {
19 }
20
~PrefNotifierImpl()21 PrefNotifierImpl::~PrefNotifierImpl() {
22 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
23
24 // Verify that there are no pref observers when we shut down.
25 for (const auto& observer_list : pref_observers_) {
26 if (observer_list.second.begin() != observer_list.second.end()) {
27 // Generally, there should not be any subscribers left when the profile
28 // is destroyed because a) those may indicate that the subscriber class
29 // maintains an active pointer to the profile that might be used for
30 // accessing a destroyed profile and b) those subscribers will try to
31 // unsubscribe from a PrefService that has been destroyed with the
32 // profile.
33 // There is one exception that is safe: Static objects that are leaked
34 // on process termination, if these objects just subscribe to preferences
35 // and never access the profile after destruction. As these objects are
36 // leaked on termination, it is guaranteed that they don't attempt to
37 // unsubscribe.
38 const auto& pref_name = observer_list.first;
39 std::string message = base::StrCat(
40 {"Pref observer for ", pref_name, " found at shutdown."});
41 LOG(WARNING) << message;
42 DEBUG_ALIAS_FOR_CSTR(aliased_message, message.c_str(), 128);
43
44 // TODO(crbug.com/942491, 946668, 945772) The following code collects
45 // stacktraces that show how the profile is destroyed that owns
46 // preferences which are known to have subscriptions outliving the
47 // profile.
48 if (
49 // For DbusAppmenu, crbug.com/946668
50 pref_name == "bookmark_bar.show_on_all_tabs" ||
51 // For BrowserWindowPropertyManager, crbug.com/942491
52 pref_name == "profile.icon_version") {
53 base::debug::DumpWithoutCrashing();
54 }
55 }
56 }
57
58 // Same for initialization observers.
59 if (!init_observers_.empty())
60 LOG(WARNING) << "Init observer found at shutdown.";
61
62 pref_observers_.clear();
63 init_observers_.clear();
64 }
65
AddPrefObserver(std::string_view path,PrefObserver * obs)66 void PrefNotifierImpl::AddPrefObserver(std::string_view path,
67 PrefObserver* obs) {
68 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
69
70 // Add the pref observer. ObserverList hits a DCHECK if it already is
71 // in the list.
72 pref_observers_[std::string(path)].AddObserver(obs);
73 }
74
RemovePrefObserver(std::string_view path,PrefObserver * obs)75 void PrefNotifierImpl::RemovePrefObserver(std::string_view path,
76 PrefObserver* obs) {
77 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
78
79 auto observer_iterator = pref_observers_.find(path);
80 if (observer_iterator == pref_observers_.end()) {
81 return;
82 }
83
84 PrefObserverList& observer_list = observer_iterator->second;
85 observer_list.RemoveObserver(obs);
86 }
87
AddPrefObserverAllPrefs(PrefObserver * observer)88 void PrefNotifierImpl::AddPrefObserverAllPrefs(PrefObserver* observer) {
89 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
90 all_prefs_pref_observers_.AddObserver(observer);
91 }
92
RemovePrefObserverAllPrefs(PrefObserver * observer)93 void PrefNotifierImpl::RemovePrefObserverAllPrefs(PrefObserver* observer) {
94 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
95 all_prefs_pref_observers_.RemoveObserver(observer);
96 }
97
AddInitObserver(base::OnceCallback<void (bool)> obs)98 void PrefNotifierImpl::AddInitObserver(base::OnceCallback<void(bool)> obs) {
99 init_observers_.push_back(std::move(obs));
100 }
101
OnPreferenceChanged(std::string_view path)102 void PrefNotifierImpl::OnPreferenceChanged(std::string_view path) {
103 FireObservers(path);
104 }
105
OnInitializationCompleted(bool succeeded)106 void PrefNotifierImpl::OnInitializationCompleted(bool succeeded) {
107 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
108
109 // We must move init_observers_ to a local variable before we run
110 // observers, or we can end up in this method re-entrantly before
111 // clearing the observers list.
112 PrefInitObserverList observers;
113 std::swap(observers, init_observers_);
114
115 for (auto& observer : observers)
116 std::move(observer).Run(succeeded);
117 }
118
FireObservers(std::string_view path)119 void PrefNotifierImpl::FireObservers(std::string_view path) {
120 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
121
122 // Only send notifications for registered preferences.
123 if (!pref_service_->FindPreference(path))
124 return;
125
126 // Fire observers for any preference change.
127 for (PrefObserver& observer : all_prefs_pref_observers_) {
128 observer.OnPreferenceChanged(pref_service_, path);
129 }
130
131 auto observer_iterator = pref_observers_.find(path);
132 if (observer_iterator == pref_observers_.end())
133 return;
134
135 for (PrefObserver& observer : observer_iterator->second) {
136 observer.OnPreferenceChanged(pref_service_, path);
137 }
138 }
139
SetPrefService(PrefService * pref_service)140 void PrefNotifierImpl::SetPrefService(PrefService* pref_service) {
141 DCHECK(pref_service_ == nullptr);
142 pref_service_ = pref_service;
143 }
144