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 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
|
// 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 "components/printing/renderer/print_web_view_helper.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <string>
#include <utility>
#include "base/auto_reset.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/process/process_handle.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "components/printing/common/print_messages.h"
#include "content/public/common/web_preferences.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/render_view.h"
#include "grit/components_resources.h"
#include "net/base/escape.h"
#include "printing/features/features.h"
#include "printing/metafile_skia_wrapper.h"
#include "printing/pdf_metafile_skia.h"
#include "printing/units.h"
#include "third_party/WebKit/public/platform/WebDoubleSize.h"
#include "third_party/WebKit/public/platform/WebSize.h"
#include "third_party/WebKit/public/platform/WebURLRequest.h"
#include "third_party/WebKit/public/web/WebConsoleMessage.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFrameClient.h"
#include "third_party/WebKit/public/web/WebFrameOwnerProperties.h"
#include "third_party/WebKit/public/web/WebFrameWidget.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebPlugin.h"
#include "third_party/WebKit/public/web/WebPluginDocument.h"
#include "third_party/WebKit/public/web/WebPrintParams.h"
#include "third_party/WebKit/public/web/WebPrintPresetOptions.h"
#include "third_party/WebKit/public/web/WebSandboxFlags.h"
#include "third_party/WebKit/public/web/WebScriptSource.h"
#include "third_party/WebKit/public/web/WebSettings.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "third_party/WebKit/public/web/WebViewClient.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/base/resource/resource_bundle.h"
using content::WebPreferences;
namespace printing {
namespace {
#define STATIC_ASSERT_ENUM(a, b) \
static_assert(static_cast<int>(a) == static_cast<int>(b), \
"mismatching enums: " #a)
// Check blink and printing enums are kept in sync.
STATIC_ASSERT_ENUM(blink::WebUnknownDuplexMode, UNKNOWN_DUPLEX_MODE);
STATIC_ASSERT_ENUM(blink::WebSimplex, SIMPLEX);
STATIC_ASSERT_ENUM(blink::WebLongEdge, LONG_EDGE);
STATIC_ASSERT_ENUM(blink::WebShortEdge, SHORT_EDGE);
enum PrintPreviewHelperEvents {
PREVIEW_EVENT_REQUESTED,
PREVIEW_EVENT_CACHE_HIT, // Unused
PREVIEW_EVENT_CREATE_DOCUMENT,
PREVIEW_EVENT_NEW_SETTINGS, // Unused
PREVIEW_EVENT_MAX,
};
const double kMinDpi = 1.0;
// Also set in third_party/WebKit/Source/core/page/PrintContext.cpp
const float kPrintingMinimumShrinkFactor = 1.333f;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
bool g_is_preview_enabled = true;
const char kPageLoadScriptFormat[] =
"document.open(); document.write(%s); document.close();";
const char kPageSetupScriptFormat[] = "setup(%s);";
void ExecuteScript(blink::WebFrame* frame,
const char* script_format,
const base::Value& parameters) {
std::string json;
base::JSONWriter::Write(parameters, &json);
std::string script = base::StringPrintf(script_format, json.c_str());
frame->executeScript(blink::WebString(base::UTF8ToUTF16(script)));
}
#else
bool g_is_preview_enabled = false;
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
int GetDPI(const PrintMsg_Print_Params* print_params) {
#if defined(OS_MACOSX)
// On the Mac, the printable area is in points, don't do any scaling based
// on dpi.
return kPointsPerInch;
#else
return static_cast<int>(print_params->dpi);
#endif // defined(OS_MACOSX)
}
bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) {
return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() &&
!params.printable_area.IsEmpty() && params.document_cookie &&
params.desired_dpi && params.dpi && params.margin_top >= 0 &&
params.margin_left >= 0 && params.dpi > kMinDpi &&
params.document_cookie != 0;
}
// Helper function to check for fit to page
bool IsWebPrintScalingOptionFitToPage(const PrintMsg_Print_Params& params) {
return params.print_scaling_option ==
blink::WebPrintScalingOptionFitToPrintableArea;
}
PrintMsg_Print_Params GetCssPrintParams(
blink::WebLocalFrame* frame,
int page_index,
const PrintMsg_Print_Params& page_params) {
PrintMsg_Print_Params page_css_params = page_params;
int dpi = GetDPI(&page_params);
blink::WebDoubleSize page_size_in_pixels(
ConvertUnitDouble(page_params.page_size.width(), dpi, kPixelsPerInch),
ConvertUnitDouble(page_params.page_size.height(), dpi, kPixelsPerInch));
int margin_top_in_pixels =
ConvertUnit(page_params.margin_top, dpi, kPixelsPerInch);
int margin_right_in_pixels = ConvertUnit(
page_params.page_size.width() -
page_params.content_size.width() - page_params.margin_left,
dpi, kPixelsPerInch);
int margin_bottom_in_pixels = ConvertUnit(
page_params.page_size.height() -
page_params.content_size.height() - page_params.margin_top,
dpi, kPixelsPerInch);
int margin_left_in_pixels = ConvertUnit(
page_params.margin_left,
dpi, kPixelsPerInch);
if (frame) {
frame->pageSizeAndMarginsInPixels(page_index,
page_size_in_pixels,
margin_top_in_pixels,
margin_right_in_pixels,
margin_bottom_in_pixels,
margin_left_in_pixels);
}
double new_content_width = page_size_in_pixels.width() -
margin_left_in_pixels - margin_right_in_pixels;
double new_content_height = page_size_in_pixels.height() -
margin_top_in_pixels - margin_bottom_in_pixels;
// Invalid page size and/or margins. We just use the default setting.
if (new_content_width < 1 || new_content_height < 1) {
CHECK(frame != NULL);
page_css_params = GetCssPrintParams(NULL, page_index, page_params);
return page_css_params;
}
page_css_params.page_size =
gfx::Size(ConvertUnit(page_size_in_pixels.width(), kPixelsPerInch, dpi),
ConvertUnit(page_size_in_pixels.height(), kPixelsPerInch, dpi));
page_css_params.content_size =
gfx::Size(ConvertUnit(new_content_width, kPixelsPerInch, dpi),
ConvertUnit(new_content_height, kPixelsPerInch, dpi));
page_css_params.margin_top =
ConvertUnit(margin_top_in_pixels, kPixelsPerInch, dpi);
page_css_params.margin_left =
ConvertUnit(margin_left_in_pixels, kPixelsPerInch, dpi);
return page_css_params;
}
double FitPrintParamsToPage(const PrintMsg_Print_Params& page_params,
PrintMsg_Print_Params* params_to_fit) {
double content_width =
static_cast<double>(params_to_fit->content_size.width());
double content_height =
static_cast<double>(params_to_fit->content_size.height());
int default_page_size_height = page_params.page_size.height();
int default_page_size_width = page_params.page_size.width();
int css_page_size_height = params_to_fit->page_size.height();
int css_page_size_width = params_to_fit->page_size.width();
double scale_factor = 1.0f;
if (page_params.page_size == params_to_fit->page_size)
return scale_factor;
if (default_page_size_width < css_page_size_width ||
default_page_size_height < css_page_size_height) {
double ratio_width =
static_cast<double>(default_page_size_width) / css_page_size_width;
double ratio_height =
static_cast<double>(default_page_size_height) / css_page_size_height;
scale_factor = ratio_width < ratio_height ? ratio_width : ratio_height;
content_width *= scale_factor;
content_height *= scale_factor;
}
params_to_fit->margin_top = static_cast<int>(
(default_page_size_height - css_page_size_height * scale_factor) / 2 +
(params_to_fit->margin_top * scale_factor));
params_to_fit->margin_left = static_cast<int>(
(default_page_size_width - css_page_size_width * scale_factor) / 2 +
(params_to_fit->margin_left * scale_factor));
params_to_fit->content_size = gfx::Size(static_cast<int>(content_width),
static_cast<int>(content_height));
params_to_fit->page_size = page_params.page_size;
return scale_factor;
}
void CalculatePageLayoutFromPrintParams(
const PrintMsg_Print_Params& params,
double scale_factor,
PageSizeMargins* page_layout_in_points) {
bool fit_to_page = IsWebPrintScalingOptionFitToPage(params);
int dpi = GetDPI(¶ms);
int content_width = params.content_size.width();
int content_height = params.content_size.height();
// Scale the content to its normal size for purpose of computing page layout.
// Otherwise we will get negative margins.
if (scale_factor >= PrintWebViewHelper::kEpsilon &&
(fit_to_page || params.print_to_pdf)) {
content_width =
static_cast<int>(static_cast<double>(content_width) * scale_factor);
content_height =
static_cast<int>(static_cast<double>(content_height) * scale_factor);
}
int margin_bottom =
params.page_size.height() - content_height - params.margin_top;
int margin_right =
params.page_size.width() - content_width - params.margin_left;
page_layout_in_points->content_width =
ConvertUnit(content_width, dpi, kPointsPerInch);
page_layout_in_points->content_height =
ConvertUnit(content_height, dpi, kPointsPerInch);
page_layout_in_points->margin_top =
ConvertUnit(params.margin_top, dpi, kPointsPerInch);
page_layout_in_points->margin_right =
ConvertUnit(margin_right, dpi, kPointsPerInch);
page_layout_in_points->margin_bottom =
ConvertUnit(margin_bottom, dpi, kPointsPerInch);
page_layout_in_points->margin_left =
ConvertUnit(params.margin_left, dpi, kPointsPerInch);
}
void EnsureOrientationMatches(const PrintMsg_Print_Params& css_params,
PrintMsg_Print_Params* page_params) {
if ((page_params->page_size.width() > page_params->page_size.height()) ==
(css_params.page_size.width() > css_params.page_size.height())) {
return;
}
// Swap the |width| and |height| values.
page_params->page_size.SetSize(page_params->page_size.height(),
page_params->page_size.width());
page_params->content_size.SetSize(page_params->content_size.height(),
page_params->content_size.width());
page_params->printable_area.set_size(
gfx::Size(page_params->printable_area.height(),
page_params->printable_area.width()));
}
void ComputeWebKitPrintParamsInDesiredDpi(
const PrintMsg_Print_Params& print_params,
blink::WebPrintParams* webkit_print_params) {
int dpi = GetDPI(&print_params);
webkit_print_params->printerDPI = dpi;
webkit_print_params->rasterizePDF = print_params.rasterize_pdf;
webkit_print_params->printScalingOption = print_params.print_scaling_option;
webkit_print_params->printContentArea.width = ConvertUnit(
print_params.content_size.width(), dpi, print_params.desired_dpi);
webkit_print_params->printContentArea.height = ConvertUnit(
print_params.content_size.height(), dpi, print_params.desired_dpi);
webkit_print_params->printableArea.x = ConvertUnit(
print_params.printable_area.x(), dpi, print_params.desired_dpi);
webkit_print_params->printableArea.y = ConvertUnit(
print_params.printable_area.y(), dpi, print_params.desired_dpi);
webkit_print_params->printableArea.width = ConvertUnit(
print_params.printable_area.width(), dpi, print_params.desired_dpi);
webkit_print_params->printableArea.height = ConvertUnit(
print_params.printable_area.height(), dpi, print_params.desired_dpi);
webkit_print_params->paperSize.width = ConvertUnit(
print_params.page_size.width(), dpi, print_params.desired_dpi);
webkit_print_params->paperSize.height = ConvertUnit(
print_params.page_size.height(), dpi, print_params.desired_dpi);
}
blink::WebPlugin* GetPlugin(const blink::WebLocalFrame* frame) {
return frame->document().isPluginDocument()
? frame->document().to<blink::WebPluginDocument>().plugin()
: NULL;
}
bool PrintingNodeOrPdfFrame(const blink::WebLocalFrame* frame,
const blink::WebNode& node) {
if (!node.isNull())
return true;
blink::WebPlugin* plugin = GetPlugin(frame);
return plugin && plugin->supportsPaginatedPrint();
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Returns true if the current destination printer is PRINT_TO_PDF.
bool IsPrintToPdfRequested(const base::DictionaryValue& job_settings) {
bool print_to_pdf = false;
if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf))
NOTREACHED();
return print_to_pdf;
}
bool PrintingFrameHasPageSizeStyle(blink::WebLocalFrame* frame,
int total_page_count) {
if (!frame)
return false;
bool frame_has_custom_page_size_style = false;
for (int i = 0; i < total_page_count; ++i) {
if (frame->hasCustomPageSizeStyle(i)) {
frame_has_custom_page_size_style = true;
break;
}
}
return frame_has_custom_page_size_style;
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
#if BUILDFLAG(ENABLE_PRINTING)
// Disable scaling when either:
// - The PDF specifies disabling scaling.
// - All the pages in the PDF are the same size,
// - |ignore_page_size| is false and the uniform size is the same as the paper
// size.
bool PDFShouldDisableScalingBasedOnPreset(
const blink::WebPrintPresetOptions& options,
const PrintMsg_Print_Params& params,
bool ignore_page_size) {
if (options.isScalingDisabled)
return true;
if (!options.isPageSizeUniform)
return false;
int dpi = GetDPI(¶ms);
if (!dpi) {
// Likely |params| is invalid, in which case the return result does not
// matter. Check for this so ConvertUnit() does not divide by zero.
return true;
}
if (ignore_page_size)
return false;
blink::WebSize page_size(
ConvertUnit(params.page_size.width(), dpi, kPointsPerInch),
ConvertUnit(params.page_size.height(), dpi, kPointsPerInch));
return options.uniformPageSize == page_size;
}
bool PDFShouldDisableScaling(blink::WebLocalFrame* frame,
const blink::WebNode& node,
const PrintMsg_Print_Params& params,
bool ignore_page_size) {
const bool kDefaultPDFShouldDisableScalingSetting = true;
blink::WebPrintPresetOptions preset_options;
if (!frame->getPrintPresetOptionsForPlugin(node, &preset_options))
return kDefaultPDFShouldDisableScalingSetting;
return PDFShouldDisableScalingBasedOnPreset(preset_options, params,
ignore_page_size);
}
#endif
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
MarginType GetMarginsForPdf(blink::WebLocalFrame* frame,
const blink::WebNode& node,
const PrintMsg_Print_Params& params) {
return PDFShouldDisableScaling(frame, node, params, false) ?
NO_MARGINS : PRINTABLE_AREA_MARGINS;
}
#endif
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
bool FitToPageEnabled(const base::DictionaryValue& job_settings) {
bool fit_to_paper_size = false;
if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) {
NOTREACHED();
}
return fit_to_paper_size;
}
// Returns the print scaling option to retain/scale/crop the source page size
// to fit the printable area of the paper.
//
// We retain the source page size when the current destination printer is
// SAVE_AS_PDF.
//
// We crop the source page size to fit the printable area or we print only the
// left top page contents when
// (1) Source is PDF and the user has requested not to fit to printable area
// via |job_settings|.
// (2) Source is PDF. This is the first preview request and print scaling
// option is disabled for initiator renderer plugin.
//
// In all other cases, we scale the source page to fit the printable area.
blink::WebPrintScalingOption GetPrintScalingOption(
blink::WebLocalFrame* frame,
const blink::WebNode& node,
bool source_is_html,
const base::DictionaryValue& job_settings,
const PrintMsg_Print_Params& params) {
if (params.print_to_pdf)
return blink::WebPrintScalingOptionSourceSize;
if (!source_is_html) {
if (!FitToPageEnabled(job_settings))
return blink::WebPrintScalingOptionNone;
bool no_plugin_scaling = PDFShouldDisableScaling(frame, node, params,
true);
if (params.is_first_request && no_plugin_scaling)
return blink::WebPrintScalingOptionNone;
}
return blink::WebPrintScalingOptionFitToPrintableArea;
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Helper function to scale and round an integer value with a double valued
// scaling.
int ScaleAndRound(int value, double scaling) {
return static_cast<int>(static_cast<double>(value) / scaling);
}
// Helper function to scale the width and height of a gfx::Size by scaling.
gfx::Size ScaleAndRoundSize(gfx::Size original, double scaling) {
return gfx::Size(ScaleAndRound(original.width(), scaling),
ScaleAndRound(original.height(), scaling));
}
PrintMsg_Print_Params CalculatePrintParamsForCss(
blink::WebLocalFrame* frame,
int page_index,
const PrintMsg_Print_Params& page_params,
bool ignore_css_margins,
bool fit_to_page,
double* scale_factor) {
PrintMsg_Print_Params css_params =
GetCssPrintParams(frame, page_index, page_params);
PrintMsg_Print_Params params = page_params;
EnsureOrientationMatches(css_params, ¶ms);
params.content_size = ScaleAndRoundSize(params.content_size, *scale_factor);
if (ignore_css_margins && fit_to_page)
return params;
PrintMsg_Print_Params result_params = css_params;
// If not printing a pdf or fitting to page, scale the page size.
double page_scaling = params.print_to_pdf ? 1.0f : *scale_factor;
if (!fit_to_page) {
result_params.page_size =
ScaleAndRoundSize(result_params.page_size, page_scaling);
}
if (ignore_css_margins) {
// Since not fitting to page, scale the page size and margins.
params.margin_left = ScaleAndRound(params.margin_left, page_scaling);
params.margin_top = ScaleAndRound(params.margin_top, page_scaling);
params.page_size = ScaleAndRoundSize(params.page_size, page_scaling);
result_params.margin_top = params.margin_top;
result_params.margin_left = params.margin_left;
DCHECK(!fit_to_page);
// Since we are ignoring the margins, the css page size is no longer
// valid for content.
int default_margin_right = params.page_size.width() -
params.content_size.width() - params.margin_left;
int default_margin_bottom = params.page_size.height() -
params.content_size.height() -
params.margin_top;
result_params.content_size =
gfx::Size(result_params.page_size.width() - result_params.margin_left -
default_margin_right,
result_params.page_size.height() - result_params.margin_top -
default_margin_bottom);
} else {
// Using the CSS parameters. Scale CSS content size.
result_params.content_size =
ScaleAndRoundSize(result_params.content_size, *scale_factor);
if (fit_to_page) {
double factor = FitPrintParamsToPage(params, &result_params);
if (scale_factor)
*scale_factor = (*scale_factor) * factor;
} else {
// Already scaled the page, need to also scale the CSS margins since they
// are begin applied
result_params.margin_left =
ScaleAndRound(result_params.margin_left, page_scaling);
result_params.margin_top =
ScaleAndRound(result_params.margin_top, page_scaling);
}
}
return result_params;
}
} // namespace
FrameReference::FrameReference(blink::WebLocalFrame* frame) {
Reset(frame);
}
FrameReference::FrameReference() {
Reset(NULL);
}
FrameReference::~FrameReference() {
}
void FrameReference::Reset(blink::WebLocalFrame* frame) {
if (frame) {
view_ = frame->view();
frame_ = frame;
} else {
view_ = NULL;
frame_ = NULL;
}
}
blink::WebLocalFrame* FrameReference::GetFrame() {
if (view_ == NULL || frame_ == NULL)
return NULL;
for (blink::WebFrame* frame = view_->mainFrame(); frame != NULL;
frame = frame->traverseNext()) {
if (frame == frame_)
return frame_;
}
return NULL;
}
blink::WebView* FrameReference::view() {
return view_;
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// static - Not anonymous so that platform implementations can use it.
void PrintWebViewHelper::PrintHeaderAndFooter(
blink::WebCanvas* canvas,
int page_number,
int total_pages,
const blink::WebFrame& source_frame,
float webkit_scale_factor,
const PageSizeMargins& page_layout,
const PrintMsg_Print_Params& params) {
SkAutoCanvasRestore auto_restore(canvas, true);
canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor);
blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right +
page_layout.content_width,
page_layout.margin_top + page_layout.margin_bottom +
page_layout.content_height);
blink::WebView* web_view =
blink::WebView::create(nullptr, blink::WebPageVisibilityStateVisible);
web_view->settings()->setJavaScriptEnabled(true);
blink::WebFrameClient frame_client;
blink::WebLocalFrame* frame = blink::WebLocalFrame::create(
blink::WebTreeScopeType::Document, &frame_client);
web_view->setMainFrame(frame);
blink::WebFrameWidget::create(nullptr, web_view, frame);
base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString(
IDR_PRINT_PREVIEW_PAGE));
// Load page with script to avoid async operations.
ExecuteScript(frame, kPageLoadScriptFormat, html);
std::unique_ptr<base::DictionaryValue> options(new base::DictionaryValue());
options.reset(new base::DictionaryValue());
options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime());
options->SetDouble("width", page_size.width);
options->SetDouble("height", page_size.height);
options->SetDouble("topMargin", page_layout.margin_top);
options->SetDouble("bottomMargin", page_layout.margin_bottom);
options->SetString("pageNumber",
base::StringPrintf("%d/%d", page_number, total_pages));
options->SetString("url", params.url);
base::string16 title = source_frame.document().title();
options->SetString("title", title.empty() ? params.title : title);
ExecuteScript(frame, kPageSetupScriptFormat, *options);
blink::WebPrintParams webkit_params(page_size);
webkit_params.printerDPI = GetDPI(¶ms);
frame->printBegin(webkit_params);
frame->printPage(0, canvas);
frame->printEnd();
web_view->close();
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
// static - Not anonymous so that platform implementations can use it.
float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame,
int page_number,
const gfx::Rect& canvas_area,
const gfx::Rect& content_area,
double scale_factor,
blink::WebCanvas* canvas) {
SkAutoCanvasRestore auto_restore(canvas, true);
canvas->translate((content_area.x() - canvas_area.x()) / scale_factor,
(content_area.y() - canvas_area.y()) / scale_factor);
return frame->printPage(page_number, canvas);
}
// Class that calls the Begin and End print functions on the frame and changes
// the size of the view temporarily to support full page printing..
class PrepareFrameAndViewForPrint : public blink::WebViewClient,
public blink::WebFrameClient {
public:
PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params,
blink::WebLocalFrame* frame,
const blink::WebNode& node,
bool ignore_css_margins);
~PrepareFrameAndViewForPrint() override;
// Optional. Replaces |frame_| with selection if needed. Will call |on_ready|
// when completed.
void CopySelectionIfNeeded(const WebPreferences& preferences,
const base::Closure& on_ready);
// Prepares frame for printing.
void StartPrinting();
blink::WebLocalFrame* frame() { return frame_.GetFrame(); }
const blink::WebNode& node() const { return node_to_print_; }
int GetExpectedPageCount() const { return expected_pages_count_; }
void FinishPrinting();
bool IsLoadingSelection() {
// It's not selection if not |owns_web_view_|.
return owns_web_view_ && frame() && frame()->isLoading();
}
private:
// blink::WebViewClient:
void didStopLoading() override;
// TODO(ojan): Remove this override and have this class use a non-null
// layerTreeView.
bool allowsBrokenNullLayerTreeView() const override;
// blink::WebFrameClient:
blink::WebLocalFrame* createChildFrame(
blink::WebLocalFrame* parent,
blink::WebTreeScopeType scope,
const blink::WebString& name,
const blink::WebString& unique_name,
blink::WebSandboxFlags sandbox_flags,
const blink::WebFrameOwnerProperties& frame_owner_properties) override;
void CallOnReady();
void ResizeForPrinting();
void RestoreSize();
void CopySelection(const WebPreferences& preferences);
FrameReference frame_;
blink::WebNode node_to_print_;
bool owns_web_view_;
blink::WebPrintParams web_print_params_;
gfx::Size prev_view_size_;
gfx::Size prev_scroll_offset_;
int expected_pages_count_;
base::Closure on_ready_;
bool should_print_backgrounds_;
bool should_print_selection_only_;
bool is_printing_started_;
base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint);
};
PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
const PrintMsg_Print_Params& params,
blink::WebLocalFrame* frame,
const blink::WebNode& node,
bool ignore_css_margins)
: frame_(frame),
node_to_print_(node),
owns_web_view_(false),
expected_pages_count_(0),
should_print_backgrounds_(params.should_print_backgrounds),
should_print_selection_only_(params.selection_only),
is_printing_started_(false),
weak_ptr_factory_(this) {
PrintMsg_Print_Params print_params = params;
if (!should_print_selection_only_ ||
!PrintingNodeOrPdfFrame(frame, node_to_print_)) {
bool fit_to_page =
ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params);
ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_);
frame->printBegin(web_print_params_, node_to_print_);
double scale_factor = 1.0f;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
if (print_params.scale_factor >= PrintWebViewHelper::kEpsilon)
scale_factor = print_params.scale_factor;
#endif
print_params = CalculatePrintParamsForCss(
frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor);
frame->printEnd();
}
ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_);
}
PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
FinishPrinting();
}
void PrepareFrameAndViewForPrint::ResizeForPrinting() {
// Layout page according to printer page size. Since WebKit shrinks the
// size of the page automatically (from 133.3% to 200%) we trick it to
// think the page is 133.3% larger so the size of the page is correct for
// minimum (default) scaling.
// The scaling factor 1.25 was originally chosen as a magic number that
// was 'about right'. However per https://crbug.com/273306 1.333 seems to be
// the number that produces output with the correct physical size for elements
// that are specified in cm, mm, pt etc.
// This is important for sites that try to fill the page.
gfx::Size print_layout_size(web_print_params_.printContentArea.width,
web_print_params_.printContentArea.height);
print_layout_size.set_height(
ScaleAndRound(print_layout_size.height(), kPrintingMinimumShrinkFactor));
if (!frame())
return;
// Backup size and offset if it's a local frame.
blink::WebView* web_view = frame_.view();
if (blink::WebFrame* web_frame = web_view->mainFrame()) {
if (web_frame->isWebLocalFrame())
prev_scroll_offset_ = web_frame->getScrollOffset();
}
prev_view_size_ = web_view->size();
web_view->resize(print_layout_size);
}
void PrepareFrameAndViewForPrint::StartPrinting() {
blink::WebView* web_view = frame_.view();
web_view->settings()->setShouldPrintBackgrounds(should_print_backgrounds_);
expected_pages_count_ =
frame()->printBegin(web_print_params_, node_to_print_);
ResizeForPrinting();
is_printing_started_ = true;
}
void PrepareFrameAndViewForPrint::CopySelectionIfNeeded(
const WebPreferences& preferences,
const base::Closure& on_ready) {
on_ready_ = on_ready;
if (should_print_selection_only_) {
CopySelection(preferences);
} else {
// Call immediately, async call crashes scripting printing.
CallOnReady();
}
}
void PrepareFrameAndViewForPrint::CopySelection(
const WebPreferences& preferences) {
ResizeForPrinting();
std::string url_str = "data:text/html;charset=utf-8,";
url_str.append(
net::EscapeQueryParamValue(frame()->selectionAsMarkup().utf8(), false));
RestoreSize();
// Create a new WebView with the same settings as the current display one.
// Except that we disable javascript (don't want any active content running
// on the page).
WebPreferences prefs = preferences;
prefs.javascript_enabled = false;
blink::WebView* web_view =
blink::WebView::create(this, blink::WebPageVisibilityStateVisible);
owns_web_view_ = true;
content::RenderView::ApplyWebPreferences(prefs, web_view);
blink::WebLocalFrame* main_frame =
blink::WebLocalFrame::create(blink::WebTreeScopeType::Document, this);
web_view->setMainFrame(main_frame);
blink::WebFrameWidget::create(this, web_view, main_frame);
frame_.Reset(web_view->mainFrame()->toWebLocalFrame());
node_to_print_.reset();
// When loading is done this will call didStopLoading() and that will do the
// actual printing.
blink::WebURLRequest request = blink::WebURLRequest(GURL(url_str));
frame()->loadRequest(request);
}
bool PrepareFrameAndViewForPrint::allowsBrokenNullLayerTreeView() const {
return true;
}
void PrepareFrameAndViewForPrint::didStopLoading() {
DCHECK(!on_ready_.is_null());
// Don't call callback here, because it can delete |this| and WebView that is
// called didStopLoading.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&PrepareFrameAndViewForPrint::CallOnReady,
weak_ptr_factory_.GetWeakPtr()));
}
blink::WebLocalFrame* PrepareFrameAndViewForPrint::createChildFrame(
blink::WebLocalFrame* parent,
blink::WebTreeScopeType scope,
const blink::WebString& name,
const blink::WebString& unique_name,
blink::WebSandboxFlags sandbox_flags,
const blink::WebFrameOwnerProperties& frame_owner_properties) {
blink::WebLocalFrame* frame = blink::WebLocalFrame::create(scope, this);
parent->appendChild(frame);
return frame;
}
void PrepareFrameAndViewForPrint::CallOnReady() {
return on_ready_.Run(); // Can delete |this|.
}
void PrepareFrameAndViewForPrint::RestoreSize() {
if (!frame())
return;
blink::WebView* web_view = frame_.GetFrame()->view();
web_view->resize(prev_view_size_);
if (blink::WebFrame* web_frame = web_view->mainFrame()) {
if (web_frame->isWebLocalFrame())
web_frame->setScrollOffset(prev_scroll_offset_);
}
}
void PrepareFrameAndViewForPrint::FinishPrinting() {
blink::WebLocalFrame* frame = frame_.GetFrame();
if (frame) {
blink::WebView* web_view = frame->view();
if (is_printing_started_) {
is_printing_started_ = false;
if (!owns_web_view_) {
web_view->settings()->setShouldPrintBackgrounds(false);
RestoreSize();
}
frame->printEnd();
}
if (owns_web_view_) {
DCHECK(!frame->isLoading());
owns_web_view_ = false;
frame->frameWidget()->close();
web_view->close();
}
}
frame_.Reset(NULL);
on_ready_.Reset();
}
bool PrintWebViewHelper::Delegate::IsAskPrintSettingsEnabled() {
return true;
}
bool PrintWebViewHelper::Delegate::IsScriptedPrintEnabled() {
return true;
}
PrintWebViewHelper::PrintWebViewHelper(content::RenderFrame* render_frame,
std::unique_ptr<Delegate> delegate)
: content::RenderFrameObserver(render_frame),
content::RenderFrameObserverTracker<PrintWebViewHelper>(render_frame),
reset_prep_frame_view_(false),
is_print_ready_metafile_sent_(false),
ignore_css_margins_(false),
is_printing_enabled_(true),
notify_browser_of_print_failure_(true),
print_for_preview_(false),
delegate_(std::move(delegate)),
print_node_in_progress_(false),
is_loading_(false),
is_scripted_preview_delayed_(false),
ipc_nesting_level_(0),
weak_ptr_factory_(this) {
if (!delegate_->IsPrintPreviewEnabled())
DisablePreview();
}
PrintWebViewHelper::~PrintWebViewHelper() {
}
// static
void PrintWebViewHelper::DisablePreview() {
g_is_preview_enabled = false;
}
bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed(
blink::WebLocalFrame* frame,
bool user_initiated) {
if (!is_printing_enabled_ || !delegate_->IsScriptedPrintEnabled())
return false;
// If preview is enabled, then the print dialog is tab modal, and the user
// can always close the tab on a mis-behaving page (the system print dialog
// is app modal). If the print was initiated through user action, don't
// throttle. Or, if the command line flag to skip throttling has been set.
return user_initiated || g_is_preview_enabled ||
scripting_throttler_.IsAllowed(frame);
}
void PrintWebViewHelper::DidStartProvisionalLoad() {
is_loading_ = true;
}
void PrintWebViewHelper::DidFailProvisionalLoad(
const blink::WebURLError& error) {
DidFinishLoad();
}
void PrintWebViewHelper::DidFinishLoad() {
is_loading_ = false;
if (!on_stop_loading_closure_.is_null()) {
on_stop_loading_closure_.Run();
on_stop_loading_closure_.Reset();
}
}
void PrintWebViewHelper::ScriptedPrint(bool user_initiated) {
// Allow Prerendering to cancel this print request if necessary.
if (delegate_->CancelPrerender(render_frame()))
return;
blink::WebLocalFrame* web_frame = render_frame()->GetWebFrame();
if (!IsScriptInitiatedPrintAllowed(web_frame, user_initiated))
return;
if (delegate_->OverridePrint(web_frame))
return;
if (g_is_preview_enabled) {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
print_preview_context_.InitWithFrame(web_frame);
RequestPrintPreview(PRINT_PREVIEW_SCRIPTED);
#endif
} else {
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
Print(web_frame, blink::WebNode(), true /* is_scripted? */);
#endif
}
// WARNING: |this| may be gone at this point. Do not do any more work here and
// just return.
}
bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
// The class is not designed to handle recursive messages. This is not
// expected during regular flow. However, during rendering of content for
// printing, lower level code may run nested message loop. E.g. PDF may has
// script to show message box http://crbug.com/502562. In that moment browser
// may receive updated printer capabilities and decide to restart print
// preview generation. When this happened message handling function may
// choose to ignore message or safely crash process.
++ipc_nesting_level_;
auto self = weak_ptr_factory_.GetWeakPtr();
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message)
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog)
#endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
#if BUILDFLAG(ENABLE_BASIC_PRINTING) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview)
#endif
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
IPC_MESSAGE_HANDLER(PrintMsg_SetPrintingEnabled, OnSetPrintingEnabled)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
// Check if |this| is still valid. e.g. when OnPrintPages() returns.
if (self)
--ipc_nesting_level_;
return handled;
}
void PrintWebViewHelper::OnDestruct() {
delete this;
}
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
void PrintWebViewHelper::OnPrintPages() {
if (ipc_nesting_level_> 1)
return;
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
// If we are printing a PDF extension frame, find the plugin node and print
// that instead.
auto plugin = delegate_->GetPdfElement(frame);
Print(frame, plugin, false /* is_scripted? */);
// WARNING: |this| may be gone at this point. Do not do any more work here and
// just return.
}
void PrintWebViewHelper::OnPrintForSystemDialog() {
if (ipc_nesting_level_> 1)
return;
blink::WebLocalFrame* frame = print_preview_context_.source_frame();
if (!frame) {
NOTREACHED();
return;
}
Print(frame, print_preview_context_.source_node(), false);
// WARNING: |this| may be gone at this point. Do not do any more work here and
// just return.
}
#endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
#if BUILDFLAG(ENABLE_BASIC_PRINTING) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintWebViewHelper::OnPrintForPrintPreview(
const base::DictionaryValue& job_settings) {
CHECK_LE(ipc_nesting_level_, 1);
// If still not finished with earlier print request simply ignore.
if (prep_frame_view_)
return;
blink::WebDocument document = render_frame()->GetWebFrame()->document();
// <object>/<iframe> with id="pdf-viewer" is created in
// chrome/browser/resources/print_preview/print_preview.js
blink::WebElement pdf_element = document.getElementById("pdf-viewer");
if (pdf_element.isNull()) {
NOTREACHED();
return;
}
// The out-of-process plugin element is nested within a frame. In tests, there
// may not be an iframe containing the out-of-process plugin, so continue with
// the element with ID "pdf-viewer" if it isn't an iframe.
blink::WebLocalFrame* plugin_frame = pdf_element.document().frame();
blink::WebElement plugin_element = pdf_element;
if (pdf_element.hasHTMLTagName("iframe")) {
plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element);
plugin_element = delegate_->GetPdfElement(plugin_frame);
if (plugin_element.isNull()) {
NOTREACHED();
return;
}
}
// Set |print_for_preview_| flag and autoreset it to back to original
// on return.
base::AutoReset<bool> set_printing_flag(&print_for_preview_, true);
if (!UpdatePrintSettings(plugin_frame, plugin_element, job_settings)) {
LOG(ERROR) << "UpdatePrintSettings failed";
DidFinishPrinting(FAIL_PRINT);
return;
}
// Print page onto entire page not just printable area. Preview PDF already
// has content in correct position taking into account page size and printable
// area.
// TODO(vitalybuka) : Make this consistent on all platform. This change
// affects Windows only. On Linux and OSX RenderPagesForPrint does not use
// printable_area. Also we can't change printable_area deeper inside
// RenderPagesForPrint for Windows, because it's used also by native
// printing and it expects real printable_area value.
// See http://crbug.com/123408
PrintMsg_Print_Params& print_params = print_pages_params_->params;
print_params.printable_area = gfx::Rect(print_params.page_size);
// Render Pages for printing.
if (!RenderPagesForPrint(plugin_frame, plugin_element)) {
LOG(ERROR) << "RenderPagesForPrint failed";
DidFinishPrinting(FAIL_PRINT);
}
}
#endif // BUILDFLAG(ENABLE_BASIC_PRINTING) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout(
const PageSizeMargins& page_layout_in_points,
gfx::Size* page_size,
gfx::Rect* content_area) {
*page_size = gfx::Size(
page_layout_in_points.content_width + page_layout_in_points.margin_right +
page_layout_in_points.margin_left,
page_layout_in_points.content_height + page_layout_in_points.margin_top +
page_layout_in_points.margin_bottom);
*content_area = gfx::Rect(page_layout_in_points.margin_left,
page_layout_in_points.margin_top,
page_layout_in_points.content_width,
page_layout_in_points.content_height);
}
void PrintWebViewHelper::UpdateFrameMarginsCssInfo(
const base::DictionaryValue& settings) {
int margins_type = 0;
if (!settings.GetInteger(kSettingMarginsType, &margins_type))
margins_type = DEFAULT_MARGINS;
ignore_css_margins_ = (margins_type != DEFAULT_MARGINS);
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) {
if (ipc_nesting_level_ > 1)
return;
print_preview_context_.OnPrintPreview();
UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent",
PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX);
if (!print_preview_context_.source_frame()) {
DidFinishPrinting(FAIL_PREVIEW);
return;
}
if (!UpdatePrintSettings(print_preview_context_.source_frame(),
print_preview_context_.source_node(), settings)) {
if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
routing_id(), print_pages_params_
? print_pages_params_->params.document_cookie
: 0));
notify_browser_of_print_failure_ = false; // Already sent.
}
DidFinishPrinting(FAIL_PREVIEW);
return;
}
// Set the options from document if we are previewing a pdf and send a
// message to browser.
if (print_pages_params_->params.is_first_request &&
!print_preview_context_.IsModifiable()) {
PrintHostMsg_SetOptionsFromDocument_Params options;
if (SetOptionsFromPdfDocument(&options))
Send(new PrintHostMsg_SetOptionsFromDocument(routing_id(), options));
}
is_print_ready_metafile_sent_ = false;
// PDF printer device supports alpha blending.
print_pages_params_->params.supports_alpha_blend = true;
bool generate_draft_pages = false;
if (!settings.GetBoolean(kSettingGenerateDraftData, &generate_draft_pages)) {
NOTREACHED();
}
print_preview_context_.set_generate_draft_pages(generate_draft_pages);
PrepareFrameForPreviewDocument();
}
void PrintWebViewHelper::PrepareFrameForPreviewDocument() {
reset_prep_frame_view_ = false;
if (!print_pages_params_ || CheckForCancel()) {
DidFinishPrinting(FAIL_PREVIEW);
return;
}
// Don't reset loading frame or WebKit will fail assert. Just retry when
// current selection is loaded.
if (prep_frame_view_ && prep_frame_view_->IsLoadingSelection()) {
reset_prep_frame_view_ = true;
return;
}
const PrintMsg_Print_Params& print_params = print_pages_params_->params;
prep_frame_view_.reset(new PrepareFrameAndViewForPrint(
print_params, print_preview_context_.source_frame(),
print_preview_context_.source_node(), ignore_css_margins_));
prep_frame_view_->CopySelectionIfNeeded(
render_frame()->GetWebkitPreferences(),
base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument,
base::Unretained(this)));
}
void PrintWebViewHelper::OnFramePreparedForPreviewDocument() {
if (reset_prep_frame_view_) {
PrepareFrameForPreviewDocument();
return;
}
DidFinishPrinting(CreatePreviewDocument() ? OK : FAIL_PREVIEW);
}
bool PrintWebViewHelper::CreatePreviewDocument() {
if (!print_pages_params_ || CheckForCancel())
return false;
UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent",
PREVIEW_EVENT_CREATE_DOCUMENT, PREVIEW_EVENT_MAX);
const PrintMsg_Print_Params& print_params = print_pages_params_->params;
const std::vector<int>& pages = print_pages_params_->pages;
if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(),
pages)) {
return false;
}
PageSizeMargins default_page_layout;
double scale_factor =
print_params.scale_factor >= kEpsilon ? print_params.scale_factor : 1.0f;
ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0,
print_params, ignore_css_margins_,
&scale_factor, &default_page_layout);
bool has_page_size_style =
PrintingFrameHasPageSizeStyle(print_preview_context_.prepared_frame(),
print_preview_context_.total_page_count());
int dpi = GetDPI(&print_params);
gfx::Rect printable_area_in_points(
ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch),
ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch),
ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch),
ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch));
double fit_to_page_scale_factor = 1.0f;
if (!print_preview_context_.IsModifiable()) {
blink::WebLocalFrame* source_frame = print_preview_context_.source_frame();
const blink::WebNode& source_node = print_preview_context_.source_node();
blink::WebPrintPresetOptions preset_options;
if (source_frame->getPrintPresetOptionsForPlugin(source_node,
&preset_options)) {
if (preset_options.isPageSizeUniform) {
double scale_width =
static_cast<double>(printable_area_in_points.width()) /
static_cast<double>(preset_options.uniformPageSize.width);
double scale_height =
static_cast<double>(printable_area_in_points.height()) /
static_cast<double>(preset_options.uniformPageSize.height);
fit_to_page_scale_factor = std::min(scale_width, scale_height);
} else {
fit_to_page_scale_factor = 0.0f;
}
}
}
int fit_to_page_scaling = static_cast<int>(100.0f * fit_to_page_scale_factor);
// Margins: Send default page layout to browser process.
Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(),
default_page_layout,
printable_area_in_points,
has_page_size_style));
PrintHostMsg_DidGetPreviewPageCount_Params params;
params.page_count = print_preview_context_.total_page_count();
params.is_modifiable = print_preview_context_.IsModifiable();
params.fit_to_page_scaling = fit_to_page_scaling;
params.document_cookie = print_params.document_cookie;
params.preview_request_id = print_params.preview_request_id;
params.clear_preview_data = print_preview_context_.generate_draft_pages();
Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params));
if (CheckForCancel())
return false;
while (!print_preview_context_.IsFinalPageRendered()) {
int page_number = print_preview_context_.GetNextPageNumber();
DCHECK_GE(page_number, 0);
if (!RenderPreviewPage(page_number, print_params))
return false;
if (CheckForCancel())
return false;
// We must call PrepareFrameAndViewForPrint::FinishPrinting() (by way of
// print_preview_context_.AllPagesRendered()) before calling
// FinalizePrintReadyDocument() when printing a PDF because the plugin
// code does not generate output until we call FinishPrinting(). We do not
// generate draft pages for PDFs, so IsFinalPageRendered() and
// IsLastPageOfPrintReadyMetafile() will be true in the same iteration of
// the loop.
if (print_preview_context_.IsFinalPageRendered())
print_preview_context_.AllPagesRendered();
if (print_preview_context_.IsLastPageOfPrintReadyMetafile()) {
DCHECK(print_preview_context_.IsModifiable() ||
print_preview_context_.IsFinalPageRendered());
if (!FinalizePrintReadyDocument())
return false;
}
}
print_preview_context_.Finished();
return true;
}
#if !defined(OS_MACOSX) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
bool PrintWebViewHelper::RenderPreviewPage(
int page_number,
const PrintMsg_Print_Params& print_params) {
PrintMsg_PrintPage_Params page_params;
page_params.params = print_params;
page_params.page_number = page_number;
std::unique_ptr<PdfMetafileSkia> draft_metafile;
PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) {
draft_metafile.reset(new PdfMetafileSkia(PDF_SKIA_DOCUMENT_TYPE));
initial_render_metafile = draft_metafile.get();
}
base::TimeTicks begin_time = base::TimeTicks::Now();
PrintPageInternal(page_params, print_preview_context_.prepared_frame(),
initial_render_metafile, nullptr, nullptr);
print_preview_context_.RenderedPreviewPage(
base::TimeTicks::Now() - begin_time);
if (draft_metafile.get()) {
draft_metafile->FinishDocument();
} else if (print_preview_context_.IsModifiable() &&
print_preview_context_.generate_draft_pages()) {
DCHECK(!draft_metafile.get());
draft_metafile =
print_preview_context_.metafile()->GetMetafileForCurrentPage(
PDF_SKIA_DOCUMENT_TYPE);
}
return PreviewPageRendered(page_number, draft_metafile.get());
}
#endif // !defined(OS_MACOSX) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
bool PrintWebViewHelper::FinalizePrintReadyDocument() {
DCHECK(!is_print_ready_metafile_sent_);
print_preview_context_.FinalizePrintReadyDocument();
PdfMetafileSkia* metafile = print_preview_context_.metafile();
PrintHostMsg_DidPreviewDocument_Params preview_params;
// Ask the browser to create the shared memory for us.
if (!CopyMetafileDataToSharedMem(*metafile,
&(preview_params.metafile_data_handle))) {
LOG(ERROR) << "CopyMetafileDataToSharedMem failed";
print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
return false;
}
preview_params.data_size = metafile->GetDataSize();
preview_params.document_cookie = print_pages_params_->params.document_cookie;
preview_params.expected_pages_count =
print_preview_context_.total_page_count();
preview_params.modifiable = print_preview_context_.IsModifiable();
preview_params.preview_request_id =
print_pages_params_->params.preview_request_id;
is_print_ready_metafile_sent_ = true;
Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), preview_params));
return true;
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintWebViewHelper::OnPrintingDone(bool success) {
if (ipc_nesting_level_ > 1)
return;
notify_browser_of_print_failure_ = false;
if (!success)
LOG(ERROR) << "Failure in OnPrintingDone";
DidFinishPrinting(success ? OK : FAIL_PRINT);
}
void PrintWebViewHelper::OnSetPrintingEnabled(bool enabled) {
is_printing_enabled_ = enabled;
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintWebViewHelper::OnInitiatePrintPreview(bool has_selection) {
if (ipc_nesting_level_ > 1)
return;
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
// If we are printing a PDF extension frame, find the plugin node and print
// that instead.
auto plugin = delegate_->GetPdfElement(frame);
if (!plugin.isNull()) {
PrintNode(plugin);
return;
}
print_preview_context_.InitWithFrame(frame);
RequestPrintPreview(has_selection
? PRINT_PREVIEW_USER_INITIATED_SELECTION
: PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME);
}
#endif
bool PrintWebViewHelper::IsPrintingEnabled() const {
return is_printing_enabled_;
}
void PrintWebViewHelper::PrintNode(const blink::WebNode& node) {
if (node.isNull() || !node.document().frame()) {
// This can occur when the context menu refers to an invalid WebNode.
// See http://crbug.com/100890#c17 for a repro case.
return;
}
if (print_node_in_progress_) {
// This can happen as a result of processing sync messages when printing
// from ppapi plugins. It's a rare case, so its OK to just fail here.
// See http://crbug.com/159165.
return;
}
print_node_in_progress_ = true;
if (g_is_preview_enabled) {
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
print_preview_context_.InitWithNode(node);
RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE);
#endif
} else {
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
// Make a copy of the node, in case RenderView::OnContextMenuClosed() resets
// its |context_menu_node_|.
blink::WebNode duplicate_node(node);
auto self = weak_ptr_factory_.GetWeakPtr();
Print(duplicate_node.document().frame(), duplicate_node,
false /* is_scripted? */);
// Check if |this| is still valid.
if (!self)
return;
#endif
}
print_node_in_progress_ = false;
}
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
void PrintWebViewHelper::Print(blink::WebLocalFrame* frame,
const blink::WebNode& node,
bool is_scripted) {
// If still not finished with earlier print request simply ignore.
if (prep_frame_view_)
return;
FrameReference frame_ref(frame);
int expected_page_count = 0;
if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
DidFinishPrinting(FAIL_PRINT_INIT);
return; // Failed to init print page settings.
}
// Some full screen plugins can say they don't want to print.
if (!expected_page_count) {
DidFinishPrinting(FAIL_PRINT);
return;
}
// Ask the browser to show UI to retrieve the final print settings.
if (delegate_->IsAskPrintSettingsEnabled()) {
// PrintHostMsg_ScriptedPrint in GetPrintSettingsFromUser() will reset
// |print_scaling_option|, so save the value here and restore it afterwards.
blink::WebPrintScalingOption scaling_option =
print_pages_params_->params.print_scaling_option;
PrintMsg_PrintPages_Params print_settings;
auto self = weak_ptr_factory_.GetWeakPtr();
GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count,
is_scripted, &print_settings);
// Check if |this| is still valid.
if (!self)
return;
print_settings.params.print_scaling_option = scaling_option;
SetPrintPagesParams(print_settings);
if (!print_settings.params.dpi || !print_settings.params.document_cookie) {
DidFinishPrinting(OK); // Release resources and fail silently on failure.
return;
}
}
// Render Pages for printing.
if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) {
LOG(ERROR) << "RenderPagesForPrint failed";
DidFinishPrinting(FAIL_PRINT);
}
scripting_throttler_.Reset();
}
#endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
switch (result) {
case OK:
break;
case FAIL_PRINT_INIT:
DCHECK(!notify_browser_of_print_failure_);
break;
case FAIL_PRINT:
if (notify_browser_of_print_failure_ && print_pages_params_) {
int cookie = print_pages_params_->params.document_cookie;
Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie));
}
break;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
case FAIL_PREVIEW:
int cookie =
print_pages_params_ ? print_pages_params_->params.document_cookie : 0;
if (notify_browser_of_print_failure_) {
LOG(ERROR) << "CreatePreviewDocument failed";
Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie));
} else {
Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie));
}
print_preview_context_.Failed(notify_browser_of_print_failure_);
break;
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
}
prep_frame_view_.reset();
print_pages_params_.reset();
notify_browser_of_print_failure_ = true;
}
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
void PrintWebViewHelper::OnFramePreparedForPrintPages() {
PrintPages();
FinishFramePrinting();
}
void PrintWebViewHelper::PrintPages() {
if (!prep_frame_view_) // Printing is already canceled or failed.
return;
prep_frame_view_->StartPrinting();
int page_count = prep_frame_view_->GetExpectedPageCount();
if (!page_count) {
LOG(ERROR) << "Can't print 0 pages.";
return DidFinishPrinting(FAIL_PRINT);
}
const PrintMsg_PrintPages_Params& params = *print_pages_params_;
const PrintMsg_Print_Params& print_params = params.params;
#if !defined(OS_ANDROID)
// TODO(vitalybuka): should be page_count or valid pages from params.pages.
// See http://crbug.com/161576
Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(),
print_params.document_cookie,
page_count));
#endif // !defined(OS_ANDROID)
if (print_params.preview_ui_id < 0) {
// Printing for system dialog.
int printed_count = params.pages.empty() ? page_count : params.pages.size();
UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.SystemDialog", printed_count);
}
if (!PrintPagesNative(prep_frame_view_->frame(), page_count)) {
LOG(ERROR) << "Printing failed.";
return DidFinishPrinting(FAIL_PRINT);
}
}
void PrintWebViewHelper::FinishFramePrinting() {
prep_frame_view_.reset();
}
#endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
// static - Not anonymous so that platform implementations can use it.
void PrintWebViewHelper::ComputePageLayoutInPointsForCss(
blink::WebLocalFrame* frame,
int page_index,
const PrintMsg_Print_Params& page_params,
bool ignore_css_margins,
double* scale_factor,
PageSizeMargins* page_layout_in_points) {
double input_scale_factor = *scale_factor;
PrintMsg_Print_Params params = CalculatePrintParamsForCss(
frame, page_index, page_params, ignore_css_margins,
IsWebPrintScalingOptionFitToPage(page_params), scale_factor);
CalculatePageLayoutFromPrintParams(params, input_scale_factor,
page_layout_in_points);
}
// static - Not anonymous so that platform implementations can use it.
std::vector<int> PrintWebViewHelper::GetPrintedPages(
const PrintMsg_PrintPages_Params& params,
int page_count) {
std::vector<int> printed_pages;
if (params.pages.empty()) {
for (int i = 0; i < page_count; ++i) {
printed_pages.push_back(i);
}
} else {
for (int page : params.pages) {
if (page >= 0 && page < page_count) {
printed_pages.push_back(page);
}
}
}
return printed_pages;
}
bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) {
PrintMsg_PrintPages_Params settings;
Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
&settings.params));
// Check if the printer returned any settings, if the settings is empty, we
// can safely assume there are no printer drivers configured. So we safely
// terminate.
bool result = true;
if (!PrintMsg_Print_Params_IsValid(settings.params))
result = false;
// Reset to default values.
ignore_css_margins_ = false;
settings.pages.clear();
settings.params.print_scaling_option = blink::WebPrintScalingOptionSourceSize;
if (fit_to_paper_size) {
settings.params.print_scaling_option =
blink::WebPrintScalingOptionFitToPrintableArea;
}
SetPrintPagesParams(settings);
return result;
}
bool PrintWebViewHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
const blink::WebNode& node,
int* number_of_pages) {
DCHECK(frame);
bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node));
if (!InitPrintSettings(fit_to_paper_size)) {
notify_browser_of_print_failure_ = false;
Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
return false;
}
const PrintMsg_Print_Params& params = print_pages_params_->params;
PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_);
prepare.StartPrinting();
*number_of_pages = prepare.GetExpectedPageCount();
return true;
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
bool PrintWebViewHelper::SetOptionsFromPdfDocument(
PrintHostMsg_SetOptionsFromDocument_Params* options) {
blink::WebLocalFrame* source_frame = print_preview_context_.source_frame();
const blink::WebNode& source_node = print_preview_context_.source_node();
blink::WebPrintPresetOptions preset_options;
if (!source_frame->getPrintPresetOptionsForPlugin(source_node,
&preset_options)) {
return false;
}
options->is_scaling_disabled = PDFShouldDisableScalingBasedOnPreset(
preset_options, print_pages_params_->params, false);
options->copies = preset_options.copies;
// TODO(thestig) This should be a straight pass-through, but print preview
// does not currently support short-edge printing.
switch (preset_options.duplexMode) {
case blink::WebSimplex:
options->duplex = SIMPLEX;
break;
case blink::WebLongEdge:
options->duplex = LONG_EDGE;
break;
default:
options->duplex = UNKNOWN_DUPLEX_MODE;
break;
}
return true;
}
bool PrintWebViewHelper::UpdatePrintSettings(
blink::WebLocalFrame* frame,
const blink::WebNode& node,
const base::DictionaryValue& passed_job_settings) {
const base::DictionaryValue* job_settings = &passed_job_settings;
base::DictionaryValue modified_job_settings;
if (job_settings->empty()) {
if (!print_for_preview_)
print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
return false;
}
bool source_is_html = true;
if (print_for_preview_) {
if (!job_settings->GetBoolean(kSettingPreviewModifiable, &source_is_html)) {
NOTREACHED();
}
} else {
source_is_html = !PrintingNodeOrPdfFrame(frame, node);
}
if (print_for_preview_ || !source_is_html) {
modified_job_settings.MergeDictionary(job_settings);
modified_job_settings.SetBoolean(kSettingHeaderFooterEnabled, false);
modified_job_settings.SetInteger(kSettingMarginsType, NO_MARGINS);
job_settings = &modified_job_settings;
}
// Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
// possible.
int cookie =
print_pages_params_ ? print_pages_params_->params.document_cookie : 0;
PrintMsg_PrintPages_Params settings;
bool canceled = false;
Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), cookie, *job_settings,
&settings, &canceled));
if (canceled) {
notify_browser_of_print_failure_ = false;
return false;
}
if (!job_settings->GetInteger(kPreviewUIID, &settings.params.preview_ui_id)) {
NOTREACHED();
print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
return false;
}
if (!print_for_preview_) {
// Validate expected print preview settings.
if (!job_settings->GetInteger(kPreviewRequestID,
&settings.params.preview_request_id) ||
!job_settings->GetBoolean(kIsFirstRequest,
&settings.params.is_first_request)) {
NOTREACHED();
print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
return false;
}
settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
UpdateFrameMarginsCssInfo(*job_settings);
settings.params.print_scaling_option = GetPrintScalingOption(
frame, node, source_is_html, *job_settings, settings.params);
}
SetPrintPagesParams(settings);
if (PrintMsg_Print_Params_IsValid(settings.params))
return true;
if (print_for_preview_)
Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
else
print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
return false;
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
void PrintWebViewHelper::GetPrintSettingsFromUser(
blink::WebLocalFrame* frame,
const blink::WebNode& node,
int expected_pages_count,
bool is_scripted,
PrintMsg_PrintPages_Params* print_settings) {
PrintHostMsg_ScriptedPrint_Params params;
params.cookie = print_pages_params_->params.document_cookie;
params.has_selection = frame->hasSelection();
params.expected_pages_count = expected_pages_count;
MarginType margin_type = DEFAULT_MARGINS;
if (PrintingNodeOrPdfFrame(frame, node))
margin_type = GetMarginsForPdf(frame, node, print_pages_params_->params);
params.margin_type = margin_type;
params.is_scripted = is_scripted;
params.is_modifiable = !PrintingNodeOrPdfFrame(frame, node);
Send(new PrintHostMsg_DidShowPrintDialog(routing_id()));
print_pages_params_.reset();
auto msg = base::MakeUnique<PrintHostMsg_ScriptedPrint>(
routing_id(), params, print_settings);
msg->EnableMessagePumping();
Send(msg.release());
// WARNING: |this| may be gone at this point. Do not do any more work here
// and just return.
}
bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame,
const blink::WebNode& node) {
if (!frame || prep_frame_view_)
return false;
const PrintMsg_PrintPages_Params& params = *print_pages_params_;
const PrintMsg_Print_Params& print_params = params.params;
prep_frame_view_.reset(new PrepareFrameAndViewForPrint(
print_params, frame, node, ignore_css_margins_));
DCHECK(!print_pages_params_->params.selection_only ||
print_pages_params_->pages.empty());
prep_frame_view_->CopySelectionIfNeeded(
render_frame()->GetWebkitPreferences(),
base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages,
base::Unretained(this)));
return true;
}
#endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
#if !defined(OS_MACOSX)
void PrintWebViewHelper::PrintPageInternal(
const PrintMsg_PrintPage_Params& params,
blink::WebLocalFrame* frame,
PdfMetafileSkia* metafile,
gfx::Size* page_size_in_dpi,
gfx::Rect* content_area_in_dpi) {
PageSizeMargins page_layout_in_points;
double css_scale_factor = 1.0f;
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
if (params.params.scale_factor >= kEpsilon)
css_scale_factor = params.params.scale_factor;
#endif
ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
ignore_css_margins_, &css_scale_factor,
&page_layout_in_points);
gfx::Size page_size;
gfx::Rect content_area;
GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
&content_area);
int dpi = static_cast<int>(params.params.dpi);
// Calculate the actual page size and content area in dpi.
if (page_size_in_dpi) {
// Windows uses this for the actual page size. We have scaled page size
// to get blink to reflow the page, so scale it back to the real size
// before returning it.
*page_size_in_dpi =
gfx::Size(static_cast<int>(ConvertUnitDouble(page_size.width(),
kPointsPerInch, dpi) *
css_scale_factor),
static_cast<int>(ConvertUnitDouble(page_size.height(),
kPointsPerInch, dpi) *
css_scale_factor));
}
if (content_area_in_dpi) {
// Output PDF matches paper size and should be printer edge to edge.
*content_area_in_dpi =
gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height());
}
gfx::Rect canvas_area =
params.params.display_header_footer ? gfx::Rect(page_size) : content_area;
// TODO(thestig): Figure out why Linux is different.
#if defined(OS_WIN)
float webkit_page_shrink_factor =
frame->getPrintPageShrink(params.page_number);
float scale_factor = css_scale_factor * webkit_page_shrink_factor;
#else
float scale_factor = css_scale_factor;
#endif
SkCanvas* canvas = metafile->GetVectorCanvasForNewPage(
page_size, canvas_area, scale_factor);
if (!canvas)
return;
MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile);
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
if (params.params.display_header_footer) {
// TODO(thestig): Figure out why Linux needs this. It is almost certainly
// |printingMinimumShrinkFactor| from Blink.
#if defined(OS_WIN)
const float fudge_factor = 1;
#else
const float fudge_factor = kPrintingMinimumShrinkFactor;
#endif
// |page_number| is 0-based, so 1 is added.
PrintHeaderAndFooter(canvas, params.page_number + 1,
print_preview_context_.total_page_count(), *frame,
scale_factor / fudge_factor, page_layout_in_points,
params.params);
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
float webkit_scale_factor =
RenderPageContent(frame, params.page_number, canvas_area, content_area,
scale_factor, canvas);
DCHECK_GT(webkit_scale_factor, 0.0f);
// Done printing. Close the canvas to retrieve the compiled metafile.
if (!metafile->FinishPage())
NOTREACHED() << "metafile failed";
}
#endif // !defined(OS_MACOSX)
bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
const PdfMetafileSkia& metafile,
base::SharedMemoryHandle* shared_mem_handle) {
uint32_t buf_size = metafile.GetDataSize();
if (buf_size == 0)
return false;
std::unique_ptr<base::SharedMemory> shared_buf(
content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(buf_size));
if (!shared_buf)
return false;
if (!shared_buf->Map(buf_size))
return false;
if (!metafile.GetData(shared_buf->memory(), buf_size))
return false;
*shared_mem_handle =
base::SharedMemory::DuplicateHandle(shared_buf->handle());
return true;
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintWebViewHelper::ShowScriptedPrintPreview() {
if (is_scripted_preview_delayed_) {
is_scripted_preview_delayed_ = false;
Send(new PrintHostMsg_ShowScriptedPrintPreview(
routing_id(), print_preview_context_.IsModifiable()));
}
}
void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) {
const bool is_modifiable = print_preview_context_.IsModifiable();
const bool has_selection = print_preview_context_.HasSelection();
PrintHostMsg_RequestPrintPreview_Params params;
params.is_modifiable = is_modifiable;
params.has_selection = has_selection;
switch (type) {
case PRINT_PREVIEW_SCRIPTED: {
// Shows scripted print preview in two stages.
// 1. PrintHostMsg_SetupScriptedPrintPreview blocks this call and JS by
// pumping messages here.
// 2. PrintHostMsg_ShowScriptedPrintPreview shows preview once the
// document has been loaded.
is_scripted_preview_delayed_ = true;
if (is_loading_ && GetPlugin(print_preview_context_.source_frame())) {
// Wait for DidStopLoading. Plugins may not know the correct
// |is_modifiable| value until they are fully loaded, which occurs when
// DidStopLoading() is called. Defer showing the preview until then.
on_stop_loading_closure_ =
base::Bind(&PrintWebViewHelper::ShowScriptedPrintPreview,
base::Unretained(this));
} else {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&PrintWebViewHelper::ShowScriptedPrintPreview,
weak_ptr_factory_.GetWeakPtr()));
}
auto msg = base::MakeUnique<PrintHostMsg_SetupScriptedPrintPreview>(
routing_id());
msg->EnableMessagePumping();
auto self = weak_ptr_factory_.GetWeakPtr();
Send(msg.release());
// Check if |this| is still valid.
if (self)
is_scripted_preview_delayed_ = false;
return;
}
case PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME: {
// Wait for DidStopLoading. Continuing with this function while
// |is_loading_| is true will cause print preview to hang when try to
// print a PDF document.
if (is_loading_ && GetPlugin(print_preview_context_.source_frame())) {
on_stop_loading_closure_ =
base::Bind(&PrintWebViewHelper::RequestPrintPreview,
base::Unretained(this), type);
return;
}
break;
}
case PRINT_PREVIEW_USER_INITIATED_SELECTION: {
DCHECK(has_selection);
DCHECK(!GetPlugin(print_preview_context_.source_frame()));
params.selection_only = has_selection;
break;
}
case PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE: {
if (is_loading_ && GetPlugin(print_preview_context_.source_frame())) {
on_stop_loading_closure_ =
base::Bind(&PrintWebViewHelper::RequestPrintPreview,
base::Unretained(this), type);
return;
}
params.webnode_only = true;
break;
}
default: {
NOTREACHED();
return;
}
}
Send(new PrintHostMsg_RequestPrintPreview(routing_id(), params));
}
bool PrintWebViewHelper::CheckForCancel() {
const PrintMsg_Print_Params& print_params = print_pages_params_->params;
bool cancel = false;
Send(new PrintHostMsg_CheckForCancel(routing_id(),
print_params.preview_ui_id,
print_params.preview_request_id,
&cancel));
if (cancel)
notify_browser_of_print_failure_ = false;
return cancel;
}
bool PrintWebViewHelper::PreviewPageRendered(int page_number,
PdfMetafileSkia* metafile) {
DCHECK_GE(page_number, FIRST_PAGE_INDEX);
// For non-modifiable files, |metafile| should be NULL, so do not bother
// sending a message. If we don't generate draft metafiles, |metafile| is
// NULL.
if (!print_preview_context_.IsModifiable() ||
!print_preview_context_.generate_draft_pages()) {
DCHECK(!metafile);
return true;
}
if (!metafile) {
NOTREACHED();
print_preview_context_.set_error(
PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE);
return false;
}
PrintHostMsg_DidPreviewPage_Params preview_page_params;
// Get the size of the resulting metafile.
if (!CopyMetafileDataToSharedMem(
*metafile, &(preview_page_params.metafile_data_handle))) {
LOG(ERROR) << "CopyMetafileDataToSharedMem failed";
print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
return false;
}
preview_page_params.data_size = metafile->GetDataSize();
preview_page_params.page_number = page_number;
preview_page_params.preview_request_id =
print_pages_params_->params.preview_request_id;
Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params));
return true;
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext()
: total_page_count_(0),
current_page_index_(0),
generate_draft_pages_(true),
print_ready_metafile_page_count_(0),
error_(PREVIEW_ERROR_NONE),
state_(UNINITIALIZED) {
}
PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() {
}
void PrintWebViewHelper::PrintPreviewContext::InitWithFrame(
blink::WebLocalFrame* web_frame) {
DCHECK(web_frame);
DCHECK(!IsRendering());
state_ = INITIALIZED;
source_frame_.Reset(web_frame);
source_node_.reset();
}
void PrintWebViewHelper::PrintPreviewContext::InitWithNode(
const blink::WebNode& web_node) {
DCHECK(!web_node.isNull());
DCHECK(web_node.document().frame());
DCHECK(!IsRendering());
state_ = INITIALIZED;
source_frame_.Reset(web_node.document().frame());
source_node_ = web_node;
}
void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() {
DCHECK_EQ(INITIALIZED, state_);
ClearContext();
}
bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument(
PrepareFrameAndViewForPrint* prepared_frame,
const std::vector<int>& pages) {
DCHECK_EQ(INITIALIZED, state_);
state_ = RENDERING;
// Need to make sure old object gets destroyed first.
prep_frame_view_.reset(prepared_frame);
prep_frame_view_->StartPrinting();
total_page_count_ = prep_frame_view_->GetExpectedPageCount();
if (total_page_count_ == 0) {
LOG(ERROR) << "CreatePreviewDocument got 0 page count";
set_error(PREVIEW_ERROR_ZERO_PAGES);
return false;
}
metafile_.reset(new PdfMetafileSkia(PDF_SKIA_DOCUMENT_TYPE));
CHECK(metafile_->Init());
current_page_index_ = 0;
pages_to_render_ = pages;
// Sort and make unique.
std::sort(pages_to_render_.begin(), pages_to_render_.end());
pages_to_render_.resize(
std::unique(pages_to_render_.begin(), pages_to_render_.end()) -
pages_to_render_.begin());
// Remove invalid pages.
pages_to_render_.resize(std::lower_bound(pages_to_render_.begin(),
pages_to_render_.end(),
total_page_count_) -
pages_to_render_.begin());
print_ready_metafile_page_count_ = pages_to_render_.size();
if (pages_to_render_.empty()) {
print_ready_metafile_page_count_ = total_page_count_;
// Render all pages.
for (int i = 0; i < total_page_count_; ++i)
pages_to_render_.push_back(i);
} else if (generate_draft_pages_) {
int pages_index = 0;
for (int i = 0; i < total_page_count_; ++i) {
if (pages_index < print_ready_metafile_page_count_ &&
i == pages_to_render_[pages_index]) {
pages_index++;
continue;
}
pages_to_render_.push_back(i);
}
}
document_render_time_ = base::TimeDelta();
begin_time_ = base::TimeTicks::Now();
return true;
}
void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage(
const base::TimeDelta& page_time) {
DCHECK_EQ(RENDERING, state_);
document_render_time_ += page_time;
UMA_HISTOGRAM_TIMES("PrintPreview.RenderPDFPageTime", page_time);
}
void PrintWebViewHelper::PrintPreviewContext::AllPagesRendered() {
DCHECK_EQ(RENDERING, state_);
state_ = DONE;
prep_frame_view_->FinishPrinting();
}
void PrintWebViewHelper::PrintPreviewContext::FinalizePrintReadyDocument() {
DCHECK(IsRendering());
base::TimeTicks begin_time = base::TimeTicks::Now();
metafile_->FinishDocument();
if (print_ready_metafile_page_count_ <= 0) {
NOTREACHED();
return;
}
UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderToPDFTime",
document_render_time_);
base::TimeDelta total_time =
(base::TimeTicks::Now() - begin_time) + document_render_time_;
UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTime",
total_time);
UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage",
total_time / pages_to_render_.size());
}
void PrintWebViewHelper::PrintPreviewContext::Finished() {
DCHECK_EQ(DONE, state_);
state_ = INITIALIZED;
ClearContext();
}
void PrintWebViewHelper::PrintPreviewContext::Failed(bool report_error) {
DCHECK(state_ == INITIALIZED || state_ == RENDERING);
state_ = INITIALIZED;
if (report_error) {
DCHECK_NE(PREVIEW_ERROR_NONE, error_);
UMA_HISTOGRAM_ENUMERATION("PrintPreview.RendererError", error_,
PREVIEW_ERROR_LAST_ENUM);
}
ClearContext();
}
int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() {
DCHECK_EQ(RENDERING, state_);
if (IsFinalPageRendered())
return -1;
return pages_to_render_[current_page_index_++];
}
bool PrintWebViewHelper::PrintPreviewContext::IsRendering() const {
return state_ == RENDERING || state_ == DONE;
}
bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() {
// The only kind of node we can print right now is a PDF node.
return !PrintingNodeOrPdfFrame(source_frame(), source_node_);
}
bool PrintWebViewHelper::PrintPreviewContext::HasSelection() {
return IsModifiable() && source_frame()->hasSelection();
}
bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile()
const {
DCHECK(IsRendering());
return current_page_index_ == print_ready_metafile_page_count_;
}
bool PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const {
DCHECK(IsRendering());
return static_cast<size_t>(current_page_index_) == pages_to_render_.size();
}
void PrintWebViewHelper::PrintPreviewContext::set_generate_draft_pages(
bool generate_draft_pages) {
DCHECK_EQ(INITIALIZED, state_);
generate_draft_pages_ = generate_draft_pages;
}
void PrintWebViewHelper::PrintPreviewContext::set_error(
enum PrintPreviewErrorBuckets error) {
error_ = error;
}
blink::WebLocalFrame* PrintWebViewHelper::PrintPreviewContext::source_frame() {
DCHECK(state_ != UNINITIALIZED);
return source_frame_.GetFrame();
}
const blink::WebNode&
PrintWebViewHelper::PrintPreviewContext::source_node() const {
DCHECK(state_ != UNINITIALIZED);
return source_node_;
}
blink::WebLocalFrame*
PrintWebViewHelper::PrintPreviewContext::prepared_frame() {
DCHECK(state_ != UNINITIALIZED);
return prep_frame_view_->frame();
}
const blink::WebNode&
PrintWebViewHelper::PrintPreviewContext::prepared_node() const {
DCHECK(state_ != UNINITIALIZED);
return prep_frame_view_->node();
}
int PrintWebViewHelper::PrintPreviewContext::total_page_count() const {
DCHECK(state_ != UNINITIALIZED);
return total_page_count_;
}
bool PrintWebViewHelper::PrintPreviewContext::generate_draft_pages() const {
return generate_draft_pages_;
}
PdfMetafileSkia* PrintWebViewHelper::PrintPreviewContext::metafile() {
DCHECK(IsRendering());
return metafile_.get();
}
int PrintWebViewHelper::PrintPreviewContext::last_error() const {
return error_;
}
void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
prep_frame_view_.reset();
metafile_.reset();
pages_to_render_.clear();
error_ = PREVIEW_ERROR_NONE;
}
void PrintWebViewHelper::SetPrintPagesParams(
const PrintMsg_PrintPages_Params& settings) {
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
settings.params.document_cookie));
}
PrintWebViewHelper::ScriptingThrottler::ScriptingThrottler() : count_(0) {
}
bool PrintWebViewHelper::ScriptingThrottler::IsAllowed(
blink::WebLocalFrame* frame) {
const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2;
const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 32;
bool too_frequent = false;
// Check if there is script repeatedly trying to print and ignore it if too
// frequent. The first 3 times, we use a constant wait time, but if this
// gets excessive, we switch to exponential wait time. So for a page that
// calls print() in a loop the user will need to cancel the print dialog
// after: [2, 2, 2, 4, 8, 16, 32, 32, ...] seconds.
// This gives the user time to navigate from the page.
if (count_ > 0) {
base::TimeDelta diff = base::Time::Now() - last_print_;
int min_wait_seconds = kMinSecondsToIgnoreJavascriptInitiatedPrint;
if (count_ > 3) {
min_wait_seconds =
std::min(kMinSecondsToIgnoreJavascriptInitiatedPrint << (count_ - 3),
kMaxSecondsToIgnoreJavascriptInitiatedPrint);
}
if (diff.InSeconds() < min_wait_seconds) {
too_frequent = true;
}
}
if (!too_frequent) {
++count_;
last_print_ = base::Time::Now();
return true;
}
blink::WebString message(
blink::WebString::fromUTF8("Ignoring too frequent calls to print()."));
frame->addMessageToConsole(blink::WebConsoleMessage(
blink::WebConsoleMessage::LevelWarning, message));
return false;
}
void PrintWebViewHelper::ScriptingThrottler::Reset() {
// Reset counter on successful print.
count_ = 0;
}
} // namespace printing
|