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