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 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/renderer/bindings/api_binding.h"
#include <string_view>
#include <tuple>
#include "base/auto_reset.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "extensions/renderer/bindings/api_binding_hooks.h"
#include "extensions/renderer/bindings/api_binding_hooks_test_delegate.h"
#include "extensions/renderer/bindings/api_binding_test.h"
#include "extensions/renderer/bindings/api_binding_test_util.h"
#include "extensions/renderer/bindings/api_binding_types.h"
#include "extensions/renderer/bindings/api_binding_util.h"
#include "extensions/renderer/bindings/api_event_handler.h"
#include "extensions/renderer/bindings/api_invocation_errors.h"
#include "extensions/renderer/bindings/api_request_handler.h"
#include "extensions/renderer/bindings/api_signature.h"
#include "extensions/renderer/bindings/api_type_reference_map.h"
#include "extensions/renderer/bindings/binding_access_checker.h"
#include "extensions/renderer/bindings/exception_handler.h"
#include "extensions/renderer/bindings/test_interaction_provider.h"
#include "extensions/renderer/bindings/test_js_runner.h"
#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/public/context_holder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8.h"
namespace extensions {
namespace {
const char kBindingName[] = "test";
// Function spec; we use single quotes for readability and then replace them.
const char kFunctions[] =
"[{"
" 'name': 'oneString',"
" 'parameters': [{"
" 'type': 'string',"
" 'name': 'str'"
" }]"
"}, {"
" 'name': 'stringAndInt',"
" 'parameters': [{"
" 'type': 'string',"
" 'name': 'str'"
" }, {"
" 'type': 'integer',"
" 'name': 'int'"
" }]"
"}, {"
" 'name': 'oneObject',"
" 'parameters': [{"
" 'type': 'object',"
" 'name': 'foo',"
" 'properties': {"
" 'prop1': {'type': 'string'},"
" 'prop2': {'type': 'string', 'optional': true}"
" }"
" }]"
"}, {"
" 'name': 'intAndCallback',"
" 'parameters': [{"
" 'name': 'int',"
" 'type': 'integer'"
" }],"
" 'returns_async': {"
" 'name': 'callback',"
" 'type': 'function'"
" }"
"}]";
constexpr char kFunctionsWithCallbackSignatures[] = R"(
[{
"name": "noCallback",
"parameters": [{
"name": "int",
"type": "integer"
}]
}, {
"name": "intCallback",
"parameters": [],
"returns_async": {
"name": "callback",
"does_not_support_promises": "Test",
"parameters": [{
"name": "int",
"type": "integer"
}]
}
}, {
"name": "noParamCallback",
"parameters": [],
"returns_async": {
"name": "callback",
"does_not_support_promises": "Test",
"parameters": []
}
}])";
constexpr char kFunctionsWithPromiseSignatures[] =
R"([{
"name": "supportsPromises",
"parameters": [{
"name": "int",
"type": "integer"
}],
"returns_async": {
"name": "callback",
"parameters": [{
"name": "strResult",
"type": "string"
}]
}
},
{
"name": "callbackOptional",
"parameters": [{
"name": "int",
"type": "integer"
}],
"returns_async": {
"name": "callback",
"optional": true,
"parameters": [{
"name": "strResult",
"type": "string"
}]
}
}])";
bool AllowAllFeatures(v8::Local<v8::Context> context, const std::string& name) {
return true;
}
bool DisallowPromises(v8::Local<v8::Context> context) {
return false;
}
void OnEventListenersChanged(const std::string& event_name,
binding::EventListenersChanged change,
const base::Value::Dict* filter,
bool was_manual,
v8::Local<v8::Context> context) {}
} // namespace
class APIBindingUnittest : public APIBindingTest {
public:
APIBindingUnittest(const APIBindingUnittest&) = delete;
APIBindingUnittest& operator=(const APIBindingUnittest&) = delete;
void OnFunctionCall(std::unique_ptr<APIRequestHandler::Request> request,
v8::Local<v8::Context> context) {
last_request_ = std::move(request);
}
using GetParentCallback = base::RepeatingCallback<v8::Local<v8::Object>()>;
v8::Local<v8::Object> GetParent(v8::Local<v8::Context> context,
v8::Local<v8::Object>* secondary_parent) {
DCHECK(!get_last_error_parent_.is_null())
<< "You must have get_last_error_parent_ set if a test is dealing with"
"lastError being set";
return get_last_error_parent_.Run();
}
void AddConsoleError(v8::Local<v8::Context> context,
const std::string& error) {
console_errors_.push_back(error);
}
protected:
APIBindingUnittest()
: type_refs_(APITypeReferenceMap::InitializeTypeCallback()) {}
void SetUp() override {
APIBindingTest::SetUp();
interaction_provider_ = std::make_unique<TestInteractionProvider>();
binding::AddConsoleError add_console_error(base::BindRepeating(
&APIBindingUnittest::AddConsoleError, base::Unretained(this)));
exception_handler_ = std::make_unique<ExceptionHandler>(add_console_error);
request_handler_ = std::make_unique<APIRequestHandler>(
base::BindRepeating(&APIBindingUnittest::OnFunctionCall,
base::Unretained(this)),
APILastError(base::BindRepeating(&APIBindingUnittest::GetParent,
base::Unretained(this)),
add_console_error),
exception_handler_.get(), interaction_provider_.get());
}
void TearDown() override {
DisposeAllContexts();
access_checker_.reset();
interaction_provider_.reset();
request_handler_.reset();
event_handler_.reset();
binding_.reset();
APIBindingTest::TearDown();
}
void OnWillDisposeContext(v8::Local<v8::Context> context) override {
event_handler_->InvalidateContext(context);
request_handler_->InvalidateContext(context);
}
void SetFunctions(const char* functions) {
binding_functions_ = ListValueFromString(functions);
}
void SetEvents(const char* events) {
binding_events_ = ListValueFromString(events);
}
void SetTypes(const char* types) {
binding_types_ = ListValueFromString(types);
}
void SetProperties(const char* properties) {
binding_properties_ = DictValueFromString(properties);
}
void SetHooks(std::unique_ptr<APIBindingHooks> hooks) {
binding_hooks_ = std::move(hooks);
ASSERT_TRUE(binding_hooks_);
}
void SetHooksDelegate(
std::unique_ptr<APIBindingHooksDelegate> hooks_delegate) {
binding_hooks_delegate_ = std::move(hooks_delegate);
ASSERT_TRUE(binding_hooks_delegate_);
}
void SetCreateCustomType(const APIBinding::CreateCustomType& callback) {
create_custom_type_ = callback;
}
void SetOnSilentRequest(const APIBinding::OnSilentRequest& callback) {
on_silent_request_ = callback;
}
void SetAPIAvailabilityCallback(
const BindingAccessChecker::APIAvailabilityCallback& callback) {
api_availability_callback_ = callback;
}
void SetPromiseAvailabilityFlag(bool* availability_flag) {
promise_availability_callback_ = base::BindRepeating(
[](bool* flag, v8::Local<v8::Context> context) { return *flag; },
availability_flag);
}
void SetLastErrorParentCallback(GetParentCallback get_parent) {
get_last_error_parent_ = std::move(get_parent);
}
void ClearConsoleErrors() { console_errors_.clear(); }
void InitializeJSHooks(
const char* register_hook,
v8::Local<v8::Value> additional_arg = v8::Local<v8::Value>()) {
auto hooks =
std::make_unique<APIBindingHooks>(kBindingName, request_handler());
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
{
v8::Local<v8::Object> js_hooks = hooks->GetJSHookInterface(context);
v8::Local<v8::Function> function =
FunctionFromString(context, register_hook);
if (!additional_arg.IsEmpty()) {
v8::Local<v8::Value> args[] = {js_hooks, additional_arg};
RunFunctionOnGlobal(function, context, std::size(args), args);
} else {
v8::Local<v8::Value> args[] = {js_hooks};
RunFunctionOnGlobal(function, context, std::size(args), args);
}
}
SetHooks(std::move(hooks));
}
void InitializeBinding() {
if (!binding_hooks_)
binding_hooks_ =
std::make_unique<APIBindingHooks>(kBindingName, request_handler());
if (binding_hooks_delegate_)
binding_hooks_->SetDelegate(std::move(binding_hooks_delegate_));
if (!on_silent_request_)
on_silent_request_ = base::DoNothing();
if (!api_availability_callback_)
api_availability_callback_ = base::BindRepeating(&AllowAllFeatures);
if (!promise_availability_callback_)
promise_availability_callback_ = base::BindRepeating(&DisallowPromises);
auto get_context_owner = [](v8::Local<v8::Context>) {
return std::string("context");
};
event_handler_ = std::make_unique<APIEventHandler>(
base::BindRepeating(&OnEventListenersChanged),
base::BindRepeating(get_context_owner), nullptr);
access_checker_ = std::make_unique<BindingAccessChecker>(
api_availability_callback_, promise_availability_callback_);
binding_ = std::make_unique<APIBinding>(
kBindingName, &binding_functions_, &binding_types_, &binding_events_,
&binding_properties_, create_custom_type_, on_silent_request_,
std::move(binding_hooks_), &type_refs_, request_handler_.get(),
event_handler_.get(), access_checker_.get());
}
v8::Local<v8::Value> ExpectPass(
v8::Local<v8::Object> object,
const std::string& script_source,
const std::string& expected_json_arguments_single_quotes,
bool expect_async_handler) {
return ExpectPass(MainContext(), object, script_source,
expected_json_arguments_single_quotes,
expect_async_handler);
}
v8::Local<v8::Value> ExpectPass(
v8::Local<v8::Context> context,
v8::Local<v8::Object> object,
const std::string& script_source,
const std::string& expected_json_arguments_single_quotes,
bool expect_async_handler) {
return RunTest(context, object, script_source, true,
ReplaceSingleQuotes(expected_json_arguments_single_quotes),
expect_async_handler, std::string());
}
void ExpectFailure(v8::Local<v8::Object> object,
const std::string& script_source,
const std::string& expected_error) {
RunTest(MainContext(), object, script_source, false, std::string(), false,
"Uncaught TypeError: " + expected_error);
}
void ExpectThrow(v8::Local<v8::Object> object,
const std::string& script_source,
const std::string& expected_error) {
RunTest(MainContext(), object, script_source, false, std::string(), false,
"Uncaught Error: " + expected_error);
}
bool HandlerWasInvoked() const { return last_request_ != nullptr; }
const APIRequestHandler::Request* last_request() const {
return last_request_.get();
}
void reset_last_request() { last_request_.reset(); }
const std::vector<std::string>& console_errors() const {
return console_errors_;
}
APIBinding* binding() { return binding_.get(); }
APIEventHandler* event_handler() { return event_handler_.get(); }
APIRequestHandler* request_handler() { return request_handler_.get(); }
const APITypeReferenceMap& type_refs() const { return type_refs_; }
private:
v8::Local<v8::Value> RunTest(v8::Local<v8::Context> context,
v8::Local<v8::Object> object,
const std::string& script_source,
bool should_pass,
const std::string& expected_json_arguments,
bool expect_async_handler,
const std::string& expected_error);
std::unique_ptr<APIRequestHandler::Request> last_request_;
std::vector<std::string> console_errors_;
GetParentCallback get_last_error_parent_;
std::unique_ptr<APIBinding> binding_;
std::unique_ptr<APIEventHandler> event_handler_;
std::unique_ptr<TestInteractionProvider> interaction_provider_;
std::unique_ptr<ExceptionHandler> exception_handler_;
std::unique_ptr<APIRequestHandler> request_handler_;
std::unique_ptr<BindingAccessChecker> access_checker_;
APITypeReferenceMap type_refs_;
base::Value::List binding_functions_;
base::Value::List binding_events_;
base::Value::List binding_types_;
base::Value::Dict binding_properties_;
std::unique_ptr<APIBindingHooks> binding_hooks_;
std::unique_ptr<APIBindingHooksDelegate> binding_hooks_delegate_;
APIBinding::CreateCustomType create_custom_type_;
APIBinding::OnSilentRequest on_silent_request_;
BindingAccessChecker::APIAvailabilityCallback api_availability_callback_;
BindingAccessChecker::PromiseAvailabilityCallback
promise_availability_callback_;
};
using APIBindingDeathTest = APIBindingUnittest;
v8::Local<v8::Value> APIBindingUnittest::RunTest(
v8::Local<v8::Context> context,
v8::Local<v8::Object> object,
const std::string& script_source,
bool should_pass,
const std::string& expected_json_arguments,
bool expect_async_handler,
const std::string& expected_error) {
EXPECT_FALSE(last_request_);
std::string wrapped_script_source =
base::StringPrintf("(function(obj) { %s })", script_source.c_str());
v8::Local<v8::Function> func =
FunctionFromString(context, wrapped_script_source);
if (func.IsEmpty()) {
ADD_FAILURE() << "Script source couldn't be converted to a function: "
<< script_source;
return v8::Local<v8::Value>();
}
v8::Local<v8::Value> argv[] = {object};
v8::Local<v8::Value> result;
if (should_pass) {
result = RunFunction(func, context, 1, argv);
if (!last_request_) {
ADD_FAILURE() << "No request was made. Script source: " << script_source;
return v8::Local<v8::Value>();
}
EXPECT_EQ(expected_json_arguments,
ValueToString(last_request_->arguments_list));
EXPECT_EQ(expect_async_handler, last_request_->has_async_response_handler)
<< script_source;
} else {
RunFunctionAndExpectError(func, context, 1, argv, expected_error);
EXPECT_FALSE(last_request_);
}
last_request_.reset();
return result;
}
TEST_F(APIBindingUnittest, TestEmptyAPI) {
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
EXPECT_EQ(
0u,
binding_object->GetOwnPropertyNames(context).ToLocalChecked()->Length());
}
// Tests the basic call -> request flow of the API binding (ensuring that
// functions are set up correctly and correctly enforced).
TEST_F(APIBindingUnittest, TestBasicAPICalls) {
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// Argument parsing is tested primarily in APISignature and ArgumentSpec
// tests, so do a few quick sanity checks...
ExpectPass(binding_object, "obj.oneString('foo');", "['foo']", false);
ExpectFailure(binding_object, "obj.oneString(1);",
api_errors::InvocationError("test.oneString", "string str",
api_errors::NoMatchingSignature()));
ExpectPass(binding_object, "obj.stringAndInt('foo', 1)", "['foo',1]", false);
ExpectFailure(binding_object, "obj.stringAndInt(1)",
api_errors::InvocationError("test.stringAndInt",
"string str, integer int",
api_errors::NoMatchingSignature()));
ExpectPass(binding_object, "obj.intAndCallback(1, function() {})", "[1]",
true);
ExpectFailure(binding_object, "obj.intAndCallback(function() {})",
api_errors::InvocationError("test.intAndCallback",
"integer int, function callback",
api_errors::NoMatchingSignature()));
// ...And an interesting case (throwing an error during parsing).
ExpectThrow(binding_object,
"obj.oneObject({ get prop1() { throw new Error('Badness'); } });",
"Badness");
}
// Test that enum values are properly exposed on the binding object.
TEST_F(APIBindingUnittest, EnumValues) {
const char kTypes[] =
"[{"
" 'id': 'first',"
" 'type': 'string',"
" 'enum': ['alpha', 'camelCase', 'Hyphen-ated',"
" 'SCREAMING', 'nums123', '42nums']"
"}, {"
" 'id': 'last',"
" 'type': 'string',"
" 'enum': [{'name': 'omega'}]"
"}]";
SetTypes(kTypes);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
const char kExpected[] =
"{'ALPHA':'alpha','CAMEL_CASE':'camelCase','HYPHEN_ATED':'Hyphen-ated',"
"'NUMS123':'nums123','SCREAMING':'SCREAMING','_42NUMS':'42nums'}";
EXPECT_EQ(ReplaceSingleQuotes(kExpected),
GetStringPropertyFromObject(binding_object, context, "first"));
EXPECT_EQ(ReplaceSingleQuotes("{'OMEGA':'omega'}"),
GetStringPropertyFromObject(binding_object, context, "last"));
}
// Test that empty enum entries are (unfortunately) allowed.
TEST_F(APIBindingUnittest, EnumWithEmptyEntry) {
const char kTypes[] =
"[{"
" 'id': 'enumWithEmpty',"
" 'type': 'string',"
" 'enum': [{'name': ''}, {'name': 'other'}]"
"}]";
SetTypes(kTypes);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
EXPECT_EQ(
"{\"\":\"\",\"OTHER\":\"other\"}",
GetStringPropertyFromObject(binding_object, context, "enumWithEmpty"));
}
// Test that type references are correctly set up in the API.
TEST_F(APIBindingUnittest, TypeRefsTest) {
const char kTypes[] =
"[{"
" 'id': 'refObj',"
" 'type': 'object',"
" 'properties': {"
" 'prop1': {'type': 'string'},"
" 'prop2': {'type': 'integer', 'optional': true}"
" }"
"}, {"
" 'id': 'refEnum',"
" 'type': 'string',"
" 'enum': ['alpha', 'beta']"
"}]";
const char kRefFunctions[] =
"[{"
" 'name': 'takesRefObj',"
" 'parameters': [{"
" 'name': 'o',"
" '$ref': 'refObj'"
" }]"
"}, {"
" 'name': 'takesRefEnum',"
" 'parameters': [{"
" 'name': 'e',"
" '$ref': 'refEnum'"
" }]"
"}]";
SetFunctions(kRefFunctions);
SetTypes(kTypes);
InitializeBinding();
EXPECT_EQ(2u, type_refs().size());
EXPECT_TRUE(type_refs().GetSpec("refObj"));
EXPECT_TRUE(type_refs().GetSpec("refEnum"));
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// Parsing in general is tested in APISignature and ArgumentSpec tests, but
// we test that the binding a) correctly finds the definitions, and b) accepts
// properties from the API object.
ExpectPass(binding_object, "obj.takesRefObj({prop1: 'foo'})",
"[{'prop1':'foo'}]", false);
ExpectFailure(binding_object, "obj.takesRefObj({prop1: 'foo', prop2: 'a'})",
api_errors::InvocationError(
"test.takesRefObj", "refObj o",
api_errors::ArgumentError(
"o", api_errors::PropertyError(
"prop2", api_errors::InvalidType(
api_errors::kTypeInteger,
api_errors::kTypeString)))));
ExpectPass(binding_object, "obj.takesRefEnum('alpha')", "['alpha']", false);
ExpectPass(binding_object, "obj.takesRefEnum(obj.refEnum.BETA)", "['beta']",
false);
ExpectFailure(binding_object, "obj.takesRefEnum('gamma')",
api_errors::InvocationError(
"test.takesRefEnum", "refEnum e",
api_errors::ArgumentError(
"e", api_errors::InvalidEnumValue({"alpha", "beta"}))));
}
TEST_F(APIBindingUnittest, RestrictedAPIs) {
const char kLocalFunctions[] =
"[{"
" 'name': 'allowedOne',"
" 'parameters': []"
"}, {"
" 'name': 'allowedTwo',"
" 'parameters': []"
"}, {"
" 'name': 'restrictedOne',"
" 'parameters': []"
"}, {"
" 'name': 'restrictedTwo',"
" 'parameters': []"
"}]";
SetFunctions(kLocalFunctions);
const char kEvents[] =
"[{'name': 'allowedEvent'}, {'name': 'restrictedEvent'}]";
SetEvents(kEvents);
const char kProperties[] =
R"({
"allowedProperty": { "type": "integer", "value": 3 },
"restrictedProperty": { "type": "string", "value": "restricted" }
})";
SetProperties(kProperties);
auto is_available = [](v8::Local<v8::Context> context,
const std::string& name) {
std::set<std::string> allowed = {"test.allowedOne", "test.allowedTwo",
"test.allowedEvent",
"test.allowedProperty"};
std::set<std::string> restricted = {
"test.restrictedOne", "test.restrictedTwo", "test.restrictedEvent",
"test.restrictedProperty"};
EXPECT_TRUE(allowed.count(name) || restricted.count(name)) << name;
return allowed.count(name) != 0;
};
SetAPIAvailabilityCallback(base::BindRepeating(is_available));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
auto is_defined = [&binding_object, context](const std::string& name) {
v8::Local<v8::Value> val =
GetPropertyFromObject(binding_object, context, name);
EXPECT_FALSE(val.IsEmpty());
return !val->IsUndefined() && !val->IsNull();
};
EXPECT_TRUE(is_defined("allowedOne"));
EXPECT_TRUE(is_defined("allowedTwo"));
EXPECT_TRUE(is_defined("allowedEvent"));
EXPECT_TRUE(is_defined("allowedProperty"));
EXPECT_FALSE(is_defined("restrictedOne"));
EXPECT_FALSE(is_defined("restrictedTwo"));
EXPECT_FALSE(is_defined("restrictedEvent"));
EXPECT_FALSE(is_defined("restrictedProperty"));
}
// Tests that events specified in the API are created as properties of the API
// object.
TEST_F(APIBindingUnittest, TestEventCreation) {
SetEvents(
R"([
{'name': 'onFoo'},
{'name': 'onBar'},
{'name': 'onBaz', 'options': {'maxListeners': 1}}
])");
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// Event behavior is tested in the APIEventHandler unittests as well as the
// APIBindingsSystem tests, so we really only need to check that the events
// are being initialized on the object.
v8::Maybe<bool> has_on_foo =
binding_object->Has(context, gin::StringToV8(isolate(), "onFoo"));
EXPECT_TRUE(has_on_foo.IsJust());
EXPECT_TRUE(has_on_foo.FromJust());
v8::Maybe<bool> has_on_bar =
binding_object->Has(context, gin::StringToV8(isolate(), "onBar"));
EXPECT_TRUE(has_on_bar.IsJust());
EXPECT_TRUE(has_on_bar.FromJust());
v8::Maybe<bool> has_on_baz =
binding_object->Has(context, gin::StringToV8(isolate(), "onBaz"));
EXPECT_TRUE(has_on_baz.IsJust());
EXPECT_TRUE(has_on_baz.FromJust());
// Test that the maxListeners property is correctly used.
v8::Local<v8::Function> add_listener = FunctionFromString(
context, "(function(e) { e.addListener(function() {}); })");
v8::Local<v8::Value> args[] = {
GetPropertyFromObject(binding_object, context, "onBaz")};
RunFunction(add_listener, context, std::size(args), args);
EXPECT_EQ(1u, event_handler()->GetNumEventListenersForTesting("test.onBaz",
context));
RunFunctionAndExpectError(add_listener, context, std::size(args), args,
"Uncaught TypeError: Too many listeners.");
EXPECT_EQ(1u, event_handler()->GetNumEventListenersForTesting("test.onBaz",
context));
v8::Maybe<bool> has_nonexistent_event = binding_object->Has(
context, gin::StringToV8(isolate(), "onNonexistentEvent"));
EXPECT_TRUE(has_nonexistent_event.IsJust());
EXPECT_FALSE(has_nonexistent_event.FromJust());
}
TEST_F(APIBindingUnittest, TestProperties) {
SetProperties(
"{"
" 'prop1': { 'value': 17, 'type': 'integer' },"
" 'prop2': {"
" 'type': 'object',"
" 'properties': {"
" 'subprop1': { 'value': 'some value', 'type': 'string' },"
" 'subprop2': { 'value': true, 'type': 'boolean' }"
" }"
" },"
" 'linuxOnly': {"
" 'value': 'linux',"
" 'type': 'string',"
" 'platforms': ['linux']"
" },"
" 'notLinux': {"
" 'value': 'nonlinux',"
" 'type': 'string',"
" 'platforms': ["
" 'win', 'mac', 'chromeos', 'fuchsia', 'desktop_android']"
" }"
"}");
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
EXPECT_EQ("17",
GetStringPropertyFromObject(binding_object, context, "prop1"));
EXPECT_EQ(R"({"subprop1":"some value","subprop2":true})",
GetStringPropertyFromObject(binding_object, context, "prop2"));
#if BUILDFLAG(IS_LINUX)
EXPECT_EQ("\"linux\"",
GetStringPropertyFromObject(binding_object, context, "linuxOnly"));
EXPECT_EQ("undefined",
GetStringPropertyFromObject(binding_object, context, "notLinux"));
#else
EXPECT_EQ("undefined",
GetStringPropertyFromObject(binding_object, context, "linuxOnly"));
EXPECT_EQ("\"nonlinux\"",
GetStringPropertyFromObject(binding_object, context, "notLinux"));
#endif
}
TEST_F(APIBindingUnittest, TestRefProperties) {
SetProperties(
"{"
" 'alpha': {"
" '$ref': 'AlphaRef',"
" 'value': ['a']"
" },"
" 'beta': {"
" '$ref': 'BetaRef',"
" 'value': ['b']"
" }"
"}");
auto create_custom_type = [](v8::Isolate* isolate,
const std::string& type_name,
const std::string& property_name,
const base::Value::List* property_values) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Object> result = v8::Object::New(isolate);
if (type_name == "AlphaRef") {
EXPECT_EQ("alpha", property_name);
EXPECT_EQ("[\"a\"]", ValueToString(*property_values));
result
->Set(context, gin::StringToSymbol(isolate, "alphaProp"),
gin::StringToV8(isolate, "alphaVal"))
.ToChecked();
} else if (type_name == "BetaRef") {
EXPECT_EQ("beta", property_name);
EXPECT_EQ("[\"b\"]", ValueToString(*property_values));
result
->Set(context, gin::StringToSymbol(isolate, "betaProp"),
gin::StringToV8(isolate, "betaVal"))
.ToChecked();
} else {
EXPECT_TRUE(false) << type_name;
}
return result;
};
SetCreateCustomType(base::BindRepeating(create_custom_type));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
EXPECT_EQ(R"({"alphaProp":"alphaVal"})",
GetStringPropertyFromObject(binding_object, context, "alpha"));
EXPECT_EQ(
R"({"betaProp":"betaVal"})",
GetStringPropertyFromObject(binding_object, context, "beta"));
}
TEST_F(APIBindingUnittest, TestDisposedContext) {
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> func =
FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })");
v8::Local<v8::Value> argv[] = {binding_object};
DisposeContext(context);
RunFunctionAndExpectError(func, context, std::size(argv), argv,
"Uncaught Error: Extension context invalidated.");
EXPECT_FALSE(HandlerWasInvoked());
// This test passes if this does not crash, even under AddressSanitizer
// builds.
}
TEST_F(APIBindingUnittest, TestInvalidatedContext) {
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> func =
FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })");
v8::Local<v8::Value> argv[] = {binding_object};
binding::InvalidateContext(context);
RunFunctionAndExpectError(func, context, std::size(argv), argv,
"Uncaught Error: Extension context invalidated.");
EXPECT_FALSE(HandlerWasInvoked());
// This test passes if this does not crash, even under AddressSanitizer
// builds.
}
TEST_F(APIBindingUnittest, MultipleContexts) {
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context_a = MainContext();
v8::Local<v8::Context> context_b = AddContext();
SetFunctions(kFunctions);
InitializeBinding();
v8::Local<v8::Object> binding_object_a = binding()->CreateInstance(context_a);
v8::Local<v8::Object> binding_object_b = binding()->CreateInstance(context_b);
ExpectPass(context_a, binding_object_a, "obj.oneString('foo');", "['foo']",
false);
ExpectPass(context_b, binding_object_b, "obj.oneString('foo');", "['foo']",
false);
DisposeContext(context_b);
ExpectPass(context_a, binding_object_a, "obj.oneString('foo');", "['foo']",
false);
}
// Tests adding custom hooks for an API method.
TEST_F(APIBindingUnittest, TestCustomHooks) {
SetFunctions(kFunctions);
// Register a hook for the test.oneString method.
auto hooks = std::make_unique<APIBindingHooksTestDelegate>();
bool did_call = false;
auto hook = [](bool* did_call, const APISignature* signature,
v8::Local<v8::Context> context,
v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& ref_map) {
*did_call = true;
APIBindingHooks::RequestResult result(
APIBindingHooks::RequestResult::HANDLED);
if (arguments->size() != 1u) { // ASSERT* messes with the return type.
EXPECT_EQ(1u, arguments->size());
return result;
}
EXPECT_EQ("foo", gin::V8ToString(context->GetIsolate(), arguments->at(0)));
return result;
};
hooks->AddHandler("test.oneString", base::BindRepeating(hook, &did_call));
SetHooksDelegate(std::move(hooks));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// First try calling the oneString() method, which has a custom hook
// installed.
v8::Local<v8::Function> func =
FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })");
v8::Local<v8::Value> args[] = {binding_object};
RunFunction(func, context, 1, args);
EXPECT_TRUE(did_call);
// Other methods, like stringAndInt(), should behave normally.
ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]",
false);
}
TEST_F(APIBindingUnittest, TestJSCustomHook) {
// Register a hook for the test.oneString method.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setHandleRequest('oneString', function() {
this.requestArguments = Array.from(arguments);
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// First try calling with an invalid invocation. An error should be raised and
// the hook should never have been called, since the arguments didn't match.
ExpectFailure(binding_object, "obj.oneString(1);",
api_errors::InvocationError("test.oneString", "string str",
api_errors::NoMatchingSignature()));
v8::Local<v8::Value> property =
GetPropertyFromObject(context->Global(), context, "requestArguments");
ASSERT_FALSE(property.IsEmpty());
EXPECT_TRUE(property->IsUndefined());
// Try calling the oneString() method with valid arguments. The hook should
// be called.
v8::Local<v8::Function> func =
FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })");
v8::Local<v8::Value> args[] = {binding_object};
RunFunction(func, context, 1, args);
EXPECT_EQ("[\"foo\"]", GetStringPropertyFromObject(
context->Global(), context, "requestArguments"));
// Other methods, like stringAndInt(), should behave normally.
ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]",
false);
}
// Tests the updateArgumentsPreValidate hook.
TEST_F(APIBindingUnittest, TestUpdateArgumentsPreValidate) {
// Register a hook for the test.oneString method.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setUpdateArgumentsPreValidate('oneString', function() {
this.requestArguments = Array.from(arguments);
if (this.requestArguments[0] === true)
return ['hooked']
return this.requestArguments
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// Call the method with a hook. Since the hook updates arguments before
// validation, we should be able to pass in invalid arguments and still
// have the hook called.
ExpectFailure(binding_object, "obj.oneString(false);",
api_errors::InvocationError("test.oneString", "string str",
api_errors::NoMatchingSignature()));
EXPECT_EQ("[false]", GetStringPropertyFromObject(
context->Global(), context, "requestArguments"));
ExpectPass(binding_object, "obj.oneString(true);", "['hooked']", false);
EXPECT_EQ("[true]", GetStringPropertyFromObject(
context->Global(), context, "requestArguments"));
// Other methods, like stringAndInt(), should behave normally.
ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]",
false);
}
// Tests the updateArgumentsPreValidate hook.
TEST_F(APIBindingUnittest, TestThrowInUpdateArgumentsPreValidate) {
// Register a hook for the test.oneString method.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setUpdateArgumentsPreValidate('oneString', function() {
throw new Error('Custom Hook Error');
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> function =
FunctionFromString(context,
"(function(obj) { return obj.oneString('ping'); })");
v8::Local<v8::Value> args[] = {binding_object};
{
TestJSRunner::AllowErrors allow_errors;
RunFunctionAndExpectError(function, context, v8::Undefined(isolate()),
std::size(args), args,
"Uncaught Error: Custom Hook Error");
}
// Other methods, like stringAndInt(), should behave normally.
ExpectPass(binding_object, "obj.stringAndInt('foo', 42);", "['foo',42]",
false);
}
// Tests that custom JS hooks can return results synchronously.
TEST_F(APIBindingUnittest, TestReturningResultFromCustomJSHook) {
// Register a hook for the test.oneString method.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setHandleRequest('oneString', str => {
return str + ' pong';
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> function =
FunctionFromString(context,
"(function(obj) { return obj.oneString('ping'); })");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> result =
RunFunction(function, context, std::size(args), args);
ASSERT_FALSE(result.IsEmpty());
std::unique_ptr<base::Value> json_result = V8ToBaseValue(result, context);
ASSERT_TRUE(json_result);
EXPECT_EQ("\"ping pong\"", ValueToString(*json_result));
}
// Tests that the setHandleRequest hook can use callbacks and promises.
TEST_F(APIBindingUnittest, TestReturningPromiseFromHandleRequestHook) {
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
// Register a hook for supportsPromises.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setHandleRequest('supportsPromises', (firstArg, callback) => {
this.firstArgument = firstArg;
this.secondArgument = callback;
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctionsWithPromiseSignatures);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
{
// Calling supportsPromises normally with a callback should work fine and
// the callback should be invoked immediately.
const char kFunctionCall[] =
R"((function(obj) {
return obj.supportsPromises(5, (arg) => {
this.sentToCallback = arg;
});
}))";
v8::Local<v8::Function> function =
FunctionFromString(context, kFunctionCall);
v8::Local<v8::Value> args[] = {binding_object};
auto result = RunFunction(function, context, v8::Undefined(isolate()),
std::size(args), args);
ASSERT_FALSE(result.IsEmpty());
EXPECT_TRUE(result->IsUndefined());
EXPECT_EQ("5", GetStringPropertyFromObject(context->Global(), context,
"firstArgument"));
v8::Local<v8::Function> resolve_callback;
ASSERT_TRUE(GetPropertyFromObjectAs(context->Global(), context,
"secondArgument", &resolve_callback));
// The callback arg will not be set until the callback has been invoked.
EXPECT_TRUE(
GetPropertyFromObject(context->Global(), context, "sentToCallabck")
->IsUndefined());
v8::Local<v8::Value> callback_arguments[] = {
gin::StringToV8(isolate(), "foo")};
RunFunctionOnGlobal(resolve_callback, context,
std::size(callback_arguments), callback_arguments);
EXPECT_EQ(R"("foo")", GetStringPropertyFromObject(
context->Global(), context, "sentToCallback"));
}
{
// Calling supportsPromises normally without the callback should work fine
// and a promise should be returned that is resolved when the callback is
// invoked.
v8::Local<v8::Function> function = FunctionFromString(
context, "(function(obj) { return obj.supportsPromises(6); })");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> result = RunFunction(
function, context, v8::Undefined(isolate()), std::size(args), args);
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(result, &promise));
EXPECT_EQ(v8::Promise::kPending, promise->State());
EXPECT_EQ("6", GetStringPropertyFromObject(context->Global(), context,
"firstArgument"));
// Since we trigger the promise to be resolved with a function that calls
// back into the C++ side, the second argument is actually a function here.
v8::Local<v8::Function> resolve_callback;
ASSERT_TRUE(GetPropertyFromObjectAs(context->Global(), context,
"secondArgument", &resolve_callback));
// Invoking this callback should result in the promise being resolved.
v8::Local<v8::Value> callback_arguments[] = {
gin::StringToV8(isolate(), "bar")};
RunFunctionOnGlobal(resolve_callback, context,
std::size(callback_arguments), callback_arguments);
EXPECT_EQ(v8::Promise::kFulfilled, promise->State());
EXPECT_EQ(R"("bar")", V8ToString(promise->Result(), context));
}
{
// If the context doesn't support promises, there should be an error if a
// required callback isn't supplied.
context_allows_promises = false;
v8::Local<v8::Function> function = FunctionFromString(
context, "(function(obj) { return obj.supportsPromises(7); })");
v8::Local<v8::Value> args[] = {binding_object};
auto expected_error =
"Uncaught TypeError: " +
api_errors::InvocationError("test.supportsPromises",
"integer int, function callback",
api_errors::NoMatchingSignature());
RunFunctionAndExpectError(function, context, std::size(args), args,
expected_error);
}
}
// Tests that JS custom hooks can throw exceptions for bad invocations.
TEST_F(APIBindingUnittest, TestThrowingFromCustomJSHook) {
// Register a hook for the test.oneString method.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setHandleRequest('oneString', str => {
throw new Error('Custom Hook Error');
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> function =
FunctionFromString(context,
"(function(obj) { return obj.oneString('ping'); })");
v8::Local<v8::Value> args[] = {binding_object};
TestJSRunner::AllowErrors allow_errors;
RunFunctionAndExpectError(function, context, v8::Undefined(isolate()),
std::size(args), args,
"Uncaught Error: Custom Hook Error");
}
// Tests that JS setHandleRequestHooks can use the failure callback to return a
// failure result for an API.
TEST_F(APIBindingUnittest, TestHandleRequestFailureCallback) {
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
// Register a hook for supportsPromises that calls the failure callback when
// the API is called with the integer 6.
const char kRegisterHook[] = R"(
(function(hooks) {
function handler(firstArg, callback, failureCallback) {
if (firstArg == 6)
failureCallback('This is the error');
else
callback(firstArg);
};
hooks.setHandleRequest('supportsPromises', handler);
hooks.setHandleRequest('callbackOptional', handler);
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctionsWithPromiseSignatures);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Object> last_error_parent = v8::Object::New(isolate());
auto get_last_error_parent = [&last_error_parent]() {
return last_error_parent;
};
SetLastErrorParentCallback(base::BindLambdaForTesting(get_last_error_parent));
{
// Calling supportsPromises normally should resolve as expected with no
// error.
v8::Local<v8::Function> function = FunctionFromString(
context, "(function(obj) { return obj.supportsPromises(42); })");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> result = RunFunction(
function, context, v8::Undefined(isolate()), std::size(args), args);
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(result, &promise));
EXPECT_EQ(v8::Promise::kFulfilled, promise->State());
EXPECT_EQ(R"(42)", V8ToString(promise->Result(), context));
}
{
// Calling supportsPromises to trigger the failureCallback should result in
// the promise being rejected.
v8::Local<v8::Function> function = FunctionFromString(
context, "(function(obj) { return obj.supportsPromises(6); })");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> result = RunFunction(
function, context, v8::Undefined(isolate()), std::size(args), args);
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(result, &promise));
EXPECT_EQ(v8::Promise::kRejected, promise->State());
ASSERT_TRUE(promise->Result()->IsObject());
EXPECT_EQ(R"("This is the error")",
GetStringPropertyFromObject(promise->Result().As<v8::Object>(),
context, "message"));
}
{
// Calling supportsPromises with a callback and triggering the
// failureCallback should call the callback with lastError set.
const char kFunctionCall[] =
R"((function(obj, lastErrorParent) {
return obj.supportsPromises(6, (arg) => {
this.sentToCallback = arg;
// LastError is only set for the duration of the callback, so set
// it to a global we retrieve and can check later.
this.lastError = lastErrorParent.lastError;
});
}))";
v8::Local<v8::Function> function =
FunctionFromString(context, kFunctionCall);
v8::Local<v8::Value> args[] = {binding_object, last_error_parent};
RunFunction(function, context, v8::Undefined(isolate()), std::size(args),
args);
// In the case of errors, callbacks are not passed any arguments.
EXPECT_TRUE(
GetPropertyFromObject(context->Global(), context, "sentToCallabck")
->IsUndefined());
v8::Local<v8::Object> last_error;
ASSERT_TRUE(GetPropertyFromObjectAs(context->Global(), context, "lastError",
&last_error));
EXPECT_EQ(R"("This is the error")",
GetStringPropertyFromObject(last_error, context, "message"));
}
// Set the context to not support promises for the following test cases.
context_allows_promises = false;
{
// Calling callbackOptional without a callback and triggering the
// failureCallback in a context that does not support promises should result
// in a console error about an unchecked last error.
const char kFunctionCall[] =
R"((function(obj) {
return obj.callbackOptional(6);
}))";
v8::Local<v8::Function> function =
FunctionFromString(context, kFunctionCall);
v8::Local<v8::Value> args[] = {binding_object, last_error_parent};
RunFunction(function, context, v8::Undefined(isolate()), std::size(args),
args);
ASSERT_EQ(1u, console_errors().size());
EXPECT_THAT(console_errors()[0],
"Unchecked runtime.lastError: This is the error");
// Clear the console errors in case any other test case uses them.
ClearConsoleErrors();
}
}
// Tests that a JS handle request hook that calls the resolver callback more
// than once will fail gracefully on a release build. Regression test for
// https://crbug.com/1298409.
TEST_F(APIBindingUnittest, TestHandleRequestHookCalledTwiceGracefulRegression) {
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
// Register a hook for supportsPromises that calls the success callback twice.
static const char* const kRegisterHook = R"(
(function(hooks) {
function handler(firstArg, callback, failureCallback) {
callback(firstArg);
// Calling the callback to resolve the request a second time is
// something our custom hooks shouldn't be doing, but this test
// intentionally does it to verify behavior if it does happen by
// accident.
callback(firstArg);
};
hooks.setHandleRequest('supportsPromises', handler);
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctionsWithPromiseSignatures);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> function = FunctionFromString(
context, "(function(obj) { return obj.supportsPromises(42); })");
v8::Local<v8::Value> args[] = {binding_object};
// Calling supportsPromises will trigger the HandleRequest hook which attempts
// to resolve the request twice by calling the success callback twice. This
// should gracefully fail without a crash and still result in the request
// resolving as expected.
v8::Local<v8::Value> result = RunFunction(
function, context, v8::Undefined(isolate()), std::size(args), args);
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(result, &promise));
EXPECT_EQ(v8::Promise::kFulfilled, promise->State());
EXPECT_EQ(R"(42)", V8ToString(promise->Result(), context));
}
// Tests that JS custom hooks correctly handle the context being invalidated.
// Regression test for https://crbug.com/944014.
TEST_F(APIBindingUnittest, TestInvalidatingInCustomHook) {
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
auto context_invalidator =
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
gin::Arguments arguments(info);
binding::InvalidateContext(arguments.GetHolderCreationContext());
};
v8::Local<v8::Function> v8_context_invalidator =
v8::Function::New(context, context_invalidator).ToLocalChecked();
// Register two hooks. Since the context is invalidated in the first, the
// second should never run.
const char kRegisterHook[] = R"(
(function(hooks, contextInvalidator) {
hooks.setUpdateArgumentsPreValidate('oneString', () => {
contextInvalidator();
return ['foo'];
});
hooks.setHandleRequest('oneString', () => {
this.ranHandleHook = true;
});
}))";
InitializeJSHooks(kRegisterHook, v8_context_invalidator);
SetFunctions(kFunctions);
InitializeBinding();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> function = FunctionFromString(
context, "(function(obj) { return obj.oneString('ping'); })");
v8::Local<v8::Value> args[] = {binding_object};
RunFunction(function, context, v8::Undefined(isolate()), std::size(args),
args);
// The context should be properly invalidated, and the second hook (which
// sets "ranHandleHook") shouldn't have ran.
EXPECT_FALSE(binding::IsContextValid(context));
EXPECT_EQ("undefined", GetStringPropertyFromObject(context->Global(), context,
"ranHandleHook"));
}
// Tests that native custom hooks can return results synchronously, or throw
// exceptions for bad invocations.
TEST_F(APIBindingUnittest,
TestReturningResultAndThrowingExceptionFromCustomNativeHook) {
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
// Register a hook for the test.oneString method.
auto hooks = std::make_unique<APIBindingHooksTestDelegate>();
bool did_call = false;
auto hook = [](bool* did_call, const APISignature* signature,
v8::Local<v8::Context> context,
v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& ref_map) {
APIBindingHooks::RequestResult result(
APIBindingHooks::RequestResult::HANDLED);
if (arguments->size() != 1u) { // ASSERT* messes with the return type.
EXPECT_EQ(1u, arguments->size());
return result;
}
v8::Isolate* isolate = context->GetIsolate();
std::string arg_value = gin::V8ToString(isolate, arguments->at(0));
if (arg_value == "throw") {
isolate->ThrowException(v8::Exception::Error(
gin::StringToV8(isolate, "Custom Hook Error")));
result.code = APIBindingHooks::RequestResult::THROWN;
return result;
}
result.return_value =
gin::StringToV8(context->GetIsolate(), arg_value + " pong");
return result;
};
hooks->AddHandler("test.oneString", base::BindRepeating(hook, &did_call));
SetHooksDelegate(std::move(hooks));
SetFunctions(kFunctions);
InitializeBinding();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
{
// Test an invocation that we expect to throw an exception.
v8::Local<v8::Function> function =
FunctionFromString(
context, "(function(obj) { return obj.oneString('throw'); })");
v8::Local<v8::Value> args[] = {binding_object};
RunFunctionAndExpectError(function, context, v8::Undefined(isolate()),
std::size(args), args,
"Uncaught Error: Custom Hook Error");
}
{
// Test an invocation we expect to succeed.
v8::Local<v8::Function> function =
FunctionFromString(context,
"(function(obj) { return obj.oneString('ping'); })");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> result =
RunFunction(function, context, std::size(args), args);
ASSERT_FALSE(result.IsEmpty());
std::unique_ptr<base::Value> json_result = V8ToBaseValue(result, context);
ASSERT_TRUE(json_result);
EXPECT_EQ("\"ping pong\"", ValueToString(*json_result));
}
}
// Tests the updateArgumentsPostValidate hook.
TEST_F(APIBindingUnittest, TestUpdateArgumentsPostValidate) {
// Register a hook for the test.oneString method.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setUpdateArgumentsPostValidate('oneString', function() {
this.requestArguments = Array.from(arguments);
return ['pong'];
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// Try calling the method with an invalid signature. Since it's invalid, we
// should never enter the hook.
ExpectFailure(binding_object, "obj.oneString(false);",
api_errors::InvocationError("test.oneString", "string str",
api_errors::NoMatchingSignature()));
EXPECT_EQ("undefined", GetStringPropertyFromObject(
context->Global(), context, "requestArguments"));
// Call the method with a valid signature. The hook should be entered and
// manipulate the arguments.
ExpectPass(binding_object, "obj.oneString('ping');", "['pong']", false);
EXPECT_EQ("[\"ping\"]", GetStringPropertyFromObject(
context->Global(), context, "requestArguments"));
// Other methods, like stringAndInt(), should behave normally.
ExpectPass(binding_object, "obj.stringAndInt('foo', 42);",
"['foo',42]", false);
}
// Tests using setUpdateArgumentsPostValidate to return a list of arguments
// that violates the function schema. Sadly, this should succeed. :(
// See comment in api_binding.cc.
TEST_F(APIBindingUnittest, TestUpdateArgumentsPostValidateViolatingSchema) {
// Register a hook for the test.oneString method.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setUpdateArgumentsPostValidate('oneString', function() {
return [{}];
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// Call the method with a valid signature. The hook should be entered and
// manipulate the arguments.
ExpectPass(binding_object, "obj.oneString('ping');", "[{}]", false);
}
// Test that user gestures are properly recorded when calling APIs.
TEST_F(APIBindingUnittest, TestUserGestures) {
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> function =
FunctionFromString(context, "(function(obj) { obj.oneString('foo');})");
ASSERT_FALSE(function.IsEmpty());
v8::Local<v8::Value> argv[] = {binding_object};
RunFunction(function, context, std::size(argv), argv);
ASSERT_TRUE(last_request());
EXPECT_FALSE(last_request()->has_user_gesture);
reset_last_request();
ScopedTestUserActivation test_user_activation;
RunFunction(function, context, std::size(argv), argv);
ASSERT_TRUE(last_request());
EXPECT_TRUE(last_request()->has_user_gesture);
reset_last_request();
}
TEST_F(APIBindingUnittest, FilteredEvents) {
const char kEvents[] =
"[{"
" 'name': 'unfilteredOne',"
" 'parameters': []"
"}, {"
" 'name': 'unfilteredTwo',"
" 'filters': [],"
" 'parameters': []"
"}, {"
" 'name': 'unfilteredThree',"
" 'options': {'supportsFilters': false},"
" 'parameters': []"
"}, {"
" 'name': 'filteredOne',"
" 'options': {'supportsFilters': true},"
" 'parameters': []"
"}, {"
" 'name': 'filteredTwo',"
" 'filters': ["
" {'name': 'url', 'type': 'array', 'items': {'type': 'any'}}"
" ],"
" 'parameters': []"
"}]";
SetEvents(kEvents);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
const char kAddFilteredListener[] =
"(function(evt) {\n"
" evt.addListener(function() {},\n"
" {url: [{pathContains: 'simple2.html'}]});\n"
"})";
v8::Local<v8::Function> function =
FunctionFromString(context, kAddFilteredListener);
ASSERT_FALSE(function.IsEmpty());
auto check_supports_filters = [context, binding_object, function](
std::string_view name,
bool expect_supports) {
SCOPED_TRACE(name);
v8::Local<v8::Value> event =
GetPropertyFromObject(binding_object, context, name);
v8::Local<v8::Value> args[] = {event};
if (expect_supports) {
RunFunction(function, context, context->Global(), std::size(args), args);
} else {
RunFunctionAndExpectError(
function, context, context->Global(), std::size(args), args,
"Uncaught TypeError: This event does not support filters");
}
};
check_supports_filters("unfilteredOne", false);
check_supports_filters("unfilteredTwo", false);
check_supports_filters("unfilteredThree", false);
check_supports_filters("filteredOne", true);
check_supports_filters("filteredTwo", true);
}
TEST_F(APIBindingUnittest, HooksTemplateInitializer) {
SetFunctions(kFunctions);
// Register a hook for the test.oneString method.
auto hooks = std::make_unique<APIBindingHooksTestDelegate>();
auto hook = [](v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> object_template,
const APITypeReferenceMap& type_refs) {
object_template->Set(gin::StringToSymbol(isolate, "hookedProperty"),
gin::ConvertToV8(isolate, 42));
};
hooks->SetTemplateInitializer(base::BindRepeating(hook));
SetHooksDelegate(std::move(hooks));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// The extra property should be present on the binding object.
EXPECT_EQ("42", GetStringPropertyFromObject(binding_object, context,
"hookedProperty"));
// Sanity check: other values should still be there.
EXPECT_EQ("function",
GetStringPropertyFromObject(binding_object, context, "oneString"));
}
TEST_F(APIBindingUnittest, HooksInstanceInitializer) {
SetFunctions(kFunctions);
static constexpr char kHookedProperty[] = "hookedProperty";
// Register a hook for the test.oneString method.
auto hooks = std::make_unique<APIBindingHooksTestDelegate>();
int count = 0;
auto hook = [](int* count, v8::Local<v8::Context> context,
v8::Local<v8::Object> object) {
v8::Isolate* isolate = context->GetIsolate();
// Add a new property only for the first instance.
if ((*count)++ == 0) {
object
->Set(context, gin::StringToSymbol(isolate, kHookedProperty),
gin::ConvertToV8(isolate, 42))
.ToChecked();
}
};
hooks->SetInstanceInitializer(base::BindRepeating(hook, &count));
SetHooksDelegate(std::move(hooks));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
// Create two instances.
v8::Local<v8::Context> context1 = MainContext();
v8::Local<v8::Object> binding_object1 = binding()->CreateInstance(context1);
v8::Local<v8::Context> context2 = AddContext();
v8::Local<v8::Object> binding_object2 = binding()->CreateInstance(context2);
// We should have run the hooks twice (once per instance).
EXPECT_EQ(2, count);
// The extra property should be present on the first binding object, but not
// the second.
EXPECT_EQ("42", GetStringPropertyFromObject(binding_object1, context1,
kHookedProperty));
EXPECT_EQ("undefined", GetStringPropertyFromObject(binding_object2, context2,
kHookedProperty));
// Sanity check: other values should still be there.
EXPECT_EQ("function", GetStringPropertyFromObject(binding_object1, context1,
"oneString"));
EXPECT_EQ("function", GetStringPropertyFromObject(binding_object2, context1,
"oneString"));
}
// Test that running hooks returning different results correctly sends requests
// or notifies of silent requests.
TEST_F(APIBindingUnittest, TestSendingRequestsAndSilentRequestsWithHooks) {
SetFunctions(
"[{"
" 'name': 'modifyArgs',"
" 'parameters': []"
"}, {"
" 'name': 'invalidInvocation',"
" 'parameters': []"
"}, {"
" 'name': 'throwException',"
" 'parameters': []"
"}, {"
" 'name': 'dontHandle',"
" 'parameters': []"
"}, {"
" 'name': 'handle',"
" 'parameters': []"
"}, {"
" 'name': 'handleAndSendRequest',"
" 'parameters': []"
"}, {"
" 'name': 'handleWithArgs',"
" 'parameters': [{"
" 'name': 'first',"
" 'type': 'string'"
" }, {"
" 'name': 'second',"
" 'type': 'integer'"
" }]"
"}]");
using RequestResult = APIBindingHooks::RequestResult;
auto basic_handler =
[](RequestResult::ResultCode code, const APISignature*,
v8::Local<v8::Context> context, v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& map) { return RequestResult(code); };
auto hooks = std::make_unique<APIBindingHooksTestDelegate>();
hooks->AddHandler(
"test.modifyArgs",
base::BindRepeating(basic_handler, RequestResult::ARGUMENTS_UPDATED));
hooks->AddHandler(
"test.invalidInvocation",
base::BindRepeating(basic_handler, RequestResult::INVALID_INVOCATION));
hooks->AddHandler(
"test.dontHandle",
base::BindRepeating(basic_handler, RequestResult::NOT_HANDLED));
hooks->AddHandler("test.handle",
base::BindRepeating(basic_handler, RequestResult::HANDLED));
hooks->AddHandler(
"test.throwException",
base::BindRepeating([](const APISignature*,
v8::Local<v8::Context> context,
v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& map) {
context->GetIsolate()->ThrowException(
gin::StringToV8(context->GetIsolate(), "some error"));
return RequestResult(RequestResult::THROWN);
}));
hooks->AddHandler(
"test.handleWithArgs",
base::BindRepeating([](const APISignature*,
v8::Local<v8::Context> context,
v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& map) {
arguments->push_back(v8::Integer::New(context->GetIsolate(), 42));
return RequestResult(RequestResult::HANDLED);
}));
auto handle_and_send_request =
[](APIRequestHandler* handler, const APISignature*,
v8::Local<v8::Context> context, v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& map) {
handler->StartRequest(
context, "test.handleAndSendRequest", base::Value::List(),
binding::AsyncResponseType::kNone, v8::Local<v8::Function>(),
v8::Local<v8::Function>(), binding::ResultModifierFunction());
return RequestResult(RequestResult::HANDLED);
};
hooks->AddHandler(
"test.handleAndSendRequest",
base::BindRepeating(handle_and_send_request, request_handler()));
SetHooksDelegate(std::move(hooks));
auto on_silent_request = [](std::optional<std::string>* name_out,
std::optional<std::vector<std::string>>* args_out,
v8::Local<v8::Context> context,
const std::string& call_name,
const v8::LocalVector<v8::Value>& arguments) {
*name_out = call_name;
*args_out = std::vector<std::string>();
(*args_out)->reserve(arguments.size());
for (const auto& arg : arguments) {
(*args_out)->push_back(V8ToString(arg, context));
}
};
std::optional<std::string> silent_request;
std::optional<std::vector<std::string>> request_arguments;
SetOnSilentRequest(base::BindRepeating(on_silent_request, &silent_request,
&request_arguments));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
auto call_api_method = [binding_object, context](
std::string_view name,
std::string_view string_args) {
v8::Local<v8::Function> call = FunctionFromString(
context, base::StringPrintf("(function(binding) { binding.%s(%s); })",
name.data(), string_args.data()));
v8::Local<v8::Value> args[] = {binding_object};
v8::TryCatch try_catch(context->GetIsolate());
// The throwException call will throw an exception; ignore it.
std::ignore = call->Call(context, v8::Undefined(context->GetIsolate()),
std::size(args), args);
};
call_api_method("modifyArgs", "");
ASSERT_TRUE(last_request());
EXPECT_EQ("test.modifyArgs", last_request()->method_name);
EXPECT_FALSE(silent_request);
reset_last_request();
silent_request.reset();
request_arguments.reset();
call_api_method("invalidInvocation", "");
EXPECT_FALSE(last_request());
EXPECT_FALSE(silent_request);
reset_last_request();
silent_request.reset();
request_arguments.reset();
call_api_method("throwException", "");
EXPECT_FALSE(last_request());
EXPECT_FALSE(silent_request);
reset_last_request();
silent_request.reset();
request_arguments.reset();
call_api_method("dontHandle", "");
ASSERT_TRUE(last_request());
EXPECT_EQ("test.dontHandle", last_request()->method_name);
EXPECT_FALSE(silent_request);
reset_last_request();
silent_request.reset();
request_arguments.reset();
call_api_method("handle", "");
EXPECT_FALSE(last_request());
ASSERT_TRUE(silent_request);
EXPECT_EQ("test.handle", *silent_request);
ASSERT_TRUE(request_arguments);
EXPECT_TRUE(request_arguments->empty());
reset_last_request();
silent_request.reset();
request_arguments.reset();
call_api_method("handleAndSendRequest", "");
ASSERT_TRUE(last_request());
EXPECT_EQ("test.handleAndSendRequest", last_request()->method_name);
EXPECT_FALSE(silent_request);
reset_last_request();
silent_request.reset();
request_arguments.reset();
call_api_method("handleWithArgs", "'str'");
EXPECT_FALSE(last_request());
ASSERT_TRUE(silent_request);
ASSERT_EQ("test.handleWithArgs", *silent_request);
ASSERT_TRUE(request_arguments);
EXPECT_THAT(
*request_arguments,
testing::ElementsAre("\"str\"", "42")); // 42 was added by the handler.
reset_last_request();
silent_request.reset();
request_arguments.reset();
}
// Test native hooks that don't handle the result, but set a custom callback
// instead.
TEST_F(APIBindingUnittest, TestHooksWithCustomCallback) {
SetFunctions(kFunctions);
// Register a hook for the test.oneString method.
auto hooks = std::make_unique<APIBindingHooksTestDelegate>();
auto hook_with_custom_callback =
[](const APISignature* signature, v8::Local<v8::Context> context,
v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& ref_map) {
constexpr char kCustomCallback[] =
"(function() { this.calledCustomCallback = true; })";
v8::Local<v8::Function> custom_callback =
FunctionFromString(context, kCustomCallback);
APIBindingHooks::RequestResult result(
APIBindingHooks::RequestResult::NOT_HANDLED, custom_callback);
return result;
};
hooks->AddHandler("test.oneString",
base::BindRepeating(hook_with_custom_callback));
SetHooksDelegate(std::move(hooks));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// First try calling the oneString() method, which has a custom hook
// installed.
v8::Local<v8::Function> func =
FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })");
v8::Local<v8::Value> args[] = {binding_object};
RunFunction(func, context, 1, args);
ASSERT_TRUE(last_request());
EXPECT_TRUE(last_request()->has_async_response_handler);
request_handler()->CompleteRequest(last_request()->request_id,
base::Value::List(), std::string());
EXPECT_EQ("true", GetStringPropertyFromObject(context->Global(), context,
"calledCustomCallback"));
}
// Test native hooks that don't handle the result, but add a result modifier.
TEST_F(APIBindingUnittest, TestHooksWithResultModifier) {
SetFunctions(kFunctionsWithPromiseSignatures);
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
// Register a hook for the test.supportsPromises method with a result modifier
// that changes the result when the async response type is callback based.
auto hooks = std::make_unique<APIBindingHooksTestDelegate>();
int total_modifier_call_count = 0;
auto result_modifier = [&total_modifier_call_count](
const v8::LocalVector<v8::Value>& result_args,
v8::Local<v8::Context> context,
binding::AsyncResponseType async_type) {
total_modifier_call_count++;
if (async_type == binding::AsyncResponseType::kCallback) {
// For callback based calls change the result to a vector with
// multiple arguments by appending "bar" to the end.
v8::LocalVector<v8::Value> new_args(
context->GetIsolate(),
{result_args[0], gin::StringToV8(context->GetIsolate(), "bar")});
return new_args;
}
return result_args;
};
auto hook_with_result_modifier =
[&result_modifier](const APISignature* signature,
v8::Local<v8::Context> context,
v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& ref_map) {
APIBindingHooks::RequestResult result(
APIBindingHooks::RequestResult::NOT_HANDLED,
v8::Local<v8::Function>(),
base::BindLambdaForTesting(result_modifier));
return result;
};
hooks->AddHandler("test.supportsPromises",
base::BindLambdaForTesting(hook_with_result_modifier));
SetHooksDelegate(std::move(hooks));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// A promise-based call should remain unmodified and return as normal.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(1); });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(api_result, &promise));
EXPECT_EQ(v8::Promise::kPending, promise->State());
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["foo"])"),
std::string());
EXPECT_EQ(v8::Promise::kFulfilled, promise->State());
EXPECT_EQ(R"("foo")", V8ToString(promise->Result(), context));
EXPECT_EQ(1, total_modifier_call_count);
}
// A callback-based call will be modified by the hook and return with multiple
// parameters.
{
constexpr char kFunctionCall[] =
R"((function(api) {
api.supportsPromises(2, (normalResult, addedResult) => {
this.argument1 = normalResult;
this.argument2 = addedResult;
});
}))";
v8::Local<v8::Function> callback_api_call =
FunctionFromString(context, kFunctionCall);
v8::Local<v8::Value> args[] = {binding_object};
RunFunctionOnGlobal(callback_api_call, context, std::size(args), args);
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["foo"])"),
std::string());
EXPECT_EQ(R"("foo")", GetStringPropertyFromObject(context->Global(),
context, "argument1"));
EXPECT_EQ(R"("bar")", GetStringPropertyFromObject(context->Global(),
context, "argument2"));
EXPECT_EQ(2, total_modifier_call_count);
}
// A call which results in an error should reject as expected and the result
// modifier should never be called.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(3) });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
v8::Local<v8::Promise> promise = api_result.As<v8::Promise>();
ASSERT_FALSE(api_result.IsEmpty());
EXPECT_EQ(v8::Promise::kPending, promise->State());
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
base::Value::List(), "Error message");
EXPECT_EQ(v8::Promise::kRejected, promise->State());
ASSERT_TRUE(promise->Result()->IsObject());
EXPECT_EQ(R"("Error message")",
GetStringPropertyFromObject(promise->Result().As<v8::Object>(),
context, "message"));
// Since the result modifier should have never been called, the total call
// count should still be the same as in the previous test case.
EXPECT_EQ(2, total_modifier_call_count);
}
}
// Test native hooks that add a result modifier are compatible with JS hooks
// which handle the request.
TEST_F(APIBindingUnittest, TestHooksWithResultModifierAndJSHook) {
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
// Register a JS hook for supportsPromises.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setHandleRequest('supportsPromises', (firstArg, callback) => {
// Call the callback, appending "-foo" to the argument passed in.
callback(firstArg + '-foo');
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctionsWithPromiseSignatures);
// Register a native hook for test.supportsPromises with a result modifier
// that changes the result when the async response type is callback based.
auto hooks = std::make_unique<APIBindingHooksTestDelegate>();
auto result_modifier = [](const v8::LocalVector<v8::Value>& result_args,
v8::Local<v8::Context> context,
binding::AsyncResponseType async_type) {
if (async_type == binding::AsyncResponseType::kCallback) {
// For callback based calls change the result to a vector with
// multiple arguments by appending "bar" to the end.
v8::LocalVector<v8::Value> new_args(
context->GetIsolate(),
{result_args[0], gin::StringToV8(context->GetIsolate(), "bar")});
return new_args;
}
return result_args;
};
auto hook_with_result_modifier =
[&result_modifier](const APISignature* signature,
v8::Local<v8::Context> context,
v8::LocalVector<v8::Value>* arguments,
const APITypeReferenceMap& ref_map) {
APIBindingHooks::RequestResult result(
APIBindingHooks::RequestResult::NOT_HANDLED,
v8::Local<v8::Function>(), base::BindOnce(result_modifier));
return result;
};
// Normally handlers are bound using base::BindRepeating, but to bind a lambda
// with a capture we have to use BindLambdaForTesting.
hooks->AddHandler("test.supportsPromises",
base::BindLambdaForTesting(hook_with_result_modifier));
SetHooksDelegate(std::move(hooks));
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// A promise-based call should just be modified by the JS hook..
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(1); });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
// Since the JS callback completes the request right away, the promise
// should already be fulfilled without us needing to manually complete the
// request.
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(api_result, &promise));
EXPECT_EQ(v8::Promise::kFulfilled, promise->State());
EXPECT_EQ(R"("1-foo")", V8ToString(promise->Result(), context));
}
// A callback-based call will be modified by the native hook to return with
// multiple parameters, as well as having the first parameter modified by the
// JS hook.
{
constexpr char kFunctionCall[] =
R"((function(api) {
api.supportsPromises(2, (normalResult, addedResult) => {
this.argument1 = normalResult;
this.argument2 = addedResult;
});
}))";
v8::Local<v8::Function> promise_api_call =
FunctionFromString(context, kFunctionCall);
v8::Local<v8::Value> args[] = {binding_object};
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
EXPECT_EQ(R"("2-foo")", GetStringPropertyFromObject(context->Global(),
context, "argument1"));
EXPECT_EQ(R"("bar")", GetStringPropertyFromObject(context->Global(),
context, "argument2"));
}
}
TEST_F(APIBindingUnittest, AccessAPIMethodsAndEventsAfterInvalidation) {
SetEvents(R"([{"name": "onFoo"}])");
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
v8::Local<v8::Function> function = FunctionFromString(
context, "(function(obj) { obj.onFoo.addListener(function() {}); })");
binding::InvalidateContext(context);
v8::Local<v8::Value> argv[] = {binding_object};
RunFunctionAndExpectError(function, context, std::size(argv), argv,
"Uncaught Error: Extension context invalidated.");
}
TEST_F(APIBindingUnittest, CallbackSignaturesAreAdded) {
std::unique_ptr<base::AutoReset<bool>> response_validation_override =
binding::SetResponseValidationEnabledForTesting(true);
SetFunctions(kFunctionsWithCallbackSignatures);
InitializeBinding();
{
const APISignature* signature =
type_refs().GetAPIMethodSignature("test.noCallback");
ASSERT_TRUE(signature);
EXPECT_FALSE(signature->has_async_return());
EXPECT_FALSE(signature->has_async_return_signature());
}
{
const APISignature* signature =
type_refs().GetAPIMethodSignature("test.intCallback");
ASSERT_TRUE(signature);
EXPECT_TRUE(signature->has_async_return());
EXPECT_TRUE(signature->has_async_return_signature());
}
{
const APISignature* signature =
type_refs().GetAPIMethodSignature("test.noParamCallback");
ASSERT_TRUE(signature);
EXPECT_TRUE(signature->has_async_return());
EXPECT_TRUE(signature->has_async_return_signature());
}
}
TEST_F(APIBindingUnittest,
CallbackSignaturesAreNotAddedWhenValidationDisabled) {
std::unique_ptr<base::AutoReset<bool>> response_validation_override =
binding::SetResponseValidationEnabledForTesting(false);
SetFunctions(kFunctionsWithCallbackSignatures);
InitializeBinding();
EXPECT_FALSE(
type_refs().GetAPIMethodSignature("test.noCallback")->has_async_return());
EXPECT_TRUE(type_refs()
.GetAPIMethodSignature("test.intCallback")
->has_async_return());
EXPECT_FALSE(type_refs()
.GetAPIMethodSignature("test.intCallback")
->has_async_return_signature());
EXPECT_TRUE(type_refs()
.GetAPIMethodSignature("test.noParamCallback")
->has_async_return());
EXPECT_FALSE(type_refs()
.GetAPIMethodSignature("test.noParamCallback")
->has_async_return_signature());
}
// Tests promise-based APIs exposed on bindings.
TEST_F(APIBindingUnittest, PromiseBasedAPIs) {
SetFunctions(kFunctionsWithPromiseSignatures);
// Set a local boolean we can change to simulate if the context supports
// promises or not.
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// A normal call into the promised based API should return a promise. When the
// request is completed with a value, the promise will be resolved with that
// value.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(3); })");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(result, &promise));
EXPECT_EQ(v8::Promise::kPending, promise->State());
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["foo"])"),
std::string());
EXPECT_EQ(v8::Promise::kFulfilled, promise->State());
EXPECT_EQ(R"("foo")", V8ToString(promise->Result(), context));
}
// Also test that promise-based APIs still support passing a callback.
{
constexpr char kFunctionCall[] =
R"((function(api) {
api.supportsPromises(3, (strResult) => {
this.callbackResult = strResult
});
}))";
v8::Local<v8::Function> promise_api_call =
FunctionFromString(context, kFunctionCall);
v8::Local<v8::Value> args[] = {binding_object};
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["bar"])"),
std::string());
EXPECT_EQ(R"("bar")", GetStringPropertyFromObject(
context->Global(), context, "callbackResult"));
}
// If a request is completed with an error, the promise should be rejected.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(3) });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
v8::Local<v8::Promise> promise = api_result.As<v8::Promise>();
ASSERT_FALSE(api_result.IsEmpty());
EXPECT_EQ(v8::Promise::kPending, promise->State());
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
base::Value::List(), "Error message");
EXPECT_EQ(v8::Promise::kRejected, promise->State());
ASSERT_TRUE(promise->Result()->IsObject());
EXPECT_EQ(R"("Error message")",
GetStringPropertyFromObject(promise->Result().As<v8::Object>(),
context, "message"));
}
// If a request is completed with a result and an error, the promise should be
// rejected and the result will not be returned. Note: ideally no APIs would
// do this but some legacy APIs do it through returning ErrorWithArguments as
// their ResponseValue. This testcase documents how this behaves with
// promises.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(3) });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
v8::Local<v8::Promise> promise = api_result.As<v8::Promise>();
ASSERT_FALSE(api_result.IsEmpty());
EXPECT_EQ(v8::Promise::kPending, promise->State());
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["bar"])"),
"Error message");
EXPECT_EQ(v8::Promise::kRejected, promise->State());
ASSERT_TRUE(promise->Result()->IsObject());
EXPECT_EQ(R"("Error message")",
GetStringPropertyFromObject(promise->Result().As<v8::Object>(),
context, "message"));
}
// If the context doesn't support promises, there should be an error if a
// required callback isn't supplied.
context_allows_promises = false;
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(3) });");
v8::Local<v8::Value> args[] = {binding_object};
auto expected_error =
"Uncaught TypeError: " +
api_errors::InvocationError("test.supportsPromises",
"integer int, function callback",
api_errors::NoMatchingSignature());
RunFunctionAndExpectError(promise_api_call, context, std::size(args), args,
expected_error);
}
// Test that required callbacks still work when the context doesn't support
// promises.
{
constexpr char kFunctionCall[] =
R"((function(api) {
api.supportsPromises(3, (strResult) => {
this.callbackResult = strResult
});
}))";
v8::Local<v8::Function> promise_api_call =
FunctionFromString(context, kFunctionCall);
v8::Local<v8::Value> args[] = {binding_object};
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["foo"])"),
std::string());
EXPECT_EQ(R"("foo")", GetStringPropertyFromObject(
context->Global(), context, "callbackResult"));
}
// If a returns_async field is marked as optional, then a context which
// doesn't support promises should be able to leave it off of the call.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.callbackOptional(3) });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
ASSERT_TRUE(last_request());
ASSERT_TRUE(api_result->IsNullOrUndefined());
}
}
TEST_F(APIBindingUnittest, TestPromisesWithJSCustomCallback) {
// Set a local boolean we can change to simulate if the context supports
// promises or not.
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
// Register a custom callback hook for the supportsPromises method.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setCustomCallback('supportsPromises',
(callback, response) => {
this.response = response;
this.resolveCallback = callback;
if (response == 'resolveNow')
callback('bar');
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctionsWithPromiseSignatures);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
// A normal call into the promise-based API should return a promise.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(1); });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(api_result, &promise));
EXPECT_EQ(v8::Promise::kPending, promise->State());
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["foo"])"),
std::string());
// The promise should still be unfulfilled until the callback is invoked.
EXPECT_EQ(v8::Promise::kPending, promise->State());
v8::Local<v8::Function> resolve_callback;
ASSERT_TRUE(GetPropertyFromObjectAs(context->Global(), context,
"resolveCallback", &resolve_callback));
v8::Local<v8::Value> callback_arguments[] = {
GetPropertyFromObject(context->Global(), context, "response")};
EXPECT_EQ(R"("foo")", V8ToString(callback_arguments[0], context));
RunFunctionOnGlobal(resolve_callback, context,
std::size(callback_arguments), callback_arguments);
EXPECT_EQ(v8::Promise::kFulfilled, promise->State());
EXPECT_EQ(R"("foo")", V8ToString(promise->Result(), context));
}
// Sending a response to the hook to make it resolve immediately should result
// in the promise being resolved right after CompleteRequest is called.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(2); });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
v8::Local<v8::Promise> promise;
ASSERT_TRUE(GetValueAs(api_result, &promise));
EXPECT_EQ(v8::Promise::kPending, promise->State());
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["resolveNow"])"),
std::string());
EXPECT_EQ(v8::Promise::kFulfilled, promise->State());
EXPECT_EQ(R"("bar")", V8ToString(promise->Result(), context));
}
// Completing the request with an error should still call into the custom
// callback, which will reject the promise with the error when the callback
// passed to it is called.
{
v8::Local<v8::Function> promise_api_call = FunctionFromString(
context, "(function(api) { return api.supportsPromises(3) });");
v8::Local<v8::Value> args[] = {binding_object};
v8::Local<v8::Value> api_result =
RunFunctionOnGlobal(promise_api_call, context, std::size(args), args);
v8::Local<v8::Promise> promise = api_result.As<v8::Promise>();
ASSERT_FALSE(api_result.IsEmpty());
EXPECT_EQ(v8::Promise::kPending, promise->State());
ASSERT_TRUE(last_request());
request_handler()->CompleteRequest(last_request()->request_id,
ListValueFromString(R"(["baz"])"),
"Error message");
EXPECT_EQ(v8::Promise::kPending, promise->State());
v8::Local<v8::Value> resolve_callback =
GetPropertyFromObject(context->Global(), context, "resolveCallback");
ASSERT_TRUE(resolve_callback->IsFunction());
RunFunctionOnGlobal(resolve_callback.As<v8::Function>(), context, 0,
nullptr);
EXPECT_EQ(v8::Promise::kRejected, promise->State());
ASSERT_TRUE(promise->Result()->IsObject());
EXPECT_EQ(R"("Error message")",
GetStringPropertyFromObject(promise->Result().As<v8::Object>(),
context, "message"));
}
}
TEST_F(APIBindingUnittest, TestPromiseWithJSUpdateArgumentsPreValidate) {
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
// Register an update arguments pre validate hook for supportsPromises.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setUpdateArgumentsPreValidate('supportsPromises',
(...arguments) => {
this.firstArgument = arguments[0];
this.secondArgument = arguments[1];
if (arguments[0] == 'hooked')
arguments[0] = 42;
return arguments;
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctionsWithPromiseSignatures);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
{
// Calling supportsPromises normally with a callback should work fine.
auto result =
ExpectPass(binding_object, "return obj.supportsPromises(5, () => {});",
"[5]", true);
ASSERT_FALSE(result.IsEmpty());
EXPECT_TRUE(result->IsUndefined());
EXPECT_TRUE(
GetPropertyFromObject(context->Global(), context, "secondArgument")
->IsFunction());
}
{
// Calling supportsPromises normally while omitting the callback should work
// fine.
auto result = ExpectPass(binding_object, "return obj.supportsPromises(5);",
"[5]", true);
EXPECT_TRUE(V8ValueIs<v8::Promise>(result));
}
{
// Calling supportsPromises with a string which we have not set up the
// custom hook for should cause an error.
ExpectFailure(binding_object, "obj.supportsPromises('foo');",
api_errors::InvocationError(
"test.supportsPromises", "integer int, function callback",
api_errors::NoMatchingSignature()));
EXPECT_EQ(R"("foo")", GetStringPropertyFromObject(
context->Global(), context, "firstArgument"));
}
{
// supportsPromises expects an int, but our custom hook should allow the
// string 'hooked' to work as well.
auto result = ExpectPass(
binding_object, "return obj.supportsPromises('hooked');", "[42]", true);
EXPECT_TRUE(V8ValueIs<v8::Promise>(result));
EXPECT_EQ(R"("hooked")", GetStringPropertyFromObject(
context->Global(), context, "firstArgument"));
}
{
// We should also be able to hit the custom hook with a callback still.
auto result = ExpectPass(binding_object,
"return obj.supportsPromises('hooked', () => {});",
"[42]", true);
ASSERT_FALSE(result.IsEmpty());
EXPECT_TRUE(result->IsUndefined());
EXPECT_EQ(R"("hooked")", GetStringPropertyFromObject(
context->Global(), context, "firstArgument"));
EXPECT_TRUE(
GetPropertyFromObject(context->Global(), context, "secondArgument")
->IsFunction());
}
}
TEST_F(APIBindingUnittest, TestPromiseWithJSUpdateArgumentsPostValidate) {
bool context_allows_promises = true;
SetPromiseAvailabilityFlag(&context_allows_promises);
// Register an update arguments post validate hook for supportsPromises.
const char kRegisterHook[] = R"(
(function(hooks) {
hooks.setUpdateArgumentsPostValidate('supportsPromises',
(...arguments) => {
this.firstArgument = arguments[0];
this.secondArgument = arguments[1];
arguments[0] = 'bar' + this.firstArgument;
return arguments;
});
}))";
InitializeJSHooks(kRegisterHook);
SetFunctions(kFunctionsWithPromiseSignatures);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
{
// Calling the method with an invalid signature should never enter the hook.
ExpectFailure(binding_object, "return obj.supportsPromises('foo');",
api_errors::InvocationError(
"test.supportsPromises", "integer int, function callback",
api_errors::NoMatchingSignature()));
EXPECT_EQ("undefined", GetStringPropertyFromObject(
context->Global(), context, "firstArgument"));
}
{
// Calling supportsPromises normally with a callback should work fine and
// the arguments should be manipulated.
auto result =
ExpectPass(binding_object, "return obj.supportsPromises(5, () => {});",
R"(["bar5"])", true);
ASSERT_FALSE(result.IsEmpty());
EXPECT_TRUE(result->IsUndefined());
EXPECT_EQ(R"(5)", GetStringPropertyFromObject(context->Global(), context,
"firstArgument"));
EXPECT_TRUE(
GetPropertyFromObject(context->Global(), context, "secondArgument")
->IsFunction());
}
{
// Calling supportsPromises normally while omitting the callback should work
// fine, we should get a promise back and the arguments should be
// manipulated.
auto result = ExpectPass(binding_object, "return obj.supportsPromises(6);",
R"(["bar6"])", true);
EXPECT_TRUE(V8ValueIs<v8::Promise>(result));
EXPECT_EQ(R"(6)", GetStringPropertyFromObject(context->Global(), context,
"firstArgument"));
}
}
TEST_F(APIBindingUnittest, UnicodeArgumentsPassedCorrectly) {
SetFunctions(kFunctions);
InitializeBinding();
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = MainContext();
// This contains a non-BMP Unicode character, which should be correctly passed
// as an argument to the function, through the UTF-8 -> UTF-16 -> UTF-8 round
// trip.
constexpr char kSource[] = u8"(function(obj) { obj.oneString('🤡'); })";
constexpr char kExpectation[] = u8"🤡";
v8::Local<v8::Function> func = FunctionFromString(context, kSource);
ASSERT_FALSE(func.IsEmpty());
v8::Local<v8::Value> argv[] = {binding()->CreateInstance(context)};
RunFunction(func, context, 1, argv);
ASSERT_TRUE(last_request());
ASSERT_EQ(1u, last_request()->arguments_list.size());
base::Value str_value = last_request()->arguments_list.front().Clone();
ASSERT_TRUE(str_value.is_string());
ASSERT_EQ(kExpectation, *str_value.GetIfString());
}
} // namespace extensions
|