Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(775)

Side by Side Diff: chrome/browser/prefs/pref_notifier.h

Issue 5441002: Clean up pref change notification handling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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> 10 #include <vector>
11 11
12 #include "base/hash_tables.h" 12 #include "base/hash_tables.h"
13 #include "base/non_thread_safe.h" 13 #include "base/non_thread_safe.h"
14 #include "base/observer_list.h" 14 #include "base/observer_list.h"
15 #include "chrome/common/notification_observer.h"
16 #include "chrome/common/notification_registrar.h"
17 15
18 class NotificationObserver; 16 class NotificationObserver;
19 class PrefService; 17 class PrefService;
20 class PrefValueStore;
21 class Value;
22 18
23 // Registers observers for particular preferences and sends notifications when 19 // Registers observers for particular preferences and sends notifications when
24 // preference values or sources (i.e., which preference layer controls the 20 // preference values or sources (i.e., which preference layer controls the
25 // preference) change. 21 // preference) change.
26 class PrefNotifier : public NonThreadSafe, 22 class PrefNotifier : public NonThreadSafe {
danno 2010/12/02 10:31:52 Make complete delegate interface that the PrefServ
Mattias Nissler (ping if slow) 2010/12/02 16:38:24 Done.
27 public NotificationObserver {
28 public: 23 public:
29 // PrefStores must be listed here in order from highest to lowest priority. 24 PrefNotifier(PrefService* service);
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(); 25 virtual ~PrefNotifier();
61 26
62 // For the given pref_name, fire any observer of the pref if the effective 27 // Sends out a change notification for the preference identified by
63 // value of the pref or the store controlling its value has changed, been 28 // |pref_name|.
64 // added, or been removed (but not if it's re-setting the same value it had 29 virtual void OnPreferenceChanged(const std::string& pref_name);
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 30
70 // Convenience method to be called when a preference is set in the 31 // Broadcasts the intialization completed notification.
71 // USER_STORE. See OnPreferenceSet(). 32 virtual void OnInitializationCompleted();
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 33
78 // If the pref at the given path changes, we call the observer's Observe 34 // If the pref at the given path changes, we call the observer's Observe
79 // method with PREF_CHANGED. 35 // method with PREF_CHANGED.
80 void AddPrefObserver(const char* path, NotificationObserver* obs); 36 void AddPrefObserver(const char* path, NotificationObserver* obs);
81 void RemovePrefObserver(const char* path, NotificationObserver* obs); 37 void RemovePrefObserver(const char* path, NotificationObserver* obs);
82 38
83 protected: 39 protected:
84 // A map from pref names to a list of observers. Observers get fired in the 40 // 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 41 // order they are added. These should only be accessed externally for unit
86 // testing. 42 // testing.
87 typedef ObserverList<NotificationObserver> NotificationObserverList; 43 typedef ObserverList<NotificationObserver> NotificationObserverList;
88 typedef base::hash_map<std::string, NotificationObserverList*> 44 typedef base::hash_map<std::string, NotificationObserverList*>
89 PrefObserverMap; 45 PrefObserverMap;
90 const PrefObserverMap* pref_observers() { return &pref_observers_; } 46 const PrefObserverMap* pref_observers() { return &pref_observers_; }
91 47
48 // For the given pref_name, fire any observer of the pref. Virtual so it can
battre (please use the other) 2010/12/02 10:41:19 |pref_name| vs. |path| below
Mattias Nissler (ping if slow) 2010/12/02 16:38:24 not applicable any longer.
49 // be mocked for unit tests.
50 virtual void FireObservers(const std::string& path);
51
92 private: 52 private:
93 // Weak references. 53 // Weak reference; the notifier is owned by the PrefService.
94 PrefService* pref_service_; 54 PrefService* pref_service_;
95 PrefValueStore* pref_value_store_;
96
97 NotificationRegistrar registrar_;
98 55
99 PrefObserverMap pref_observers_; 56 PrefObserverMap pref_observers_;
100 57
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); 58 DISALLOW_COPY_AND_ASSIGN(PrefNotifier);
116 }; 59 };
117 60
118 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_H_ 61 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698