diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-04 11:11:34 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-04 11:11:34 +0000 |
commit | 24e7a9d2b4d7925ca25d2cdf75808d3ff88b5f6e (patch) | |
tree | 809ce617d8e41b1ba284eba33694af966654fb87 /chrome/browser/privacy_blacklist | |
parent | a6cf2a3e9f1d7b44dda39975139f5ae39524e363 (diff) | |
download | chromium_src-24e7a9d2b4d7925ca25d2cdf75808d3ff88b5f6e.zip chromium_src-24e7a9d2b4d7925ca25d2cdf75808d3ff88b5f6e.tar.gz chromium_src-24e7a9d2b4d7925ca25d2cdf75808d3ff88b5f6e.tar.bz2 |
Implement loading blacklists from extensions.
It doesn't yet work in full-browser scenario, but allows me
to write a simple test.
TEST=Covered by browser_tests.
BUG=21541
Review URL: http://codereview.chromium.org/341050
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30952 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/privacy_blacklist')
3 files changed, 139 insertions, 30 deletions
diff --git a/chrome/browser/privacy_blacklist/blacklist_manager.cc b/chrome/browser/privacy_blacklist/blacklist_manager.cc index f7c9311..0629806 100644 --- a/chrome/browser/privacy_blacklist/blacklist_manager.cc +++ b/chrome/browser/privacy_blacklist/blacklist_manager.cc @@ -35,7 +35,7 @@ class BlacklistManagerTask : public Task { protected: BlacklistManager* blacklist_manager() const { return manager_; } - + MessageLoop* original_loop_; private: @@ -59,7 +59,7 @@ class BlacklistManager::CompileBlacklistTask : public BlacklistManagerTask { virtual void Run() { bool success = true; - + Blacklist blacklist; std::string error_string; @@ -107,7 +107,7 @@ class BlacklistManager::ReadBlacklistTask : public BlacklistManagerTask { ReportReadResult(NULL); return; } - + std::string error_string; std::vector<FilePath>::const_iterator i; for (i = transient_blacklists_.begin(); @@ -117,7 +117,7 @@ class BlacklistManager::ReadBlacklistTask : public BlacklistManagerTask { return; } } - + ReportReadResult(blacklist.release()); } @@ -128,7 +128,7 @@ class BlacklistManager::ReadBlacklistTask : public BlacklistManagerTask { &BlacklistManager::OnBlacklistReadFinished, blacklist)); } - + FilePath compiled_blacklist_; std::vector<FilePath> transient_blacklists_; @@ -145,7 +145,10 @@ BlacklistManager::BlacklistManager(Profile* profile, path_provider_(path_provider), backend_thread_(backend_thread) { registrar_.Add(this, - NotificationType::BLACKLIST_PATH_PROVIDER_UPDATED, + NotificationType::EXTENSION_LOADED, + Source<Profile>(profile)); + registrar_.Add(this, + NotificationType::EXTENSION_UNLOADED, Source<Profile>(profile)); ReadBlacklist(); } @@ -153,8 +156,15 @@ BlacklistManager::BlacklistManager(Profile* profile, void BlacklistManager::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { - DCHECK(type == NotificationType::BLACKLIST_PATH_PROVIDER_UPDATED); - CompileBlacklist(); + switch (type.value) { + case NotificationType::EXTENSION_LOADED: + case NotificationType::EXTENSION_UNLOADED: + CompileBlacklist(); + break; + default: + NOTREACHED(); + break; + } } void BlacklistManager::CompileBlacklist() { @@ -167,7 +177,7 @@ void BlacklistManager::CompileBlacklist() { void BlacklistManager::ReadBlacklist() { DCHECK(CalledOnValidThread()); - + RunTaskOnBackendThread(new ReadBlacklistTask( this, compiled_blacklist_path_, path_provider_->GetTransientBlacklistPaths())); @@ -175,7 +185,7 @@ void BlacklistManager::ReadBlacklist() { void BlacklistManager::OnBlacklistCompilationFinished(bool success) { DCHECK(CalledOnValidThread()); - + if (success) { ReadBlacklist(); } else { @@ -189,7 +199,7 @@ void BlacklistManager::OnBlacklistCompilationFinished(bool success) { void BlacklistManager::OnBlacklistReadFinished(Blacklist* blacklist) { DCHECK(CalledOnValidThread()); - + if (!blacklist) { if (!first_read_finished_) { // If we're loading for the first time, the compiled blacklist could @@ -207,7 +217,7 @@ void BlacklistManager::OnBlacklistReadFinished(Blacklist* blacklist) { } first_read_finished_ = true; compiled_blacklist_.reset(blacklist); - + NotificationService::current()->Notify( NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, Source<Profile>(profile_), diff --git a/chrome/browser/privacy_blacklist/blacklist_manager_browsertest.cc b/chrome/browser/privacy_blacklist/blacklist_manager_browsertest.cc new file mode 100644 index 0000000..bbc30c4 --- /dev/null +++ b/chrome/browser/privacy_blacklist/blacklist_manager_browsertest.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2009 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/privacy_blacklist/blacklist_manager.h" + +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/extensions/extension_browsertest.h" +#include "chrome/browser/profile.h" +#include "chrome/common/notification_registrar.h" +#include "chrome/common/notification_source.h" +#include "chrome/test/in_process_browser_test.h" +#include "chrome/test/ui_test_utils.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +// Returns true if |blacklist| contains a match for |url|. +bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { + Blacklist::Match* match = blacklist->findMatch(GURL(url)); + + if (!match) + return false; + + delete match; + return true; +} + +} // namespace + +class BlacklistManagerBrowserTest : public ExtensionBrowserTest { + public: + void InitializeBlacklistManager() { + Profile* profile = browser()->profile(); + blacklist_manager_ = new BlacklistManager( + profile, profile->GetExtensionsService(), + g_browser_process->io_thread()); + WaitForBlacklistUpdate(); + } + + virtual void CleanUpOnMainThread() { + blacklist_manager_ = NULL; + ExtensionBrowserTest::CleanUpOnMainThread(); + } + + // NotificationObserver + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type != NotificationType::BLACKLIST_MANAGER_ERROR && + type != NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED) { + ExtensionBrowserTest::Observe(type, source, details); + return; + } + MessageLoop::current()->Quit(); + } + + protected: + void WaitForBlacklistUpdate() { + NotificationRegistrar registrar; + registrar.Add(this, + NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, + Source<Profile>(browser()->profile())); + ui_test_utils::RunMessageLoop(); + } + + scoped_refptr<BlacklistManager> blacklist_manager_; +}; + +IN_PROC_BROWSER_TEST_F(BlacklistManagerBrowserTest, Basic) { + InitializeBlacklistManager(); + ASSERT_TRUE(blacklist_manager_->GetCompiledBlacklist()); + EXPECT_FALSE(BlacklistHasMatch(blacklist_manager_->GetCompiledBlacklist(), + "http://host/annoying_ads/ad.jpg")); + + // Test loading an extension with blacklist. + ASSERT_TRUE(LoadExtension( + test_data_dir_.AppendASCII("common").AppendASCII("privacy_blacklist"))); + WaitForBlacklistUpdate(); + EXPECT_TRUE(BlacklistHasMatch(blacklist_manager_->GetCompiledBlacklist(), + "http://host/annoying_ads/ad.jpg")); + + // Make sure that after unloading the extension we update the blacklist. + ExtensionsService* extensions_service = + browser()->profile()->GetExtensionsService(); + ASSERT_EQ(1U, extensions_service->extensions()->size()); + UnloadExtension(extensions_service->extensions()->front()->id()); + WaitForBlacklistUpdate(); + EXPECT_FALSE(BlacklistHasMatch(blacklist_manager_->GetCompiledBlacklist(), + "http://host/annoying_ads/ad.jpg")); +} diff --git a/chrome/browser/privacy_blacklist/blacklist_manager_unittest.cc b/chrome/browser/privacy_blacklist/blacklist_manager_unittest.cc index b2e6996..e7d9f9d 100644 --- a/chrome/browser/privacy_blacklist/blacklist_manager_unittest.cc +++ b/chrome/browser/privacy_blacklist/blacklist_manager_unittest.cc @@ -10,6 +10,7 @@ #include "base/thread.h" #include "chrome/browser/privacy_blacklist/blacklist.h" #include "chrome/common/chrome_paths.h" +#include "chrome/common/extensions/extension.h" #include "chrome/common/notification_service.h" #include "chrome/test/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" @@ -37,7 +38,7 @@ class TestBlacklistPathProvider : public BlacklistPathProvider { virtual std::vector<FilePath> GetPersistentBlacklistPaths() { return persistent_paths_; } - + virtual std::vector<FilePath> GetTransientBlacklistPaths() { return transient_paths_; } @@ -46,12 +47,12 @@ class TestBlacklistPathProvider : public BlacklistPathProvider { persistent_paths_.push_back(path); SendUpdateNotification(); } - + void AddTransientPath(const FilePath& path) { transient_paths_.push_back(path); SendUpdateNotification(); } - + void clear() { persistent_paths_.clear(); transient_paths_.clear(); @@ -60,12 +61,18 @@ class TestBlacklistPathProvider : public BlacklistPathProvider { private: void SendUpdateNotification() { +#if defined(OS_WIN) + FilePath path(FILE_PATH_LITERAL("c:\\foo")); +#elif defined(OS_POSIX) + FilePath path(FILE_PATH_LITERAL("/foo")); +#endif + Extension extension(path); NotificationService::current()->Notify( - NotificationType::BLACKLIST_PATH_PROVIDER_UPDATED, + NotificationType::EXTENSION_LOADED, Source<Profile>(profile_), - Details<BlacklistPathProvider>(this)); + Details<Extension>(&extension)); } - + Profile* profile_; std::vector<FilePath> persistent_paths_; @@ -78,7 +85,7 @@ class BlacklistManagerTest : public testing::Test, public NotificationObserver { public: BlacklistManagerTest() : path_provider_(&profile_) { } - + virtual void SetUp() { ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); test_data_dir_ = test_data_dir_.AppendASCII("blacklist_samples"); @@ -103,7 +110,7 @@ class BlacklistManagerTest : public testing::Test, public NotificationObserver { Source<Profile>(&profile_)); MessageLoop::current()->Run(); } - + void WaitForBlacklistUpdate() { NotificationRegistrar registrar; registrar.Add(this, @@ -111,11 +118,11 @@ class BlacklistManagerTest : public testing::Test, public NotificationObserver { Source<Profile>(&profile_)); MessageLoop::current()->Run(); } - + FilePath test_data_dir_; MyTestingProfile profile_; - + TestBlacklistPathProvider path_provider_; private: @@ -157,24 +164,24 @@ TEST_F(BlacklistManagerTest, BlacklistPathProvider) { path_provider_.AddPersistentPath( test_data_dir_.AppendASCII("annoying_ads.pbl")); WaitForBlacklistUpdate(); - + const Blacklist* blacklist2 = manager->GetCompiledBlacklist(); // Added a real blacklist, the manager should recompile. EXPECT_NE(blacklist1, blacklist2); EXPECT_TRUE(BlacklistHasMatch(blacklist2, "http://host/annoying_ads/ad.jpg")); EXPECT_FALSE(BlacklistHasMatch(blacklist2, "http://host/other_ads/ad.jpg")); - + path_provider_.AddTransientPath(test_data_dir_.AppendASCII("other_ads.pbl")); WaitForBlacklistUpdate(); - + const Blacklist* blacklist3 = manager->GetCompiledBlacklist(); - + // In theory blacklist2 and blacklist3 could be the same object, so we're // not checking for inequality. EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/annoying_ads/ad.jpg")); EXPECT_TRUE(BlacklistHasMatch(blacklist3, "http://host/other_ads/ad.jpg")); - + // Now make sure that transient blacklists don't survive after re-creating // the BlacklistManager. manager = NULL; @@ -183,9 +190,9 @@ TEST_F(BlacklistManagerTest, BlacklistPathProvider) { test_data_dir_.AppendASCII("annoying_ads.pbl")); manager = new BlacklistManager(&profile_, &path_provider_, NULL); WaitForBlacklistUpdate(); - + const Blacklist* blacklist4 = manager->GetCompiledBlacklist(); - + EXPECT_TRUE(BlacklistHasMatch(blacklist4, "http://host/annoying_ads/ad.jpg")); EXPECT_FALSE(BlacklistHasMatch(blacklist4, "http://host/other_ads/ad.jpg")); } @@ -222,7 +229,7 @@ TEST_F(BlacklistManagerTest, BlacklistPathReadError) { ASSERT_FALSE(file_util::PathExists(bogus_path)); path_provider_.AddPersistentPath(bogus_path); WaitForBlacklistError(); - + const Blacklist* blacklist = manager->GetCompiledBlacklist(); EXPECT_TRUE(blacklist); } |