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

Unified Diff: chrome/browser/prefs/pref_notifier_impl.h

Issue 5441002: Clean up pref change notification handling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix memory leaks in tests. 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/prefs/pref_notifier_impl.h
diff --git a/chrome/browser/prefs/pref_notifier_impl.h b/chrome/browser/prefs/pref_notifier_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..6d319a1657c27d5bcdff232065b74e943945a703
--- /dev/null
+++ b/chrome/browser/prefs/pref_notifier_impl.h
@@ -0,0 +1,57 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_
+#define CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_
+#pragma once
+
+#include <string>
+
+#include "base/hash_tables.h"
+#include "base/non_thread_safe.h"
+#include "base/observer_list.h"
+#include "chrome/browser/prefs/pref_notifier.h"
+
+class PrefService;
+class NotificationObserver;
+
+// The PrefNotifier implementation used by the PrefService.
+class PrefNotifierImpl : public PrefNotifier,
+ public NonThreadSafe {
+ public:
+ explicit PrefNotifierImpl(PrefService* pref_service);
+ virtual ~PrefNotifierImpl();
+
+ // If the pref at the given path changes, we call the observer's Observe
+ // method with PREF_CHANGED.
+ void AddPrefObserver(const char* path, NotificationObserver* obs);
+ void RemovePrefObserver(const char* path, NotificationObserver* obs);
+
+ // PrefNotifier overrides.
+ virtual void OnPreferenceChanged(const std::string& pref_name);
+ virtual void OnInitializationCompleted();
+
+ protected:
+ // A map from pref names to a list of observers. Observers get fired in the
+ // order they are added. These should only be accessed externally for unit
danno 2010/12/06 09:20:14 nit: consistency with number of spaces after ".",
Mattias Nissler (ping if slow) 2010/12/06 10:58:12 Done.
+ // testing.
+ typedef ObserverList<NotificationObserver> NotificationObserverList;
+ typedef base::hash_map<std::string, NotificationObserverList*>
+ PrefObserverMap;
danno 2010/12/06 09:20:14 nit: space between the typedefs and the method bel
Mattias Nissler (ping if slow) 2010/12/06 10:58:12 Done.
+ const PrefObserverMap* pref_observers() { return &pref_observers_; }
danno 2010/12/06 09:20:14 method const too?
Mattias Nissler (ping if slow) 2010/12/06 10:58:12 Done.
+
+ private:
+ // For the given pref_name, fire any observer of the pref. Virtual so it can
+ // be mocked for unit testing.
+ virtual void FireObservers(const std::string& path);
+
+ // Weak reference; the notifier is owned by the PrefService.
+ PrefService* pref_service_;
+
+ PrefObserverMap pref_observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl);
+};
+
+#endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_

Powered by Google App Engine
This is Rietveld 408576698