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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/background_sync/background_sync_manager.h"
#include <algorithm>
#include <utility>
#include "base/barrier_closure.h"
#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "content/browser/background_sync/background_sync_metrics.h"
#include "content/browser/background_sync/background_sync_network_observer.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/background_sync_controller.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/permission_descriptor_util.h"
#include "content/public/browser/render_process_host.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/common/service_worker/service_worker_type_converters.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#if BUILDFLAG(IS_ANDROID)
#include "content/browser/android/background_sync_network_observer_android.h"
#include "content/browser/background_sync/background_sync_launcher.h"
#endif
using blink::mojom::BackgroundSyncType;
using blink::mojom::PermissionStatus;
using SyncAndNotificationPermissions =
std::pair<PermissionStatus, PermissionStatus>;
namespace content {
// TODO(crbug.com/40614176): Use blink::mojom::BackgroundSyncError
// directly and eliminate these checks.
#define COMPILE_ASSERT_MATCHING_ENUM(mojo_name, manager_name) \
static_assert(static_cast<int>(blink::mojo_name) == \
static_cast<int>(content::manager_name), \
"mojo and manager enums must match")
COMPILE_ASSERT_MATCHING_ENUM(mojom::BackgroundSyncError::NONE,
BACKGROUND_SYNC_STATUS_OK);
COMPILE_ASSERT_MATCHING_ENUM(mojom::BackgroundSyncError::STORAGE,
BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
COMPILE_ASSERT_MATCHING_ENUM(mojom::BackgroundSyncError::NOT_FOUND,
BACKGROUND_SYNC_STATUS_NOT_FOUND);
COMPILE_ASSERT_MATCHING_ENUM(mojom::BackgroundSyncError::NO_SERVICE_WORKER,
BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER);
COMPILE_ASSERT_MATCHING_ENUM(mojom::BackgroundSyncError::NOT_ALLOWED,
BACKGROUND_SYNC_STATUS_NOT_ALLOWED);
COMPILE_ASSERT_MATCHING_ENUM(mojom::BackgroundSyncError::PERMISSION_DENIED,
BACKGROUND_SYNC_STATUS_PERMISSION_DENIED);
COMPILE_ASSERT_MATCHING_ENUM(mojom::BackgroundSyncError::MAX,
BACKGROUND_SYNC_STATUS_PERMISSION_DENIED);
namespace {
// The only allowed value of min_interval for one shot Background Sync
// registrations.
constexpr int kMinIntervalForOneShotSync = -1;
// The key used to index the background sync data in ServiceWorkerStorage.
const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData";
void RecordFailureAndPostError(
BackgroundSyncType sync_type,
BackgroundSyncStatus status,
BackgroundSyncManager::StatusAndRegistrationCallback callback) {
BackgroundSyncMetrics::CountRegisterFailure(sync_type, status);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), status, nullptr));
}
// Returns nullptr if the browser context cannot be accessed for any reason.
BrowserContext* GetBrowserContext(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!service_worker_context)
return nullptr;
StoragePartitionImpl* storage_partition_impl =
service_worker_context->storage_partition();
if (!storage_partition_impl) // may be null in tests
return nullptr;
return storage_partition_impl->browser_context();
}
// Returns nullptr if the controller cannot be accessed for any reason.
BackgroundSyncController* GetBackgroundSyncController(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserContext* browser_context =
GetBrowserContext(std::move(service_worker_context));
if (!browser_context)
return nullptr;
return browser_context->GetBackgroundSyncController();
}
SyncAndNotificationPermissions GetBackgroundSyncPermission(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
const url::Origin& origin,
RenderProcessHost* render_process_host,
BackgroundSyncType sync_type) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserContext* browser_context =
GetBrowserContext(std::move(service_worker_context));
if (!browser_context)
return {PermissionStatus::DENIED, PermissionStatus::DENIED};
PermissionController* permission_controller =
browser_context->GetPermissionController();
DCHECK(permission_controller);
// The requesting origin always matches the embedding origin.
auto sync_permission = permission_controller->GetPermissionStatusForWorker(
content::PermissionDescriptorUtil::
CreatePermissionDescriptorForPermissionType(
sync_type == BackgroundSyncType::ONE_SHOT
? blink::PermissionType::BACKGROUND_SYNC
: blink::PermissionType::PERIODIC_BACKGROUND_SYNC),
render_process_host, origin);
auto notification_permission =
permission_controller->GetPermissionStatusForWorker(
content::PermissionDescriptorUtil::
CreatePermissionDescriptorForPermissionType(
blink::PermissionType::NOTIFICATIONS),
render_process_host, origin);
return {sync_permission, notification_permission};
}
void NotifyOneShotBackgroundSyncRegistered(
scoped_refptr<ServiceWorkerContextWrapper> sw_context_wrapper,
const url::Origin& origin,
bool can_fire,
bool is_reregistered) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncController* background_sync_controller =
GetBackgroundSyncController(std::move(sw_context_wrapper));
if (!background_sync_controller)
return;
background_sync_controller->NotifyOneShotBackgroundSyncRegistered(
origin, can_fire, is_reregistered);
}
void NotifyPeriodicBackgroundSyncRegistered(
scoped_refptr<ServiceWorkerContextWrapper> sw_context_wrapper,
const url::Origin& origin,
int min_interval,
bool is_reregistered) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncController* background_sync_controller =
GetBackgroundSyncController(std::move(sw_context_wrapper));
if (!background_sync_controller)
return;
background_sync_controller->NotifyPeriodicBackgroundSyncRegistered(
origin, min_interval, is_reregistered);
}
void NotifyOneShotBackgroundSyncCompleted(
scoped_refptr<ServiceWorkerContextWrapper> sw_context_wrapper,
const url::Origin& origin,
blink::ServiceWorkerStatusCode status_code,
int num_attempts,
int max_attempts) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncController* background_sync_controller =
GetBackgroundSyncController(std::move(sw_context_wrapper));
if (!background_sync_controller)
return;
background_sync_controller->NotifyOneShotBackgroundSyncCompleted(
origin, status_code, num_attempts, max_attempts);
}
void NotifyPeriodicBackgroundSyncCompleted(
scoped_refptr<ServiceWorkerContextWrapper> sw_context_wrapper,
const url::Origin& origin,
blink::ServiceWorkerStatusCode status_code,
int num_attempts,
int max_attempts) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncController* background_sync_controller =
GetBackgroundSyncController(std::move(sw_context_wrapper));
if (!background_sync_controller)
return;
background_sync_controller->NotifyPeriodicBackgroundSyncCompleted(
origin, status_code, num_attempts, max_attempts);
}
std::unique_ptr<BackgroundSyncParameters> GetControllerParameters(
scoped_refptr<ServiceWorkerContextWrapper> sw_context_wrapper,
std::unique_ptr<BackgroundSyncParameters> parameters) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncController* background_sync_controller =
GetBackgroundSyncController(sw_context_wrapper);
if (!background_sync_controller) {
// If there is no controller then BackgroundSync can't run in the
// background, disable it.
parameters->disable = true;
return parameters;
}
background_sync_controller->GetParameterOverrides(parameters.get());
return parameters;
}
base::TimeDelta GetNextEventDelay(
scoped_refptr<ServiceWorkerContextWrapper> sw_context_wrapper,
const BackgroundSyncRegistration& registration,
std::unique_ptr<BackgroundSyncParameters> parameters,
base::TimeDelta time_till_soonest_scheduled_event_for_origin) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncController* background_sync_controller =
GetBackgroundSyncController(sw_context_wrapper);
if (!background_sync_controller)
return base::TimeDelta::Max();
return background_sync_controller->GetNextEventDelay(
registration, parameters.get(),
time_till_soonest_scheduled_event_for_origin);
}
void OnSyncEventFinished(scoped_refptr<ServiceWorkerVersion> active_version,
int request_id,
ServiceWorkerVersion::StatusCallback callback,
blink::mojom::ServiceWorkerEventStatus status) {
if (!active_version->FinishRequest(
request_id,
status == blink::mojom::ServiceWorkerEventStatus::COMPLETED)) {
return;
}
std::move(callback).Run(
mojo::ConvertTo<blink::ServiceWorkerStatusCode>(status));
}
void DidStartWorkerForSyncEvent(
base::OnceCallback<void(ServiceWorkerVersion::StatusCallback)> task,
ServiceWorkerVersion::StatusCallback callback,
blink::ServiceWorkerStatusCode start_worker_status) {
if (start_worker_status != blink::ServiceWorkerStatusCode::kOk) {
std::move(callback).Run(start_worker_status);
return;
}
std::move(task).Run(std::move(callback));
}
BackgroundSyncType GetBackgroundSyncType(
const blink::mojom::SyncRegistrationOptions& options) {
return options.min_interval == -1 ? BackgroundSyncType::ONE_SHOT
: BackgroundSyncType::PERIODIC;
}
std::string GetSyncEventName(const BackgroundSyncType sync_type) {
if (sync_type == BackgroundSyncType::ONE_SHOT)
return "sync";
else
return "periodicsync";
}
DevToolsBackgroundService GetDevToolsBackgroundService(
BackgroundSyncType sync_type) {
if (sync_type == BackgroundSyncType::ONE_SHOT)
return DevToolsBackgroundService::kBackgroundSync;
else
return DevToolsBackgroundService::kPeriodicBackgroundSync;
}
std::string GetDelayAsString(base::TimeDelta delay) {
if (delay.is_max())
return "infinite";
return base::NumberToString(delay.InMilliseconds());
}
std::string GetEventStatusString(blink::ServiceWorkerStatusCode status_code) {
// The |status_code| is derived from blink::mojom::ServiceWorkerEventStatus.
switch (status_code) {
case blink::ServiceWorkerStatusCode::kOk:
return "succeeded";
case blink::ServiceWorkerStatusCode::kErrorEventWaitUntilRejected:
return "waitUntil rejected";
case blink::ServiceWorkerStatusCode::kErrorFailed:
return "failed";
case blink::ServiceWorkerStatusCode::kErrorAbort:
return "aborted";
case blink::ServiceWorkerStatusCode::kErrorTimeout:
return "timeout";
default:
SCOPED_CRASH_KEY_NUMBER("BGSM", "status_code",
static_cast<int>(status_code));
DUMP_WILL_BE_NOTREACHED()
<< "status_code " << static_cast<int>(status_code);
return "unknown error";
}
}
int GetNumAttemptsAfterEvent(BackgroundSyncType sync_type,
int current_num_attempts,
int max_attempts,
blink::mojom::BackgroundSyncState sync_state,
bool succeeded) {
int num_attempts = ++current_num_attempts;
if (sync_type == BackgroundSyncType::PERIODIC) {
if (succeeded)
return 0;
if (num_attempts == max_attempts)
return 0;
}
if (sync_state ==
blink::mojom::BackgroundSyncState::REREGISTERED_WHILE_FIRING) {
return 0;
}
return num_attempts;
}
// This prevents the browser process from shutting down when the last browser
// window is closed and there are one-shot Background Sync events ready to fire.
std::unique_ptr<BackgroundSyncController::BackgroundSyncEventKeepAlive>
CreateBackgroundSyncEventKeepAlive(
scoped_refptr<ServiceWorkerContextWrapper> sw_context_wrapper,
const blink::mojom::BackgroundSyncRegistrationInfo& registration_info) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncController* controller =
GetBackgroundSyncController(sw_context_wrapper);
if (!controller ||
registration_info.sync_type != BackgroundSyncType::ONE_SHOT) {
return nullptr;
}
return controller->CreateBackgroundSyncEventKeepAlive();
}
} // namespace
BackgroundSyncManager::BackgroundSyncRegistrations::
BackgroundSyncRegistrations() = default;
BackgroundSyncManager::BackgroundSyncRegistrations::BackgroundSyncRegistrations(
const BackgroundSyncRegistrations& other) = default;
BackgroundSyncManager::BackgroundSyncRegistrations::
~BackgroundSyncRegistrations() = default;
// static
std::unique_ptr<BackgroundSyncManager> BackgroundSyncManager::Create(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
DevToolsBackgroundServicesContextImpl& devtools_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncManager* sync_manager = new BackgroundSyncManager(
std::move(service_worker_context), devtools_context);
sync_manager->Init();
return base::WrapUnique(sync_manager);
}
BackgroundSyncManager::~BackgroundSyncManager() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
service_worker_context_->RemoveObserver(this);
}
void BackgroundSyncManager::Register(
int64_t sw_registration_id,
int render_process_host_id,
blink::mojom::SyncRegistrationOptions options,
StatusAndRegistrationCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
std::move(callback));
return;
}
DCHECK(options.min_interval >= 0 ||
options.min_interval == kMinIntervalForOneShotSync);
op_scheduler_.ScheduleOperation(
base::BindOnce(&BackgroundSyncManager::RegisterCheckIfHasMainFrame,
weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
render_process_host_id, std::move(options),
op_scheduler_.WrapCallbackToRunNext(std::move(callback))));
}
void BackgroundSyncManager::UnregisterPeriodicSync(
int64_t sw_registration_id,
const std::string& tag,
BackgroundSyncManager::StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
return;
}
op_scheduler_.ScheduleOperation(
base::BindOnce(&BackgroundSyncManager::UnregisterPeriodicSyncImpl,
weak_ptr_factory_.GetWeakPtr(), sw_registration_id, tag,
op_scheduler_.WrapCallbackToRunNext(std::move(callback))));
}
void BackgroundSyncManager::DidResolveRegistration(
blink::mojom::BackgroundSyncRegistrationInfoPtr registration_info) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_)
return;
op_scheduler_.ScheduleOperation(base::BindOnce(
&BackgroundSyncManager::DidResolveRegistrationImpl,
weak_ptr_factory_.GetWeakPtr(), std::move(registration_info)));
}
void BackgroundSyncManager::GetOneShotSyncRegistrations(
int64_t sw_registration_id,
StatusAndRegistrationsCallback callback) {
GetRegistrations(BackgroundSyncType::ONE_SHOT, sw_registration_id,
std::move(callback));
}
void BackgroundSyncManager::GetPeriodicSyncRegistrations(
int64_t sw_registration_id,
StatusAndRegistrationsCallback callback) {
GetRegistrations(BackgroundSyncType::PERIODIC, sw_registration_id,
std::move(callback));
}
void BackgroundSyncManager::UnregisterPeriodicSyncForOrigin(
const url::Origin& origin) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
op_scheduler_.ScheduleOperation(
base::BindOnce(&BackgroundSyncManager::UnregisterForOriginImpl,
weak_ptr_factory_.GetWeakPtr(), std::move(origin),
MakeEmptyCompletion()));
}
void BackgroundSyncManager::UnregisterForOriginImpl(
const url::Origin& origin,
base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
std::vector<int64_t> service_worker_registrations_affected;
for (const auto& service_worker_and_registration : active_registrations_) {
const auto registrations = service_worker_and_registration.second;
if (registrations.origin != origin)
continue;
service_worker_registrations_affected.emplace_back(
service_worker_and_registration.first);
}
if (service_worker_registrations_affected.empty()) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
base::RepeatingClosure barrier_closure = base::BarrierClosure(
service_worker_registrations_affected.size(),
base::BindOnce(
&BackgroundSyncManager::UnregisterForOriginScheduleDelayedProcessing,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
for (int64_t service_worker_registration_id :
service_worker_registrations_affected) {
StoreRegistrations(
service_worker_registration_id,
base::BindOnce(&BackgroundSyncManager::UnregisterForOriginDidStore,
weak_ptr_factory_.GetWeakPtr(),
service_worker_registration_id, barrier_closure));
}
}
void BackgroundSyncManager::UnregisterForOriginDidStore(
int64_t service_worker_registration_id_to_remove,
base::OnceClosure done_closure,
blink::ServiceWorkerStatusCode status) {
active_registrations_.erase(service_worker_registration_id_to_remove);
if (status == blink::ServiceWorkerStatusCode::kErrorNotFound) {
// The service worker registration is gone.
std::move(done_closure).Run();
return;
}
if (status != blink::ServiceWorkerStatusCode::kOk) {
DisableAndClearManager(std::move(done_closure));
return;
}
std::move(done_closure).Run();
}
void BackgroundSyncManager::UnregisterForOriginScheduleDelayedProcessing(
base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ScheduleOrCancelDelayedProcessing(BackgroundSyncType::PERIODIC);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
}
void BackgroundSyncManager::GetRegistrations(
BackgroundSyncType sync_type,
int64_t sw_registration_id,
StatusAndRegistrationsCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The renderer should have checked and disallowed the request for fenced
// frames and thrown an exception in blink::SyncManager or
// blink::PeriodicSyncManager. Return a not allowed error if the renderer side
// check didn't happen for some reason.
scoped_refptr<ServiceWorkerRegistration> sw_registration =
service_worker_context_->GetLiveRegistration(sw_registration_id);
if (sw_registration && sw_registration->ancestor_frame_type() ==
blink::mojom::AncestorFrameType::kFencedFrame) {
mojo::ReportBadMessage("Background Sync is not allowed in a fenced frame");
return;
}
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(
std::move(callback), BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
std::vector<std::unique_ptr<BackgroundSyncRegistration>>()));
return;
}
op_scheduler_.ScheduleOperation(base::BindOnce(
&BackgroundSyncManager::GetRegistrationsImpl,
weak_ptr_factory_.GetWeakPtr(), sync_type, sw_registration_id,
op_scheduler_.WrapCallbackToRunNext(std::move(callback))));
}
void BackgroundSyncManager::OnRegistrationDeleted(
int64_t sw_registration_id,
const GURL& pattern,
const blink::StorageKey& key) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Operations already in the queue will either fail when they write to storage
// or return stale results based on registrations loaded in memory. This is
// inconsequential since the service worker is gone.
op_scheduler_.ScheduleOperation(
base::BindOnce(&BackgroundSyncManager::OnRegistrationDeletedImpl,
weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
MakeEmptyCompletion()));
}
void BackgroundSyncManager::OnStorageWiped() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Operations already in the queue will either fail when they write to storage
// or return stale results based on registrations loaded in memory. This is
// inconsequential since the service workers are gone.
op_scheduler_.ScheduleOperation(
base::BindOnce(&BackgroundSyncManager::OnStorageWipedImpl,
weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion()));
}
void BackgroundSyncManager::EmulateDispatchSyncEvent(
const std::string& tag,
scoped_refptr<ServiceWorkerVersion> active_version,
bool last_chance,
ServiceWorkerVersion::StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
blink::ServiceWorkerStatusCode code = CanEmulateSyncEvent(active_version);
if (code != blink::ServiceWorkerStatusCode::kOk) {
std::move(callback).Run(code);
return;
}
DispatchSyncEvent(tag, std::move(active_version), last_chance,
std::move(callback));
}
void BackgroundSyncManager::EmulateDispatchPeriodicSyncEvent(
const std::string& tag,
scoped_refptr<ServiceWorkerVersion> active_version,
ServiceWorkerVersion::StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
blink::ServiceWorkerStatusCode code = CanEmulateSyncEvent(active_version);
if (code != blink::ServiceWorkerStatusCode::kOk) {
std::move(callback).Run(code);
return;
}
DispatchPeriodicSyncEvent(tag, std::move(active_version),
std::move(callback));
}
void BackgroundSyncManager::EmulateServiceWorkerOffline(
int64_t service_worker_id,
bool is_offline) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Multiple DevTools sessions may want to set the same SW offline, which
// is supposed to disable the background sync. For consistency with the
// network stack, SW remains offline until all DevTools sessions disable
// the offline mode.
emulated_offline_sw_[service_worker_id] += is_offline ? 1 : -1;
if (emulated_offline_sw_[service_worker_id] > 0)
return;
emulated_offline_sw_.erase(service_worker_id);
FireReadyEvents(BackgroundSyncType::ONE_SHOT, /* reschedule= */ true,
base::DoNothing());
}
BackgroundSyncManager::BackgroundSyncManager(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
DevToolsBackgroundServicesContextImpl& devtools_context)
: op_scheduler_(base::SingleThreadTaskRunner::GetCurrentDefault()),
service_worker_context_(std::move(service_worker_context)),
proxy_(std::make_unique<BackgroundSyncProxy>(service_worker_context_)),
devtools_context_(&devtools_context),
parameters_(std::make_unique<BackgroundSyncParameters>()),
disabled_(false),
num_firing_registrations_one_shot_(0),
num_firing_registrations_periodic_(0),
clock_(base::DefaultClock::GetInstance()) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(devtools_context_);
DCHECK(service_worker_context_);
service_worker_context_->AddObserver(this);
#if BUILDFLAG(IS_ANDROID)
network_observer_ = std::make_unique<BackgroundSyncNetworkObserverAndroid>(
base::BindRepeating(&BackgroundSyncManager::OnNetworkChanged,
weak_ptr_factory_.GetWeakPtr()));
#else
network_observer_ = std::make_unique<BackgroundSyncNetworkObserver>(
base::BindRepeating(&BackgroundSyncManager::OnNetworkChanged,
weak_ptr_factory_.GetWeakPtr()));
#endif
}
void BackgroundSyncManager::Init() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!op_scheduler_.ScheduledOperations());
DCHECK(!disabled_);
op_scheduler_.ScheduleOperation(
base::BindOnce(&BackgroundSyncManager::InitImpl,
weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion()));
}
void BackgroundSyncManager::InitImpl(base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
InitDidGetControllerParameters(
std::move(callback),
GetControllerParameters(
service_worker_context_,
std::make_unique<BackgroundSyncParameters>(*parameters_)));
}
void BackgroundSyncManager::InitDidGetControllerParameters(
base::OnceClosure callback,
std::unique_ptr<BackgroundSyncParameters> updated_parameters) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
parameters_ = std::move(updated_parameters);
if (parameters_->disable) {
disabled_ = true;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
network_observer_->Init();
GetDataFromBackend(
kBackgroundSyncUserDataKey,
base::BindOnce(&BackgroundSyncManager::InitDidGetDataFromBackend,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void BackgroundSyncManager::InitDidGetDataFromBackend(
base::OnceClosure callback,
const std::vector<std::pair<int64_t, std::string>>& user_data,
blink::ServiceWorkerStatusCode status) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (status != blink::ServiceWorkerStatusCode::kOk &&
status != blink::ServiceWorkerStatusCode::kErrorNotFound) {
DisableAndClearManager(std::move(callback));
return;
}
std::set<url::Origin> suspended_periodic_sync_origins;
std::set<url::Origin> registered_origins;
for (const std::pair<int64_t, std::string>& data : user_data) {
BackgroundSyncRegistrationsProto registrations_proto;
if (registrations_proto.ParseFromString(data.second)) {
BackgroundSyncRegistrations* registrations =
&active_registrations_[data.first];
registrations->origin =
url::Origin::Create(GURL(registrations_proto.origin()));
for (const auto& registration_proto :
registrations_proto.registration()) {
BackgroundSyncType sync_type =
registration_proto.has_periodic_sync_options()
? BackgroundSyncType::PERIODIC
: BackgroundSyncType::ONE_SHOT;
BackgroundSyncRegistration* registration =
®istrations
->registration_map[{registration_proto.tag(), sync_type}];
blink::mojom::SyncRegistrationOptions* options =
registration->options();
options->tag = registration_proto.tag();
if (sync_type == BackgroundSyncType::PERIODIC) {
options->min_interval =
registration_proto.periodic_sync_options().min_interval();
if (options->min_interval < 0) {
DisableAndClearManager(std::move(callback));
return;
}
} else {
options->min_interval = kMinIntervalForOneShotSync;
}
registration->set_num_attempts(registration_proto.num_attempts());
registration->set_delay_until(
base::Time::FromInternalValue(registration_proto.delay_until()));
registration->set_origin(registrations->origin);
registered_origins.insert(registration->origin());
if (registration->is_suspended()) {
suspended_periodic_sync_origins.insert(registration->origin());
}
registration->set_resolved();
if (registration_proto.has_max_attempts())
registration->set_max_attempts(registration_proto.max_attempts());
else
registration->set_max_attempts(parameters_->max_sync_attempts);
}
}
}
FireReadyEvents(BackgroundSyncType::ONE_SHOT, /* reschedule= */ true,
base::DoNothing());
FireReadyEvents(BackgroundSyncType::PERIODIC, /* reschedule= */ true,
base::DoNothing());
proxy_->SendSuspendedPeriodicSyncOrigins(
std::move(suspended_periodic_sync_origins));
proxy_->SendRegisteredPeriodicSyncOrigins(std::move(registered_origins));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
}
void BackgroundSyncManager::RegisterCheckIfHasMainFrame(
int64_t sw_registration_id,
int render_process_host_id,
blink::mojom::SyncRegistrationOptions options,
StatusAndRegistrationCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
scoped_refptr<ServiceWorkerRegistration> sw_registration =
service_worker_context_->GetLiveRegistration(sw_registration_id);
if (!sw_registration || !sw_registration->active_version()) {
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER,
std::move(callback));
return;
}
// The renderer should have checked and disallowed the request for fenced
// frames and thrown an exception in blink::SyncManager or
// blink::PeriodicSyncManager. Return a not allowed error if the renderer side
// check didn't happen for some reason.
if (sw_registration->ancestor_frame_type() ==
blink::mojom::AncestorFrameType::kFencedFrame) {
mojo::ReportBadMessage("Background Sync is not allowed in a fenced frame");
return;
}
HasMainFrameWindowClient(
sw_registration->key(),
base::BindOnce(&BackgroundSyncManager::RegisterDidCheckIfMainFrame,
weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
render_process_host_id, std::move(options),
std::move(callback)));
}
void BackgroundSyncManager::RegisterDidCheckIfMainFrame(
int64_t sw_registration_id,
int render_process_host_id,
blink::mojom::SyncRegistrationOptions options,
StatusAndRegistrationCallback callback,
bool has_main_frame_client) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!has_main_frame_client) {
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_NOT_ALLOWED,
std::move(callback));
return;
}
RegisterImpl(sw_registration_id, render_process_host_id, std::move(options),
std::move(callback));
}
void BackgroundSyncManager::RegisterImpl(
int64_t sw_registration_id,
int render_process_host_id,
blink::mojom::SyncRegistrationOptions options,
StatusAndRegistrationCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
std::move(callback));
return;
}
if (options.tag.length() > kMaxTagLength) {
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_NOT_ALLOWED,
std::move(callback));
return;
}
scoped_refptr<ServiceWorkerRegistration> sw_registration =
service_worker_context_->GetLiveRegistration(sw_registration_id);
if (!sw_registration || !sw_registration->active_version()) {
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER,
std::move(callback));
return;
}
RenderProcessHost* render_process_host =
RenderProcessHost::FromID(render_process_host_id);
if (!render_process_host) {
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER,
std::move(callback));
return;
}
BackgroundSyncType sync_type = GetBackgroundSyncType(options);
if (parameters_->skip_permissions_check_for_testing) {
RegisterDidAskForPermission(
sw_registration_id, std::move(options), std::move(callback),
{PermissionStatus::GRANTED, PermissionStatus::GRANTED});
return;
}
SyncAndNotificationPermissions permission = GetBackgroundSyncPermission(
service_worker_context_, sw_registration->key().origin(),
render_process_host, sync_type);
RegisterDidAskForPermission(sw_registration_id, std::move(options),
std::move(callback), permission);
}
void BackgroundSyncManager::RegisterDidAskForPermission(
int64_t sw_registration_id,
blink::mojom::SyncRegistrationOptions options,
StatusAndRegistrationCallback callback,
SyncAndNotificationPermissions permission_statuses) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (permission_statuses.first == PermissionStatus::DENIED) {
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_PERMISSION_DENIED,
std::move(callback));
return;
}
DCHECK_EQ(permission_statuses.first, PermissionStatus::GRANTED);
scoped_refptr<ServiceWorkerRegistration> sw_registration =
service_worker_context_->GetLiveRegistration(sw_registration_id);
if (!sw_registration || !sw_registration->active_version()) {
// The service worker was shut down in the interim.
RecordFailureAndPostError(GetBackgroundSyncType(options),
BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER,
std::move(callback));
return;
}
BackgroundSyncRegistration* existing_registration =
LookupActiveRegistration(blink::mojom::BackgroundSyncRegistrationInfo(
sw_registration_id, options.tag, GetBackgroundSyncType(options)));
const url::Origin& origin = sw_registration->key().origin();
if (GetBackgroundSyncType(options) ==
blink::mojom::BackgroundSyncType::ONE_SHOT) {
bool is_reregistered =
existing_registration && existing_registration->IsFiring();
NotifyOneShotBackgroundSyncRegistered(
service_worker_context_, origin,
/* can_fire= */ AreOptionConditionsMet(), is_reregistered);
} else {
NotifyPeriodicBackgroundSyncRegistered(
service_worker_context_, origin, options.min_interval,
/* is_reregistered= */ static_cast<bool>(existing_registration));
}
if (existing_registration) {
DCHECK_EQ(existing_registration->options()->tag, options.tag);
DCHECK_EQ(existing_registration->sync_type(),
GetBackgroundSyncType(options));
if (existing_registration->options()->Equals(options)) {
BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
AreOptionConditionsMet()
? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
: BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
BackgroundSyncMetrics::CountRegisterSuccess(
existing_registration->sync_type(), options.min_interval,
registration_could_fire,
BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE);
if (existing_registration->IsFiring()) {
existing_registration->set_sync_state(
blink::mojom::BackgroundSyncState::REREGISTERED_WHILE_FIRING);
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), BACKGROUND_SYNC_STATUS_OK,
std::make_unique<BackgroundSyncRegistration>(
*existing_registration)));
return;
}
}
BackgroundSyncRegistration registration;
registration.set_origin(origin);
*registration.options() = std::move(options);
// TODO(crbug.com/40627578): This section below is really confusing. Add a
// comment explaining what's going on here, or annotate permission_statuses.
registration.set_max_attempts(
permission_statuses.second == PermissionStatus::GRANTED
? parameters_->max_sync_attempts_with_notification_permission
: parameters_->max_sync_attempts);
// Skip the current registration when getting time till next scheduled
// periodic sync event for the origin. This is because we'll be updating the
// schedule time of this registration soon anyway, so considering its
// schedule time would cause us to calculate incorrect delay.
if (registration.sync_type() == BackgroundSyncType::PERIODIC) {
base::TimeDelta delay = GetNextEventDelay(
service_worker_context_, registration,
std::make_unique<BackgroundSyncParameters>(*parameters_),
GetSmallestPeriodicSyncEventDelayForOrigin(
origin, registration.options()->tag));
RegisterDidGetDelay(sw_registration_id, registration, std::move(callback),
delay);
return;
}
RegisterDidGetDelay(sw_registration_id, registration, std::move(callback),
base::TimeDelta());
}
void BackgroundSyncManager::RegisterDidGetDelay(
int64_t sw_registration_id,
BackgroundSyncRegistration registration,
StatusAndRegistrationCallback callback,
base::TimeDelta delay) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// We don't fire periodic Background Sync registrations immediately after
// registration, so set delay_until to override its default value.
if (registration.sync_type() == BackgroundSyncType::PERIODIC)
registration.set_delay_until(clock_->Now() + delay);
scoped_refptr<ServiceWorkerRegistration> sw_registration =
service_worker_context_->GetLiveRegistration(sw_registration_id);
if (!sw_registration || !sw_registration->active_version()) {
// The service worker was shut down in the interim.
RecordFailureAndPostError(registration.sync_type(),
BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER,
std::move(callback));
return;
}
if (registration.sync_type() == BackgroundSyncType::PERIODIC &&
ShouldLogToDevTools(registration.sync_type())) {
devtools_context_->LogBackgroundServiceEvent(
sw_registration_id,
blink::StorageKey::CreateFirstParty(registration.origin()),
DevToolsBackgroundService::kPeriodicBackgroundSync,
/* event_name= */ "Got next event delay",
/* instance_id= */ registration.options()->tag,
{{"Next Attempt Delay (ms)",
GetDelayAsString(registration.delay_until() - clock_->Now())}});
}
AddOrUpdateActiveRegistration(sw_registration_id,
sw_registration->key().origin(), registration);
StoreRegistrations(
sw_registration_id,
base::BindOnce(&BackgroundSyncManager::RegisterDidStore,
weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
registration, std::move(callback)));
}
void BackgroundSyncManager::UnregisterPeriodicSyncImpl(
int64_t sw_registration_id,
const std::string& tag,
BackgroundSyncManager::StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto registration_info = blink::mojom::BackgroundSyncRegistrationInfo(
sw_registration_id, tag, BackgroundSyncType::PERIODIC);
if (!LookupActiveRegistration(registration_info)) {
// It's okay to not find a matching tag.
UnregisterPeriodicSyncDidStore(std::move(callback),
blink::ServiceWorkerStatusCode::kOk);
return;
}
RemoveActiveRegistration(std::move(registration_info));
StoreRegistrations(
sw_registration_id,
base::BindOnce(&BackgroundSyncManager::UnregisterPeriodicSyncDidStore,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void BackgroundSyncManager::UnregisterPeriodicSyncDidStore(
BackgroundSyncManager::StatusCallback callback,
blink::ServiceWorkerStatusCode status) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (status != blink::ServiceWorkerStatusCode::kOk) {
BackgroundSyncMetrics::CountUnregisterPeriodicSync(
BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
DisableAndClearManager(base::BindOnce(
std::move(callback), BACKGROUND_SYNC_STATUS_STORAGE_ERROR));
return;
}
BackgroundSyncMetrics::CountUnregisterPeriodicSync(BACKGROUND_SYNC_STATUS_OK);
ScheduleOrCancelDelayedProcessing(BackgroundSyncType::PERIODIC);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), BACKGROUND_SYNC_STATUS_OK));
}
void BackgroundSyncManager::DisableAndClearManager(base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
disabled_ = true;
active_registrations_.clear();
// Delete all backend entries. The memory representation of registered syncs
// may be out of sync with storage (e.g., due to corruption detection on
// loading from storage), so reload the registrations from storage again.
GetDataFromBackend(
kBackgroundSyncUserDataKey,
base::BindOnce(&BackgroundSyncManager::DisableAndClearDidGetRegistrations,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void BackgroundSyncManager::DisableAndClearDidGetRegistrations(
base::OnceClosure callback,
const std::vector<std::pair<int64_t, std::string>>& user_data,
blink::ServiceWorkerStatusCode status) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (status != blink::ServiceWorkerStatusCode::kOk || user_data.empty()) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
base::RepeatingClosure barrier_closure =
base::BarrierClosure(user_data.size(), std::move(callback));
for (const auto& sw_id_and_regs : user_data) {
service_worker_context_->ClearRegistrationUserData(
sw_id_and_regs.first, {kBackgroundSyncUserDataKey},
base::BindOnce(&BackgroundSyncManager::DisableAndClearManagerClearedOne,
weak_ptr_factory_.GetWeakPtr(), barrier_closure));
}
}
void BackgroundSyncManager::DisableAndClearManagerClearedOne(
base::OnceClosure barrier_closure,
blink::ServiceWorkerStatusCode status) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The status doesn't matter at this point, there is nothing else to be done.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(barrier_closure));
}
BackgroundSyncRegistration* BackgroundSyncManager::LookupActiveRegistration(
const blink::mojom::BackgroundSyncRegistrationInfo& registration_info) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto it = active_registrations_.find(
registration_info.service_worker_registration_id);
if (it == active_registrations_.end())
return nullptr;
BackgroundSyncRegistrations& registrations = it->second;
DCHECK(!registrations.origin.opaque());
auto key_and_registration_iter = registrations.registration_map.find(
{registration_info.tag, registration_info.sync_type});
if (key_and_registration_iter == registrations.registration_map.end())
return nullptr;
return &key_and_registration_iter->second;
}
void BackgroundSyncManager::StoreRegistrations(
int64_t sw_registration_id,
ServiceWorkerRegistry::StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Serialize the data.
const BackgroundSyncRegistrations& registrations =
active_registrations_[sw_registration_id];
BackgroundSyncRegistrationsProto registrations_proto;
registrations_proto.set_origin(registrations.origin.Serialize());
for (const auto& key_and_registration : registrations.registration_map) {
const BackgroundSyncRegistration& registration =
key_and_registration.second;
BackgroundSyncRegistrationProto* registration_proto =
registrations_proto.add_registration();
registration_proto->set_tag(registration.options()->tag);
if (registration.options()->min_interval >= 0) {
registration_proto->mutable_periodic_sync_options()->set_min_interval(
registration.options()->min_interval);
}
registration_proto->set_num_attempts(registration.num_attempts());
registration_proto->set_max_attempts(registration.max_attempts());
registration_proto->set_delay_until(
registration.delay_until().ToInternalValue());
}
std::string serialized;
bool success = registrations_proto.SerializeToString(&serialized);
DCHECK(success);
StoreDataInBackend(sw_registration_id, registrations.origin,
kBackgroundSyncUserDataKey, serialized,
std::move(callback));
}
void BackgroundSyncManager::RegisterDidStore(
int64_t sw_registration_id,
const BackgroundSyncRegistration& registration,
StatusAndRegistrationCallback callback,
blink::ServiceWorkerStatusCode status) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (status == blink::ServiceWorkerStatusCode::kErrorNotFound) {
// The service worker registration is gone.
active_registrations_.erase(sw_registration_id);
RecordFailureAndPostError(registration.sync_type(),
BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
std::move(callback));
return;
}
if (status != blink::ServiceWorkerStatusCode::kOk) {
BackgroundSyncMetrics::CountRegisterFailure(
registration.sync_type(), BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
DisableAndClearManager(base::BindOnce(
std::move(callback), BACKGROUND_SYNC_STATUS_STORAGE_ERROR, nullptr));
return;
}
// Update controller of this new origin.
if (registration.sync_type() == BackgroundSyncType::PERIODIC)
proxy_->AddToTrackedOrigins(registration.origin());
BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
AreOptionConditionsMet()
? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
: BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
BackgroundSyncMetrics::CountRegisterSuccess(
registration.sync_type(), registration.options()->min_interval,
registration_could_fire,
BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE);
ScheduleOrCancelDelayedProcessing(BackgroundSyncType::PERIODIC);
// Tell the client that the registration is ready. We won't fire it until the
// client has resolved the registration event.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), BACKGROUND_SYNC_STATUS_OK,
std::make_unique<BackgroundSyncRegistration>(
registration)));
}
void BackgroundSyncManager::DidResolveRegistrationImpl(
blink::mojom::BackgroundSyncRegistrationInfoPtr registration_info) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
BackgroundSyncRegistration* registration =
LookupActiveRegistration(*registration_info);
if (!registration) {
// There might not be a registration if the client ack's a registration that
// was a duplicate in the first place and was already firing and finished by
// the time the client acknowledged the second registration.
op_scheduler_.CompleteOperationAndRunNext();
return;
}
registration->set_resolved();
ResolveRegistrationDidCreateKeepAlive(CreateBackgroundSyncEventKeepAlive(
service_worker_context_, std::move(*registration_info)));
}
void BackgroundSyncManager::ResolveRegistrationDidCreateKeepAlive(
std::unique_ptr<BackgroundSyncEventKeepAlive> keepalive) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
FireReadyEvents(BackgroundSyncType::ONE_SHOT, /* reschedule= */ true,
base::DoNothing(), std::move(keepalive));
op_scheduler_.CompleteOperationAndRunNext();
}
void BackgroundSyncManager::RemoveActiveRegistration(
const blink::mojom::BackgroundSyncRegistrationInfo& registration_info) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(LookupActiveRegistration(registration_info));
BackgroundSyncRegistrations* registrations =
&active_registrations_[registration_info.service_worker_registration_id];
const url::Origin& origin = registrations->origin;
registrations->registration_map.erase(
{registration_info.tag, registration_info.sync_type});
// Update controller's list of registered origin if necessary.
if (registrations->registration_map.empty())
proxy_->RemoveFromTrackedOrigins(origin);
else {
bool no_more_periodic_sync_registrations = true;
for (auto& key_and_registration : registrations->registration_map) {
if (key_and_registration.second.sync_type() ==
BackgroundSyncType::PERIODIC) {
no_more_periodic_sync_registrations = false;
break;
}
}
if (no_more_periodic_sync_registrations)
proxy_->RemoveFromTrackedOrigins(origin);
}
if (registration_info.sync_type == BackgroundSyncType::PERIODIC &&
ShouldLogToDevTools(registration_info.sync_type)) {
devtools_context_->LogBackgroundServiceEvent(
registration_info.service_worker_registration_id,
blink::StorageKey::CreateFirstParty(origin),
DevToolsBackgroundService::kPeriodicBackgroundSync,
/* event_name= */ "Unregistered periodicsync",
/* instance_id= */ registration_info.tag,
/* event_metadata= */ {});
}
}
void BackgroundSyncManager::AddOrUpdateActiveRegistration(
int64_t sw_registration_id,
const url::Origin& origin,
const BackgroundSyncRegistration& sync_registration) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
BackgroundSyncRegistrations* registrations =
&active_registrations_[sw_registration_id];
registrations->origin = origin;
BackgroundSyncType sync_type = sync_registration.sync_type();
registrations
->registration_map[{sync_registration.options()->tag, sync_type}] =
sync_registration;
if (ShouldLogToDevTools(sync_registration.sync_type())) {
std::map<std::string, std::string> event_metadata;
if (sync_registration.sync_type() == BackgroundSyncType::PERIODIC) {
event_metadata["minInterval"] =
base::NumberToString(sync_registration.options()->min_interval);
}
devtools_context_->LogBackgroundServiceEvent(
sw_registration_id, blink::StorageKey::CreateFirstParty(origin),
GetDevToolsBackgroundService(sync_type),
/* event_name= */ "Registered " + GetSyncEventName(sync_type),
/* instance_id= */ sync_registration.options()->tag, event_metadata);
}
}
void BackgroundSyncManager::StoreDataInBackend(
int64_t sw_registration_id,
const url::Origin& origin,
const std::string& backend_key,
const std::string& data,
ServiceWorkerRegistry::StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
service_worker_context_->StoreRegistrationUserData(
sw_registration_id, blink::StorageKey::CreateFirstParty(origin),
{{backend_key, data}}, std::move(callback));
}
void BackgroundSyncManager::GetDataFromBackend(
const std::string& backend_key,
ServiceWorkerRegistry::GetUserDataForAllRegistrationsCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
service_worker_context_->GetUserDataForAllRegistrations(backend_key,
std::move(callback));
}
void BackgroundSyncManager::DispatchSyncEvent(
const std::string& tag,
scoped_refptr<ServiceWorkerVersion> active_version,
bool last_chance,
ServiceWorkerVersion::StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(active_version);
if (active_version->running_status() !=
blink::EmbeddedWorkerStatus::kRunning) {
active_version->RunAfterStartWorker(
ServiceWorkerMetrics::EventType::SYNC,
base::BindOnce(&DidStartWorkerForSyncEvent,
base::BindOnce(&BackgroundSyncManager::DispatchSyncEvent,
weak_ptr_factory_.GetWeakPtr(), tag,
active_version, last_chance),
std::move(callback)));
return;
}
auto split_callback = base::SplitOnceCallback(std::move(callback));
int request_id = active_version->StartRequestWithCustomTimeout(
ServiceWorkerMetrics::EventType::SYNC, std::move(split_callback.first),
parameters_->max_sync_event_duration,
ServiceWorkerVersion::CONTINUE_ON_TIMEOUT);
active_version->endpoint()->DispatchSyncEvent(
tag, last_chance, parameters_->max_sync_event_duration,
base::BindOnce(&OnSyncEventFinished, active_version, request_id,
std::move(split_callback.second)));
if (devtools_context_->IsRecording(
DevToolsBackgroundService::kBackgroundSync)) {
devtools_context_->LogBackgroundServiceEvent(
active_version->registration_id(), active_version->key(),
DevToolsBackgroundService::kBackgroundSync,
/* event_name= */ "Dispatched sync event",
/* instance_id= */ tag,
/* event_metadata= */
{{"Last Chance", last_chance ? "Yes" : "No"}});
}
}
void BackgroundSyncManager::DispatchPeriodicSyncEvent(
const std::string& tag,
scoped_refptr<ServiceWorkerVersion> active_version,
ServiceWorkerVersion::StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(active_version);
if (active_version->running_status() !=
blink::EmbeddedWorkerStatus::kRunning) {
active_version->RunAfterStartWorker(
ServiceWorkerMetrics::EventType::PERIODIC_SYNC,
base::BindOnce(
&DidStartWorkerForSyncEvent,
base::BindOnce(&BackgroundSyncManager::DispatchPeriodicSyncEvent,
weak_ptr_factory_.GetWeakPtr(), tag, active_version),
std::move(callback)));
return;
}
auto split_callback = base::SplitOnceCallback(std::move(callback));
int request_id = active_version->StartRequestWithCustomTimeout(
ServiceWorkerMetrics::EventType::PERIODIC_SYNC,
std::move(split_callback.first), parameters_->max_sync_event_duration,
ServiceWorkerVersion::CONTINUE_ON_TIMEOUT);
active_version->endpoint()->DispatchPeriodicSyncEvent(
tag, parameters_->max_sync_event_duration,
base::BindOnce(&OnSyncEventFinished, active_version, request_id,
std::move(split_callback.second)));
if (devtools_context_->IsRecording(
DevToolsBackgroundService::kPeriodicBackgroundSync)) {
devtools_context_->LogBackgroundServiceEvent(
active_version->registration_id(), active_version->key(),
DevToolsBackgroundService::kPeriodicBackgroundSync,
/* event_name= */ "Dispatched periodicsync event",
/* instance_id= */ tag,
/* event_metadata= */ {});
}
}
void BackgroundSyncManager::HasMainFrameWindowClient(
const blink::StorageKey& key,
BoolCallback callback) {
service_worker_context_->HasMainFrameWindowClient(key, std::move(callback));
}
void BackgroundSyncManager::GetRegistrationsImpl(
BackgroundSyncType sync_type,
int64_t sw_registration_id,
StatusAndRegistrationsCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::vector<std::unique_ptr<BackgroundSyncRegistration>> out_registrations;
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
std::move(out_registrations)));
return;
}
auto it = active_registrations_.find(sw_registration_id);
if (it != active_registrations_.end()) {
const BackgroundSyncRegistrations& registrations = it->second;
for (const auto& key_and_registration : registrations.registration_map) {
const BackgroundSyncRegistration& registration =
key_and_registration.second;
if (registration.sync_type() != sync_type)
continue;
out_registrations.push_back(
std::make_unique<BackgroundSyncRegistration>(registration));
}
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), BACKGROUND_SYNC_STATUS_OK,
std::move(out_registrations)));
}
bool BackgroundSyncManager::AreOptionConditionsMet() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return network_observer_->NetworkSufficient();
}
bool BackgroundSyncManager::AllConditionsExceptConnectivitySatisfied(
const BackgroundSyncRegistration& registration,
int64_t service_worker_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Don't fire the registration if the client hasn't yet resolved its
// registration promise.
if (!registration.resolved() &&
registration.sync_type() == BackgroundSyncType::ONE_SHOT) {
return false;
}
if (registration.sync_state() != blink::mojom::BackgroundSyncState::PENDING)
return false;
if (registration.is_suspended())
return false;
if (base::Contains(emulated_offline_sw_, service_worker_id))
return false;
return true;
}
bool BackgroundSyncManager::CanFireAnyRegistrationUponConnectivity(
BackgroundSyncType sync_type) {
for (const auto& sw_reg_id_and_registrations : active_registrations_) {
int64_t service_worker_registration_id = sw_reg_id_and_registrations.first;
for (const auto& key_and_registration :
sw_reg_id_and_registrations.second.registration_map) {
const BackgroundSyncRegistration& registration =
key_and_registration.second;
if (sync_type != registration.sync_type())
continue;
if (AllConditionsExceptConnectivitySatisfied(
registration, service_worker_registration_id)) {
return true;
}
}
}
return false;
}
bool& BackgroundSyncManager::delayed_processing_scheduled(
BackgroundSyncType sync_type) {
if (sync_type == BackgroundSyncType::ONE_SHOT)
return delayed_processing_scheduled_one_shot_sync_;
else
return delayed_processing_scheduled_periodic_sync_;
}
void BackgroundSyncManager::ScheduleOrCancelDelayedProcessing(
BackgroundSyncType sync_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
bool can_fire_with_connectivity =
CanFireAnyRegistrationUponConnectivity(sync_type);
if (delayed_processing_scheduled(sync_type) && !can_fire_with_connectivity &&
!GetNumFiringRegistrations(sync_type)) {
CancelDelayedProcessingOfRegistrations(sync_type);
delayed_processing_scheduled(sync_type) = false;
} else if (can_fire_with_connectivity ||
GetNumFiringRegistrations(sync_type)) {
ScheduleDelayedProcessingOfRegistrations(sync_type);
delayed_processing_scheduled(sync_type) = true;
}
}
bool BackgroundSyncManager::IsRegistrationReadyToFire(
const BackgroundSyncRegistration& registration,
int64_t service_worker_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (clock_->Now() < registration.delay_until())
return false;
return AllConditionsExceptConnectivitySatisfied(registration,
service_worker_id) &&
AreOptionConditionsMet();
}
int BackgroundSyncManager::GetNumFiringRegistrations(
BackgroundSyncType sync_type) {
if (sync_type == BackgroundSyncType::ONE_SHOT)
return num_firing_registrations_one_shot_;
return num_firing_registrations_periodic_;
}
void BackgroundSyncManager::UpdateNumFiringRegistrationsBy(
BackgroundSyncType sync_type,
int to_add) {
if (sync_type == BackgroundSyncType::ONE_SHOT)
num_firing_registrations_one_shot_ += to_add;
else
num_firing_registrations_periodic_ += to_add;
}
bool BackgroundSyncManager::AllRegistrationsWaitingToBeResolved() const {
for (const auto& active_registration : active_registrations_) {
for (const auto& key_and_registration :
active_registration.second.registration_map) {
const BackgroundSyncRegistration& registration =
key_and_registration.second;
if (registration.resolved())
return false;
}
}
return true;
}
base::TimeDelta BackgroundSyncManager::GetSoonestWakeupDelta(
BackgroundSyncType sync_type,
base::Time last_browser_wakeup_time) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max();
bool need_retries = false;
for (const auto& sw_reg_id_and_registrations : active_registrations_) {
for (const auto& key_and_registration :
sw_reg_id_and_registrations.second.registration_map) {
const BackgroundSyncRegistration& registration =
key_and_registration.second;
if (registration.sync_type() != sync_type)
continue;
if (registration.num_attempts() > 0 &&
registration.num_attempts() < registration.max_attempts()) {
need_retries = true;
}
if (registration.sync_state() ==
blink::mojom::BackgroundSyncState::PENDING) {
if (clock_->Now() >= registration.delay_until()) {
soonest_wakeup_delta = base::TimeDelta();
break;
} else {
base::TimeDelta delay_delta =
registration.delay_until() - clock_->Now();
soonest_wakeup_delta = std::min(delay_delta, soonest_wakeup_delta);
}
}
}
}
// If the browser is closed while firing events, the browser needs a task to
// wake it back up and try again.
if (GetNumFiringRegistrations(sync_type) > 0 &&
soonest_wakeup_delta > parameters_->min_sync_recovery_time) {
soonest_wakeup_delta = parameters_->min_sync_recovery_time;
}
// If we're still waiting for registrations to be resolved, don't schedule
// a wake up task eagerly.
if (sync_type == BackgroundSyncType::ONE_SHOT &&
AllRegistrationsWaitingToBeResolved() &&
soonest_wakeup_delta < parameters_->min_sync_recovery_time) {
soonest_wakeup_delta = parameters_->min_sync_recovery_time;
}
// The browser may impose a hard limit on how often it can be woken up to
// process periodic Background Sync registrations. This excludes retries.
if (sync_type == BackgroundSyncType::PERIODIC && !need_retries) {
soonest_wakeup_delta = MaybeApplyBrowserWakeupCountLimit(
soonest_wakeup_delta, last_browser_wakeup_time);
}
return soonest_wakeup_delta;
}
base::TimeDelta BackgroundSyncManager::MaybeApplyBrowserWakeupCountLimit(
base::TimeDelta soonest_wakeup_delta,
base::Time last_browser_wakeup_time) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (last_browser_wakeup_time.is_null())
return soonest_wakeup_delta;
base::TimeDelta time_since_last_browser_wakeup =
clock_->Now() - last_browser_wakeup_time;
if (time_since_last_browser_wakeup >=
parameters_->min_periodic_sync_events_interval) {
return soonest_wakeup_delta;
}
base::TimeDelta time_till_next_allowed_browser_wakeup =
parameters_->min_periodic_sync_events_interval -
time_since_last_browser_wakeup;
return std::max(soonest_wakeup_delta, time_till_next_allowed_browser_wakeup);
}
base::TimeDelta
BackgroundSyncManager::GetSmallestPeriodicSyncEventDelayForOrigin(
const url::Origin& origin,
const std::string& tag_to_skip) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::Time soonest_wakeup_time = base::Time();
for (const auto& active_registration : active_registrations_) {
if (active_registration.second.origin != origin)
continue;
const auto& tag_and_registrations =
active_registration.second.registration_map;
for (const auto& tag_and_registration : tag_and_registrations) {
if (/* tag= */ tag_and_registration.first.first == tag_to_skip)
continue;
if (/* sync_type= */ tag_and_registration.first.second !=
BackgroundSyncType::PERIODIC) {
continue;
}
if (tag_and_registration.second.delay_until().is_null())
continue;
if (soonest_wakeup_time.is_null() ||
tag_and_registration.second.delay_until() < soonest_wakeup_time) {
soonest_wakeup_time = tag_and_registration.second.delay_until();
}
}
}
if (soonest_wakeup_time.is_null())
return base::TimeDelta::Max();
if (soonest_wakeup_time < clock_->Now())
return base::TimeDelta();
return soonest_wakeup_time - clock_->Now();
}
void BackgroundSyncManager::RevivePeriodicSyncRegistrations(
url::Origin origin) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_)
return;
op_scheduler_.ScheduleOperation(base::BindOnce(
&BackgroundSyncManager::ReviveOriginImpl, weak_ptr_factory_.GetWeakPtr(),
std::move(origin), MakeEmptyCompletion()));
}
void BackgroundSyncManager::ReviveOriginImpl(url::Origin origin,
base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
// Create a list of registrations to revive.
std::vector<const BackgroundSyncRegistration*> to_revive;
std::map<const BackgroundSyncRegistration*, int64_t>
service_worker_registration_ids;
for (const auto& active_registration : active_registrations_) {
int64_t service_worker_registration_id = active_registration.first;
if (active_registration.second.origin != origin)
continue;
for (const auto& key_and_registration :
active_registration.second.registration_map) {
const BackgroundSyncRegistration* registration =
&key_and_registration.second;
if (!registration->is_suspended())
continue;
to_revive.push_back(registration);
service_worker_registration_ids[registration] =
service_worker_registration_id;
}
}
if (to_revive.empty()) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
base::RepeatingClosure received_new_delays_closure = base::BarrierClosure(
to_revive.size(),
base::BindOnce(
&BackgroundSyncManager::DidReceiveDelaysForSuspendedRegistrations,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
for (const auto* registration : to_revive) {
base::TimeDelta delay = GetNextEventDelay(
service_worker_context_, *registration,
std::make_unique<BackgroundSyncParameters>(*parameters_),
GetSmallestPeriodicSyncEventDelayForOrigin(
origin, registration->options()->tag));
ReviveDidGetNextEventDelay(service_worker_registration_ids[registration],
*registration, received_new_delays_closure,
delay);
}
}
void BackgroundSyncManager::ReviveDidGetNextEventDelay(
int64_t service_worker_registration_id,
BackgroundSyncRegistration registration,
base::OnceClosure done_closure,
base::TimeDelta delay) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (delay.is_max()) {
std::move(done_closure).Run();
return;
}
BackgroundSyncRegistration* active_registration =
LookupActiveRegistration(blink::mojom::BackgroundSyncRegistrationInfo(
service_worker_registration_id, registration.options()->tag,
registration.sync_type()));
if (!active_registration) {
std::move(done_closure).Run();
return;
}
active_registration->set_delay_until(clock_->Now() + delay);
StoreRegistrations(
service_worker_registration_id,
base::BindOnce(&BackgroundSyncManager::ReviveDidStoreRegistration,
weak_ptr_factory_.GetWeakPtr(),
service_worker_registration_id, std::move(done_closure)));
}
void BackgroundSyncManager::ReviveDidStoreRegistration(
int64_t service_worker_registration_id,
base::OnceClosure done_closure,
blink::ServiceWorkerStatusCode status) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (status == blink::ServiceWorkerStatusCode::kErrorNotFound) {
// The service worker registration is gone.
active_registrations_.erase(service_worker_registration_id);
std::move(done_closure).Run();
return;
}
if (status != blink::ServiceWorkerStatusCode::kOk) {
DisableAndClearManager(std::move(done_closure));
return;
}
std::move(done_closure).Run();
}
void BackgroundSyncManager::DidReceiveDelaysForSuspendedRegistrations(
base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ScheduleOrCancelDelayedProcessing(BackgroundSyncType::PERIODIC);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
}
void BackgroundSyncManager::ScheduleDelayedProcessingOfRegistrations(
blink::mojom::BackgroundSyncType sync_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto fire_events_callback = base::BindOnce(
&BackgroundSyncManager::FireReadyEvents, weak_ptr_factory_.GetWeakPtr(),
sync_type, /* reschedule= */ true, base::DoNothing(),
/* keepalive= */ nullptr);
proxy_->ScheduleDelayedProcessing(
sync_type,
GetSoonestWakeupDelta(sync_type,
/* last_browser_wakeup_time= */ base::Time()),
std::move(fire_events_callback));
}
void BackgroundSyncManager::CancelDelayedProcessingOfRegistrations(
blink::mojom::BackgroundSyncType sync_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
proxy_->CancelDelayedProcessing(sync_type);
}
void BackgroundSyncManager::FireReadyEvents(
blink::mojom::BackgroundSyncType sync_type,
bool reschedule,
base::OnceClosure callback,
std::unique_ptr<BackgroundSyncEventKeepAlive> keepalive) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!reschedule) {
// This invocation has come from scheduled processing of registrations.
// Since this delayed processing is one-off, update internal state.
delayed_processing_scheduled(sync_type) = false;
}
op_scheduler_.ScheduleOperation(
base::BindOnce(&BackgroundSyncManager::FireReadyEventsImpl,
weak_ptr_factory_.GetWeakPtr(), sync_type, reschedule,
std::move(callback), std::move(keepalive)));
}
void BackgroundSyncManager::FireReadyEventsImpl(
blink::mojom::BackgroundSyncType sync_type,
bool reschedule,
base::OnceClosure callback,
std::unique_ptr<BackgroundSyncEventKeepAlive> keepalive) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, op_scheduler_.WrapCallbackToRunNext(std::move(callback)));
return;
}
// Find the registrations that are ready to run.
std::vector<blink::mojom::BackgroundSyncRegistrationInfoPtr> to_fire;
for (auto& sw_reg_id_and_registrations : active_registrations_) {
const int64_t service_worker_registration_id =
sw_reg_id_and_registrations.first;
for (auto& key_and_registration :
sw_reg_id_and_registrations.second.registration_map) {
BackgroundSyncRegistration* registration = &key_and_registration.second;
if (sync_type != registration->sync_type())
continue;
if (IsRegistrationReadyToFire(*registration,
service_worker_registration_id)) {
to_fire.emplace_back(blink::mojom::BackgroundSyncRegistrationInfo::New(
service_worker_registration_id,
/* tag= */ key_and_registration.first.first,
/* sync_type= */ key_and_registration.first.second));
// The state change is not saved to persistent storage because
// if the sync event is killed mid-sync then it should return to
// SYNC_STATE_PENDING.
registration->set_sync_state(blink::mojom::BackgroundSyncState::FIRING);
}
}
}
if (!reschedule) {
// This method has been called from a Chrome wakeup task.
BackgroundSyncMetrics::RecordEventsFiredFromWakeupTask(
sync_type, /* events_fired= */ !to_fire.empty());
}
if (to_fire.empty()) {
// TODO(crbug.com/40641360): Reschedule wakeup after a non-zero delay if
// called from a wakeup task.
if (reschedule)
ScheduleOrCancelDelayedProcessing(sync_type);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, op_scheduler_.WrapCallbackToRunNext(std::move(callback)));
return;
}
base::TimeTicks start_time = base::TimeTicks::Now();
// If we've been called from a wake up task, potentially keep the browser
// awake till all events have completed. If not, we only do so until all
// events have been fired.
// To allow the |op_scheduler_| to process other tasks after sync events
// have been fired, mark this task complete after firing events.
base::OnceClosure events_fired_callback, events_completed_callback;
bool keep_browser_awake_till_events_complete =
!reschedule && parameters_->keep_browser_awake_till_events_complete;
if (keep_browser_awake_till_events_complete) {
events_fired_callback = MakeEmptyCompletion();
events_completed_callback = std::move(callback);
} else {
events_fired_callback =
op_scheduler_.WrapCallbackToRunNext(std::move(callback));
events_completed_callback = base::DoNothing();
}
// Fire the sync event of the ready registrations and run
// |events_fired_closure| once they're all done.
base::RepeatingClosure events_fired_barrier_closure = base::BarrierClosure(
to_fire.size(),
base::BindOnce(&BackgroundSyncManager::FireReadyEventsAllEventsFiring,
weak_ptr_factory_.GetWeakPtr(), sync_type, reschedule,
std::move(events_fired_callback)));
// Record the total time taken after all events have run to completion.
base::RepeatingClosure events_completed_barrier_closure =
base::BarrierClosure(
to_fire.size(),
base::BindOnce(&BackgroundSyncManager::OnAllSyncEventsCompleted,
sync_type, start_time, !reschedule, to_fire.size(),
std::move(events_completed_callback)));
for (auto& registration_info : to_fire) {
const BackgroundSyncRegistration* registration =
LookupActiveRegistration(*registration_info);
DCHECK(registration);
int64_t service_worker_registration_id =
registration_info->service_worker_registration_id;
// If BackgroundSync becomes usable from a 3p context then
// BackgroundSyncRegistrations should be changed to use StorageKey.
service_worker_context_->FindReadyRegistrationForId(
service_worker_registration_id,
blink::StorageKey::CreateFirstParty(
active_registrations_[service_worker_registration_id].origin),
base::BindOnce(
&BackgroundSyncManager::FireReadyEventsDidFindRegistration,
weak_ptr_factory_.GetWeakPtr(), std::move(registration_info),
std::move(keepalive), events_fired_barrier_closure,
events_completed_barrier_closure));
}
}
void BackgroundSyncManager::FireReadyEventsDidFindRegistration(
blink::mojom::BackgroundSyncRegistrationInfoPtr registration_info,
std::unique_ptr<BackgroundSyncEventKeepAlive> keepalive,
base::OnceClosure event_fired_callback,
base::OnceClosure event_completed_callback,
blink::ServiceWorkerStatusCode service_worker_status,
scoped_refptr<ServiceWorkerRegistration> service_worker_registration) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
BackgroundSyncRegistration* registration =
LookupActiveRegistration(*registration_info);
if (service_worker_status != blink::ServiceWorkerStatusCode::kOk) {
if (registration)
registration->set_sync_state(blink::mojom::BackgroundSyncState::PENDING);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(event_fired_callback));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(event_completed_callback));
return;
}
DCHECK_EQ(registration_info->service_worker_registration_id,
service_worker_registration->id());
DCHECK(registration);
// The connectivity was lost before dispatching the sync event, so there is
// no point in going through with it.
if (!AreOptionConditionsMet()) {
registration->set_sync_state(blink::mojom::BackgroundSyncState::PENDING);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(event_fired_callback));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(event_completed_callback));
return;
}
auto sync_type = registration_info->sync_type;
UpdateNumFiringRegistrationsBy(sync_type, 1);
const bool last_chance =
registration->num_attempts() == registration->max_attempts() - 1;
HasMainFrameWindowClient(
service_worker_registration->key(),
base::BindOnce(&BackgroundSyncMetrics::RecordEventStarted, sync_type));
if (sync_type == BackgroundSyncType::ONE_SHOT) {
DispatchSyncEvent(
registration->options()->tag,
service_worker_registration->active_version(), last_chance,
base::BindOnce(&BackgroundSyncManager::EventComplete,
weak_ptr_factory_.GetWeakPtr(),
service_worker_registration,
std::move(registration_info), std::move(keepalive),
std::move(event_completed_callback)));
} else {
DispatchPeriodicSyncEvent(
registration->options()->tag,
service_worker_registration->active_version(),
base::BindOnce(&BackgroundSyncManager::EventComplete,
weak_ptr_factory_.GetWeakPtr(),
service_worker_registration,
std::move(registration_info), std::move(keepalive),
std::move(event_completed_callback)));
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(event_fired_callback));
}
void BackgroundSyncManager::FireReadyEventsAllEventsFiring(
BackgroundSyncType sync_type,
bool reschedule,
base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (reschedule)
ScheduleOrCancelDelayedProcessing(sync_type);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
}
// |service_worker_registration| is just to keep the registration alive
// while the event is firing.
void BackgroundSyncManager::EventComplete(
scoped_refptr<ServiceWorkerRegistration> service_worker_registration,
blink::mojom::BackgroundSyncRegistrationInfoPtr registration_info,
std::unique_ptr<BackgroundSyncEventKeepAlive> keepalive,
base::OnceClosure callback,
blink::ServiceWorkerStatusCode status_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
// The event ran to completion, we should count it, no matter what happens
// from here.
const blink::StorageKey& key = service_worker_registration->key();
HasMainFrameWindowClient(
key, base::BindOnce(&BackgroundSyncMetrics::RecordEventResult,
registration_info->sync_type,
status_code == blink::ServiceWorkerStatusCode::kOk));
op_scheduler_.ScheduleOperation(base::BindOnce(
&BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(),
std::move(registration_info), std::move(keepalive), status_code,
key.origin(), op_scheduler_.WrapCallbackToRunNext(std::move(callback))));
}
void BackgroundSyncManager::EventCompleteImpl(
blink::mojom::BackgroundSyncRegistrationInfoPtr registration_info,
std::unique_ptr<BackgroundSyncEventKeepAlive> keepalive,
blink::ServiceWorkerStatusCode status_code,
const url::Origin& origin,
base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (disabled_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
BackgroundSyncRegistration* registration =
LookupActiveRegistration(*registration_info);
if (!registration) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
DCHECK_NE(blink::mojom::BackgroundSyncState::PENDING,
registration->sync_state());
// It's important to update |num_attempts| before we update |delay_until|.
bool succeeded = status_code == blink::ServiceWorkerStatusCode::kOk;
registration->set_num_attempts(GetNumAttemptsAfterEvent(
registration->sync_type(), registration->num_attempts(),
registration->max_attempts(), registration->sync_state(), succeeded));
// If |delay_until| needs to be updated, get updated delay.
if (registration->sync_type() == BackgroundSyncType::PERIODIC ||
(!succeeded &&
registration->num_attempts() < registration->max_attempts())) {
base::TimeDelta delay = GetNextEventDelay(
service_worker_context_, *registration,
std::make_unique<BackgroundSyncParameters>(*parameters_),
GetSmallestPeriodicSyncEventDelayForOrigin(
origin, registration->options()->tag));
EventCompleteDidGetDelay(std::move(registration_info), status_code, origin,
std::move(callback), delay);
return;
}
EventCompleteDidGetDelay(std::move(registration_info), status_code, origin,
std::move(callback), base::TimeDelta::Max());
}
void BackgroundSyncManager::EventCompleteDidGetDelay(
blink::mojom::BackgroundSyncRegistrationInfoPtr registration_info,
blink::ServiceWorkerStatusCode status_code,
const url::Origin& origin,
base::OnceClosure callback,
base::TimeDelta delay) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UpdateNumFiringRegistrationsBy(registration_info->sync_type, -1);
const blink::StorageKey storage_key =
blink::StorageKey::CreateFirstParty(origin);
BackgroundSyncRegistration* registration =
LookupActiveRegistration(*registration_info);
if (!registration) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
bool succeeded = status_code == blink::ServiceWorkerStatusCode::kOk;
bool can_retry = registration->num_attempts() < registration->max_attempts();
bool registration_completed = true;
if (registration->sync_state() ==
blink::mojom::BackgroundSyncState::REREGISTERED_WHILE_FIRING) {
registration->set_sync_state(blink::mojom::BackgroundSyncState::PENDING);
registration->set_num_attempts(0);
registration_completed = false;
if (ShouldLogToDevTools(registration->sync_type())) {
devtools_context_->LogBackgroundServiceEvent(
registration_info->service_worker_registration_id, storage_key,
GetDevToolsBackgroundService(registration->sync_type()),
/* event_name= */ "Sync event reregistered",
/* instance_id= */ registration_info->tag,
/* event_metadata= */ {});
}
} else if ((!succeeded && can_retry) ||
registration->sync_type() == BackgroundSyncType::PERIODIC) {
registration->set_sync_state(blink::mojom::BackgroundSyncState::PENDING);
registration_completed = false;
registration->set_delay_until(clock_->Now() + delay);
std::string event_name = GetSyncEventName(registration->sync_type()) +
(succeeded ? " event completed" : " event failed");
base::TimeDelta display_delay =
registration->sync_type() == BackgroundSyncType::ONE_SHOT
? delay
: registration->delay_until() - clock_->Now();
std::map<std::string, std::string> event_metadata = {
{"Next Attempt Delay (ms)", GetDelayAsString(display_delay)}};
if (!succeeded) {
event_metadata.emplace("Failure Reason",
GetEventStatusString(status_code));
}
if (ShouldLogToDevTools(registration->sync_type())) {
devtools_context_->LogBackgroundServiceEvent(
registration_info->service_worker_registration_id, storage_key,
GetDevToolsBackgroundService(registration->sync_type()), event_name,
/* instance_id= */ registration_info->tag, event_metadata);
}
}
if (registration_completed) {
BackgroundSyncMetrics::RecordRegistrationComplete(
succeeded, registration->num_attempts());
if (ShouldLogToDevTools(registration->sync_type())) {
devtools_context_->LogBackgroundServiceEvent(
registration_info->service_worker_registration_id, storage_key,
GetDevToolsBackgroundService(registration->sync_type()),
/* event_name= */ "Sync completed",
/* instance_id= */ registration_info->tag,
{{"Status", GetEventStatusString(status_code)}});
}
if (registration_info->sync_type ==
blink::mojom::BackgroundSyncType::ONE_SHOT) {
NotifyOneShotBackgroundSyncCompleted(
service_worker_context_, origin, status_code,
registration->num_attempts(), registration->max_attempts());
} else {
NotifyPeriodicBackgroundSyncCompleted(
service_worker_context_, origin, status_code,
registration->num_attempts(), registration->max_attempts());
}
RemoveActiveRegistration(*registration_info);
}
StoreRegistrations(
registration_info->service_worker_registration_id,
base::BindOnce(&BackgroundSyncManager::EventCompleteDidStore,
weak_ptr_factory_.GetWeakPtr(),
registration_info->sync_type,
registration_info->service_worker_registration_id,
std::move(callback)));
}
void BackgroundSyncManager::EventCompleteDidStore(
blink::mojom::BackgroundSyncType sync_type,
int64_t service_worker_id,
base::OnceClosure callback,
blink::ServiceWorkerStatusCode status_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (status_code == blink::ServiceWorkerStatusCode::kErrorNotFound) {
// The registration is gone.
active_registrations_.erase(service_worker_id);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
if (status_code != blink::ServiceWorkerStatusCode::kOk) {
DisableAndClearManager(std::move(callback));
return;
}
// Fire any ready events and call RunInBackground if anything is waiting.
FireReadyEvents(sync_type, /* reschedule= */ true, base::DoNothing());
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
}
// static
void BackgroundSyncManager::OnAllSyncEventsCompleted(
BackgroundSyncType sync_type,
const base::TimeTicks& start_time,
bool from_wakeup_task,
int number_of_batched_sync_events,
base::OnceClosure callback) {
// Record the combined time taken by all sync events.
BackgroundSyncMetrics::RecordBatchSyncEventComplete(
sync_type, base::TimeTicks::Now() - start_time, from_wakeup_task,
number_of_batched_sync_events);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
}
void BackgroundSyncManager::OnRegistrationDeletedImpl(
int64_t sw_registration_id,
base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The backend (ServiceWorkerStorage) will delete the data, so just delete the
// memory representation here.
active_registrations_.erase(sw_registration_id);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
}
void BackgroundSyncManager::OnStorageWipedImpl(base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
active_registrations_.clear();
disabled_ = false;
InitImpl(std::move(callback));
}
void BackgroundSyncManager::OnNetworkChanged() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
#if BUILDFLAG(IS_ANDROID)
if (parameters_->rely_on_android_network_detection)
return;
#endif
if (!AreOptionConditionsMet())
return;
FireReadyEvents(BackgroundSyncType::ONE_SHOT, /* reschedule= */ true,
base::DoNothing());
FireReadyEvents(BackgroundSyncType::PERIODIC, /* reschedule= */ true,
base::DoNothing());
}
base::OnceClosure BackgroundSyncManager::MakeEmptyCompletion() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return op_scheduler_.WrapCallbackToRunNext(base::BindOnce([] {}));
}
blink::ServiceWorkerStatusCode BackgroundSyncManager::CanEmulateSyncEvent(
scoped_refptr<ServiceWorkerVersion> active_version) {
if (!active_version)
return blink::ServiceWorkerStatusCode::kErrorAbort;
if (!network_observer_->NetworkSufficient())
return blink::ServiceWorkerStatusCode::kErrorEventWaitUntilRejected;
int64_t registration_id = active_version->registration_id();
if (base::Contains(emulated_offline_sw_, registration_id))
return blink::ServiceWorkerStatusCode::kErrorEventWaitUntilRejected;
return blink::ServiceWorkerStatusCode::kOk;
}
bool BackgroundSyncManager::ShouldLogToDevTools(BackgroundSyncType sync_type) {
return devtools_context_->IsRecording(
GetDevToolsBackgroundService(sync_type));
}
} // namespace content
|