| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_NOTIFIER_H_ |
| 6 #define CHROME_BROWSER_PREFS_PREF_NOTIFIER_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_NOTIFIER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | |
| 11 | 10 |
| 12 #include "base/hash_tables.h" | 11 // Delegate interface used by PrefValueStore to notify its owner about changes |
| 13 #include "base/non_thread_safe.h" | 12 // to the preference values. |
| 14 #include "base/observer_list.h" | 13 // TODO(mnissler, danno): Move this declaration to pref_value_store.h once we've |
| 15 #include "chrome/common/notification_observer.h" | 14 // cleaned up all public uses of this interface. |
| 16 #include "chrome/common/notification_registrar.h" | 15 class PrefNotifier { |
| 16 public: |
| 17 virtual ~PrefNotifier() {} |
| 17 | 18 |
| 18 class NotificationObserver; | 19 // Sends out a change notification for the preference identified by |
| 19 class PrefService; | 20 // |pref_name|. |
| 20 class PrefValueStore; | 21 virtual void OnPreferenceChanged(const std::string& pref_name) = 0; |
| 21 class Value; | |
| 22 | 22 |
| 23 // Registers observers for particular preferences and sends notifications when | 23 // Broadcasts the intialization completed notification. |
| 24 // preference values or sources (i.e., which preference layer controls the | 24 virtual void OnInitializationCompleted() = 0; |
| 25 // preference) change. | |
| 26 class PrefNotifier : public NonThreadSafe, | |
| 27 public NotificationObserver { | |
| 28 public: | |
| 29 // PrefStores must be listed here in order from highest to lowest priority. | |
| 30 // MANAGED_PLATFORM contains all managed preference values that are | |
| 31 // provided by a platform-specific policy mechanism (e.g. Windows | |
| 32 // Group Policy). | |
| 33 // DEVICE_MANAGEMENT contains all managed preference values supplied | |
| 34 // by the device management server (cloud policy). | |
| 35 // EXTENSION contains preference values set by extensions. | |
| 36 // COMMAND_LINE contains preference values set by command-line switches. | |
| 37 // USER contains all user-set preference values. | |
| 38 // RECOMMENDED contains all recommended (policy) preference values. | |
| 39 // DEFAULT contains all application default preference values. | |
| 40 // This enum is kept in pref_notifier.h rather than pref_value_store.h in | |
| 41 // order to minimize additional headers needed by the *PrefStore files. | |
| 42 enum PrefStoreType { | |
| 43 // INVALID_STORE is not associated with an actual PrefStore but used as | |
| 44 // an invalid marker, e.g. as a return value. | |
| 45 INVALID_STORE = -1, | |
| 46 MANAGED_PLATFORM_STORE = 0, | |
| 47 DEVICE_MANAGEMENT_STORE, | |
| 48 EXTENSION_STORE, | |
| 49 COMMAND_LINE_STORE, | |
| 50 USER_STORE, | |
| 51 RECOMMENDED_STORE, | |
| 52 DEFAULT_STORE, | |
| 53 PREF_STORE_TYPE_MAX = DEFAULT_STORE | |
| 54 }; | |
| 55 | |
| 56 // The |service| with which this notifier is associated will be sent as the | |
| 57 // source of any notifications. | |
| 58 PrefNotifier(PrefService* service, PrefValueStore* value_store); | |
| 59 | |
| 60 virtual ~PrefNotifier(); | |
| 61 | |
| 62 // For the given pref_name, fire any observer of the pref if the effective | |
| 63 // value of the pref or the store controlling its value has changed, been | |
| 64 // added, or been removed (but not if it's re-setting the same value it had | |
| 65 // already). |new_store| should be the PrefStoreType of the store reporting | |
| 66 // the change. | |
| 67 void OnPreferenceSet(const char* pref_name, | |
| 68 PrefNotifier::PrefStoreType new_store); | |
| 69 | |
| 70 // Convenience method to be called when a preference is set in the | |
| 71 // USER_STORE. See OnPreferenceSet(). | |
| 72 void OnUserPreferenceSet(const char* pref_name); | |
| 73 | |
| 74 // For the given pref_name, fire any observer of the pref. Virtual so it can | |
| 75 // be mocked for unit testing. | |
| 76 virtual void FireObservers(const char* path); | |
| 77 | |
| 78 // If the pref at the given path changes, we call the observer's Observe | |
| 79 // method with PREF_CHANGED. | |
| 80 void AddPrefObserver(const char* path, NotificationObserver* obs); | |
| 81 void RemovePrefObserver(const char* path, NotificationObserver* obs); | |
| 82 | |
| 83 protected: | |
| 84 // A map from pref names to a list of observers. Observers get fired in the | |
| 85 // order they are added. These should only be accessed externally for unit | |
| 86 // testing. | |
| 87 typedef ObserverList<NotificationObserver> NotificationObserverList; | |
| 88 typedef base::hash_map<std::string, NotificationObserverList*> | |
| 89 PrefObserverMap; | |
| 90 const PrefObserverMap* pref_observers() { return &pref_observers_; } | |
| 91 | |
| 92 private: | |
| 93 // Weak references. | |
| 94 PrefService* pref_service_; | |
| 95 PrefValueStore* pref_value_store_; | |
| 96 | |
| 97 NotificationRegistrar registrar_; | |
| 98 | |
| 99 PrefObserverMap pref_observers_; | |
| 100 | |
| 101 // Called after a policy refresh to notify relevant preference observers. | |
| 102 // |changed_prefs_paths| is the vector of preference paths changed by the | |
| 103 // policy update. It is passed by value and not reference because | |
| 104 // this method is called asynchronously as a callback from another thread. | |
| 105 // Copying the vector guarantees that the vector's lifecycle spans the | |
| 106 // method's invocation. | |
| 107 void FireObserversForRefreshedManagedPrefs( | |
| 108 std::vector<std::string> changed_prefs_paths); | |
| 109 | |
| 110 // NotificationObserver methods: | |
| 111 virtual void Observe(NotificationType type, | |
| 112 const NotificationSource& source, | |
| 113 const NotificationDetails& details); | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(PrefNotifier); | |
| 116 }; | 25 }; |
| 117 | 26 |
| 118 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_H_ | 27 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_H_ |
| OLD | NEW |