blob: a28fbaffad9f5c17bcc0a410c403943d81bda01d [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
[email protected]a6a7ced2012-11-01 17:24:1864void PrefNotifierImpl::AddInitObserver(base::Callback<void(bool)> obs) {
65 init_observers_.push_back(obs);
66}
67
[email protected]acd78969c2010-12-08 09:49:1168void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) {
69 FireObservers(path);
70}
71
[email protected]845b43a82011-05-11 10:14:4372void PrefNotifierImpl::OnInitializationCompleted(bool succeeded) {
[email protected]5b7238f2013-04-22 15:56:4073 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]acd78969c2010-12-08 09:49:1174
[email protected]a6a7ced2012-11-01 17:24:1875 // We must make a copy of init_observers_ and clear it before we run
76 // observers, or we can end up in this method re-entrantly before
77 // clearing the observers list.
78 PrefInitObserverList observers(init_observers_);
79 init_observers_.clear();
80
avia92d13b2016-09-21 07:48:4081 for (auto& observer : observers)
82 observer.Run(succeeded);
[email protected]acd78969c2010-12-08 09:49:1183}
84
85void PrefNotifierImpl::FireObservers(const std::string& path) {
[email protected]5b7238f2013-04-22 15:56:4086 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]acd78969c2010-12-08 09:49:1187
88 // Only send notifications for registered preferences.
georgesak7da6e9d2014-12-03 01:10:2989 if (!pref_service_->FindPreference(path))
[email protected]acd78969c2010-12-08 09:49:1190 return;
91
avia92d13b2016-09-21 07:48:4092 auto observer_iterator = pref_observers_.find(path);
[email protected]acd78969c2010-12-08 09:49:1193 if (observer_iterator == pref_observers_.end())
94 return;
95
ericwilligers42b92c12016-10-24 20:21:1396 for (PrefObserver& observer : *(observer_iterator->second))
97 observer.OnPreferenceChanged(pref_service_, path);
[email protected]acd78969c2010-12-08 09:49:1198}
[email protected]361d37f62011-11-22 10:37:0299
100void PrefNotifierImpl::SetPrefService(PrefService* pref_service) {
avia92d13b2016-09-21 07:48:40101 DCHECK(pref_service_ == nullptr);
[email protected]361d37f62011-11-22 10:37:02102 pref_service_ = pref_service;
103}