1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
|
// Copyright (c) 2012 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/prerender/prerender_manager.h"
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/net/chrome_cookie_notification_details.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/predictors/predictor_database.h"
#include "chrome/browser/predictors/predictor_database_factory.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_field_trial.h"
#include "chrome/browser/prerender/prerender_final_status.h"
#include "chrome/browser/prerender/prerender_handle.h"
#include "chrome/browser/prerender/prerender_histograms.h"
#include "chrome/browser/prerender/prerender_history.h"
#include "chrome/browser/prerender/prerender_local_predictor.h"
#include "chrome/browser/prerender/prerender_manager_factory.h"
#include "chrome/browser/prerender/prerender_tab_helper.h"
#include "chrome/browser/prerender/prerender_tracker.h"
#include "chrome/browser/prerender/prerender_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/prerender_messages.h"
#include "chrome/common/prerender_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_request_details.h"
#include "content/public/browser/session_storage_namespace.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
using content::BrowserThread;
using content::RenderViewHost;
using content::RenderFrameHost;
using content::SessionStorageNamespace;
using content::WebContents;
using predictors::LoggedInPredictorTable;
namespace prerender {
namespace {
// Time interval at which periodic cleanups are performed.
const int kPeriodicCleanupIntervalMs = 1000;
// Valid HTTP methods for prerendering.
const char* const kValidHttpMethods[] = {
"GET",
"HEAD",
"OPTIONS",
"POST",
"TRACE",
};
// Length of prerender history, for display in chrome://net-internals
const int kHistoryLength = 100;
// Timeout, in ms, for a session storage namespace merge.
const int kSessionStorageNamespaceMergeTimeoutMs = 500;
// If true, all session storage merges hang indefinitely.
bool g_hang_session_storage_merges_for_testing = false;
// Indicates whether a Prerender has been cancelled such that we need
// a dummy replacement for the purpose of recording the correct PPLT for
// the Match Complete case.
// Traditionally, "Match" means that a prerendered page was actually visited &
// the prerender was used. Our goal is to have "Match" cases line up in the
// control group & the experiment group, so that we can make meaningful
// comparisons of improvements. However, in the control group, since we don't
// actually perform prerenders, many of the cancellation reasons cannot be
// detected. Therefore, in the Prerender group, when we cancel for one of these
// reasons, we keep track of a dummy Prerender representing what we would
// have in the control group. If that dummy prerender in the prerender group
// would then be swapped in (but isn't actually b/c it's a dummy), we record
// this as a MatchComplete. This allows us to compare MatchComplete's
// across Prerender & Control group which ideally should be lining up.
// This ensures that there is no bias in terms of the page load times
// of the pages forming the difference between the two sets.
bool NeedMatchCompleteDummyForFinalStatus(FinalStatus final_status) {
return final_status != FINAL_STATUS_USED &&
final_status != FINAL_STATUS_TIMED_OUT &&
final_status != FINAL_STATUS_MANAGER_SHUTDOWN &&
final_status != FINAL_STATUS_PROFILE_DESTROYED &&
final_status != FINAL_STATUS_APP_TERMINATING &&
final_status != FINAL_STATUS_WINDOW_OPENER &&
final_status != FINAL_STATUS_CACHE_OR_HISTORY_CLEARED &&
final_status != FINAL_STATUS_CANCELLED &&
final_status != FINAL_STATUS_DEVTOOLS_ATTACHED &&
final_status != FINAL_STATUS_CROSS_SITE_NAVIGATION_PENDING &&
final_status != FINAL_STATUS_PAGE_BEING_CAPTURED &&
final_status != FINAL_STATUS_NAVIGATION_UNCOMMITTED &&
final_status != FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE;
}
void CheckIfCookiesExistForDomainResultOnUIThread(
const net::CookieMonster::HasCookiesForETLDP1Callback& callback,
bool cookies_exist) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
callback.Run(cookies_exist);
}
void CheckIfCookiesExistForDomainResultOnIOThread(
const net::CookieMonster::HasCookiesForETLDP1Callback& callback,
bool cookies_exist) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&CheckIfCookiesExistForDomainResultOnUIThread,
callback,
cookies_exist));
}
void CheckIfCookiesExistForDomainOnIOThread(
net::URLRequestContextGetter* rq_context,
const std::string& domain_key,
const net::CookieMonster::HasCookiesForETLDP1Callback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
net::CookieStore* cookie_store =
rq_context->GetURLRequestContext()->cookie_store();
cookie_store->GetCookieMonster()->HasCookiesForETLDP1Async(
domain_key,
base::Bind(&CheckIfCookiesExistForDomainResultOnIOThread, callback));
}
} // namespace
class PrerenderManager::OnCloseWebContentsDeleter
: public content::WebContentsDelegate,
public base::SupportsWeakPtr<
PrerenderManager::OnCloseWebContentsDeleter> {
public:
OnCloseWebContentsDeleter(PrerenderManager* manager,
WebContents* tab)
: manager_(manager),
tab_(tab),
suppressed_dialog_(false) {
tab_->SetDelegate(this);
base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
base::Bind(&OnCloseWebContentsDeleter::ScheduleWebContentsForDeletion,
AsWeakPtr(), true),
base::TimeDelta::FromSeconds(kDeleteWithExtremePrejudiceSeconds));
}
void CloseContents(WebContents* source) override {
DCHECK_EQ(tab_, source);
ScheduleWebContentsForDeletion(false);
}
void SwappedOut(WebContents* source) override {
DCHECK_EQ(tab_, source);
ScheduleWebContentsForDeletion(false);
}
bool ShouldSuppressDialogs(WebContents* source) override {
// Use this as a proxy for getting statistics on how often we fail to honor
// the beforeunload event.
DCHECK_EQ(tab_, source);
suppressed_dialog_ = true;
return true;
}
private:
static const int kDeleteWithExtremePrejudiceSeconds = 3;
void ScheduleWebContentsForDeletion(bool timeout) {
UMA_HISTOGRAM_BOOLEAN("Prerender.TabContentsDeleterTimeout", timeout);
UMA_HISTOGRAM_BOOLEAN("Prerender.TabContentsDeleterSuppressedDialog",
suppressed_dialog_);
tab_->SetDelegate(NULL);
manager_->ScheduleDeleteOldWebContents(tab_.release(), this);
// |this| is deleted at this point.
}
PrerenderManager* manager_;
scoped_ptr<WebContents> tab_;
bool suppressed_dialog_;
DISALLOW_COPY_AND_ASSIGN(OnCloseWebContentsDeleter);
};
// static
int PrerenderManager::prerenders_per_session_count_ = 0;
// static
PrerenderManager::PrerenderManagerMode PrerenderManager::mode_ =
PRERENDER_MODE_ENABLED;
struct PrerenderManager::NavigationRecord {
NavigationRecord(const GURL& url, base::TimeTicks time)
: url(url),
time(time) {
}
GURL url;
base::TimeTicks time;
};
PrerenderManager::PrerenderManager(Profile* profile,
PrerenderTracker* prerender_tracker)
: profile_(profile),
prerender_tracker_(prerender_tracker),
prerender_contents_factory_(PrerenderContents::CreateFactory()),
last_prerender_start_time_(GetCurrentTimeTicks() -
base::TimeDelta::FromMilliseconds(kMinTimeBetweenPrerendersMs)),
prerender_history_(new PrerenderHistory(kHistoryLength)),
histograms_(new PrerenderHistograms()),
profile_network_bytes_(0),
last_recorded_profile_network_bytes_(0),
cookie_store_loaded_(false) {
// There are some assumptions that the PrerenderManager is on the UI thread.
// Any other checks simply make sure that the PrerenderManager is accessed on
// the same thread that it was created on.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (IsLocalPredictorEnabled())
local_predictor_.reset(new PrerenderLocalPredictor(this));
if (IsLoggedInPredictorEnabled() && !profile_->IsOffTheRecord()) {
predictors::PredictorDatabase* predictor_db =
predictors::PredictorDatabaseFactory::GetForProfile(profile);
if (predictor_db) {
logged_in_predictor_table_ = predictor_db->logged_in_table();
scoped_ptr<LoggedInStateMap> new_state_map(new LoggedInStateMap);
LoggedInStateMap* new_state_map_ptr = new_state_map.get();
BrowserThread::PostTaskAndReply(
BrowserThread::DB, FROM_HERE,
base::Bind(&LoggedInPredictorTable::GetAllData,
logged_in_predictor_table_,
new_state_map_ptr),
base::Bind(&PrerenderManager::LoggedInPredictorDataReceived,
AsWeakPtr(),
base::Passed(&new_state_map)));
}
}
// Certain experiments override our default config_ values.
switch (PrerenderManager::GetMode()) {
case PrerenderManager::PRERENDER_MODE_EXPERIMENT_MULTI_PRERENDER_GROUP:
config_.max_link_concurrency = 4;
config_.max_link_concurrency_per_launcher = 2;
break;
case PrerenderManager::PRERENDER_MODE_EXPERIMENT_15MIN_TTL_GROUP:
config_.time_to_live = base::TimeDelta::FromMinutes(15);
break;
default:
break;
}
notification_registrar_.Add(
this, chrome::NOTIFICATION_COOKIE_CHANGED,
content::NotificationService::AllBrowserContextsAndSources());
notification_registrar_.Add(
this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(profile_));
MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
}
PrerenderManager::~PrerenderManager() {
MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
// The earlier call to KeyedService::Shutdown() should have
// emptied these vectors already.
DCHECK(active_prerenders_.empty());
DCHECK(to_delete_prerenders_.empty());
for (PrerenderProcessSet::const_iterator it =
prerender_process_hosts_.begin();
it != prerender_process_hosts_.end();
++it) {
(*it)->RemoveObserver(this);
}
}
void PrerenderManager::Shutdown() {
DestroyAllContents(FINAL_STATUS_MANAGER_SHUTDOWN);
on_close_web_contents_deleters_.clear();
// Must happen before |profile_| is set to NULL as
// |local_predictor_| accesses it.
if (local_predictor_)
local_predictor_->Shutdown();
profile_ = NULL;
DCHECK(active_prerenders_.empty());
}
PrerenderHandle* PrerenderManager::AddPrerenderFromLinkRelPrerender(
int process_id,
int route_id,
const GURL& url,
const uint32 rel_types,
const content::Referrer& referrer,
const gfx::Size& size) {
Origin origin = rel_types & PrerenderRelTypePrerender ?
ORIGIN_LINK_REL_PRERENDER_CROSSDOMAIN :
ORIGIN_LINK_REL_NEXT;
SessionStorageNamespace* session_storage_namespace = NULL;
// Unit tests pass in a process_id == -1.
if (process_id != -1) {
RenderViewHost* source_render_view_host =
RenderViewHost::FromID(process_id, route_id);
if (!source_render_view_host)
return NULL;
WebContents* source_web_contents =
WebContents::FromRenderViewHost(source_render_view_host);
if (!source_web_contents)
return NULL;
if (origin == ORIGIN_LINK_REL_PRERENDER_CROSSDOMAIN &&
source_web_contents->GetURL().host() == url.host()) {
origin = ORIGIN_LINK_REL_PRERENDER_SAMEDOMAIN;
}
// TODO(ajwong): This does not correctly handle storage for isolated apps.
session_storage_namespace =
source_web_contents->GetController()
.GetDefaultSessionStorageNamespace();
}
return AddPrerender(origin, url, referrer, size, session_storage_namespace);
}
PrerenderHandle* PrerenderManager::AddPrerenderFromOmnibox(
const GURL& url,
SessionStorageNamespace* session_storage_namespace,
const gfx::Size& size) {
if (!IsOmniboxEnabled(profile_))
return NULL;
return AddPrerender(ORIGIN_OMNIBOX, url, content::Referrer(), size,
session_storage_namespace);
}
PrerenderHandle* PrerenderManager::AddPrerenderFromLocalPredictor(
const GURL& url,
SessionStorageNamespace* session_storage_namespace,
const gfx::Size& size) {
return AddPrerender(ORIGIN_LOCAL_PREDICTOR, url, content::Referrer(),
size, session_storage_namespace);
}
PrerenderHandle* PrerenderManager::AddPrerenderFromExternalRequest(
const GURL& url,
const content::Referrer& referrer,
SessionStorageNamespace* session_storage_namespace,
const gfx::Size& size) {
return AddPrerender(ORIGIN_EXTERNAL_REQUEST, url, referrer, size,
session_storage_namespace);
}
PrerenderHandle* PrerenderManager::AddPrerenderForInstant(
const GURL& url,
content::SessionStorageNamespace* session_storage_namespace,
const gfx::Size& size) {
DCHECK(chrome::ShouldPrefetchSearchResults());
return AddPrerender(ORIGIN_INSTANT, url, content::Referrer(), size,
session_storage_namespace);
}
void PrerenderManager::CancelAllPrerenders() {
DCHECK(CalledOnValidThread());
while (!active_prerenders_.empty()) {
PrerenderContents* prerender_contents =
active_prerenders_.front()->contents();
prerender_contents->Destroy(FINAL_STATUS_CANCELLED);
}
}
bool PrerenderManager::MaybeUsePrerenderedPage(const GURL& url,
chrome::NavigateParams* params) {
DCHECK(CalledOnValidThread());
content::WebContents* web_contents = params->target_contents;
DCHECK(!IsWebContentsPrerendering(web_contents, NULL));
// Don't prerender if the navigation involves some special parameters.
if (params->uses_post || !params->extra_headers.empty())
return false;
DeleteOldEntries();
to_delete_prerenders_.clear();
// First, try to find prerender data with the correct session storage
// namespace.
// TODO(ajwong): This doesn't handle isolated apps correctly.
PrerenderData* prerender_data = FindPrerenderData(
url,
web_contents->GetController().GetDefaultSessionStorageNamespace());
// If this failed, we may still find a prerender for the same URL, but a
// different session storage namespace. If we do, we might have to perform
// a merge.
if (!prerender_data) {
prerender_data = FindPrerenderData(url, NULL);
} else {
RecordEvent(prerender_data->contents(),
PRERENDER_EVENT_SWAPIN_CANDIDATE_NAMESPACE_MATCHES);
}
if (!prerender_data)
return false;
RecordEvent(prerender_data->contents(), PRERENDER_EVENT_SWAPIN_CANDIDATE);
DCHECK(prerender_data->contents());
// If there is currently a merge pending for this prerender data, don't swap.
if (prerender_data->pending_swap())
return false;
// Abort any existing pending swap on the target contents.
PrerenderData* pending_swap =
FindPrerenderDataForTargetContents(web_contents);
if (pending_swap) {
pending_swap->ClearPendingSwap();
DCHECK(FindPrerenderDataForTargetContents(web_contents) == NULL);
}
RecordEvent(prerender_data->contents(),
PRERENDER_EVENT_SWAPIN_NO_MERGE_PENDING);
SessionStorageNamespace* target_namespace =
web_contents->GetController().GetDefaultSessionStorageNamespace();
SessionStorageNamespace* prerender_namespace =
prerender_data->contents()->GetSessionStorageNamespace();
// Only when actually prerendering is session storage namespace merging an
// issue. For the control group, it will be assumed that the merge succeeded.
if (prerender_namespace && prerender_namespace != target_namespace &&
!prerender_namespace->IsAliasOf(target_namespace)) {
if (!ShouldMergeSessionStorageNamespaces()) {
RecordEvent(prerender_data->contents(),
PRERENDER_EVENT_SWAPIN_MERGING_DISABLED);
return false;
}
RecordEvent(prerender_data->contents(),
PRERENDER_EVENT_SWAPIN_ISSUING_MERGE);
prerender_data->set_pending_swap(new PendingSwap(
this, web_contents, prerender_data, url,
params->should_replace_current_entry));
prerender_data->pending_swap()->BeginSwap();
// Although this returns false, creating a PendingSwap registers with
// PrerenderTracker to throttle MAIN_FRAME navigations while the swap is
// pending.
return false;
}
// No need to merge; swap synchronously.
WebContents* new_web_contents = SwapInternal(
url, web_contents, prerender_data,
params->should_replace_current_entry);
if (!new_web_contents)
return false;
// Record the new target_contents for the callers.
params->target_contents = new_web_contents;
return true;
}
WebContents* PrerenderManager::SwapInternal(
const GURL& url,
WebContents* web_contents,
PrerenderData* prerender_data,
bool should_replace_current_entry) {
DCHECK(CalledOnValidThread());
DCHECK(!IsWebContentsPrerendering(web_contents, NULL));
// Only swap if the target WebContents has a CoreTabHelper delegate to swap
// out of it. For a normal WebContents, this is if it is in a TabStripModel.
CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents(web_contents);
if (!core_tab_helper || !core_tab_helper->delegate()) {
RecordEvent(prerender_data->contents(), PRERENDER_EVENT_SWAPIN_NO_DELEGATE);
return NULL;
}
PrerenderTabHelper* target_tab_helper =
PrerenderTabHelper::FromWebContents(web_contents);
if (!target_tab_helper) {
NOTREACHED();
return NULL;
}
if (IsNoSwapInExperiment(prerender_data->contents()->experiment_id()))
return NULL;
if (WebContents* new_web_contents =
prerender_data->contents()->prerender_contents()) {
if (web_contents == new_web_contents)
return NULL; // Do not swap in to ourself.
// We cannot swap in if there is no last committed entry, because we would
// show a blank page under an existing entry from the current tab. Even if
// there is a pending entry, it may not commit.
// TODO(creis): If there is a pending navigation and no last committed
// entry, we might be able to transfer the network request instead.
if (!new_web_contents->GetController().CanPruneAllButLastCommitted()) {
// Abort this prerender so it is not used later. http://crbug.com/292121
prerender_data->contents()->Destroy(FINAL_STATUS_NAVIGATION_UNCOMMITTED);
return NULL;
}
}
// Do not swap if the target WebContents is not the only WebContents in its
// current BrowsingInstance.
if (web_contents->GetSiteInstance()->GetRelatedActiveContentsCount() != 1u) {
DCHECK_GT(
web_contents->GetSiteInstance()->GetRelatedActiveContentsCount(), 1u);
prerender_data->contents()->Destroy(
FINAL_STATUS_NON_EMPTY_BROWSING_INSTANCE);
return NULL;
}
// Do not use the prerendered version if there is an opener object.
if (web_contents->HasOpener()) {
prerender_data->contents()->Destroy(FINAL_STATUS_WINDOW_OPENER);
return NULL;
}
// Do not swap in the prerender if the current WebContents is being captured.
if (web_contents->GetCapturerCount() > 0) {
prerender_data->contents()->Destroy(FINAL_STATUS_PAGE_BEING_CAPTURED);
return NULL;
}
// If we are just in the control group (which can be detected by noticing
// that prerendering hasn't even started yet), record that |web_contents| now
// would be showing a prerendered contents, but otherwise, don't do anything.
if (!prerender_data->contents()->prerendering_has_started()) {
target_tab_helper->WouldHavePrerenderedNextLoad(
prerender_data->contents()->origin());
prerender_data->contents()->Destroy(FINAL_STATUS_WOULD_HAVE_BEEN_USED);
return NULL;
}
// Don't use prerendered pages if debugger is attached to the tab.
// See http://crbug.com/98541
if (content::DevToolsAgentHost::IsDebuggerAttached(web_contents)) {
DestroyAndMarkMatchCompleteAsUsed(prerender_data->contents(),
FINAL_STATUS_DEVTOOLS_ATTACHED);
return NULL;
}
// If the prerendered page is in the middle of a cross-site navigation,
// don't swap it in because there isn't a good way to merge histories.
if (prerender_data->contents()->IsCrossSiteNavigationPending()) {
DestroyAndMarkMatchCompleteAsUsed(
prerender_data->contents(),
FINAL_STATUS_CROSS_SITE_NAVIGATION_PENDING);
return NULL;
}
// For bookkeeping purposes, we need to mark this WebContents to
// reflect that it would have been prerendered.
if (GetMode() == PRERENDER_MODE_EXPERIMENT_NO_USE_GROUP) {
target_tab_helper->WouldHavePrerenderedNextLoad(
prerender_data->contents()->origin());
prerender_data->contents()->Destroy(FINAL_STATUS_WOULD_HAVE_BEEN_USED);
return NULL;
}
// At this point, we've determined that we will use the prerender.
content::RenderProcessHost* process_host =
prerender_data->contents()->GetRenderViewHost()->GetProcess();
prerender_process_hosts_.erase(process_host);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&PrerenderTracker::RemovePrerenderCookieStoreOnIOThread,
base::Unretained(prerender_tracker()), process_host->GetID(),
true));
if (!prerender_data->contents()->load_start_time().is_null()) {
histograms_->RecordTimeUntilUsed(
prerender_data->contents()->origin(),
GetCurrentTimeTicks() - prerender_data->contents()->load_start_time());
}
histograms_->RecordAbandonTimeUntilUsed(
prerender_data->contents()->origin(),
prerender_data->abandon_time().is_null() ?
base::TimeDelta() :
GetCurrentTimeTicks() - prerender_data->abandon_time());
histograms_->RecordPerSessionCount(prerender_data->contents()->origin(),
++prerenders_per_session_count_);
histograms_->RecordUsedPrerender(prerender_data->contents()->origin());
if (prerender_data->pending_swap())
prerender_data->pending_swap()->set_swap_successful(true);
ScopedVector<PrerenderData>::iterator to_erase =
FindIteratorForPrerenderContents(prerender_data->contents());
DCHECK(active_prerenders_.end() != to_erase);
DCHECK_EQ(prerender_data, *to_erase);
scoped_ptr<PrerenderContents>
prerender_contents(prerender_data->ReleaseContents());
active_prerenders_.erase(to_erase);
// Mark prerender as used.
prerender_contents->PrepareForUse();
WebContents* new_web_contents =
prerender_contents->ReleasePrerenderContents();
WebContents* old_web_contents = web_contents;
DCHECK(new_web_contents);
DCHECK(old_web_contents);
// Merge the browsing history.
new_web_contents->GetController().CopyStateFromAndPrune(
&old_web_contents->GetController(),
should_replace_current_entry);
CoreTabHelper::FromWebContents(old_web_contents)->delegate()->
SwapTabContents(old_web_contents,
new_web_contents,
true,
prerender_contents->has_finished_loading());
prerender_contents->CommitHistory(new_web_contents);
// Update PPLT metrics:
// If the tab has finished loading, record a PPLT of 0.
// If the tab is still loading, reset its start time to the current time.
PrerenderTabHelper* prerender_tab_helper =
PrerenderTabHelper::FromWebContents(new_web_contents);
DCHECK(prerender_tab_helper != NULL);
prerender_tab_helper->PrerenderSwappedIn();
if (old_web_contents->NeedToFireBeforeUnload()) {
// Schedule the delete to occur after the tab has run its unload handlers.
// TODO(davidben): Honor the beforeunload event. http://crbug.com/304932
on_close_web_contents_deleters_.push_back(
new OnCloseWebContentsDeleter(this, old_web_contents));
old_web_contents->DispatchBeforeUnload(false);
} else {
// No unload handler to run, so delete asap.
ScheduleDeleteOldWebContents(old_web_contents, NULL);
}
// TODO(cbentzel): Should prerender_contents move to the pending delete
// list, instead of deleting directly here?
AddToHistory(prerender_contents.get());
RecordNavigation(url);
return new_web_contents;
}
void PrerenderManager::MoveEntryToPendingDelete(PrerenderContents* entry,
FinalStatus final_status) {
DCHECK(CalledOnValidThread());
DCHECK(entry);
ScopedVector<PrerenderData>::iterator it =
FindIteratorForPrerenderContents(entry);
DCHECK(it != active_prerenders_.end());
// If this PrerenderContents is being deleted due to a cancellation any time
// after the prerender has started then we need to create a dummy replacement
// for PPLT accounting purposes for the Match Complete group. This is the case
// if the cancellation is for any reason that would not occur in the control
// group case.
if (entry->prerendering_has_started() &&
entry->match_complete_status() ==
PrerenderContents::MATCH_COMPLETE_DEFAULT &&
NeedMatchCompleteDummyForFinalStatus(final_status) &&
ActuallyPrerendering() &&
GetMode() == PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP) {
// TODO(tburkard): I'd like to DCHECK that we are actually prerendering.
// However, what if new conditions are added and
// NeedMatchCompleteDummyForFinalStatus is not being updated. Not sure
// what's the best thing to do here. For now, I will just check whether
// we are actually prerendering.
(*it)->MakeIntoMatchCompleteReplacement();
} else {
to_delete_prerenders_.push_back(*it);
(*it)->ClearPendingSwap();
active_prerenders_.weak_erase(it);
}
// Destroy the old WebContents relatively promptly to reduce resource usage.
PostCleanupTask();
}
void PrerenderManager::RecordPageLoadTimeNotSwappedIn(
Origin origin,
base::TimeDelta page_load_time,
const GURL& url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
histograms_->RecordPageLoadTimeNotSwappedIn(origin, page_load_time, url);
}
void PrerenderManager::RecordPerceivedPageLoadTime(
Origin origin,
NavigationType navigation_type,
base::TimeDelta perceived_page_load_time,
double fraction_plt_elapsed_at_swap_in,
const GURL& url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!IsEnabled())
return;
histograms_->RecordPerceivedPageLoadTime(
origin, perceived_page_load_time, navigation_type, url);
if (navigation_type == NAVIGATION_TYPE_PRERENDERED) {
histograms_->RecordPercentLoadDoneAtSwapin(
origin, fraction_plt_elapsed_at_swap_in);
}
if (local_predictor_) {
local_predictor_->OnPLTEventForURL(url, perceived_page_load_time);
}
}
// static
PrerenderManager::PrerenderManagerMode PrerenderManager::GetMode() {
return mode_;
}
// static
void PrerenderManager::SetMode(PrerenderManagerMode mode) {
mode_ = mode;
}
// static
const char* PrerenderManager::GetModeString() {
switch (mode_) {
case PRERENDER_MODE_DISABLED:
return "_Disabled";
case PRERENDER_MODE_ENABLED:
case PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP:
return "_Enabled";
case PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP:
return "_Control";
case PRERENDER_MODE_EXPERIMENT_MULTI_PRERENDER_GROUP:
return "_Multi";
case PRERENDER_MODE_EXPERIMENT_15MIN_TTL_GROUP:
return "_15MinTTL";
case PRERENDER_MODE_EXPERIMENT_NO_USE_GROUP:
return "_NoUse";
case PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP:
return "_MatchComplete";
case PRERENDER_MODE_MAX:
default:
NOTREACHED() << "Invalid PrerenderManager mode.";
break;
}
return "";
}
// static
bool PrerenderManager::IsPrerenderingPossible() {
return GetMode() != PRERENDER_MODE_DISABLED;
}
// static
bool PrerenderManager::ActuallyPrerendering() {
return IsPrerenderingPossible() && !IsControlGroup(kNoExperiment);
}
// static
bool PrerenderManager::IsControlGroup(uint8 experiment_id) {
return GetMode() == PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP ||
IsControlGroupExperiment(experiment_id);
}
// static
bool PrerenderManager::IsNoUseGroup() {
return GetMode() == PRERENDER_MODE_EXPERIMENT_NO_USE_GROUP;
}
bool PrerenderManager::IsWebContentsPrerendering(
const WebContents* web_contents,
Origin* origin) const {
DCHECK(CalledOnValidThread());
if (PrerenderContents* prerender_contents =
GetPrerenderContents(web_contents)) {
if (origin)
*origin = prerender_contents->origin();
return true;
}
return false;
}
bool PrerenderManager::HasPrerenderedUrl(
GURL url,
content::WebContents* web_contents) const {
content::SessionStorageNamespace* session_storage_namespace = web_contents->
GetController().GetDefaultSessionStorageNamespace();
for (ScopedVector<PrerenderData>::const_iterator it =
active_prerenders_.begin();
it != active_prerenders_.end(); ++it) {
PrerenderContents* prerender_contents = (*it)->contents();
if (prerender_contents->Matches(url, session_storage_namespace)) {
return true;
}
}
return false;
}
PrerenderContents* PrerenderManager::GetPrerenderContents(
const content::WebContents* web_contents) const {
DCHECK(CalledOnValidThread());
for (ScopedVector<PrerenderData>::const_iterator it =
active_prerenders_.begin();
it != active_prerenders_.end(); ++it) {
WebContents* prerender_web_contents =
(*it)->contents()->prerender_contents();
if (prerender_web_contents == web_contents) {
return (*it)->contents();
}
}
// Also check the pending-deletion list. If the prerender is in pending
// delete, anyone with a handle on the WebContents needs to know.
for (ScopedVector<PrerenderData>::const_iterator it =
to_delete_prerenders_.begin();
it != to_delete_prerenders_.end(); ++it) {
WebContents* prerender_web_contents =
(*it)->contents()->prerender_contents();
if (prerender_web_contents == web_contents) {
return (*it)->contents();
}
}
return NULL;
}
PrerenderContents* PrerenderManager::GetPrerenderContentsForRoute(
int child_id,
int route_id) const {
content::WebContents* web_contents =
tab_util::GetWebContentsByID(child_id, route_id);
if (web_contents == NULL)
return NULL;
return GetPrerenderContents(web_contents);
}
const std::vector<WebContents*>
PrerenderManager::GetAllPrerenderingContents() const {
DCHECK(CalledOnValidThread());
std::vector<WebContents*> result;
for (ScopedVector<PrerenderData>::const_iterator it =
active_prerenders_.begin();
it != active_prerenders_.end(); ++it) {
if (WebContents* contents = (*it)->contents()->prerender_contents())
result.push_back(contents);
}
return result;
}
bool PrerenderManager::HasRecentlyBeenNavigatedTo(Origin origin,
const GURL& url) {
DCHECK(CalledOnValidThread());
CleanUpOldNavigations();
std::list<NavigationRecord>::const_reverse_iterator end = navigations_.rend();
for (std::list<NavigationRecord>::const_reverse_iterator it =
navigations_.rbegin();
it != end;
++it) {
if (it->url == url) {
base::TimeDelta delta = GetCurrentTimeTicks() - it->time;
histograms_->RecordTimeSinceLastRecentVisit(origin, delta);
return true;
}
}
return false;
}
// static
bool PrerenderManager::IsValidHttpMethod(const std::string& method) {
// method has been canonicalized to upper case at this point so we can just
// compare them.
DCHECK_EQ(method, StringToUpperASCII(method));
for (size_t i = 0; i < arraysize(kValidHttpMethods); ++i) {
if (method.compare(kValidHttpMethods[i]) == 0)
return true;
}
return false;
}
// static
bool PrerenderManager::DoesURLHaveValidScheme(const GURL& url) {
return (url.SchemeIsHTTPOrHTTPS() ||
url.SchemeIs(extensions::kExtensionScheme) ||
url.SchemeIs("data"));
}
// static
bool PrerenderManager::DoesSubresourceURLHaveValidScheme(const GURL& url) {
return DoesURLHaveValidScheme(url) || url == GURL(url::kAboutBlankURL);
}
base::DictionaryValue* PrerenderManager::GetAsValue() const {
DCHECK(CalledOnValidThread());
base::DictionaryValue* dict_value = new base::DictionaryValue();
dict_value->Set("history", prerender_history_->GetEntriesAsValue());
dict_value->Set("active", GetActivePrerendersAsValue());
dict_value->SetBoolean("enabled", IsEnabled());
dict_value->SetBoolean("omnibox_enabled", IsOmniboxEnabled(profile_));
// If prerender is disabled via a flag this method is not even called.
std::string enabled_note;
if (IsControlGroup(kNoExperiment))
enabled_note += "(Control group: Not actually prerendering) ";
if (IsNoUseGroup())
enabled_note += "(No-use group: Not swapping in prerendered pages) ";
if (GetMode() == PRERENDER_MODE_EXPERIMENT_15MIN_TTL_GROUP)
enabled_note +=
"(15 min TTL group: Extended prerender eviction to 15 mins) ";
dict_value->SetString("enabled_note", enabled_note);
return dict_value;
}
void PrerenderManager::ClearData(int clear_flags) {
DCHECK_GE(clear_flags, 0);
DCHECK_LT(clear_flags, CLEAR_MAX);
if (clear_flags & CLEAR_PRERENDER_CONTENTS)
DestroyAllContents(FINAL_STATUS_CACHE_OR_HISTORY_CLEARED);
// This has to be second, since destroying prerenders can add to the history.
if (clear_flags & CLEAR_PRERENDER_HISTORY)
prerender_history_->Clear();
}
void PrerenderManager::RecordFinalStatusWithMatchCompleteStatus(
Origin origin,
uint8 experiment_id,
PrerenderContents::MatchCompleteStatus mc_status,
FinalStatus final_status) const {
histograms_->RecordFinalStatus(origin,
experiment_id,
mc_status,
final_status);
}
void PrerenderManager::RecordNavigation(const GURL& url) {
DCHECK(CalledOnValidThread());
navigations_.push_back(NavigationRecord(url, GetCurrentTimeTicks()));
CleanUpOldNavigations();
}
// protected
struct PrerenderManager::PrerenderData::OrderByExpiryTime {
bool operator()(const PrerenderData* a, const PrerenderData* b) const {
return a->expiry_time() < b->expiry_time();
}
};
PrerenderManager::PrerenderData::PrerenderData(PrerenderManager* manager,
PrerenderContents* contents,
base::TimeTicks expiry_time)
: manager_(manager),
contents_(contents),
handle_count_(0),
expiry_time_(expiry_time) {
DCHECK_NE(static_cast<PrerenderContents*>(NULL), contents_);
}
PrerenderManager::PrerenderData::~PrerenderData() {
}
void PrerenderManager::PrerenderData::MakeIntoMatchCompleteReplacement() {
DCHECK(contents_);
contents_->set_match_complete_status(
PrerenderContents::MATCH_COMPLETE_REPLACED);
PrerenderData* to_delete = new PrerenderData(manager_, contents_.release(),
expiry_time_);
contents_.reset(to_delete->contents_->CreateMatchCompleteReplacement());
manager_->to_delete_prerenders_.push_back(to_delete);
}
void PrerenderManager::PrerenderData::OnHandleCreated(PrerenderHandle* handle) {
DCHECK_NE(static_cast<PrerenderContents*>(NULL), contents_);
++handle_count_;
contents_->AddObserver(handle);
}
void PrerenderManager::PrerenderData::OnHandleNavigatedAway(
PrerenderHandle* handle) {
DCHECK_LT(0, handle_count_);
DCHECK_NE(static_cast<PrerenderContents*>(NULL), contents_);
if (abandon_time_.is_null())
abandon_time_ = base::TimeTicks::Now();
// We intentionally don't decrement the handle count here, so that the
// prerender won't be canceled until it times out.
manager_->SourceNavigatedAway(this);
}
void PrerenderManager::PrerenderData::OnHandleCanceled(
PrerenderHandle* handle) {
DCHECK_LT(0, handle_count_);
DCHECK_NE(static_cast<PrerenderContents*>(NULL), contents_);
if (--handle_count_ == 0) {
// This will eventually remove this object from active_prerenders_.
contents_->Destroy(FINAL_STATUS_CANCELLED);
}
}
void PrerenderManager::PrerenderData::ClearPendingSwap() {
pending_swap_.reset(NULL);
}
PrerenderContents* PrerenderManager::PrerenderData::ReleaseContents() {
return contents_.release();
}
PrerenderManager::PendingSwap::PendingSwap(
PrerenderManager* manager,
content::WebContents* target_contents,
PrerenderData* prerender_data,
const GURL& url,
bool should_replace_current_entry)
: content::WebContentsObserver(target_contents),
manager_(manager),
prerender_data_(prerender_data),
url_(url),
should_replace_current_entry_(should_replace_current_entry),
start_time_(base::TimeTicks::Now()),
seen_target_route_id_(false),
swap_successful_(false),
weak_factory_(this) {
}
PrerenderManager::PendingSwap::~PendingSwap() {
manager_->prerender_tracker()->RemovePrerenderPendingSwap(
target_route_id_, swap_successful_);
}
void PrerenderManager::PendingSwap::BeginSwap() {
if (g_hang_session_storage_merges_for_testing)
return;
SessionStorageNamespace* target_namespace =
web_contents()->GetController().GetDefaultSessionStorageNamespace();
SessionStorageNamespace* prerender_namespace =
prerender_data_->contents()->GetSessionStorageNamespace();
prerender_namespace->Merge(
true, prerender_data_->contents()->child_id(),
target_namespace,
base::Bind(&PrerenderManager::PendingSwap::OnMergeCompleted,
weak_factory_.GetWeakPtr()));
merge_timeout_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(
kSessionStorageNamespaceMergeTimeoutMs),
this, &PrerenderManager::PendingSwap::OnMergeTimeout);
}
void PrerenderManager::PendingSwap::AboutToNavigateRenderFrame(
RenderFrameHost* render_frame_host) {
// TODO(davidben): Update prerendering for --site-per-process.
if (render_frame_host->GetParent())
return;
if (seen_target_route_id_) {
// A second navigation began browser-side.
prerender_data_->ClearPendingSwap();
return;
}
seen_target_route_id_ = true;
target_route_id_ = PrerenderTracker::ChildRouteIdPair(
render_frame_host->GetProcess()->GetID(),
render_frame_host->GetRoutingID());
manager_->prerender_tracker()->AddPrerenderPendingSwap(
target_route_id_, url_);
}
void PrerenderManager::PendingSwap::DidStartProvisionalLoadForFrame(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
bool is_error_page,
bool is_iframe_srcdoc) {
if (render_frame_host->GetParent())
return;
// We must only cancel the pending swap if the url navigated to is not
// the URL being attempted to be swapped in. That's because in the normal
// flow, a ProvisionalChangeToMainFrameUrl will happen for the URL attempted
// to be swapped in immediately after the pending swap has issued its merge.
if (validated_url != url_)
prerender_data_->ClearPendingSwap();
}
void PrerenderManager::PendingSwap::DidCommitProvisionalLoadForFrame(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
ui::PageTransition transition_type) {
if (render_frame_host->GetParent())
return;
prerender_data_->ClearPendingSwap();
}
void PrerenderManager::PendingSwap::DidFailProvisionalLoad(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
const base::string16& error_description) {
if (render_frame_host->GetParent())
return;
prerender_data_->ClearPendingSwap();
}
void PrerenderManager::PendingSwap::WebContentsDestroyed() {
prerender_data_->ClearPendingSwap();
}
void PrerenderManager::PendingSwap::RecordEvent(PrerenderEvent event) const {
manager_->RecordEvent(prerender_data_->contents(), event);
}
void PrerenderManager::PendingSwap::OnMergeCompleted(
SessionStorageNamespace::MergeResult result) {
UMA_HISTOGRAM_TIMES("Prerender.SessionStorageNamespaceMergeTime",
base::TimeTicks::Now() - start_time_);
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_MERGE_DONE);
// Log the exact merge result in a histogram.
switch (result) {
case SessionStorageNamespace::MERGE_RESULT_NAMESPACE_NOT_FOUND:
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_RESULT_NAMESPACE_NOT_FOUND);
break;
case SessionStorageNamespace::MERGE_RESULT_NAMESPACE_NOT_ALIAS:
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_RESULT_NAMESPACE_NOT_ALIAS);
break;
case SessionStorageNamespace::MERGE_RESULT_NOT_LOGGING:
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_RESULT_NOT_LOGGING);
break;
case SessionStorageNamespace::MERGE_RESULT_NO_TRANSACTIONS:
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_RESULT_NO_TRANSACTIONS);
break;
case SessionStorageNamespace::MERGE_RESULT_TOO_MANY_TRANSACTIONS:
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_RESULT_TOO_MANY_TRANSACTIONS);
break;
case SessionStorageNamespace::MERGE_RESULT_NOT_MERGEABLE:
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_RESULT_NOT_MERGEABLE);
break;
case SessionStorageNamespace::MERGE_RESULT_MERGEABLE:
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_RESULT_MERGEABLE);
break;
default:
NOTREACHED();
}
if (result != SessionStorageNamespace::MERGE_RESULT_MERGEABLE &&
result != SessionStorageNamespace::MERGE_RESULT_NO_TRANSACTIONS) {
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_MERGE_FAILED);
prerender_data_->ClearPendingSwap();
return;
}
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_SWAPPING_IN);
// Note that SwapInternal will, on success, delete |prerender_data_| and
// |this|. It will also delete |this| in some failure cases. Pass in a new
// GURL object rather than a reference to |url_|. Also hold on to |manager_|
// and |prerender_data_|.
//
// TODO(davidben): Can we make this less fragile?
PrerenderManager* manager = manager_;
PrerenderData* prerender_data = prerender_data_;
WebContents* new_web_contents =
manager_->SwapInternal(GURL(url_),
web_contents(),
prerender_data_,
should_replace_current_entry_);
if (!new_web_contents) {
manager->RecordEvent(prerender_data->contents(),
PRERENDER_EVENT_MERGE_RESULT_SWAPIN_FAILED);
// Depending on whether SwapInternal called Destroy() or simply failed to
// swap, |this| may or may not be deleted. Either way, if the swap failed,
// |prerender_data| is deleted asynchronously, so this call is a no-op if
// |this| is already gone.
prerender_data->ClearPendingSwap();
}
}
void PrerenderManager::PendingSwap::OnMergeTimeout() {
RecordEvent(PRERENDER_EVENT_MERGE_RESULT_TIMED_OUT);
prerender_data_->ClearPendingSwap();
}
void PrerenderManager::SetPrerenderContentsFactory(
PrerenderContents::Factory* prerender_contents_factory) {
DCHECK(CalledOnValidThread());
prerender_contents_factory_.reset(prerender_contents_factory);
}
void PrerenderManager::SourceNavigatedAway(PrerenderData* prerender_data) {
// The expiry time of our prerender data will likely change because of
// this navigation. This requires a resort of active_prerenders_.
ScopedVector<PrerenderData>::iterator it =
std::find(active_prerenders_.begin(), active_prerenders_.end(),
prerender_data);
if (it == active_prerenders_.end())
return;
(*it)->set_expiry_time(
std::min((*it)->expiry_time(),
GetExpiryTimeForNavigatedAwayPrerender()));
SortActivePrerenders();
}
net::URLRequestContextGetter* PrerenderManager::GetURLRequestContext() {
return content::BrowserContext::GetDefaultStoragePartition(profile_)->
GetURLRequestContext();
}
// private
PrerenderHandle* PrerenderManager::AddPrerender(
Origin origin,
const GURL& url_arg,
const content::Referrer& referrer,
const gfx::Size& size,
SessionStorageNamespace* session_storage_namespace) {
DCHECK(CalledOnValidThread());
if (!IsEnabled())
return NULL;
if ((origin == ORIGIN_LINK_REL_PRERENDER_CROSSDOMAIN ||
origin == ORIGIN_LINK_REL_PRERENDER_SAMEDOMAIN) &&
IsGoogleSearchResultURL(referrer.url)) {
origin = ORIGIN_GWS_PRERENDER;
}
GURL url = url_arg;
GURL alias_url;
uint8 experiment = GetQueryStringBasedExperiment(url_arg);
if (IsControlGroup(experiment) &&
MaybeGetQueryStringBasedAliasURL(url, &alias_url)) {
url = alias_url;
}
// From here on, we will record a FinalStatus so we need to register with the
// histogram tracking.
histograms_->RecordPrerender(origin, url_arg);
if (PrerenderData* preexisting_prerender_data =
FindPrerenderData(url, session_storage_namespace)) {
RecordFinalStatusWithoutCreatingPrerenderContents(
url, origin, experiment, FINAL_STATUS_DUPLICATE);
return new PrerenderHandle(preexisting_prerender_data);
}
// Do not prerender if there are too many render processes, and we would
// have to use an existing one. We do not want prerendering to happen in
// a shared process, so that we can always reliably lower the CPU
// priority for prerendering.
// In single-process mode, ShouldTryToUseExistingProcessHost() always returns
// true, so that case needs to be explicitly checked for.
// TODO(tburkard): Figure out how to cancel prerendering in the opposite
// case, when a new tab is added to a process used for prerendering.
// TODO(ppi): Check whether there are usually enough render processes
// available on Android. If not, kill an existing renderers so that we can
// create a new one.
if (content::RenderProcessHost::ShouldTryToUseExistingProcessHost(
profile_, url) &&
!content::RenderProcessHost::run_renderer_in_process()) {
RecordFinalStatusWithoutCreatingPrerenderContents(
url, origin, experiment, FINAL_STATUS_TOO_MANY_PROCESSES);
return NULL;
}
// Check if enough time has passed since the last prerender.
if (!DoesRateLimitAllowPrerender(origin)) {
// Cancel the prerender. We could add it to the pending prerender list but
// this doesn't make sense as the next prerender request will be triggered
// by a navigation and is unlikely to be the same site.
RecordFinalStatusWithoutCreatingPrerenderContents(
url, origin, experiment, FINAL_STATUS_RATE_LIMIT_EXCEEDED);
return NULL;
}
if (IsPrerenderCookieStoreEnabled() && !cookie_store_loaded()) {
// Only prerender if the cookie store for this profile has been loaded.
// This is required by PrerenderCookieMonster.
RecordFinalStatusWithoutCreatingPrerenderContents(
url, origin, experiment, FINAL_STATUS_COOKIE_STORE_NOT_LOADED);
return NULL;
}
PrerenderContents* prerender_contents = CreatePrerenderContents(
url, referrer, origin, experiment);
DCHECK(prerender_contents);
active_prerenders_.push_back(
new PrerenderData(this, prerender_contents,
GetExpiryTimeForNewPrerender(origin)));
if (!prerender_contents->Init()) {
DCHECK(active_prerenders_.end() ==
FindIteratorForPrerenderContents(prerender_contents));
return NULL;
}
histograms_->RecordPrerenderStarted(origin);
DCHECK(!prerender_contents->prerendering_has_started());
PrerenderHandle* prerender_handle =
new PrerenderHandle(active_prerenders_.back());
SortActivePrerenders();
last_prerender_start_time_ = GetCurrentTimeTicks();
gfx::Size contents_size =
size.IsEmpty() ? config_.default_tab_bounds.size() : size;
net::URLRequestContextGetter* request_context =
(IsPrerenderCookieStoreEnabled() ? GetURLRequestContext() : NULL);
prerender_contents->StartPrerendering(contents_size,
session_storage_namespace,
request_context);
DCHECK(IsControlGroup(experiment) ||
prerender_contents->prerendering_has_started() ||
(origin == ORIGIN_LOCAL_PREDICTOR &&
IsLocalPredictorPrerenderAlwaysControlEnabled()));
if (GetMode() == PRERENDER_MODE_EXPERIMENT_MULTI_PRERENDER_GROUP)
histograms_->RecordConcurrency(active_prerenders_.size());
// Query the history to see if the URL being prerendered has ever been
// visited before.
HistoryService* history_service = HistoryServiceFactory::GetForProfile(
profile_, Profile::EXPLICIT_ACCESS);
if (history_service) {
history_service->QueryURL(
url,
false,
base::Bind(&PrerenderManager::OnHistoryServiceDidQueryURL,
base::Unretained(this),
origin,
experiment),
&query_url_tracker_);
}
StartSchedulingPeriodicCleanups();
return prerender_handle;
}
void PrerenderManager::StartSchedulingPeriodicCleanups() {
DCHECK(CalledOnValidThread());
if (repeating_timer_.IsRunning())
return;
repeating_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kPeriodicCleanupIntervalMs),
this,
&PrerenderManager::PeriodicCleanup);
}
void PrerenderManager::StopSchedulingPeriodicCleanups() {
DCHECK(CalledOnValidThread());
repeating_timer_.Stop();
}
void PrerenderManager::PeriodicCleanup() {
DCHECK(CalledOnValidThread());
base::ElapsedTimer resource_timer;
// Grab a copy of the current PrerenderContents pointers, so that we
// will not interfere with potential deletions of the list.
std::vector<PrerenderContents*>
prerender_contents(active_prerenders_.size());
std::transform(active_prerenders_.begin(), active_prerenders_.end(),
prerender_contents.begin(),
std::mem_fun(&PrerenderData::contents));
// And now check for prerenders using too much memory.
std::for_each(prerender_contents.begin(), prerender_contents.end(),
std::mem_fun(
&PrerenderContents::DestroyWhenUsingTooManyResources));
// Measure how long the resource checks took. http://crbug.com/305419.
UMA_HISTOGRAM_TIMES("Prerender.PeriodicCleanupResourceCheckTime",
resource_timer.Elapsed());
base::ElapsedTimer cleanup_timer;
// Perform deferred cleanup work.
DeleteOldWebContents();
DeleteOldEntries();
if (active_prerenders_.empty())
StopSchedulingPeriodicCleanups();
to_delete_prerenders_.clear();
// Measure how long a the various cleanup tasks took. http://crbug.com/305419.
UMA_HISTOGRAM_TIMES("Prerender.PeriodicCleanupDeleteContentsTime",
cleanup_timer.Elapsed());
}
void PrerenderManager::PostCleanupTask() {
DCHECK(CalledOnValidThread());
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&PrerenderManager::PeriodicCleanup, AsWeakPtr()));
}
base::TimeTicks PrerenderManager::GetExpiryTimeForNewPrerender(
Origin origin) const {
base::TimeDelta ttl = config_.time_to_live;
if (origin == ORIGIN_LOCAL_PREDICTOR)
ttl = base::TimeDelta::FromSeconds(GetLocalPredictorTTLSeconds());
return GetCurrentTimeTicks() + ttl;
}
base::TimeTicks PrerenderManager::GetExpiryTimeForNavigatedAwayPrerender()
const {
return GetCurrentTimeTicks() + config_.abandon_time_to_live;
}
void PrerenderManager::DeleteOldEntries() {
DCHECK(CalledOnValidThread());
while (!active_prerenders_.empty()) {
PrerenderData* prerender_data = active_prerenders_.front();
DCHECK(prerender_data);
DCHECK(prerender_data->contents());
if (prerender_data->expiry_time() > GetCurrentTimeTicks())
return;
prerender_data->contents()->Destroy(FINAL_STATUS_TIMED_OUT);
}
}
base::Time PrerenderManager::GetCurrentTime() const {
return base::Time::Now();
}
base::TimeTicks PrerenderManager::GetCurrentTimeTicks() const {
return base::TimeTicks::Now();
}
PrerenderContents* PrerenderManager::CreatePrerenderContents(
const GURL& url,
const content::Referrer& referrer,
Origin origin,
uint8 experiment_id) {
DCHECK(CalledOnValidThread());
return prerender_contents_factory_->CreatePrerenderContents(
this, profile_, url, referrer, origin, experiment_id);
}
void PrerenderManager::SortActivePrerenders() {
std::sort(active_prerenders_.begin(), active_prerenders_.end(),
PrerenderData::OrderByExpiryTime());
}
PrerenderManager::PrerenderData* PrerenderManager::FindPrerenderData(
const GURL& url,
const SessionStorageNamespace* session_storage_namespace) {
for (ScopedVector<PrerenderData>::iterator it = active_prerenders_.begin();
it != active_prerenders_.end(); ++it) {
if ((*it)->contents()->Matches(url, session_storage_namespace))
return *it;
}
return NULL;
}
PrerenderManager::PrerenderData*
PrerenderManager::FindPrerenderDataForTargetContents(
WebContents* target_contents) {
for (ScopedVector<PrerenderData>::iterator it = active_prerenders_.begin();
it != active_prerenders_.end(); ++it) {
if ((*it)->pending_swap() &&
(*it)->pending_swap()->web_contents() == target_contents)
return *it;
}
return NULL;
}
ScopedVector<PrerenderManager::PrerenderData>::iterator
PrerenderManager::FindIteratorForPrerenderContents(
PrerenderContents* prerender_contents) {
for (ScopedVector<PrerenderData>::iterator it = active_prerenders_.begin();
it != active_prerenders_.end(); ++it) {
if (prerender_contents == (*it)->contents())
return it;
}
return active_prerenders_.end();
}
bool PrerenderManager::DoesRateLimitAllowPrerender(Origin origin) const {
DCHECK(CalledOnValidThread());
base::TimeDelta elapsed_time =
GetCurrentTimeTicks() - last_prerender_start_time_;
histograms_->RecordTimeBetweenPrerenderRequests(origin, elapsed_time);
if (!config_.rate_limit_enabled)
return true;
// The LocalPredictor may issue multiple prerenders simultaneously (if so
// configured), so no throttling.
if (origin == ORIGIN_LOCAL_PREDICTOR)
return true;
return elapsed_time >=
base::TimeDelta::FromMilliseconds(kMinTimeBetweenPrerendersMs);
}
void PrerenderManager::DeleteOldWebContents() {
while (!old_web_contents_list_.empty()) {
WebContents* web_contents = old_web_contents_list_.front();
old_web_contents_list_.pop_front();
// TODO(dominich): should we use Instant Unload Handler here?
delete web_contents;
}
}
void PrerenderManager::CleanUpOldNavigations() {
DCHECK(CalledOnValidThread());
// Cutoff. Navigations before this cutoff can be discarded.
base::TimeTicks cutoff = GetCurrentTimeTicks() -
base::TimeDelta::FromMilliseconds(kNavigationRecordWindowMs);
while (!navigations_.empty()) {
if (navigations_.front().time > cutoff)
break;
navigations_.pop_front();
}
}
void PrerenderManager::ScheduleDeleteOldWebContents(
WebContents* tab,
OnCloseWebContentsDeleter* deleter) {
old_web_contents_list_.push_back(tab);
PostCleanupTask();
if (deleter) {
ScopedVector<OnCloseWebContentsDeleter>::iterator i = std::find(
on_close_web_contents_deleters_.begin(),
on_close_web_contents_deleters_.end(),
deleter);
DCHECK(i != on_close_web_contents_deleters_.end());
on_close_web_contents_deleters_.erase(i);
}
}
void PrerenderManager::AddToHistory(PrerenderContents* contents) {
PrerenderHistory::Entry entry(contents->prerender_url(),
contents->final_status(),
contents->origin(),
base::Time::Now());
prerender_history_->AddEntry(entry);
}
base::Value* PrerenderManager::GetActivePrerendersAsValue() const {
base::ListValue* list_value = new base::ListValue();
for (ScopedVector<PrerenderData>::const_iterator it =
active_prerenders_.begin();
it != active_prerenders_.end(); ++it) {
if (base::Value* prerender_value = (*it)->contents()->GetAsValue())
list_value->Append(prerender_value);
}
return list_value;
}
void PrerenderManager::DestroyAllContents(FinalStatus final_status) {
DeleteOldWebContents();
while (!active_prerenders_.empty()) {
PrerenderContents* contents = active_prerenders_.front()->contents();
contents->Destroy(final_status);
}
to_delete_prerenders_.clear();
}
void PrerenderManager::DestroyAndMarkMatchCompleteAsUsed(
PrerenderContents* prerender_contents,
FinalStatus final_status) {
prerender_contents->set_match_complete_status(
PrerenderContents::MATCH_COMPLETE_REPLACED);
histograms_->RecordFinalStatus(prerender_contents->origin(),
prerender_contents->experiment_id(),
PrerenderContents::MATCH_COMPLETE_REPLACEMENT,
FINAL_STATUS_WOULD_HAVE_BEEN_USED);
prerender_contents->Destroy(final_status);
}
void PrerenderManager::RecordFinalStatusWithoutCreatingPrerenderContents(
const GURL& url, Origin origin, uint8 experiment_id,
FinalStatus final_status) const {
PrerenderHistory::Entry entry(url, final_status, origin, base::Time::Now());
prerender_history_->AddEntry(entry);
RecordFinalStatusWithMatchCompleteStatus(
origin, experiment_id,
PrerenderContents::MATCH_COMPLETE_DEFAULT,
final_status);
}
void PrerenderManager::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_COOKIE_CHANGED: {
Profile* profile = content::Source<Profile>(source).ptr();
if (!profile || !profile_->IsSameProfile(profile) ||
profile->IsOffTheRecord()) {
return;
}
CookieChanged(content::Details<ChromeCookieDetails>(details).ptr());
break;
}
case chrome::NOTIFICATION_PROFILE_DESTROYED:
DestroyAllContents(FINAL_STATUS_PROFILE_DESTROYED);
on_close_web_contents_deleters_.clear();
break;
default:
NOTREACHED() << "Unexpected notification sent.";
break;
}
}
void PrerenderManager::OnCreatingAudioStream(int render_process_id,
int render_frame_id) {
content::RenderFrameHost* render_frame_host =
content::RenderFrameHost::FromID(render_process_id, render_frame_id);
WebContents* tab = WebContents::FromRenderFrameHost(render_frame_host);
if (!tab)
return;
PrerenderContents* prerender_contents = GetPrerenderContents(tab);
if (!prerender_contents)
return;
prerender_contents->Destroy(prerender::FINAL_STATUS_CREATING_AUDIO_STREAM);
}
void PrerenderManager::RecordLikelyLoginOnURL(const GURL& url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!url.SchemeIsHTTPOrHTTPS())
return;
if (logged_in_predictor_table_.get()) {
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(&LoggedInPredictorTable::AddDomainFromURL,
logged_in_predictor_table_,
url));
}
std::string key = LoggedInPredictorTable::GetKey(url);
if (!logged_in_state_.get())
return;
if (logged_in_state_->count(key))
return;
(*logged_in_state_)[key] = base::Time::Now().ToInternalValue();
}
void PrerenderManager::CheckIfLikelyLoggedInOnURL(
const GURL& url,
bool* lookup_result,
bool* database_was_present,
const base::Closure& result_cb) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!logged_in_predictor_table_.get()) {
*database_was_present = false;
*lookup_result = false;
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, result_cb);
return;
}
BrowserThread::PostTaskAndReply(
BrowserThread::DB, FROM_HERE,
base::Bind(&LoggedInPredictorTable::HasUserLoggedIn,
logged_in_predictor_table_,
url,
lookup_result,
database_was_present),
result_cb);
}
void PrerenderManager::CookieChanged(ChromeCookieDetails* details) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!logged_in_predictor_table_.get())
return;
// We only care when a cookie has been removed.
if (!details->removed)
return;
std::string domain_key =
LoggedInPredictorTable::GetKeyFromDomain(details->cookie->Domain());
// If we have no record of this domain as a potentially logged in domain,
// nothing to do here.
if (logged_in_state_.get() && logged_in_state_->count(domain_key) < 1)
return;
net::URLRequestContextGetter* rq_context = profile_->GetRequestContext();
if (!rq_context)
return;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&CheckIfCookiesExistForDomainOnIOThread,
base::Unretained(rq_context),
domain_key,
base::Bind(
&PrerenderManager::CookieChangedAnyCookiesLeftLookupResult,
AsWeakPtr(),
domain_key)
));
}
void PrerenderManager::CookieChangedAnyCookiesLeftLookupResult(
const std::string& domain_key,
bool cookies_exist) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (cookies_exist)
return;
if (logged_in_predictor_table_.get()) {
BrowserThread::PostTask(BrowserThread::DB,
FROM_HERE,
base::Bind(&LoggedInPredictorTable::DeleteDomain,
logged_in_predictor_table_,
domain_key));
}
if (logged_in_state_.get())
logged_in_state_->erase(domain_key);
}
void PrerenderManager::LoggedInPredictorDataReceived(
scoped_ptr<LoggedInStateMap> new_map) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
logged_in_state_.swap(new_map);
}
void PrerenderManager::RecordEvent(PrerenderContents* contents,
PrerenderEvent event) const {
if (!contents)
histograms_->RecordEvent(ORIGIN_NONE, kNoExperiment, event);
else
histograms_->RecordEvent(contents->origin(), contents->experiment_id(),
event);
}
// static
void PrerenderManager::RecordCookieEvent(int process_id,
int frame_id,
const GURL& url,
const GURL& frame_url,
bool is_for_blocking_resource,
PrerenderContents::CookieEvent event,
const net::CookieList* cookie_list) {
RenderFrameHost* rfh = RenderFrameHost::FromID(process_id, frame_id);
WebContents* web_contents = WebContents::FromRenderFrameHost(rfh);
if (!web_contents)
return;
bool is_main_frame = (rfh == web_contents->GetMainFrame());
bool is_third_party_cookie =
(!frame_url.is_empty() &&
!net::registry_controlled_domains::SameDomainOrHost(
url, frame_url,
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
PrerenderContents* prerender_contents =
PrerenderContents::FromWebContents(web_contents);
if (!prerender_contents)
return;
base::Time earliest_create_date;
if (event == PrerenderContents::COOKIE_EVENT_SEND) {
if (!cookie_list || cookie_list->empty())
return;
for (size_t i = 0; i < cookie_list->size(); i++) {
if (earliest_create_date.is_null() ||
(*cookie_list)[i].CreationDate() < earliest_create_date) {
earliest_create_date = (*cookie_list)[i].CreationDate();
}
}
}
prerender_contents->RecordCookieEvent(event,
is_main_frame && url == frame_url,
is_third_party_cookie,
is_for_blocking_resource,
earliest_create_date);
}
void PrerenderManager::RecordCookieStatus(Origin origin,
uint8 experiment_id,
int cookie_status) const {
histograms_->RecordCookieStatus(origin, experiment_id, cookie_status);
}
void PrerenderManager::RecordCookieSendType(Origin origin,
uint8 experiment_id,
int cookie_send_type) const {
histograms_->RecordCookieSendType(origin, experiment_id, cookie_send_type);
}
void PrerenderManager::OnHistoryServiceDidQueryURL(
Origin origin,
uint8 experiment_id,
bool success,
const history::URLRow& url_row,
const history::VisitVector& /*visits*/) {
histograms_->RecordPrerenderPageVisitedStatus(origin, experiment_id, success);
}
// static
void PrerenderManager::HangSessionStorageMergesForTesting() {
g_hang_session_storage_merges_for_testing = true;
}
void PrerenderManager::RecordNetworkBytes(Origin origin,
bool used,
int64 prerender_bytes) {
if (!ActuallyPrerendering())
return;
int64 recent_profile_bytes =
profile_network_bytes_ - last_recorded_profile_network_bytes_;
last_recorded_profile_network_bytes_ = profile_network_bytes_;
DCHECK_GE(recent_profile_bytes, 0);
histograms_->RecordNetworkBytes(
origin, used, prerender_bytes, recent_profile_bytes);
}
bool PrerenderManager::IsEnabled() const {
DCHECK(CalledOnValidThread());
return chrome_browser_net::CanPrefetchAndPrerenderUI(profile_->GetPrefs());
}
void PrerenderManager::AddProfileNetworkBytesIfEnabled(int64 bytes) {
DCHECK_GE(bytes, 0);
if (IsEnabled() && ActuallyPrerendering())
profile_network_bytes_ += bytes;
}
void PrerenderManager::OnCookieStoreLoaded() {
cookie_store_loaded_ = true;
if (!on_cookie_store_loaded_cb_for_testing_.is_null())
on_cookie_store_loaded_cb_for_testing_.Run();
}
void PrerenderManager::AddPrerenderProcessHost(
content::RenderProcessHost* process_host) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(prerender_process_hosts_.find(process_host) ==
prerender_process_hosts_.end());
prerender_process_hosts_.insert(process_host);
process_host->AddObserver(this);
}
bool PrerenderManager::MayReuseProcessHost(
content::RenderProcessHost* process_host) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// If prerender cookie stores are disabled, there is no need to require
// isolated prerender processes.
if (!IsPrerenderCookieStoreEnabled())
return true;
return (prerender_process_hosts_.find(process_host) ==
prerender_process_hosts_.end());
}
void PrerenderManager::RenderProcessHostDestroyed(
content::RenderProcessHost* host) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
prerender_process_hosts_.erase(host);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&PrerenderTracker::RemovePrerenderCookieStoreOnIOThread,
base::Unretained(prerender_tracker()), host->GetID(), false));
}
} // namespace prerender
|