| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "chrome/browser/prefs/pref_notifier.h" | 14 #include "chrome/browser/prefs/pref_notifier.h" |
| 15 | 15 |
| 16 class PrefService; | 16 class PrefService; |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 class NotificationObserver; | 19 class NotificationObserver; |
| 20 } | 20 } |
| 21 | 21 |
| 22 // The PrefNotifier implementation used by the PrefService. | 22 // The PrefNotifier implementation used by the PrefService. |
| 23 class PrefNotifierImpl : public PrefNotifier, | 23 class PrefNotifierImpl : public PrefNotifier, |
| 24 public base::NonThreadSafe { | 24 public base::NonThreadSafe { |
| 25 public: | 25 public: |
| 26 PrefNotifierImpl(); |
| 26 explicit PrefNotifierImpl(PrefService* pref_service); | 27 explicit PrefNotifierImpl(PrefService* pref_service); |
| 27 virtual ~PrefNotifierImpl(); | 28 virtual ~PrefNotifierImpl(); |
| 28 | 29 |
| 29 // If the pref at the given path changes, we call the observer's Observe | 30 // If the pref at the given path changes, we call the observer's Observe |
| 30 // method with PREF_CHANGED. | 31 // method with PREF_CHANGED. |
| 31 void AddPrefObserver(const char* path, content::NotificationObserver* obs); | 32 void AddPrefObserver(const char* path, content::NotificationObserver* obs); |
| 32 void RemovePrefObserver(const char* path, content::NotificationObserver* obs); | 33 void RemovePrefObserver(const char* path, content::NotificationObserver* obs); |
| 33 | 34 |
| 34 // PrefNotifier overrides. | 35 // PrefNotifier overrides. |
| 35 virtual void OnPreferenceChanged(const std::string& pref_name); | 36 virtual void OnPreferenceChanged(const std::string& pref_name); |
| 36 virtual void OnInitializationCompleted(bool succeeded); | 37 virtual void OnInitializationCompleted(bool succeeded); |
| 37 | 38 |
| 39 void SetPrefService(PrefService* pref_service); |
| 40 |
| 38 protected: | 41 protected: |
| 39 // A map from pref names to a list of observers. Observers get fired in the | 42 // A map from pref names to a list of observers. Observers get fired in the |
| 40 // order they are added. These should only be accessed externally for unit | 43 // order they are added. These should only be accessed externally for unit |
| 41 // testing. | 44 // testing. |
| 42 typedef ObserverList<content::NotificationObserver> NotificationObserverList; | 45 typedef ObserverList<content::NotificationObserver> NotificationObserverList; |
| 43 typedef base::hash_map<std::string, NotificationObserverList*> | 46 typedef base::hash_map<std::string, NotificationObserverList*> |
| 44 PrefObserverMap; | 47 PrefObserverMap; |
| 45 | 48 |
| 46 const PrefObserverMap* pref_observers() const { return &pref_observers_; } | 49 const PrefObserverMap* pref_observers() const { return &pref_observers_; } |
| 47 | 50 |
| 48 private: | 51 private: |
| 49 // For the given pref_name, fire any observer of the pref. Virtual so it can | 52 // For the given pref_name, fire any observer of the pref. Virtual so it can |
| 50 // be mocked for unit testing. | 53 // be mocked for unit testing. |
| 51 virtual void FireObservers(const std::string& path); | 54 virtual void FireObservers(const std::string& path); |
| 52 | 55 |
| 53 // Weak reference; the notifier is owned by the PrefService. | 56 // Weak reference; the notifier is owned by the PrefService. |
| 54 PrefService* pref_service_; | 57 PrefService* pref_service_; |
| 55 | 58 |
| 56 PrefObserverMap pref_observers_; | 59 PrefObserverMap pref_observers_; |
| 57 | 60 |
| 58 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); | 61 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); |
| 59 }; | 62 }; |
| 60 | 63 |
| 61 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | 64 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ |
| OLD | NEW |