diff options
author | danno@chromium.org <danno@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-10 13:55:18 +0000 |
---|---|---|
committer | danno@chromium.org <danno@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-10 13:55:18 +0000 |
commit | 492d214950b16d229ced62021486e9e0ec859e40 (patch) | |
tree | ac098e0dc234b894433b13d409ddb198cd9d24b1 /chrome/browser/prefs | |
parent | 0ef5163fe4f9a76f516f09f7aa9d19c322e79691 (diff) | |
download | chromium_src-492d214950b16d229ced62021486e9e0ec859e40.zip chromium_src-492d214950b16d229ced62021486e9e0ec859e40.tar.gz chromium_src-492d214950b16d229ced62021486e9e0ec859e40.tar.bz2 |
Policy: plugins disabled by policy should honor policy changes without Chrome relaunch.
BUG=54620
TEST=PrefChangeRegistrarTest.*
Review URL: http://codereview.chromium.org/3316007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@59094 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/prefs')
-rw-r--r-- | chrome/browser/prefs/pref_change_registrar.cc | 53 | ||||
-rw-r--r-- | chrome/browser/prefs/pref_change_registrar.h | 50 | ||||
-rw-r--r-- | chrome/browser/prefs/pref_change_registrar_unittest.cc | 90 | ||||
-rw-r--r-- | chrome/browser/prefs/pref_service.h | 2 |
4 files changed, 194 insertions, 1 deletions
diff --git a/chrome/browser/prefs/pref_change_registrar.cc b/chrome/browser/prefs/pref_change_registrar.cc new file mode 100644 index 0000000..dc8bb97 --- /dev/null +++ b/chrome/browser/prefs/pref_change_registrar.cc @@ -0,0 +1,53 @@ +// 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/browser/prefs/pref_change_registrar.h" + +#include "chrome/browser/prefs/pref_service.h" + +PrefChangeRegistrar::PrefChangeRegistrar() : service_(NULL) {} + +PrefChangeRegistrar::~PrefChangeRegistrar() { + if (service_) { + for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); + it != observers_.end(); ++it) { + service_->RemovePrefObserver(it->first.c_str(), it->second); + } + } +} + +void PrefChangeRegistrar::Init(PrefService* service) { + DCHECK(!service_); + service_ = service; +} + +void PrefChangeRegistrar::Add(const char* path, NotificationObserver* obs) { + if (!service_) { + NOTREACHED(); + return; + } + ObserverRegistration registration(path, obs); + if (observers_.find(registration) != observers_.end()) { + NOTREACHED(); + return; + } + observers_.insert(registration); + service_->AddPrefObserver(path, obs); +} + +void PrefChangeRegistrar::Remove(const char* path, NotificationObserver* obs) { + if (!service_) { + NOTREACHED(); + return; + } + ObserverRegistration registration(path, obs); + std::set<ObserverRegistration>::iterator it = + observers_.find(registration); + if (it == observers_.end()) { + NOTREACHED(); + return; + } + service_->RemovePrefObserver(it->first.c_str(), it->second); + observers_.erase(it); +} diff --git a/chrome/browser/prefs/pref_change_registrar.h b/chrome/browser/prefs/pref_change_registrar.h new file mode 100644 index 0000000..cd8f5eb --- /dev/null +++ b/chrome/browser/prefs/pref_change_registrar.h @@ -0,0 +1,50 @@ +// 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_CHANGE_REGISTRAR_H_ +#define CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ +#pragma once + +#include <set> +#include <string> + +#include "base/basictypes.h" + +class PrefService; +class NotificationObserver; + +// Automatically manages the registration of one or more pref change observers +// with a PrefStore. Functions much like NotificationRegistrar, but specifically +// manages observers of preference changes. When the Registrar is destroyed, +// all registered observers are automatically unregistered with the PrefStore. +class PrefChangeRegistrar { + public: + PrefChangeRegistrar(); + virtual ~PrefChangeRegistrar(); + + // Must be called before adding or removing observers. + void Init(PrefService* service); + + // Adds an pref observer for the specified pref |path| and |obs| observer + // object. All registered observers will be automatically unregistered + // when the registrar's destructor is called unless the observer has been + // explicitly removed by a call to Remove beforehand. + void Add(const char* path, + NotificationObserver* obs); + + // Removes a preference observer that has previously been added with a call to + // Add. + void Remove(const char* path, + NotificationObserver* obs); + + private: + typedef std::pair<std::string, NotificationObserver*> ObserverRegistration; + + std::set<ObserverRegistration> observers_; + PrefService* service_; + + DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar); +}; + +#endif // CHROME_BROWSER_PREFS_PREF_CHANGE_REGISTRAR_H_ diff --git a/chrome/browser/prefs/pref_change_registrar_unittest.cc b/chrome/browser/prefs/pref_change_registrar_unittest.cc new file mode 100644 index 0000000..4d7f747 --- /dev/null +++ b/chrome/browser/prefs/pref_change_registrar_unittest.cc @@ -0,0 +1,90 @@ +// 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/browser/prefs/pref_change_registrar.h" +#include "chrome/common/notification_details.h" +#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_source.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/testing_pref_service.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +using testing::Mock; +using testing::Eq; + +// A mock provider that allows us to capture pref observer changes. +class MockPrefService : public TestingPrefService { + public: + MockPrefService() {} + virtual ~MockPrefService() {}; + + MOCK_METHOD2(AddPrefObserver, void(const char*, NotificationObserver*)); + MOCK_METHOD2(RemovePrefObserver, void(const char*, NotificationObserver*)); +}; + +// A mock observer used as a pref observer +class MockObserver : public NotificationObserver { + public: + MOCK_METHOD3(Observe, void(NotificationType, const NotificationSource& source, + const NotificationDetails& details)); +}; + +class PrefChangeRegistrarTest : public testing::Test { + public: + PrefChangeRegistrarTest() {} + virtual ~PrefChangeRegistrarTest() {} + + protected: + virtual void SetUp(); + + NotificationObserver* observer() const { return observer_.get(); } + MockPrefService* service() const { return service_.get(); } + + private: + scoped_ptr<MockPrefService> service_; + scoped_ptr<MockObserver> observer_; +}; + +void PrefChangeRegistrarTest::SetUp() { + service_.reset(new MockPrefService()); + observer_.reset(new MockObserver()); +} + +TEST_F(PrefChangeRegistrarTest, AddAndRemove) { + PrefChangeRegistrar registrar; + registrar.Init(service()); + + // Test adding. + EXPECT_CALL(*service(), + AddPrefObserver(Eq(std::string("test.pref.1")), observer())); + EXPECT_CALL(*service(), + AddPrefObserver(Eq(std::string("test.pref.2")), observer())); + registrar.Add("test.pref.1", observer()); + registrar.Add("test.pref.2", observer()); + + // Test removing. + Mock::VerifyAndClearExpectations(service()); + EXPECT_CALL(*service(), + RemovePrefObserver(Eq(std::string("test.pref.1")), observer())); + EXPECT_CALL(*service(), + RemovePrefObserver(Eq(std::string("test.pref.2")), observer())); + registrar.Remove("test.pref.1", observer()); + registrar.Remove("test.pref.2", observer()); +} + +TEST_F(PrefChangeRegistrarTest, AutoRemove) { + PrefChangeRegistrar registrar; + registrar.Init(service()); + + // Setup of auto-remove. + EXPECT_CALL(*service(), + AddPrefObserver(Eq(std::string("test.pref.1")), observer())); + registrar.Add("test.pref.1", observer()); + Mock::VerifyAndClearExpectations(service()); + + // Test auto-removing. + EXPECT_CALL(*service(), + RemovePrefObserver(Eq(std::string("test.pref.1")), observer())); +} diff --git a/chrome/browser/prefs/pref_service.h b/chrome/browser/prefs/pref_service.h index 07fa5e5..5a94542 100644 --- a/chrome/browser/prefs/pref_service.h +++ b/chrome/browser/prefs/pref_service.h @@ -165,7 +165,7 @@ class PrefService : public NonThreadSafe { // If the pref at the given path changes, we call the observer's Observe // method with NOTIFY_PREF_CHANGED. virtual void AddPrefObserver(const char* path, NotificationObserver* obs); - void RemovePrefObserver(const char* path, NotificationObserver* obs); + virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); // Removes a user pref and restores the pref to its default value. void ClearPref(const char* path); |