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