| 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 <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 | 11 |
| 12 #include "base/base64.h" | 12 #include "base/base64.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/json/json_string_value_serializer.h" | 16 #include "base/json/json_string_value_serializer.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
| 21 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
| 22 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
| 23 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 25 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
| 26 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 27 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
| 28 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" |
| 26 #include "chrome/browser/net/predictor.h" | 29 #include "chrome/browser/net/predictor.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/ui/browser.h" | 31 #include "chrome/browser/ui/browser.h" |
| 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 32 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 30 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
| 31 #include "chrome/test/base/in_process_browser_test.h" | 34 #include "chrome/test/base/in_process_browser_test.h" |
| 32 #include "chrome/test/base/ui_test_utils.h" | 35 #include "chrome/test/base/ui_test_utils.h" |
| 33 #include "components/prefs/pref_service.h" | 36 #include "components/prefs/pref_service.h" |
| 34 #include "content/public/common/content_switches.h" | 37 #include "content/public/common/content_switches.h" |
| 35 #include "content/public/test/browser_test_utils.h" | 38 #include "content/public/test/browser_test_utils.h" |
| (...skipping 1307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1343 // preconnects to go through. | 1346 // preconnects to go through. |
| 1344 // Note that ceil(2 + .33) + 1 = 4. | 1347 // Note that ceil(2 + .33) + 1 = 4. |
| 1345 cross_site_connection_listener_->WaitForAcceptedConnectionsOnUI(4u); | 1348 cross_site_connection_listener_->WaitForAcceptedConnectionsOnUI(4u); |
| 1346 } | 1349 } |
| 1347 | 1350 |
| 1348 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, | 1351 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, |
| 1349 PRE_ShutdownStartupCyclePreresolve) { | 1352 PRE_ShutdownStartupCyclePreresolve) { |
| 1350 // Prepare state that will be serialized on this shut-down and read on next | 1353 // Prepare state that will be serialized on this shut-down and read on next |
| 1351 // start-up. Ensure preresolution over preconnection. | 1354 // start-up. Ensure preresolution over preconnection. |
| 1352 LearnAboutInitialNavigation(startup_url_); | 1355 LearnAboutInitialNavigation(startup_url_); |
| 1353 // The target url will have a expected connection count of 2 after this call. | 1356 // The target URL will have an expected connection count of 2 after this call. |
| 1354 InstallPredictorObserver(referring_url_, target_url_); | 1357 InstallPredictorObserver(referring_url_, target_url_); |
| 1355 LearnFromNavigation(referring_url_, target_url_); | 1358 LearnFromNavigation(referring_url_, target_url_); |
| 1356 | 1359 |
| 1357 // In order to reduce the expected connection count < .8, issue predictions 3 | 1360 // In order to reduce the expected connection count < .8, issue predictions 3 |
| 1358 // times. 2 * .66^3 ~= .58. | 1361 // times. 2 * .66^3 ~= .58. |
| 1359 PrepareFrameSubresources(referring_url_); | 1362 PrepareFrameSubresources(referring_url_); |
| 1360 PrepareFrameSubresources(referring_url_); | 1363 PrepareFrameSubresources(referring_url_); |
| 1361 PrepareFrameSubresources(referring_url_); | 1364 PrepareFrameSubresources(referring_url_); |
| 1362 } | 1365 } |
| 1363 | 1366 |
| 1364 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, ShutdownStartupCyclePreresolve) { | 1367 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, ShutdownStartupCyclePreresolve) { |
| 1365 // Make sure that the Preferences file is actually wiped of all DNS prefetch | 1368 // Make sure this data has been loaded into the Predictor, by inspecting that |
| 1366 // related data after start-up. | 1369 // the Predictor starts making the expected hostname requests. |
| 1367 std::string cleared_startup_list; | |
| 1368 std::string cleared_referral_list; | |
| 1369 GetListFromPrefsAsString(prefs::kDnsPrefetchingStartupList, | |
| 1370 &cleared_startup_list); | |
| 1371 GetListFromPrefsAsString(prefs::kDnsPrefetchingHostReferralList, | |
| 1372 &cleared_referral_list); | |
| 1373 | |
| 1374 EXPECT_THAT(cleared_startup_list, Not(HasSubstr(startup_url_.host()))); | |
| 1375 EXPECT_THAT(cleared_referral_list, Not(HasSubstr(referring_url_.host()))); | |
| 1376 EXPECT_THAT(cleared_referral_list, Not(HasSubstr(target_url_.host()))); | |
| 1377 | |
| 1378 // But also make sure this data has been first loaded into the Predictor, by | |
| 1379 // inspecting that the Predictor starts making the expected hostname requests. | |
| 1380 PrepareFrameSubresources(referring_url_); | 1370 PrepareFrameSubresources(referring_url_); |
| 1381 observer()->WaitUntilHostLookedUp(target_url_); | 1371 observer()->WaitUntilHostLookedUp(target_url_); |
| 1382 | 1372 |
| 1383 // Verify that both urls were requested by the predictor. Note that the | 1373 // Verify that both urls were requested by the predictor. Note that the |
| 1384 // startup URL may be requested before the observer attaches itself. | 1374 // startup URL may be requested before the observer attaches itself. |
| 1385 ExpectUrlRequestedFromPredictor(startup_url_); | 1375 ExpectUrlRequestedFromPredictor(startup_url_); |
| 1386 EXPECT_FALSE(observer()->HostFound(target_url_)); | 1376 EXPECT_FALSE(observer()->HostFound(target_url_)); |
| 1387 } | 1377 } |
| 1388 | 1378 |
| 1379 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PRE_ClearData) { |
| 1380 // The target url will have a expected connection count of 2 after this call. |
| 1381 InstallPredictorObserver(referring_url_, target_url_); |
| 1382 LearnFromNavigation(referring_url_, target_url_); |
| 1383 } |
| 1384 |
| 1385 // Ensure predictive data is cleared when the history is cleared. |
| 1386 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, ClearData) { |
| 1387 std::string cleared_startup_list; |
| 1388 std::string cleared_referral_list; |
| 1389 |
| 1390 // The pref should persist after startup. |
| 1391 GetListFromPrefsAsString(prefs::kDnsPrefetchingStartupList, |
| 1392 &cleared_startup_list); |
| 1393 GetListFromPrefsAsString(prefs::kDnsPrefetchingHostReferralList, |
| 1394 &cleared_referral_list); |
| 1395 EXPECT_THAT(cleared_referral_list, HasSubstr(referring_url_.host())); |
| 1396 EXPECT_THAT(cleared_referral_list, HasSubstr(target_url_.host())); |
| 1397 |
| 1398 // Clear cache which should clear all prefs. |
| 1399 BrowsingDataRemover* remover = |
| 1400 BrowsingDataRemoverFactory::GetForBrowserContext(browser()->profile()); |
| 1401 remover->Remove(BrowsingDataRemover::Unbounded(), |
| 1402 BrowsingDataRemover::REMOVE_HISTORY, |
| 1403 BrowsingDataHelper::UNPROTECTED_WEB); |
| 1404 |
| 1405 GetListFromPrefsAsString(prefs::kDnsPrefetchingStartupList, |
| 1406 &cleared_startup_list); |
| 1407 GetListFromPrefsAsString(prefs::kDnsPrefetchingHostReferralList, |
| 1408 &cleared_referral_list); |
| 1409 EXPECT_THAT(cleared_referral_list, Not(HasSubstr(referring_url_.host()))); |
| 1410 EXPECT_THAT(cleared_referral_list, Not(HasSubstr(target_url_.host()))); |
| 1411 } |
| 1412 |
| 1389 // The predictor should not evict recently used (navigated to) referrers. | 1413 // The predictor should not evict recently used (navigated to) referrers. |
| 1390 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DoNotEvictRecentlyUsed) { | 1414 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DoNotEvictRecentlyUsed) { |
| 1391 observer()->SetStrict(false); | 1415 observer()->SetStrict(false); |
| 1392 for (int i = 0; i < Predictor::kMaxReferrers; ++i) { | 1416 for (int i = 0; i < Predictor::kMaxReferrers; ++i) { |
| 1393 LearnFromNavigation( | 1417 LearnFromNavigation( |
| 1394 GURL(base::StringPrintf("http://www.source%d.test", i)), | 1418 GURL(base::StringPrintf("http://www.source%d.test", i)), |
| 1395 GURL(base::StringPrintf("http://www.target%d.test", i))); | 1419 GURL(base::StringPrintf("http://www.target%d.test", i))); |
| 1396 } | 1420 } |
| 1397 ui_test_utils::NavigateToURL(browser(), GURL("http://source0.test")); | 1421 ui_test_utils::NavigateToURL(browser(), GURL("http://source0.test")); |
| 1398 | 1422 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1528 // Second navigation to content with an img. | 1552 // Second navigation to content with an img. |
| 1529 std::string img_content = | 1553 std::string img_content = |
| 1530 "<img src=\"" + preconnect_url.spec() + "test.gif\">"; | 1554 "<img src=\"" + preconnect_url.spec() + "test.gif\">"; |
| 1531 NavigateToDataURLWithContent(img_content); | 1555 NavigateToDataURLWithContent(img_content); |
| 1532 connection_listener_->WaitUntilFirstConnectionRead(); | 1556 connection_listener_->WaitUntilFirstConnectionRead(); |
| 1533 EXPECT_EQ(2u, connection_listener_->GetAcceptedSocketCount()); | 1557 EXPECT_EQ(2u, connection_listener_->GetAcceptedSocketCount()); |
| 1534 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); | 1558 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); |
| 1535 } | 1559 } |
| 1536 | 1560 |
| 1537 } // namespace chrome_browser_net | 1561 } // namespace chrome_browser_net |
| OLD | NEW |