| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "chrome/browser/privacy_blacklist/blacklist_manager.h" | 5 #include "chrome/browser/privacy_blacklist/blacklist_manager.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" |
| 7 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
| 10 #include "chrome/browser/profile.h" | 11 #include "chrome/browser/profile.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/common/notification_registrar.h" | 13 #include "chrome/common/notification_registrar.h" |
| 12 #include "chrome/common/notification_source.h" | 14 #include "chrome/common/notification_source.h" |
| 13 #include "chrome/test/in_process_browser_test.h" | 15 #include "chrome/test/in_process_browser_test.h" |
| 14 #include "chrome/test/ui_test_utils.h" | 16 #include "chrome/test/ui_test_utils.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 // Returns true if |blacklist| contains a match for |url|. | 21 void GetBlacklistHasMatchOnIOThread(const BlacklistManager* manager, |
| 20 bool BlacklistHasMatch(const Blacklist* blacklist, const char* url) { | 22 const GURL& url, |
| 21 Blacklist::Match* match = blacklist->findMatch(GURL(url)); | 23 bool *has_match) { |
| 22 | 24 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 23 if (!match) | 25 const Blacklist* blacklist = manager->GetCompiledBlacklist(); |
| 24 return false; | 26 scoped_ptr<Blacklist::Match> match(blacklist->findMatch(url)); |
| 25 | 27 *has_match = (match.get() != NULL); |
| 26 delete match; | 28 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 27 return true; | 29 new MessageLoop::QuitTask()); |
| 28 } | 30 } |
| 29 | 31 |
| 30 } // namespace | 32 } // namespace |
| 31 | 33 |
| 32 class BlacklistManagerBrowserTest : public ExtensionBrowserTest { | 34 class BlacklistManagerBrowserTest : public ExtensionBrowserTest { |
| 33 public: | 35 public: |
| 34 void InitializeBlacklistManager() { | 36 virtual void SetUp() { |
| 35 Profile* profile = browser()->profile(); | 37 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 36 blacklist_manager_ = new BlacklistManager(); | 38 switches::kEnablePrivacyBlacklists); |
| 37 blacklist_manager_->Initialize(profile, profile->GetExtensionsService()); | 39 ExtensionBrowserTest::SetUp(); |
| 38 WaitForBlacklistUpdate(); | |
| 39 } | 40 } |
| 40 | 41 |
| 41 virtual void CleanUpOnMainThread() { | 42 virtual void SetUpInProcessBrowserTestFixture() { |
| 42 blacklist_manager_ = NULL; | 43 ExtensionBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 43 ExtensionBrowserTest::CleanUpOnMainThread(); | 44 |
| 45 received_blacklist_notification_ = false; |
| 46 host_resolver()->AddSimulatedFailure("www.example.com"); |
| 44 } | 47 } |
| 45 | 48 |
| 46 // NotificationObserver | 49 // NotificationObserver |
| 47 virtual void Observe(NotificationType type, | 50 virtual void Observe(NotificationType type, |
| 48 const NotificationSource& source, | 51 const NotificationSource& source, |
| 49 const NotificationDetails& details) { | 52 const NotificationDetails& details) { |
| 50 if (type != NotificationType::BLACKLIST_MANAGER_ERROR && | 53 if (type != NotificationType::BLACKLIST_MANAGER_ERROR && |
| 51 type != NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED) { | 54 type != NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED) { |
| 52 ExtensionBrowserTest::Observe(type, source, details); | 55 ExtensionBrowserTest::Observe(type, source, details); |
| 53 return; | 56 return; |
| 54 } | 57 } |
| 58 received_blacklist_notification_ = true; |
| 55 MessageLoop::current()->Quit(); | 59 MessageLoop::current()->Quit(); |
| 56 } | 60 } |
| 57 | 61 |
| 58 protected: | 62 protected: |
| 59 void WaitForBlacklistUpdate() { | 63 BlacklistManager* GetBlacklistManager() { |
| 60 NotificationRegistrar registrar; | 64 return browser()->profile()->GetBlacklistManager(); |
| 61 registrar.Add(this, | |
| 62 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, | |
| 63 Source<Profile>(browser()->profile())); | |
| 64 ui_test_utils::RunMessageLoop(); | |
| 65 } | 65 } |
| 66 | 66 |
| 67 scoped_refptr<BlacklistManager> blacklist_manager_; | 67 bool GetAndResetReceivedBlacklistNotification() { |
| 68 bool result = received_blacklist_notification_; |
| 69 received_blacklist_notification_ = false; |
| 70 return result; |
| 71 } |
| 72 |
| 73 bool BlacklistHasMatch(const std::string& url) { |
| 74 bool has_match = false; |
| 75 ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, |
| 76 NewRunnableFunction(GetBlacklistHasMatchOnIOThread, |
| 77 GetBlacklistManager(), |
| 78 GURL(url), |
| 79 &has_match)); |
| 80 ui_test_utils::RunMessageLoop(); |
| 81 return has_match; |
| 82 } |
| 83 |
| 84 private: |
| 85 bool received_blacklist_notification_; |
| 68 }; | 86 }; |
| 69 | 87 |
| 70 IN_PROC_BROWSER_TEST_F(BlacklistManagerBrowserTest, Basic) { | 88 IN_PROC_BROWSER_TEST_F(BlacklistManagerBrowserTest, Basic) { |
| 71 static const char kTestUrl[] = "http://host/annoying_ads/ad.jpg"; | 89 static const char kTestUrl[] = "http://www.example.com/annoying_ads/ad.jpg"; |
| 72 | 90 |
| 73 InitializeBlacklistManager(); | 91 NotificationRegistrar registrar; |
| 74 ASSERT_TRUE(blacklist_manager_->GetCompiledBlacklist()); | 92 registrar.Add(this, |
| 75 EXPECT_FALSE(BlacklistHasMatch(blacklist_manager_->GetCompiledBlacklist(), | 93 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, |
| 76 kTestUrl)); | 94 Source<Profile>(browser()->profile())); |
| 77 | 95 |
| 78 // Test loading an extension with blacklist. | 96 // Test loading an extension with blacklist. |
| 79 ASSERT_TRUE(LoadExtension( | 97 ASSERT_TRUE(LoadExtension( |
| 80 test_data_dir_.AppendASCII("common").AppendASCII("privacy_blacklist"))); | 98 test_data_dir_.AppendASCII("common").AppendASCII("privacy_blacklist"))); |
| 81 if (!BlacklistHasMatch(blacklist_manager_->GetCompiledBlacklist(), | |
| 82 kTestUrl)) { | |
| 83 WaitForBlacklistUpdate(); | |
| 84 } | |
| 85 EXPECT_TRUE(BlacklistHasMatch(blacklist_manager_->GetCompiledBlacklist(), | |
| 86 kTestUrl)); | |
| 87 | 99 |
| 88 // Make sure that after unloading the extension we update the blacklist. | 100 // Wait until the blacklist is loaded and ready. |
| 89 ExtensionsService* extensions_service = | 101 if (!GetAndResetReceivedBlacklistNotification()) |
| 90 browser()->profile()->GetExtensionsService(); | 102 ui_test_utils::RunMessageLoop(); |
| 91 ASSERT_EQ(1U, extensions_service->extensions()->size()); | 103 |
| 92 UnloadExtension(extensions_service->extensions()->front()->id()); | 104 // The blacklist should block our test URL. |
| 93 if (BlacklistHasMatch(blacklist_manager_->GetCompiledBlacklist(), | 105 EXPECT_TRUE(BlacklistHasMatch(kTestUrl)); |
| 94 kTestUrl)) { | 106 |
| 95 WaitForBlacklistUpdate(); | 107 // TODO(phajdan.jr): Verify that we really blocked the request etc. |
| 96 } | 108 ui_test_utils::NavigateToURL(browser(), GURL(kTestUrl)); |
| 97 EXPECT_FALSE(BlacklistHasMatch(blacklist_manager_->GetCompiledBlacklist(), | |
| 98 kTestUrl)); | |
| 99 } | 109 } |
| OLD | NEW |