| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <memory> | 6 #include <memory> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 13 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 14 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 14 |
| 15 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" | |
| 16 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" | |
| 17 #include "chrome/browser/browsing_data/cache_counter.h" | 15 #include "chrome/browser/browsing_data/cache_counter.h" |
| 18 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" | 16 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" |
| 19 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
| 20 #include "chrome/browser/external_protocol/external_protocol_handler.h" | 18 #include "chrome/browser/external_protocol/external_protocol_handler.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/test/base/in_process_browser_test.h" | 23 #include "chrome/test/base/in_process_browser_test.h" |
| 26 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
| 27 #include "components/browsing_data/core/browsing_data_utils.h" | 25 #include "components/browsing_data/core/browsing_data_utils.h" |
| 28 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
| 29 #include "content/public/browser/browser_context.h" | 27 #include "content/public/browser/browser_context.h" |
| 30 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/browsing_data_filter_builder.h" | 29 #include "content/public/browser/browsing_data_filter_builder.h" |
| 30 #include "content/public/browser/browsing_data_remover.h" |
| 32 #include "content/public/browser/download_manager.h" | 31 #include "content/public/browser/download_manager.h" |
| 33 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 34 #include "content/public/common/content_paths.h" | 33 #include "content/public/common/content_paths.h" |
| 35 #include "content/public/test/browser_test_utils.h" | 34 #include "content/public/test/browser_test_utils.h" |
| 35 #include "content/public/test/browsing_data_remover_test_util.h" |
| 36 #include "content/public/test/download_test_observer.h" | 36 #include "content/public/test/download_test_observer.h" |
| 37 #include "net/dns/mock_host_resolver.h" | 37 #include "net/dns/mock_host_resolver.h" |
| 38 #include "net/http/transport_security_state.h" | 38 #include "net/http/transport_security_state.h" |
| 39 #include "net/test/embedded_test_server/embedded_test_server.h" | 39 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 40 #include "net/url_request/url_request_context.h" | 40 #include "net/url_request/url_request_context.h" |
| 41 #include "net/url_request/url_request_context_getter.h" | 41 #include "net/url_request/url_request_context_getter.h" |
| 42 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 43 | 43 |
| 44 using content::BrowserThread; | 44 using content::BrowserThread; |
| 45 using content::BrowsingDataFilterBuilder; | 45 using content::BrowsingDataFilterBuilder; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 browsing_data::ClearBrowsingDataTab::ADVANCED, | 109 browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 110 base::Bind(&BrowsingDataRemoverBrowserTest::OnCacheSizeResult, | 110 base::Bind(&BrowsingDataRemoverBrowserTest::OnCacheSizeResult, |
| 111 base::Unretained(this), base::Unretained(&run_loop), | 111 base::Unretained(this), base::Unretained(&run_loop), |
| 112 base::Unretained(&size))); | 112 base::Unretained(&size))); |
| 113 counter.Restart(); | 113 counter.Restart(); |
| 114 run_loop.Run(); | 114 run_loop.Run(); |
| 115 return size; | 115 return size; |
| 116 } | 116 } |
| 117 | 117 |
| 118 void RemoveAndWait(int remove_mask) { | 118 void RemoveAndWait(int remove_mask) { |
| 119 BrowsingDataRemover* remover = | 119 content::BrowsingDataRemover* remover = |
| 120 BrowsingDataRemoverFactory::GetForBrowserContext(browser()->profile()); | 120 content::BrowserContext::GetBrowsingDataRemover(browser()->profile()); |
| 121 BrowsingDataRemoverCompletionObserver completion_observer(remover); | 121 content::BrowsingDataRemoverCompletionObserver completion_observer(remover); |
| 122 remover->RemoveAndReply(base::Time(), base::Time::Max(), remove_mask, | 122 remover->RemoveAndReply( |
| 123 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, | 123 base::Time(), base::Time::Max(), remove_mask, |
| 124 &completion_observer); | 124 content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, |
| 125 &completion_observer); |
| 125 completion_observer.BlockUntilCompletion(); | 126 completion_observer.BlockUntilCompletion(); |
| 126 } | 127 } |
| 127 | 128 |
| 128 void RemoveWithFilterAndWait( | 129 void RemoveWithFilterAndWait( |
| 129 int remove_mask, | 130 int remove_mask, |
| 130 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder) { | 131 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder) { |
| 131 BrowsingDataRemover* remover = | 132 content::BrowsingDataRemover* remover = |
| 132 BrowsingDataRemoverFactory::GetForBrowserContext(browser()->profile()); | 133 content::BrowserContext::GetBrowsingDataRemover(browser()->profile()); |
| 133 BrowsingDataRemoverCompletionObserver completion_observer(remover); | 134 content::BrowsingDataRemoverCompletionObserver completion_observer(remover); |
| 134 remover->RemoveWithFilterAndReply( | 135 remover->RemoveWithFilterAndReply( |
| 135 base::Time(), base::Time::Max(), remove_mask, | 136 base::Time(), base::Time::Max(), remove_mask, |
| 136 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, | 137 content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, |
| 137 std::move(filter_builder), &completion_observer); | 138 std::move(filter_builder), &completion_observer); |
| 138 completion_observer.BlockUntilCompletion(); | 139 completion_observer.BlockUntilCompletion(); |
| 139 } | 140 } |
| 140 | 141 |
| 141 private: | 142 private: |
| 142 void OnCacheSizeResult( | 143 void OnCacheSizeResult( |
| 143 base::RunLoop* run_loop, | 144 base::RunLoop* run_loop, |
| 144 browsing_data::BrowsingDataCounter::ResultInt* out_size, | 145 browsing_data::BrowsingDataCounter::ResultInt* out_size, |
| 145 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | 146 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| 146 if (!result->Finished()) | 147 if (!result->Finished()) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); | 189 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 189 EXPECT_FALSE(state->ShouldUpgradeToSSL("example.test")); | 190 EXPECT_FALSE(state->ShouldUpgradeToSSL("example.test")); |
| 190 state->AddHSTS("example.test", expiry, false); | 191 state->AddHSTS("example.test", expiry, false); |
| 191 EXPECT_TRUE(state->ShouldUpgradeToSSL("example.test")); | 192 EXPECT_TRUE(state->ShouldUpgradeToSSL("example.test")); |
| 192 } | 193 } |
| 193 }; | 194 }; |
| 194 | 195 |
| 195 // Test BrowsingDataRemover for downloads. | 196 // Test BrowsingDataRemover for downloads. |
| 196 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Download) { | 197 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Download) { |
| 197 DownloadAnItem(); | 198 DownloadAnItem(); |
| 198 RemoveAndWait(BrowsingDataRemover::DATA_TYPE_DOWNLOADS); | 199 RemoveAndWait(content::BrowsingDataRemover::DATA_TYPE_DOWNLOADS); |
| 199 VerifyDownloadCount(0u); | 200 VerifyDownloadCount(0u); |
| 200 } | 201 } |
| 201 | 202 |
| 202 // The call to Remove() should crash in debug (DCHECK), but the browser-test | 203 // The call to Remove() should crash in debug (DCHECK), but the browser-test |
| 203 // process model prevents using a death test. | 204 // process model prevents using a death test. |
| 204 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 205 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 205 // Test BrowsingDataRemover for prohibited downloads. Note that this only | 206 // Test BrowsingDataRemover for prohibited downloads. Note that this only |
| 206 // really exercises the code in a Release build. | 207 // really exercises the code in a Release build. |
| 207 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, DownloadProhibited) { | 208 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, DownloadProhibited) { |
| 208 PrefService* prefs = browser()->profile()->GetPrefs(); | 209 PrefService* prefs = browser()->profile()->GetPrefs(); |
| 209 prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false); | 210 prefs->SetBoolean(prefs::kAllowDeletingBrowserHistory, false); |
| 210 | 211 |
| 211 DownloadAnItem(); | 212 DownloadAnItem(); |
| 212 RemoveAndWait(BrowsingDataRemover::DATA_TYPE_DOWNLOADS); | 213 RemoveAndWait(content::BrowsingDataRemover::DATA_TYPE_DOWNLOADS); |
| 213 VerifyDownloadCount(1u); | 214 VerifyDownloadCount(1u); |
| 214 } | 215 } |
| 215 #endif | 216 #endif |
| 216 | 217 |
| 217 // Verify can modify database after deleting it. | 218 // Verify can modify database after deleting it. |
| 218 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Database) { | 219 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, Database) { |
| 219 GURL url = embedded_test_server()->GetURL("/simple_database.html"); | 220 GURL url = embedded_test_server()->GetURL("/simple_database.html"); |
| 220 LOG(ERROR) << url; | 221 LOG(ERROR) << url; |
| 221 ui_test_utils::NavigateToURL(browser(), url); | 222 ui_test_utils::NavigateToURL(browser(), url); |
| 222 | 223 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 248 | 249 |
| 249 // The cache is nonempty, because we created entries by visiting websites. | 250 // The cache is nonempty, because we created entries by visiting websites. |
| 250 browsing_data::BrowsingDataCounter::ResultInt original_size = GetCacheSize(); | 251 browsing_data::BrowsingDataCounter::ResultInt original_size = GetCacheSize(); |
| 251 EXPECT_GT(original_size, 0); | 252 EXPECT_GT(original_size, 0); |
| 252 | 253 |
| 253 // Partially delete cache data. Delete data for localhost, which is the origin | 254 // Partially delete cache data. Delete data for localhost, which is the origin |
| 254 // of |url1|, but not for |kExampleHost|, which is the origin of |url2|. | 255 // of |url1|, but not for |kExampleHost|, which is the origin of |url2|. |
| 255 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder = | 256 std::unique_ptr<BrowsingDataFilterBuilder> filter_builder = |
| 256 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST); | 257 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST); |
| 257 filter_builder->AddOrigin(url::Origin(url1)); | 258 filter_builder->AddOrigin(url::Origin(url1)); |
| 258 RemoveWithFilterAndWait(BrowsingDataRemover::DATA_TYPE_CACHE, | 259 RemoveWithFilterAndWait(content::BrowsingDataRemover::DATA_TYPE_CACHE, |
| 259 std::move(filter_builder)); | 260 std::move(filter_builder)); |
| 260 | 261 |
| 261 // After the partial deletion, the cache should be smaller but still nonempty. | 262 // After the partial deletion, the cache should be smaller but still nonempty. |
| 262 browsing_data::BrowsingDataCounter::ResultInt new_size = GetCacheSize(); | 263 browsing_data::BrowsingDataCounter::ResultInt new_size = GetCacheSize(); |
| 263 EXPECT_LT(new_size, original_size); | 264 EXPECT_LT(new_size, original_size); |
| 264 | 265 |
| 265 // Another partial deletion with the same filter should have no effect. | 266 // Another partial deletion with the same filter should have no effect. |
| 266 filter_builder = | 267 filter_builder = |
| 267 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST); | 268 BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST); |
| 268 filter_builder->AddOrigin(url::Origin(url1)); | 269 filter_builder->AddOrigin(url::Origin(url1)); |
| 269 RemoveWithFilterAndWait(BrowsingDataRemover::DATA_TYPE_CACHE, | 270 RemoveWithFilterAndWait(content::BrowsingDataRemover::DATA_TYPE_CACHE, |
| 270 std::move(filter_builder)); | 271 std::move(filter_builder)); |
| 271 EXPECT_EQ(new_size, GetCacheSize()); | 272 EXPECT_EQ(new_size, GetCacheSize()); |
| 272 | 273 |
| 273 // Delete the remaining data. | 274 // Delete the remaining data. |
| 274 RemoveAndWait(BrowsingDataRemover::DATA_TYPE_CACHE); | 275 RemoveAndWait(content::BrowsingDataRemover::DATA_TYPE_CACHE); |
| 275 | 276 |
| 276 // The cache is empty. | 277 // The cache is empty. |
| 277 EXPECT_EQ(0, GetCacheSize()); | 278 EXPECT_EQ(0, GetCacheSize()); |
| 278 } | 279 } |
| 279 | 280 |
| 280 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, | 281 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverBrowserTest, |
| 281 ExternalProtocolHandlerPrefs) { | 282 ExternalProtocolHandlerPrefs) { |
| 282 Profile* profile = browser()->profile(); | 283 Profile* profile = browser()->profile(); |
| 283 base::DictionaryValue prefs; | 284 base::DictionaryValue prefs; |
| 284 prefs.SetBoolean("tel", true); | 285 prefs.SetBoolean("tel", true); |
| 285 profile->GetPrefs()->Set(prefs::kExcludedSchemes, prefs); | 286 profile->GetPrefs()->Set(prefs::kExcludedSchemes, prefs); |
| 286 ExternalProtocolHandler::BlockState block_state = | 287 ExternalProtocolHandler::BlockState block_state = |
| 287 ExternalProtocolHandler::GetBlockState("tel", profile); | 288 ExternalProtocolHandler::GetBlockState("tel", profile); |
| 288 ASSERT_EQ(ExternalProtocolHandler::BLOCK, block_state); | 289 ASSERT_EQ(ExternalProtocolHandler::BLOCK, block_state); |
| 289 RemoveAndWait(ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA); | 290 RemoveAndWait(ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA); |
| 290 block_state = ExternalProtocolHandler::GetBlockState("tel", profile); | 291 block_state = ExternalProtocolHandler::GetBlockState("tel", profile); |
| 291 ASSERT_EQ(ExternalProtocolHandler::UNKNOWN, block_state); | 292 ASSERT_EQ(ExternalProtocolHandler::UNKNOWN, block_state); |
| 292 } | 293 } |
| 293 | 294 |
| 294 // Verify that TransportSecurityState data is cleared for REMOVE_CACHE. | 295 // Verify that TransportSecurityState data is cleared for REMOVE_CACHE. |
| 295 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverTransportSecurityStateBrowserTest, | 296 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverTransportSecurityStateBrowserTest, |
| 296 ClearTransportSecurityState) { | 297 ClearTransportSecurityState) { |
| 297 RemoveAndWait(BrowsingDataRemover::DATA_TYPE_CACHE); | 298 RemoveAndWait(content::BrowsingDataRemover::DATA_TYPE_CACHE); |
| 298 base::RunLoop run_loop; | 299 base::RunLoop run_loop; |
| 299 BrowserThread::PostTaskAndReply( | 300 BrowserThread::PostTaskAndReply( |
| 300 BrowserThread::IO, FROM_HERE, | 301 BrowserThread::IO, FROM_HERE, |
| 301 base::BindOnce( | 302 base::BindOnce( |
| 302 &BrowsingDataRemoverTransportSecurityStateBrowserTest:: | 303 &BrowsingDataRemoverTransportSecurityStateBrowserTest:: |
| 303 CheckTransportSecurityState, | 304 CheckTransportSecurityState, |
| 304 base::Unretained(this), | 305 base::Unretained(this), |
| 305 base::RetainedRef(browser()->profile()->GetRequestContext()), | 306 base::RetainedRef(browser()->profile()->GetRequestContext()), |
| 306 true /* should be cleared */), | 307 true /* should be cleared */), |
| 307 run_loop.QuitClosure()); | 308 run_loop.QuitClosure()); |
| 308 } | 309 } |
| 309 | 310 |
| 310 // Verify that TransportSecurityState data is not cleared if REMOVE_CACHE is not | 311 // Verify that TransportSecurityState data is not cleared if REMOVE_CACHE is not |
| 311 // set. | 312 // set. |
| 312 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverTransportSecurityStateBrowserTest, | 313 IN_PROC_BROWSER_TEST_F(BrowsingDataRemoverTransportSecurityStateBrowserTest, |
| 313 PreserveTransportSecurityState) { | 314 PreserveTransportSecurityState) { |
| 314 RemoveAndWait(ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA); | 315 RemoveAndWait(ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA); |
| 315 base::RunLoop run_loop; | 316 base::RunLoop run_loop; |
| 316 BrowserThread::PostTaskAndReply( | 317 BrowserThread::PostTaskAndReply( |
| 317 BrowserThread::IO, FROM_HERE, | 318 BrowserThread::IO, FROM_HERE, |
| 318 base::BindOnce( | 319 base::BindOnce( |
| 319 &BrowsingDataRemoverTransportSecurityStateBrowserTest:: | 320 &BrowsingDataRemoverTransportSecurityStateBrowserTest:: |
| 320 CheckTransportSecurityState, | 321 CheckTransportSecurityState, |
| 321 base::Unretained(this), | 322 base::Unretained(this), |
| 322 base::RetainedRef(browser()->profile()->GetRequestContext()), | 323 base::RetainedRef(browser()->profile()->GetRequestContext()), |
| 323 false /* should not be cleared */), | 324 false /* should not be cleared */), |
| 324 run_loop.QuitClosure()); | 325 run_loop.QuitClosure()); |
| 325 } | 326 } |
| OLD | NEW |