blob: 68a240872e0590fad7e5fc98311533659453b804 [file] [log] [blame]
brettw58cd1f12016-01-30 05:56:051// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettw066508682016-02-03 08:22:025#ifndef COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_
6#define COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_
brettw58cd1f12016-01-30 05:56:057
8#include <list>
avia92d13b2016-09-21 07:48:409#include <memory>
brettw58cd1f12016-01-30 05:56:0510#include <string>
11
12#include "base/callback.h"
13#include "base/compiler_specific.h"
14#include "base/containers/hash_tables.h"
15#include "base/macros.h"
16#include "base/observer_list.h"
brettw58cd1f12016-01-30 05:56:0517#include "base/threading/thread_checker.h"
brettwf00b9b42016-02-01 22:11:3818#include "components/prefs/pref_notifier.h"
19#include "components/prefs/pref_observer.h"
Brett Wilson5c6cf262017-09-09 02:05:5420#include "components/prefs/prefs_export.h"
brettw58cd1f12016-01-30 05:56:0521
22class PrefService;
23
24// The PrefNotifier implementation used by the PrefService.
Nico Weber43ddd7a32017-08-15 19:19:2725class COMPONENTS_PREFS_EXPORT PrefNotifierImpl : public PrefNotifier {
brettw58cd1f12016-01-30 05:56:0526 public:
27 PrefNotifierImpl();
28 explicit PrefNotifierImpl(PrefService* pref_service);
29 ~PrefNotifierImpl() override;
30
31 // If the pref at the given path changes, we call the observer's
32 // OnPreferenceChanged method.
33 void AddPrefObserver(const std::string& path, PrefObserver* observer);
34 void RemovePrefObserver(const std::string& path, PrefObserver* observer);
35
Brett Wilson21cf626a2017-09-07 00:30:2036 // These observers are called for any pref changes.
37 //
38 // AVOID ADDING THESE. See the long comment in the identically-named
39 // functions on PrefService for background.
40 void AddPrefObserverAllPrefs(PrefObserver* observer);
41 void RemovePrefObserverAllPrefs(PrefObserver* observer);
42
brettw58cd1f12016-01-30 05:56:0543 // We run the callback once, when initialization completes. The bool
44 // parameter will be set to true for successful initialization,
45 // false for unsuccessful.
46 void AddInitObserver(base::Callback<void(bool)> observer);
47
48 void SetPrefService(PrefService* pref_service);
49
50 protected:
51 // PrefNotifier overrides.
52 void OnPreferenceChanged(const std::string& pref_name) override;
53 void OnInitializationCompleted(bool succeeded) override;
54
55 // A map from pref names to a list of observers. Observers get fired in the
56 // order they are added. These should only be accessed externally for unit
57 // testing.
58 typedef base::ObserverList<PrefObserver> PrefObserverList;
avia92d13b2016-09-21 07:48:4059 typedef base::hash_map<std::string, std::unique_ptr<PrefObserverList>>
60 PrefObserverMap;
brettw58cd1f12016-01-30 05:56:0561
62 typedef std::list<base::Callback<void(bool)>> PrefInitObserverList;
63
64 const PrefObserverMap* pref_observers() const { return &pref_observers_; }
65
66 private:
67 // For the given pref_name, fire any observer of the pref. Virtual so it can
68 // be mocked for unit testing.
69 virtual void FireObservers(const std::string& path);
70
71 // Weak reference; the notifier is owned by the PrefService.
72 PrefService* pref_service_;
73
74 PrefObserverMap pref_observers_;
75 PrefInitObserverList init_observers_;
76
Brett Wilson21cf626a2017-09-07 00:30:2077 // Observers for changes to any preference.
78 PrefObserverList all_prefs_pref_observers_;
79
brettw58cd1f12016-01-30 05:56:0580 base::ThreadChecker thread_checker_;
81
82 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl);
83};
84
brettw066508682016-02-03 08:22:0285#endif // COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_