blob: 525e14670b73712d6862c8a82aba3f7387f5fe4f [file] [log] [blame]
[email protected]42c037e2012-06-26 22:23:321// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]acd78969c2010-12-08 09:49:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettwf00b9b42016-02-01 22:11:385#include "components/prefs/pref_notifier_impl.h"
[email protected]acd78969c2010-12-08 09:49:116
[email protected]42c037e2012-06-26 22:23:327#include "base/logging.h"
avia92d13b2016-09-21 07:48:408#include "base/memory/ptr_util.h"
brettwf00b9b42016-02-01 22:11:389#include "components/prefs/pref_service.h"
[email protected]acd78969c2010-12-08 09:49:1110
avia92d13b2016-09-21 07:48:4011PrefNotifierImpl::PrefNotifierImpl() : pref_service_(nullptr) {}
[email protected]361d37f62011-11-22 10:37:0212
[email protected]acd78969c2010-12-08 09:49:1113PrefNotifierImpl::PrefNotifierImpl(PrefService* service)
14 : pref_service_(service) {
15}
16
17PrefNotifierImpl::~PrefNotifierImpl() {
[email protected]5b7238f2013-04-22 15:56:4018 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]acd78969c2010-12-08 09:49:1119
20 // Verify that there are no pref observers when we shut down.
avia92d13b2016-09-21 07:48:4021 for (const auto& observer_list : pref_observers_) {
dcheng018341c2016-10-14 21:53:5622 if (observer_list.second->begin() != observer_list.second->end())
avia92d13b2016-09-21 07:48:4023 LOG(WARNING) << "Pref observer found at shutdown.";
[email protected]acd78969c2010-12-08 09:49:1124 }
25
[email protected]a6a7ced2012-11-01 17:24:1826 // Same for initialization observers.
27 if (!init_observers_.empty())
28 LOG(WARNING) << "Init observer found at shutdown.";
29
[email protected]acd78969c2010-12-08 09:49:1130 pref_observers_.clear();
[email protected]a6a7ced2012-11-01 17:24:1831 init_observers_.clear();
[email protected]acd78969c2010-12-08 09:49:1132}
33
georgesak7da6e9d2014-12-03 01:10:2934void PrefNotifierImpl::AddPrefObserver(const std::string& path,
[email protected]a6a7ced2012-11-01 17:24:1835 PrefObserver* obs) {
[email protected]acd78969c2010-12-08 09:49:1136 // Get the pref observer list associated with the path.
avia92d13b2016-09-21 07:48:4037 PrefObserverList* observer_list = nullptr;
38 auto observer_iterator = pref_observers_.find(path);
[email protected]acd78969c2010-12-08 09:49:1139 if (observer_iterator == pref_observers_.end()) {
[email protected]a6a7ced2012-11-01 17:24:1840 observer_list = new PrefObserverList;
avia92d13b2016-09-21 07:48:4041 pref_observers_[path] = base::WrapUnique(observer_list);
[email protected]acd78969c2010-12-08 09:49:1142 } else {
avia92d13b2016-09-21 07:48:4043 observer_list = observer_iterator->second.get();
[email protected]acd78969c2010-12-08 09:49:1144 }
45
[email protected]a6a7ced2012-11-01 17:24:1846 // Add the pref observer. ObserverList will DCHECK if it already is
47 // in the list.
[email protected]acd78969c2010-12-08 09:49:1148 observer_list->AddObserver(obs);
49}
50
georgesak7da6e9d2014-12-03 01:10:2951void PrefNotifierImpl::RemovePrefObserver(const std::string& path,
[email protected]a6a7ced2012-11-01 17:24:1852 PrefObserver* obs) {
[email protected]5b7238f2013-04-22 15:56:4053 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]acd78969c2010-12-08 09:49:1154
avia92d13b2016-09-21 07:48:4055 auto observer_iterator = pref_observers_.find(path);
[email protected]acd78969c2010-12-08 09:49:1156 if (observer_iterator == pref_observers_.end()) {
57 return;
58 }
59
avia92d13b2016-09-21 07:48:4060 PrefObserverList* observer_list = observer_iterator->second.get();
[email protected]acd78969c2010-12-08 09:49:1161 observer_list->RemoveObserver(obs);
62}
63
Brett Wilson21cf626a2017-09-07 00:30:2064void PrefNotifierImpl::AddPrefObserverAllPrefs(PrefObserver* observer) {
65 DCHECK(thread_checker_.CalledOnValidThread());
66 all_prefs_pref_observers_.AddObserver(observer);
67}
68
69void PrefNotifierImpl::RemovePrefObserverAllPrefs(PrefObserver* observer) {
70 DCHECK(thread_checker_.CalledOnValidThread());
71 all_prefs_pref_observers_.RemoveObserver(observer);
72}
73
Kenichi Ishibashif958ad62017-12-19 02:43:1474void PrefNotifierImpl::AddInitObserver(base::OnceCallback<void(bool)> obs) {
75 init_observers_.push_back(std::move(obs));
[email protected]a6a7ced2012-11-01 17:24:1876}
77
[email protected]acd78969c2010-12-08 09:49:1178void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) {
79 FireObservers(path);
80}
81
[email protected]845b43a82011-05-11 10:14:4382void PrefNotifierImpl::OnInitializationCompleted(bool succeeded) {
[email protected]5b7238f2013-04-22 15:56:4083 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]acd78969c2010-12-08 09:49:1184
Kenichi Ishibashif958ad62017-12-19 02:43:1485 // We must move init_observers_ to a local variable before we run
[email protected]a6a7ced2012-11-01 17:24:1886 // observers, or we can end up in this method re-entrantly before
87 // clearing the observers list.
Kenichi Ishibashif958ad62017-12-19 02:43:1488 PrefInitObserverList observers;
89 std::swap(observers, init_observers_);
[email protected]a6a7ced2012-11-01 17:24:1890
avia92d13b2016-09-21 07:48:4091 for (auto& observer : observers)
Kenichi Ishibashif958ad62017-12-19 02:43:1492 std::move(observer).Run(succeeded);
[email protected]acd78969c2010-12-08 09:49:1193}
94
95void PrefNotifierImpl::FireObservers(const std::string& path) {
[email protected]5b7238f2013-04-22 15:56:4096 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]acd78969c2010-12-08 09:49:1197
98 // Only send notifications for registered preferences.
georgesak7da6e9d2014-12-03 01:10:2999 if (!pref_service_->FindPreference(path))
[email protected]acd78969c2010-12-08 09:49:11100 return;
101
Brett Wilson21cf626a2017-09-07 00:30:20102 // Fire observers for any preference change.
103 for (auto& observer : all_prefs_pref_observers_)
104 observer.OnPreferenceChanged(pref_service_, path);
105
avia92d13b2016-09-21 07:48:40106 auto observer_iterator = pref_observers_.find(path);
[email protected]acd78969c2010-12-08 09:49:11107 if (observer_iterator == pref_observers_.end())
108 return;
109
ericwilligers42b92c12016-10-24 20:21:13110 for (PrefObserver& observer : *(observer_iterator->second))
111 observer.OnPreferenceChanged(pref_service_, path);
[email protected]acd78969c2010-12-08 09:49:11112}
[email protected]361d37f62011-11-22 10:37:02113
114void PrefNotifierImpl::SetPrefService(PrefService* pref_service) {
avia92d13b2016-09-21 07:48:40115 DCHECK(pref_service_ == nullptr);
[email protected]361d37f62011-11-22 10:37:02116 pref_service_ = pref_service;
117}