1 // Copyright (c) 2010 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 5 #ifndef CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ 6 #define CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "base/hash_tables.h" 12 #include "base/observer_list.h" 13 #include "base/threading/non_thread_safe.h" 14 #include "chrome/browser/prefs/pref_notifier.h" 15 16 class PrefService; 17 class NotificationObserver; 18 19 // The PrefNotifier implementation used by the PrefService. 20 class PrefNotifierImpl : public PrefNotifier, 21 public base::NonThreadSafe { 22 public: 23 explicit PrefNotifierImpl(PrefService* pref_service); 24 virtual ~PrefNotifierImpl(); 25 26 // If the pref at the given path changes, we call the observer's Observe 27 // method with PREF_CHANGED. 28 void AddPrefObserver(const char* path, NotificationObserver* obs); 29 void RemovePrefObserver(const char* path, NotificationObserver* obs); 30 31 // PrefNotifier overrides. 32 virtual void OnPreferenceChanged(const std::string& pref_name); 33 virtual void OnInitializationCompleted(); 34 35 protected: 36 // 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 38 // testing. 39 typedef ObserverList<NotificationObserver> NotificationObserverList; 40 typedef base::hash_map<std::string, NotificationObserverList*> 41 PrefObserverMap; 42 43 const PrefObserverMap* pref_observers() const { return &pref_observers_; } 44 45 private: 46 // For the given pref_name, fire any observer of the pref. Virtual so it can 47 // be mocked for unit testing. 48 virtual void FireObservers(const std::string& path); 49 50 // Weak reference; the notifier is owned by the PrefService. 51 PrefService* pref_service_; 52 53 PrefObserverMap pref_observers_; 54 55 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); 56 }; 57 58 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ 59