| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ |
| 6 #define CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ |
| 7 | 7 |
| 8 #include <list> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 11 #include "base/callback.h" |
| 10 #include "base/hash_tables.h" | 12 #include "base/hash_tables.h" |
| 11 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
| 12 #include "base/prefs/pref_notifier.h" | 14 #include "base/prefs/pref_notifier.h" |
| 15 #include "base/prefs/public/pref_observer.h" |
| 13 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 14 | 17 |
| 15 class PrefService; | 18 class PrefService; |
| 16 | 19 |
| 17 namespace content { | |
| 18 class NotificationObserver; | |
| 19 } | |
| 20 | |
| 21 // The PrefNotifier implementation used by the PrefService. | 20 // The PrefNotifier implementation used by the PrefService. |
| 22 class PrefNotifierImpl : public PrefNotifier, | 21 class PrefNotifierImpl : public PrefNotifier, |
| 23 public base::NonThreadSafe { | 22 public base::NonThreadSafe { |
| 24 public: | 23 public: |
| 25 PrefNotifierImpl(); | 24 PrefNotifierImpl(); |
| 26 explicit PrefNotifierImpl(PrefService* pref_service); | 25 explicit PrefNotifierImpl(PrefService* pref_service); |
| 27 virtual ~PrefNotifierImpl(); | 26 virtual ~PrefNotifierImpl(); |
| 28 | 27 |
| 29 // If the pref at the given path changes, we call the observer's Observe | 28 // If the pref at the given path changes, we call the observer's |
| 30 // method with PREF_CHANGED. | 29 // OnPreferenceChanged method. |
| 31 void AddPrefObserver(const char* path, content::NotificationObserver* obs); | 30 void AddPrefObserver(const char* path, PrefObserver* observer); |
| 32 void RemovePrefObserver(const char* path, content::NotificationObserver* obs); | 31 void RemovePrefObserver(const char* path, PrefObserver* observer); |
| 33 | 32 |
| 33 // We run the callback once, when initialization completes. The bool |
| 34 // parameter will be set to true for successful initialization, |
| 35 // false for unsuccessful. |
| 36 void AddInitObserver(base::Callback<void(bool)> observer); |
| 37 |
| 38 void SetPrefService(PrefService* pref_service); |
| 39 |
| 40 protected: |
| 34 // PrefNotifier overrides. | 41 // PrefNotifier overrides. |
| 35 virtual void OnPreferenceChanged(const std::string& pref_name) OVERRIDE; | 42 virtual void OnPreferenceChanged(const std::string& pref_name) OVERRIDE; |
| 36 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | 43 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
| 37 | 44 |
| 38 void SetPrefService(PrefService* pref_service); | |
| 39 | |
| 40 protected: | |
| 41 // A map from pref names to a list of observers. Observers get fired in the | 45 // A map from pref names to a list of observers. Observers get fired in the |
| 42 // order they are added. These should only be accessed externally for unit | 46 // order they are added. These should only be accessed externally for unit |
| 43 // testing. | 47 // testing. |
| 44 typedef ObserverList<content::NotificationObserver> NotificationObserverList; | 48 typedef ObserverList<PrefObserver> PrefObserverList; |
| 45 typedef base::hash_map<std::string, NotificationObserverList*> | 49 typedef base::hash_map<std::string, PrefObserverList*> PrefObserverMap; |
| 46 PrefObserverMap; | 50 |
| 51 typedef std::list<base::Callback<void(bool)> > PrefInitObserverList; |
| 47 | 52 |
| 48 const PrefObserverMap* pref_observers() const { return &pref_observers_; } | 53 const PrefObserverMap* pref_observers() const { return &pref_observers_; } |
| 49 | 54 |
| 50 private: | 55 private: |
| 51 // For the given pref_name, fire any observer of the pref. Virtual so it can | 56 // For the given pref_name, fire any observer of the pref. Virtual so it can |
| 52 // be mocked for unit testing. | 57 // be mocked for unit testing. |
| 53 virtual void FireObservers(const std::string& path); | 58 virtual void FireObservers(const std::string& path); |
| 54 | 59 |
| 55 // Weak reference; the notifier is owned by the PrefService. | 60 // Weak reference; the notifier is owned by the PrefService. |
| 56 PrefService* pref_service_; | 61 PrefService* pref_service_; |
| 57 | 62 |
| 58 PrefObserverMap pref_observers_; | 63 PrefObserverMap pref_observers_; |
| 64 PrefInitObserverList init_observers_; |
| 59 | 65 |
| 60 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); | 66 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); |
| 61 }; | 67 }; |
| 62 | 68 |
| 63 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | 69 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ |
| OLD | NEW |