blob: 5a8a1372c02fc92f118913b27ca133d8dead3636 [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/base_prefs_export.h"
19#include "components/prefs/pref_notifier.h"
20#include "components/prefs/pref_observer.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
36 // We run the callback once, when initialization completes. The bool
37 // parameter will be set to true for successful initialization,
38 // false for unsuccessful.
39 void AddInitObserver(base::Callback<void(bool)> observer);
40
41 void SetPrefService(PrefService* pref_service);
42
43 protected:
44 // PrefNotifier overrides.
45 void OnPreferenceChanged(const std::string& pref_name) override;
46 void OnInitializationCompleted(bool succeeded) override;
47
48 // A map from pref names to a list of observers. Observers get fired in the
49 // order they are added. These should only be accessed externally for unit
50 // testing.
51 typedef base::ObserverList<PrefObserver> PrefObserverList;
avia92d13b2016-09-21 07:48:4052 typedef base::hash_map<std::string, std::unique_ptr<PrefObserverList>>
53 PrefObserverMap;
brettw58cd1f12016-01-30 05:56:0554
55 typedef std::list<base::Callback<void(bool)>> PrefInitObserverList;
56
57 const PrefObserverMap* pref_observers() const { return &pref_observers_; }
58
59 private:
60 // For the given pref_name, fire any observer of the pref. Virtual so it can
61 // be mocked for unit testing.
62 virtual void FireObservers(const std::string& path);
63
64 // Weak reference; the notifier is owned by the PrefService.
65 PrefService* pref_service_;
66
67 PrefObserverMap pref_observers_;
68 PrefInitObserverList init_observers_;
69
70 base::ThreadChecker thread_checker_;
71
72 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl);
73};
74
brettw066508682016-02-03 08:22:0275#endif // COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_