blob: e0da264ac3a6cb4af3fa0d0c2853ea5a5c85cd11 [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>
9#include <string>
10
11#include "base/callback.h"
12#include "base/compiler_specific.h"
13#include "base/containers/hash_tables.h"
14#include "base/macros.h"
15#include "base/observer_list.h"
brettw58cd1f12016-01-30 05:56:0516#include "base/threading/thread_checker.h"
brettwf00b9b42016-02-01 22:11:3817#include "components/prefs/base_prefs_export.h"
18#include "components/prefs/pref_notifier.h"
19#include "components/prefs/pref_observer.h"
brettw58cd1f12016-01-30 05:56:0520
21class PrefService;
22
23// The PrefNotifier implementation used by the PrefService.
brettw066508682016-02-03 08:22:0224class COMPONENTS_PREFS_EXPORT PrefNotifierImpl
brettw58cd1f12016-01-30 05:56:0525 : public NON_EXPORTED_BASE(PrefNotifier) {
26 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;
52 typedef base::hash_map<std::string, PrefObserverList*> PrefObserverMap;
53
54 typedef std::list<base::Callback<void(bool)>> PrefInitObserverList;
55
56 const PrefObserverMap* pref_observers() const { return &pref_observers_; }
57
58 private:
59 // For the given pref_name, fire any observer of the pref. Virtual so it can
60 // be mocked for unit testing.
61 virtual void FireObservers(const std::string& path);
62
63 // Weak reference; the notifier is owned by the PrefService.
64 PrefService* pref_service_;
65
66 PrefObserverMap pref_observers_;
67 PrefInitObserverList init_observers_;
68
69 base::ThreadChecker thread_checker_;
70
71 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl);
72};
73
brettw066508682016-02-03 08:22:0274#endif // COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_