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_CHANGE_REGISTRAR_H_ 6 #define CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ 7 #pragma once 8 9 #include <set> 10 #include <string> 11 12 #include "base/basictypes.h" 13 14 class PrefService; 15 class NotificationObserver; 16 17 // Automatically manages the registration of one or more pref change observers 18 // with a PrefStore. Functions much like NotificationRegistrar, but specifically 19 // manages observers of preference changes. When the Registrar is destroyed, 20 // all registered observers are automatically unregistered with the PrefStore. 21 class PrefChangeRegistrar { 22 public: 23 PrefChangeRegistrar(); 24 virtual ~PrefChangeRegistrar(); 25 26 // Must be called before adding or removing observers. Can be called more 27 // than once as long as the value of |service| doesn't change. 28 void Init(PrefService* service); 29 30 // Adds an pref observer for the specified pref |path| and |obs| observer 31 // object. All registered observers will be automatically unregistered 32 // when the registrar's destructor is called unless the observer has been 33 // explicitly removed by a call to Remove beforehand. 34 void Add(const char* path, 35 NotificationObserver* obs); 36 37 // Removes a preference observer that has previously been added with a call to 38 // Add. 39 void Remove(const char* path, 40 NotificationObserver* obs); 41 42 // Removes all observers that have been previously added with a call to Add. 43 void RemoveAll(); 44 45 // Returns true if no pref observers are registered. 46 bool IsEmpty() const; 47 48 private: 49 typedef std::pair<std::string, NotificationObserver*> ObserverRegistration; 50 51 std::set<ObserverRegistration> observers_; 52 PrefService* service_; 53 54 DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar); 55 }; 56 57 #endif // CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ 58