summaryrefslogtreecommitdiffstats
path: root/chrome/common
diff options
context:
space:
mode:
authormnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-08 09:49:11 +0000
committermnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-08 09:49:11 +0000
commitacd78969cf6569dcbf409dceb0b6511751afd026 (patch)
treeed2168c663ae3e8a15d857838b78ed2386da3281 /chrome/common
parent5138bbffd1b2d5151ad74b4e7ae8091fc7d44114 (diff)
downloadchromium_src-acd78969cf6569dcbf409dceb0b6511751afd026.zip
chromium_src-acd78969cf6569dcbf409dceb0b6511751afd026.tar.gz
chromium_src-acd78969cf6569dcbf409dceb0b6511751afd026.tar.bz2
Clean up pref change notification handling.
This is a complete overhaul of PrefValueStore, PrefStore, PrefNotifier, and PrefService. Specifically: - Add an observer interface to PrefStore that can be used to notify the upper layers of the pref system about value changes. Currently, it's unused mostly, but that'll change when we refactor ExtensionPrefStore and ConfigurationPolicyPrefStore. - Make PrefNotifier be a dependency of PrefValueStore. That helps in keeping the pref change detection handling local to PrefValueStore. - Clean up related unit tests, removing redundant mocks and gmockify others. BUG=64893 TEST=Compiles and passes tests Review URL: http://codereview.chromium.org/5441002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68574 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r--chrome/common/json_pref_store.h2
-rw-r--r--chrome/common/notification_type.h4
-rw-r--r--chrome/common/pref_store.h38
-rw-r--r--chrome/common/pref_store_base.cc21
-rw-r--r--chrome/common/pref_store_base.h33
-rw-r--r--chrome/common/pref_store_observer_mock.h26
6 files changed, 118 insertions, 6 deletions
diff --git a/chrome/common/json_pref_store.h b/chrome/common/json_pref_store.h
index 0407d1e..44f5c8c 100644
--- a/chrome/common/json_pref_store.h
+++ b/chrome/common/json_pref_store.h
@@ -29,7 +29,7 @@ class JsonPrefStore : public PrefStore,
virtual ~JsonPrefStore();
// PrefStore methods:
- virtual bool ReadOnly() { return read_only_; }
+ virtual bool ReadOnly() const { return read_only_; }
virtual DictionaryValue* prefs() const { return prefs_.get(); }
diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h
index edf5999..8f17b85 100644
--- a/chrome/common/notification_type.h
+++ b/chrome/common/notification_type.h
@@ -736,6 +736,10 @@ class NotificationType {
// PrefService and the details a std::string of the changed path.
PREF_CHANGED,
+ // This is broadcast after the preference subsystem has completed
+ // asynchronous initalization of a PrefService.
+ PREF_INITIALIZATION_COMPLETED,
+
// Sent when a default request context has been created, so calling
// Profile::GetDefaultRequestContext() will not return NULL. This is sent
// on the thread where Profile::GetRequestContext() is first called, which
diff --git a/chrome/common/pref_store.h b/chrome/common/pref_store.h
index 7011eba..808ffbd2 100644
--- a/chrome/common/pref_store.h
+++ b/chrome/common/pref_store.h
@@ -6,15 +6,33 @@
#define CHROME_COMMON_PREF_STORE_H_
#pragma once
+#include <string>
+
+#include "base/basictypes.h"
+
class DictionaryValue;
class Value;
// This is an abstract interface for reading and writing from/to a persistent
-// preference store, used by |PrefService|. An implementation using a JSON file
-// can be found in |JsonPrefStore|, while an implementation without any backing
-// store (currently used for testing) can be found in |DummyPrefStore|.
+// preference store, used by PrefService. An implementation using a JSON file
+// can be found in JsonPrefStore, while an implementation without any backing
+// store for testing can be found in TestingPrefStore. Furthermore, there is
+// CommandLinePrefStore, which bridges command line options to preferences and
+// ConfigurationPolicyPrefStore, which is used for hooking up configuration
+// policy with the preference subsystem.
class PrefStore {
public:
+ // Observer interface for monitoring PrefStore.
+ class ObserverInterface {
+ public:
+ virtual ~ObserverInterface() {}
+
+ // Called when the value for the given |key| in the store changes.
+ virtual void OnPrefValueChanged(const std::string& key) = 0;
+ // Notification about the PrefStore being fully initialized.
+ virtual void OnInitializationCompleted() = 0;
+ };
+
// Unique integer code for each type of error so we can report them
// distinctly in a histogram.
// NOTE: Don't change the order here as it will change the server's meaning
@@ -44,12 +62,20 @@ class PrefStore {
// CreateUseDefaultSentinelValue.
static bool IsUseDefaultSentinelValue(Value* value);
- virtual ~PrefStore() { }
+ PrefStore() {}
+ virtual ~PrefStore() {}
+
+ // Add and remove observers.
+ virtual void AddObserver(ObserverInterface* observer) {}
+ virtual void RemoveObserver(ObserverInterface* observer) {}
+
+ // Whether the store has completed all asynchronous initialization.
+ virtual bool IsInitializationComplete() { return true; }
// Whether the store is in a pseudo-read-only mode where changes are not
// actually persisted to disk. This happens in some cases when there are
// read errors during startup.
- virtual bool ReadOnly() { return true; }
+ virtual bool ReadOnly() const { return true; }
// TODO(danno): PrefValueStore shouldn't allow direct access to the
// DictionaryValue. Instead, it should have getters that return a
@@ -62,6 +88,8 @@ class PrefStore {
virtual bool WritePrefs() { return true; }
virtual void ScheduleWritePrefs() { }
+
+ DISALLOW_COPY_AND_ASSIGN(PrefStore);
};
#endif // CHROME_COMMON_PREF_STORE_H_
diff --git a/chrome/common/pref_store_base.cc b/chrome/common/pref_store_base.cc
new file mode 100644
index 0000000..07f79ff
--- /dev/null
+++ b/chrome/common/pref_store_base.cc
@@ -0,0 +1,21 @@
+// 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.
+
+#include "chrome/common/pref_store_base.h"
+
+void PrefStoreBase::AddObserver(PrefStore::ObserverInterface* observer) {
+ observers_.AddObserver(observer);
+}
+
+void PrefStoreBase::RemoveObserver(PrefStore::ObserverInterface* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+void PrefStoreBase::NotifyPrefValueChanged(const std::string& key) {
+ FOR_EACH_OBSERVER(ObserverInterface, observers_, OnPrefValueChanged(key));
+}
+
+void PrefStoreBase::NotifyInitializationCompleted() {
+ FOR_EACH_OBSERVER(ObserverInterface, observers_, OnInitializationCompleted());
+}
diff --git a/chrome/common/pref_store_base.h b/chrome/common/pref_store_base.h
new file mode 100644
index 0000000..3c9f37c
--- /dev/null
+++ b/chrome/common/pref_store_base.h
@@ -0,0 +1,33 @@
+// 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_COMMON_PREF_STORE_BASE_H_
+#define CHROME_COMMON_PREF_STORE_BASE_H_
+#pragma once
+
+#include "base/observer_list.h"
+#include "chrome/common/pref_store.h"
+
+// Implements the observer-related bits of the PrefStore interface. This is
+// provided as a convenience for PrefStore implementations to derive from.
+class PrefStoreBase : public PrefStore {
+ public:
+ virtual ~PrefStoreBase() {}
+
+ // Overriden from PrefStore.
+ virtual void AddObserver(ObserverInterface* observer);
+ virtual void RemoveObserver(ObserverInterface* observer);
+
+ protected:
+ // Send a notification about a changed preference value.
+ virtual void NotifyPrefValueChanged(const std::string& key);
+
+ // Notify observers about initialization being complete.
+ virtual void NotifyInitializationCompleted();
+
+ private:
+ ObserverList<ObserverInterface, true> observers_;
+};
+
+#endif // CHROME_COMMON_PREF_STORE_BASE_H_
diff --git a/chrome/common/pref_store_observer_mock.h b/chrome/common/pref_store_observer_mock.h
new file mode 100644
index 0000000..72e27a1
--- /dev/null
+++ b/chrome/common/pref_store_observer_mock.h
@@ -0,0 +1,26 @@
+// 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_COMMON_PREF_STORE_OBSERVER_MOCK_H_
+#define CHROME_COMMON_PREF_STORE_OBSERVER_MOCK_H_
+#pragma once
+
+#include "base/basictypes.h"
+#include "chrome/common/pref_store.h"
+#include "testing/gmock/include/gmock/gmock.h"
+
+// A gmock-ified implementation of PrefStore::ObserverInterface.
+class PrefStoreObserverMock : public PrefStore::ObserverInterface {
+ public:
+ PrefStoreObserverMock() {}
+ virtual ~PrefStoreObserverMock() {}
+
+ MOCK_METHOD1(OnPrefValueChanged, void(const std::string&));
+ MOCK_METHOD0(OnInitializationCompleted, void());
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(PrefStoreObserverMock);
+};
+
+#endif // CHROME_COMMON_PREF_STORE_OBSERVER_MOCK_H_