WebSchedulerImpl::postTimerTask to post non-nestable tasks
[chromium-blink-merge.git] / base / prefs / pref_notifier_impl.h
blobcfd46ff4211ff2ef9196723d83e549e5ececeebe
1 // 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.
5 #ifndef BASE_PREFS_PREF_NOTIFIER_IMPL_H_
6 #define BASE_PREFS_PREF_NOTIFIER_IMPL_H_
8 #include <list>
9 #include <string>
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/observer_list.h"
15 #include "base/prefs/base_prefs_export.h"
16 #include "base/prefs/pref_notifier.h"
17 #include "base/prefs/pref_observer.h"
18 #include "base/threading/thread_checker.h"
20 class PrefService;
22 // The PrefNotifier implementation used by the PrefService.
23 class BASE_PREFS_EXPORT PrefNotifierImpl
24 : public NON_EXPORTED_BASE(PrefNotifier) {
25 public:
26 PrefNotifierImpl();
27 explicit PrefNotifierImpl(PrefService* pref_service);
28 ~PrefNotifierImpl() override;
30 // If the pref at the given path changes, we call the observer's
31 // OnPreferenceChanged method.
32 void AddPrefObserver(const std::string& path, PrefObserver* observer);
33 void RemovePrefObserver(const std::string& path, PrefObserver* observer);
35 // We run the callback once, when initialization completes. The bool
36 // parameter will be set to true for successful initialization,
37 // false for unsuccessful.
38 void AddInitObserver(base::Callback<void(bool)> observer);
40 void SetPrefService(PrefService* pref_service);
42 protected:
43 // PrefNotifier overrides.
44 void OnPreferenceChanged(const std::string& pref_name) override;
45 void OnInitializationCompleted(bool succeeded) override;
47 // A map from pref names to a list of observers. Observers get fired in the
48 // order they are added. These should only be accessed externally for unit
49 // testing.
50 typedef ObserverList<PrefObserver> PrefObserverList;
51 typedef base::hash_map<std::string, PrefObserverList*> PrefObserverMap;
53 typedef std::list<base::Callback<void(bool)> > PrefInitObserverList;
55 const PrefObserverMap* pref_observers() const { return &pref_observers_; }
57 private:
58 // For the given pref_name, fire any observer of the pref. Virtual so it can
59 // be mocked for unit testing.
60 virtual void FireObservers(const std::string& path);
62 // Weak reference; the notifier is owned by the PrefService.
63 PrefService* pref_service_;
65 PrefObserverMap pref_observers_;
66 PrefInitObserverList init_observers_;
68 base::ThreadChecker thread_checker_;
70 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl);
73 #endif // BASE_PREFS_PREF_NOTIFIER_IMPL_H_