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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/themes/browser_theme_pack.h"
#include <limits.h>
#include <stddef.h>
#include <algorithm>
#include <array>
#include <limits>
#include <memory>
#include <optional>
#include <string_view>
#include <utility>
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/containers/heap_array.h"
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/string_view_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/frame/window_frame_util.h"
#include "chrome/common/extensions/manifest_handlers/theme_handler.h"
#include "chrome/common/themes/autogenerated_theme_util.h"
#include "chrome/grit/theme_resources.h"
#include "components/crx_file/id_util.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/resource/data_pack.h"
#include "ui/color/color_mixer.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_recipe.h"
#include "ui/color/color_transform.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/color_analysis.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_rep.h"
using content::BrowserThread;
using extensions::Extension;
using TP = ThemeProperties;
// Persistent constants for the main images that we need. These have the same
// names as their IDR_* counterparts but these values will always stay the
// same.
enum BrowserThemePack::PersistentID : int {
kInvalid = -1,
kFrame = 1,
kFrameInactive = 2,
kFrameIncognito = 3,
kFrameIncognitoInactive = 4,
kToolbar = 5,
kTabBackground = 6,
kTabBackgroundIncognito = 7,
// kTabBackgroundV = 8, Deprecated and unused. Previously supported windows
// vista aero glass theme.
kNtpBackground = 9,
kFrameOverlay = 10,
kFrameOverlayInactive = 11,
kButtonBackground = 12,
kNtpAttribution = 13,
kWindowControlBackground = 14,
kTabBackgroundInactive = 15,
kTabBackgroundIncognitoInactive = 16,
kMaxValue = kTabBackgroundIncognitoInactive,
};
namespace {
using PRS = BrowserThemePack::PersistentID;
// The tallest tab height in any mode.
constexpr int kTallestTabHeight = 41;
// The tallest height above the tabs in any mode is 19 DIP.
constexpr int kTallestFrameHeight = kTallestTabHeight + 19;
// Version number of the current theme pack. We just throw out and rebuild
// theme packs that aren't int-equal to this. Increment this number if you
// changed default theme assets, if you need themes to recreate their generated
// images (which are cached), if you changed how missing values are
// generated, or if you changed any constants.
const int kThemePackVersion = 104;
// IDs that are in the DataPack won't clash with the positive integer
// uint16_t. kHeaderID should always have the maximum value because we want the
// "header" to be written last. That way we can detect whether the pack was
// successfully written and ignore and regenerate if it was only partially
// written (i.e. chrome crashed on a different thread while writing the pack).
const int kMaxID = 0x0000FFFF; // Max unsigned 16-bit int.
const int kHeaderID = kMaxID - 1;
const int kTintsID = kMaxID - 2;
const int kColorsID = kMaxID - 3;
const int kDisplayPropertiesID = kMaxID - 4;
const int kSourceImagesID = kMaxID - 5;
const int kScaleFactorsID = kMaxID - 6;
struct PersistingImagesTable {
// A non-changing integer ID meant to be saved in theme packs. This ID must
// not change between versions of chrome.
BrowserThemePack::PersistentID persistent_id;
// The IDR that depends on the whims of GRIT and therefore changes whenever
// someone adds a new resource.
int idr_id;
// String to check for when parsing theme manifests.
const char* const key;
};
// IDR_* resource names change whenever new resources are added; use persistent
// IDs when storing to a cached pack.
constexpr PersistingImagesTable kPersistingImages[] = {
{PRS::kFrame, IDR_THEME_FRAME, "theme_frame"},
{PRS::kFrameInactive, IDR_THEME_FRAME_INACTIVE, "theme_frame_inactive"},
{PRS::kFrameIncognito, IDR_THEME_FRAME_INCOGNITO, "theme_frame_incognito"},
{PRS::kFrameIncognitoInactive, IDR_THEME_FRAME_INCOGNITO_INACTIVE,
"theme_frame_incognito_inactive"},
{PRS::kToolbar, IDR_THEME_TOOLBAR, "theme_toolbar"},
{PRS::kTabBackground, IDR_THEME_TAB_BACKGROUND, "theme_tab_background"},
{PRS::kTabBackgroundInactive, IDR_THEME_TAB_BACKGROUND_INACTIVE,
"theme_tab_background_inactive"},
{PRS::kTabBackgroundIncognito, IDR_THEME_TAB_BACKGROUND_INCOGNITO,
"theme_tab_background_incognito"},
{PRS::kTabBackgroundIncognitoInactive,
IDR_THEME_TAB_BACKGROUND_INCOGNITO_INACTIVE,
"theme_tab_background_incognito_inactive"},
{PRS::kNtpBackground, IDR_THEME_NTP_BACKGROUND, "theme_ntp_background"},
{PRS::kFrameOverlay, IDR_THEME_FRAME_OVERLAY, "theme_frame_overlay"},
{PRS::kFrameOverlayInactive, IDR_THEME_FRAME_OVERLAY_INACTIVE,
"theme_frame_overlay_inactive"},
{PRS::kButtonBackground, IDR_THEME_BUTTON_BACKGROUND,
"theme_button_background"},
{PRS::kNtpAttribution, IDR_THEME_NTP_ATTRIBUTION, "theme_ntp_attribution"},
{PRS::kWindowControlBackground, IDR_THEME_WINDOW_CONTROL_BACKGROUND,
"theme_window_control_background"},
// /!\ If you make any changes here, you must also increment
// kThemePackVersion above, or else themes will display incorrectly.
};
BrowserThemePack::PersistentID GetPersistentIDByName(const std::string& key) {
auto* it = std::ranges::find_if(kPersistingImages, [&](const auto& image) {
return base::EqualsCaseInsensitiveASCII(key, image.key);
});
return it == std::end(kPersistingImages) ? PRS::kInvalid : it->persistent_id;
}
BrowserThemePack::PersistentID GetPersistentIDByIDR(int idr) {
auto* it =
std::ranges::find(kPersistingImages, idr, &PersistingImagesTable::idr_id);
return it == std::end(kPersistingImages) ? PRS::kInvalid : it->persistent_id;
}
// Returns true if the scales in |input| match those in |expected|.
// The order must match as the index is used in determining the raw id.
bool InputScalesValid(std::string_view input,
base::span<ui::ResourceScaleFactor> expected) {
if (input.size() != expected.size() * sizeof(float)) {
return false;
}
auto scales = base::HeapArray<float>::WithSize(expected.size());
base::as_writable_byte_span(base::allow_nonunique_obj, scales)
.copy_from(base::as_byte_span(input));
for (size_t index = 0; index < expected.size(); ++index) {
if (scales[index] != ui::GetScaleForResourceScaleFactor(expected[index])) {
return false;
}
}
return true;
}
// Returns |scale_factors| as a string to be written to disk.
std::string GetResourceScaleFactorsAsString(
base::span<const ui::ResourceScaleFactor> scale_factors) {
auto scales = base::HeapArray<float>::WithSize(scale_factors.size());
for (size_t i = 0; i < scale_factors.size(); ++i) {
scales[i] = ui::GetScaleForResourceScaleFactor(scale_factors[i]);
}
return std::string(base::as_string_view(
base::as_byte_span(base::allow_nonunique_obj, scales)));
}
struct StringToIntTable {
const char* const key;
TP::OverwritableByUserThemeProperty id;
};
// Strings used by themes to identify tints in the JSON.
const StringToIntTable kTintTable[] = {
{"background_tab", TP::TINT_BACKGROUND_TAB},
{"buttons", TP::TINT_BUTTONS},
{"frame", TP::TINT_FRAME},
{"frame_inactive", TP::TINT_FRAME_INACTIVE},
{"frame_incognito", TP::TINT_FRAME_INCOGNITO},
{"frame_incognito_inactive", TP::TINT_FRAME_INCOGNITO_INACTIVE},
// /!\ If you make any changes here, you must also increment
// kThemePackVersion above, or else themes will display incorrectly.
};
const size_t kTintTableLength = std::size(kTintTable);
// Strings used by themes to identify colors in the JSON.
constexpr StringToIntTable kOverwritableColorTable[] = {
{"background_tab", TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE},
{"background_tab_inactive",
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_INACTIVE},
{"background_tab_incognito",
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE_INCOGNITO},
{"background_tab_incognito_inactive",
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_INACTIVE_INCOGNITO},
{"bookmark_text", TP::COLOR_BOOKMARK_TEXT},
{"button_background", TP::COLOR_CONTROL_BUTTON_BACKGROUND},
{"frame", TP::COLOR_FRAME_ACTIVE},
{"frame_inactive", TP::COLOR_FRAME_INACTIVE},
{"frame_incognito", TP::COLOR_FRAME_ACTIVE_INCOGNITO},
{"frame_incognito_inactive", TP::COLOR_FRAME_INACTIVE_INCOGNITO},
{"ntp_background", TP::COLOR_NTP_BACKGROUND},
{"ntp_header", TP::COLOR_NTP_HEADER},
{"ntp_link", TP::COLOR_NTP_LINK},
{"ntp_text", TP::COLOR_NTP_TEXT},
{"omnibox_background", TP::COLOR_OMNIBOX_BACKGROUND},
{"omnibox_text", TP::COLOR_OMNIBOX_TEXT},
{"tab_background_text", TP::COLOR_TAB_FOREGROUND_INACTIVE_FRAME_ACTIVE},
{"tab_background_text_inactive",
TP::COLOR_TAB_FOREGROUND_INACTIVE_FRAME_INACTIVE},
{"tab_background_text_incognito",
TP::COLOR_TAB_FOREGROUND_INACTIVE_FRAME_ACTIVE_INCOGNITO},
{"tab_background_text_incognito_inactive",
TP::COLOR_TAB_FOREGROUND_INACTIVE_FRAME_INACTIVE_INCOGNITO},
{"tab_text", TP::COLOR_TAB_FOREGROUND_ACTIVE_FRAME_ACTIVE},
{"toolbar", TP::COLOR_TOOLBAR},
{"toolbar_button_icon", TP::COLOR_TOOLBAR_BUTTON_ICON},
{"toolbar_text", TP::COLOR_TOOLBAR_TEXT},
// /!\ If you make any changes here, you must also increment
// kThemePackVersion above, or else themes will display incorrectly.
};
constexpr size_t kOverwritableColorTableLength =
std::size(kOverwritableColorTable);
// Colors generated based on the theme, but not overwritable in the theme file.
constexpr int kNonOverwritableColorTable[] = {
TP::COLOR_NTP_LOGO,
TP::COLOR_NTP_SECTION_BORDER,
TP::COLOR_TAB_FOREGROUND_ACTIVE_FRAME_INACTIVE,
TP::COLOR_TAB_THROBBER_SPINNING,
TP::COLOR_TAB_THROBBER_WAITING,
TP::COLOR_TOOLBAR_BUTTON_ICON_HOVERED,
TP::COLOR_TOOLBAR_BUTTON_ICON_PRESSED,
TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_ACTIVE,
TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_INACTIVE,
TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_INCOGNITO_ACTIVE,
TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_INCOGNITO_INACTIVE
// /!\ If you make any changes here, you must also increment
// kThemePackVersion above, or else themes will display incorrectly.
};
constexpr size_t kNonOverwritableColorTableLength =
std::size(kNonOverwritableColorTable);
// The maximum number of colors we may need to store (includes ones that can be
// specified by the theme, and ones that we calculate but can't be specified).
constexpr size_t kColorsArrayLength =
kOverwritableColorTableLength + kNonOverwritableColorTableLength;
// Strings used by themes to identify display properties keys in JSON.
const StringToIntTable kDisplayProperties[] = {
{"ntp_background_alignment", TP::NTP_BACKGROUND_ALIGNMENT},
{"ntp_background_repeat", TP::NTP_BACKGROUND_TILING},
{"ntp_logo_alternate", TP::NTP_LOGO_ALTERNATE},
// /!\ If you make any changes here, you must also increment
// kThemePackVersion above, or else themes will display incorrectly.
};
const size_t kDisplayPropertiesSize = std::size(kDisplayProperties);
int GetIntForString(const std::string& key,
const StringToIntTable* table,
size_t table_length) {
for (size_t i = 0; i < table_length; ++i) {
if (base::EqualsCaseInsensitiveASCII(key, table[i].key)) {
return table[i].id;
}
}
return -1;
}
struct CropEntry {
BrowserThemePack::PersistentID prs_id;
// The maximum useful height of the image at |prs_id|.
int max_height;
};
// The images which should be cropped before being saved to the data pack. The
// maximum heights are meant to be conservative as to give room for the UI to
// change without the maximum heights having to be modified.
// |kThemePackVersion| must be incremented if any of the maximum heights below
// are modified.
const struct CropEntry kImagesToCrop[] = {
{PRS::kFrame, kTallestFrameHeight},
{PRS::kFrameInactive, kTallestFrameHeight},
{PRS::kFrameIncognito, kTallestFrameHeight},
{PRS::kFrameIncognitoInactive, kTallestFrameHeight},
{PRS::kFrameOverlay, kTallestFrameHeight},
{PRS::kFrameOverlayInactive, kTallestFrameHeight},
{PRS::kToolbar, 200},
{PRS::kButtonBackground, 60},
{PRS::kWindowControlBackground, 50},
};
// A list of images that don't need tinting or any other modification and can
// be byte-copied directly into the finished DataPack. This should contain the
// persistent IDs for all themeable image IDs that aren't in kFrameValues,
// kTabBackgroundMap or kImagesToCrop.
constexpr auto kPreloadIDs = std::to_array<BrowserThemePack::PersistentID>({
PRS::kNtpBackground,
PRS::kNtpAttribution,
});
// Returns a piece of memory with the contents of the file |path|.
scoped_refptr<base::RefCountedMemory> ReadFileData(const base::FilePath& path) {
if (!path.empty()) {
base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
if (file.IsValid()) {
int64_t length = file.GetLength();
if (length > 0 && length < INT_MAX) {
int size = static_cast<int>(length);
std::vector<unsigned char> raw_data;
raw_data.resize(size);
char* data = reinterpret_cast<char*>(&(raw_data.front()));
if (file.ReadAtCurrentPos(data, size) == length) {
return base::MakeRefCounted<base::RefCountedBytes>(
std::move(raw_data));
}
}
}
}
return nullptr;
}
// Computes a bitmap at one scale from a bitmap at a different scale.
SkBitmap CreateLowQualityResizedBitmap(
const SkBitmap& source_bitmap,
ui::ResourceScaleFactor source_scale_factor,
ui::ResourceScaleFactor desired_scale_factor) {
gfx::Size scaled_size = gfx::ScaleToCeiledSize(
gfx::Size(source_bitmap.width(), source_bitmap.height()),
ui::GetScaleForResourceScaleFactor(desired_scale_factor) /
ui::GetScaleForResourceScaleFactor(source_scale_factor));
SkBitmap scaled_bitmap;
scaled_bitmap.allocN32Pixels(scaled_size.width(), scaled_size.height());
scaled_bitmap.eraseARGB(0, 0, 0, 0);
SkCanvas canvas(scaled_bitmap, SkSurfaceProps{});
SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size));
// Note(oshima): The following scaling code doesn't work with
// a mask image.
canvas.drawImageRect(source_bitmap.asImage(), scaled_bounds,
SkSamplingOptions());
return scaled_bitmap;
}
// A ImageSkiaSource that scales 100P image to the target scale factor
// if the ImageSkiaRep for the target scale factor isn't available.
class ThemeImageSource : public gfx::ImageSkiaSource {
public:
explicit ThemeImageSource(const gfx::ImageSkia& source) : source_(source) {}
ThemeImageSource(const ThemeImageSource&) = delete;
ThemeImageSource& operator=(const ThemeImageSource&) = delete;
~ThemeImageSource() override = default;
gfx::ImageSkiaRep GetImageForScale(float scale) override {
if (source_.HasRepresentation(scale)) {
return source_.GetRepresentation(scale);
}
const gfx::ImageSkiaRep& rep_100p = source_.GetRepresentation(1.0f);
SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
rep_100p.GetBitmap(), ui::k100Percent,
ui::GetSupportedResourceScaleFactor(scale));
return gfx::ImageSkiaRep(scaled_bitmap, scale);
}
private:
const gfx::ImageSkia source_;
};
// An ImageSkiaSource that delays decoding PNG data into bitmaps until
// needed. Missing data for a scale factor is computed by scaling data for an
// available scale factor. Computed bitmaps are stored for future look up.
class ThemeImagePngSource : public gfx::ImageSkiaSource {
public:
typedef std::map<ui::ResourceScaleFactor,
scoped_refptr<base::RefCountedMemory>>
PngMap;
explicit ThemeImagePngSource(const PngMap& png_map) : png_map_(png_map) {}
ThemeImagePngSource(const ThemeImagePngSource&) = delete;
ThemeImagePngSource& operator=(const ThemeImagePngSource&) = delete;
~ThemeImagePngSource() override = default;
private:
gfx::ImageSkiaRep GetImageForScale(float scale) override {
ui::ResourceScaleFactor scale_factor =
ui::GetSupportedResourceScaleFactor(scale);
// Look up the bitmap for |scale factor| in the bitmap map. If found
// return it.
BitmapMap::const_iterator exact_bitmap_it = bitmap_map_.find(scale_factor);
if (exact_bitmap_it != bitmap_map_.end()) {
return gfx::ImageSkiaRep(exact_bitmap_it->second, scale);
}
// Look up the raw PNG data for |scale_factor| in the png map. If found,
// decode it, store the result in the bitmap map and return it.
PngMap::const_iterator exact_png_it = png_map_.find(scale_factor);
if (exact_png_it != png_map_.end()) {
SkBitmap bitmap = gfx::PNGCodec::Decode(*exact_png_it->second);
if (bitmap.isNull()) {
// The image is either broken or a different format.
return gfx::ImageSkiaRep();
}
bitmap_map_[scale_factor] = bitmap;
return gfx::ImageSkiaRep(bitmap, scale);
}
// Find an available PNG for another scale factor. We want to use the
// highest available scale factor.
PngMap::const_iterator available_png_it = png_map_.end();
for (PngMap::const_iterator png_it = png_map_.begin();
png_it != png_map_.end(); ++png_it) {
if (available_png_it == png_map_.end() ||
ui::GetScaleForResourceScaleFactor(png_it->first) >
ui::GetScaleForResourceScaleFactor(available_png_it->first)) {
available_png_it = png_it;
}
}
if (available_png_it == png_map_.end()) {
return gfx::ImageSkiaRep();
}
ui::ResourceScaleFactor available_scale_factor = available_png_it->first;
// Look up the bitmap for |available_scale_factor| in the bitmap map.
// If not found, decode the corresponging png data, store the result
// in the bitmap map.
BitmapMap::const_iterator available_bitmap_it =
bitmap_map_.find(available_scale_factor);
if (available_bitmap_it == bitmap_map_.end()) {
SkBitmap available_bitmap =
gfx::PNGCodec::Decode(*available_png_it->second);
if (available_bitmap.isNull()) {
NOTREACHED();
}
bitmap_map_[available_scale_factor] = available_bitmap;
available_bitmap_it = bitmap_map_.find(available_scale_factor);
}
// Scale the available bitmap to the desired scale factor, store the result
// in the bitmap map and return it.
SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
available_bitmap_it->second, available_scale_factor, scale_factor);
bitmap_map_[scale_factor] = scaled_bitmap;
return gfx::ImageSkiaRep(scaled_bitmap, scale);
}
PngMap png_map_;
typedef std::map<ui::ResourceScaleFactor, SkBitmap> BitmapMap;
BitmapMap bitmap_map_;
};
class TabBackgroundImageSource : public gfx::CanvasImageSource {
public:
TabBackgroundImageSource(SkColor background_color,
const gfx::ImageSkia& image_to_tint,
const gfx::ImageSkia& overlay,
const color_utils::HSL& hsl_shift,
int vertical_offset)
: gfx::CanvasImageSource(image_to_tint.isNull() ? overlay.size()
: image_to_tint.size()),
background_color_(background_color),
image_to_tint_(image_to_tint),
overlay_(overlay),
hsl_shift_(hsl_shift),
vertical_offset_(vertical_offset) {}
TabBackgroundImageSource(const TabBackgroundImageSource&) = delete;
TabBackgroundImageSource& operator=(const TabBackgroundImageSource&) = delete;
~TabBackgroundImageSource() override = default;
// Overridden from CanvasImageSource:
void Draw(gfx::Canvas* canvas) override {
canvas->DrawColor(background_color_);
// Begin with the frame background image, if any. Since the frame and tabs
// have grown taller and changed alignment over time, not all themes have a
// sufficiently tall image; tiling by vertically mirroring in this case is
// the least-glitchy-looking option. Note that the behavior here needs to
// stay in sync with how the browser frame will actually be drawn.
if (!image_to_tint_.isNull()) {
gfx::ImageSkia bg_tint = gfx::ImageSkiaOperations::CreateHSLShiftedImage(
image_to_tint_, hsl_shift_);
canvas->TileImageInt(bg_tint, 0, vertical_offset_, 0, 0, size().width(),
size().height(), 1.0f, SkTileMode::kRepeat,
SkTileMode::kMirror);
}
// If the theme has a custom tab background image, overlay it. Vertical
// mirroring is used for the same reason as above. This behavior needs to
// stay in sync with how tabs are drawn.
if (!overlay_.isNull()) {
canvas->TileImageInt(overlay_, 0, 0, 0, 0, size().width(),
size().height(), 1.0f, SkTileMode::kRepeat,
SkTileMode::kMirror);
}
}
private:
const SkColor background_color_;
const gfx::ImageSkia image_to_tint_;
const gfx::ImageSkia overlay_;
const color_utils::HSL hsl_shift_;
const int vertical_offset_;
};
class ControlButtonBackgroundImageSource : public gfx::CanvasImageSource {
public:
ControlButtonBackgroundImageSource(SkColor background_color,
const gfx::ImageSkia& bg_image,
const gfx::Size& dest_size)
: gfx::CanvasImageSource(dest_size),
background_color_(background_color),
bg_image_(bg_image) {
DCHECK(!bg_image.isNull());
}
ControlButtonBackgroundImageSource(
const ControlButtonBackgroundImageSource&) = delete;
ControlButtonBackgroundImageSource& operator=(
const ControlButtonBackgroundImageSource&) = delete;
~ControlButtonBackgroundImageSource() override = default;
void Draw(gfx::Canvas* canvas) override {
canvas->DrawColor(background_color_);
if (!bg_image_.isNull()) {
canvas->DrawImageInt(bg_image_, 0, 0);
}
}
private:
const SkColor background_color_;
const gfx::ImageSkia bg_image_;
};
// Returns whether the color is grayscale.
bool IsColorGrayscale(SkColor color) {
constexpr int kChannelTolerance = 9;
auto channels = {SkColorGetR(color), SkColorGetG(color), SkColorGetB(color)};
const int range = std::max(channels) - std::min(channels);
return range < kChannelTolerance;
}
// The minimum contrast the omnibox background must have against the toolbar.
constexpr float kMinOmniboxToolbarContrast = 1.3f;
ui::ColorTransform ChooseOmniboxBgBlendTarget() {
return base::BindRepeating(
[](SkColor input_color, const ui::ColorMixer& mixer) {
const SkColor toolbar_color = mixer.GetResultColor(kColorToolbar);
const SkColor endpoint_color =
color_utils::GetEndpointColorWithMinContrast(toolbar_color);
return (color_utils::GetContrastRatio(toolbar_color, endpoint_color) >=
kMinOmniboxToolbarContrast)
? endpoint_color
: color_utils::GetColorWithMaxContrast(endpoint_color);
});
}
} // namespace
BrowserThemePack::~BrowserThemePack() {
DCHECK(!data_pack_ || using_data_pack_);
if (using_data_pack_) {
if (data_pack_) {
auto task_runner = base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT});
DCHECK(task_runner);
task_runner->DeleteSoon(FROM_HERE, std::move(data_pack_));
}
} else {
// When not using a data_pack, the memory is allocated and must be freed.
delete header_;
delete[] tints_;
delete[] colors_;
delete[] display_properties_;
delete[] source_images_;
}
}
void BrowserThemePack::SetColor(int id, SkColor color) {
DCHECK(colors_);
int first_available_color = -1;
for (size_t i = 0; i < kColorsArrayLength; ++i) {
if (colors_[i].id == id) {
colors_[i].color = color;
return;
}
if (colors_[i].id == -1 && first_available_color == -1) {
first_available_color = i;
}
}
DCHECK_NE(-1, first_available_color);
colors_[first_available_color].id = id;
colors_[first_available_color].color = color;
}
void BrowserThemePack::SetColorIfUnspecified(int id, SkColor color) {
SkColor temp_color;
if (!GetColor(id, &temp_color)) {
SetColor(id, color);
}
}
void BrowserThemePack::SetTint(int id, color_utils::HSL tint) {
DCHECK(tints_);
int first_available_index = -1;
for (size_t i = 0; i < kTintTableLength; ++i) {
if (tints_[i].id == id) {
tints_[i].h = tint.h;
tints_[i].s = tint.s;
tints_[i].l = tint.l;
return;
}
if (tints_[i].id == -1 && first_available_index == -1) {
first_available_index = i;
}
}
DCHECK_NE(-1, first_available_index);
tints_[first_available_index].id = id;
tints_[first_available_index].h = tint.h;
tints_[first_available_index].s = tint.s;
tints_[first_available_index].l = tint.l;
}
void BrowserThemePack::SetDisplayProperty(int id, int value) {
DCHECK(display_properties_);
int first_available_index = -1;
for (size_t i = 0; i < kDisplayPropertiesSize; ++i) {
if (display_properties_[i].id == id) {
display_properties_[i].property = value;
return;
}
if (display_properties_[i].id == -1 && first_available_index == -1) {
first_available_index = i;
}
}
DCHECK_NE(-1, first_available_index);
display_properties_[first_available_index].id = id;
display_properties_[first_available_index].property = value;
}
SkColor BrowserThemePack::ComputeImageColor(const gfx::Image& image,
int height) {
// Include all colors in the analysis.
constexpr color_utils::HSL kNoBounds = {-1, -1, -1};
const SkColor color = color_utils::CalculateKMeanColorOfBitmap(
*image.ToSkBitmap(), height, kNoBounds, kNoBounds, false);
return color;
}
// static
void BrowserThemePack::BuildFromExtension(
const extensions::Extension* extension,
BrowserThemePack* pack) {
DCHECK(extension);
DCHECK(extension->is_theme());
DCHECK(!pack->is_valid());
// NOTE! If you make any changes here, please update kThemePackVersion.
pack->InitEmptyPack();
pack->set_extension_id(extension->id());
pack->SetHeaderId(extension);
pack->SetTintsFromJSON(extensions::ThemeInfo::GetTints(extension));
pack->SetColorsFromJSON(extensions::ThemeInfo::GetColors(extension));
pack->SetDisplayPropertiesFromJSON(
extensions::ThemeInfo::GetDisplayProperties(extension));
// Builds the images. (Image building is dependent on tints).
FilePathMap file_paths;
pack->ParseImageNamesFromJSON(extensions::ThemeInfo::GetImages(extension),
extension->path(), &file_paths);
pack->BuildSourceImagesArray(file_paths);
if (!pack->LoadRawBitmapsTo(file_paths, &pack->images_)) {
return;
}
pack->AdjustThemePack();
// The BrowserThemePack is now in a consistent state.
pack->is_valid_ = true;
}
// static
scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack(
const base::FilePath& path,
const std::string& expected_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Allow IO on UI thread due to deep-seated theme design issues.
// (see http://crbug.com/80206)
base::ScopedAllowBlocking scoped_allow_blocking;
// For now data pack can only have extension type.
scoped_refptr<BrowserThemePack> pack(
new BrowserThemePack(ThemeType::kExtension));
pack->set_extension_id(expected_id);
// The data pack is loaded in a local variable to be released synchronously
// in case of failures (see https://crbug.com/1292632).
// Scale factor parameter is moot as data pack has image resources for all
// supported scale factors.
std::unique_ptr<ui::DataPack> data_pack =
std::make_unique<ui::DataPack>(ui::kScaleFactorNone);
// The browser theme pack is using a data pack.
pack->using_data_pack_ = true;
if (!data_pack->LoadFromPath(path)) {
LOG(ERROR) << "Failed to load theme data pack.";
return nullptr;
}
std::optional<std::string_view> pointer = data_pack->GetStringView(kHeaderID);
if (!pointer) {
return nullptr;
}
pack->header_ = reinterpret_cast<BrowserThemePackHeader*>(
const_cast<char*>(pointer->data()));
if (pack->header_->version != kThemePackVersion) {
DLOG(ERROR) << "BuildFromDataPack failure! Version mismatch!";
return nullptr;
}
// TODO(erg): Check endianess once DataPack works on the other endian.
std::string theme_id(reinterpret_cast<char*>(pack->header_->theme_id),
crx_file::id_util::kIdSize);
std::string truncated_id = expected_id.substr(0, crx_file::id_util::kIdSize);
if (theme_id != truncated_id) {
DLOG(ERROR) << "Wrong id: " << theme_id << " vs " << expected_id;
return nullptr;
}
pointer = data_pack->GetStringView(kTintsID);
if (!pointer) {
return nullptr;
}
pack->tints_ =
reinterpret_cast<TintEntry*>(const_cast<char*>(pointer->data()));
pointer = data_pack->GetStringView(kColorsID);
if (!pointer) {
return nullptr;
}
pack->colors_ =
reinterpret_cast<ColorPair*>(const_cast<char*>(pointer->data()));
pointer = data_pack->GetStringView(kDisplayPropertiesID);
if (!pointer) {
return nullptr;
}
pack->display_properties_ = reinterpret_cast<DisplayPropertyPair*>(
const_cast<char*>(pointer->data()));
pointer = data_pack->GetStringView(kSourceImagesID);
if (!pointer) {
return nullptr;
}
pack->source_images_ =
reinterpret_cast<SourceImage*>(const_cast<char*>(pointer->data()));
pointer = data_pack->GetStringView(kScaleFactorsID);
if (!pointer) {
return nullptr;
}
if (!InputScalesValid(pointer.value(), pack->scale_factors_)) {
DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ "
<< "from those supported by platform.";
return nullptr;
}
// The loaded pack is valid and can be used as a theme pack.
pack->data_pack_ = std::move(data_pack);
pack->is_valid_ = true;
return pack;
}
// static
bool BrowserThemePack::IsPersistentImageID(int id) {
return GetPersistentIDByIDR(id) != PersistentID::kInvalid;
}
// static
void BrowserThemePack::BuildFromColor(SkColor color, BrowserThemePack* pack) {
BuildFromColors(GetAutogeneratedThemeColors(color), pack);
}
// static
void BrowserThemePack::BuildFromColors(AutogeneratedThemeColors colors,
BrowserThemePack* pack) {
pack->InitEmptyPackFromColors();
// NOTE! If you make any changes here, please update kThemePackVersion.
// Frame.
pack->SetColor(TP::COLOR_FRAME_ACTIVE, colors.frame_color);
// Inactive tab uses frame color.
pack->SetColor(TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE,
colors.frame_color);
pack->SetColor(TP::COLOR_TAB_FOREGROUND_INACTIVE_FRAME_ACTIVE,
colors.frame_text_color);
// Toolbar and active tab (set in SetFrameAndToolbarRelatedColors) use active
// tab color.
pack->SetColor(TP::COLOR_TOOLBAR, colors.active_tab_color);
pack->SetColor(TP::COLOR_TOOLBAR_TEXT, colors.active_tab_text_color);
pack->SetColor(TP::COLOR_TOOLBAR_BUTTON_ICON, colors.active_tab_text_color);
// NTP.
pack->SetColor(TP::COLOR_NTP_BACKGROUND, colors.ntp_color);
pack->SetColor(TP::COLOR_NTP_TEXT,
color_utils::GetColorWithMaxContrast(colors.ntp_color));
// Always use alternate logo (not colorful one) for all backgrounds except
// white.
if (colors.active_tab_color != SK_ColorWHITE) {
pack->SetDisplayProperty(TP::NTP_LOGO_ALTERNATE, 1);
}
// Don't change frame color for inactive window.
pack->SetTint(TP::TINT_FRAME_INACTIVE, {-1, -1, -1});
pack->SetTint(TP::TINT_FRAME_INCOGNITO_INACTIVE, {-1, -1, -1});
pack->FinalizePackFromColors();
}
void BrowserThemePack::BuildFromWebAppColors(SkColor theme_color,
SkColor background_color,
BrowserThemePack* pack) {
pack->InitEmptyPackFromColors();
// NOTE! If you make any changes here, please update kThemePackVersion.
// Frame and inactive tab use the theme color.
pack->SetColor(TP::COLOR_FRAME_ACTIVE, theme_color);
pack->SetColor(TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE, theme_color);
SkColor theme_text_color = color_utils::GetColorWithMaxContrast(theme_color);
pack->SetColor(TP::COLOR_TAB_FOREGROUND_INACTIVE_FRAME_ACTIVE,
theme_text_color);
// Toolbar also uses theme color.
pack->SetColor(TP::COLOR_TOOLBAR, theme_color);
pack->SetColor(TP::COLOR_TOOLBAR_TEXT, theme_text_color);
pack->SetColor(TP::COLOR_TOOLBAR_BUTTON_ICON, theme_text_color);
// Active tab however uses background color.
pack->SetColor(TP::COLOR_TAB_BACKGROUND_ACTIVE_FRAME_ACTIVE,
background_color);
pack->SetColor(TP::COLOR_TAB_FOREGROUND_ACTIVE_FRAME_ACTIVE,
color_utils::GetColorWithMaxContrast(background_color));
// Don't change frame color for inactive window.
pack->SetTint(TP::TINT_FRAME_INACTIVE, {-1, -1, -1});
pack->SetTint(TP::TINT_FRAME_INCOGNITO_INACTIVE, {-1, -1, -1});
pack->FinalizePackFromColors();
}
BrowserThemePack::BrowserThemePack(ThemeType theme_type)
: CustomThemeSupplier(theme_type) {
scale_factors_ = ui::GetSupportedResourceScaleFactors();
}
bool BrowserThemePack::WriteToDisk(const base::FilePath& path) const {
// Add resources for each of the property arrays.
RawDataForWriting resources;
resources[kHeaderID] =
std::string_view(reinterpret_cast<const char*>(header_.get()),
sizeof(BrowserThemePackHeader));
resources[kTintsID] =
std::string_view(reinterpret_cast<const char*>(tints_.get()),
sizeof(TintEntry[kTintTableLength]));
resources[kColorsID] =
std::string_view(reinterpret_cast<const char*>(colors_.get()),
sizeof(ColorPair[kColorsArrayLength]));
resources[kDisplayPropertiesID] =
std::string_view(reinterpret_cast<const char*>(display_properties_.get()),
sizeof(DisplayPropertyPair[kDisplayPropertiesSize]));
int source_count = 1;
SourceImage* end = source_images_;
for (; end->id != -1; end++) {
source_count++;
}
resources[kSourceImagesID] =
std::string_view(reinterpret_cast<const char*>(source_images_.get()),
source_count * sizeof(*source_images_));
// Store results of GetResourceScaleFactorsAsString() in std::string as
// std::string_view does not copy data in constructor.
std::string scale_factors_string =
GetResourceScaleFactorsAsString(scale_factors_);
resources[kScaleFactorsID] = scale_factors_string;
AddRawImagesTo(image_memory_, &resources);
RawImages reencoded_images;
RepackImages(images_on_file_thread_, &reencoded_images);
AddRawImagesTo(reencoded_images, &resources);
return ui::DataPack::WritePack(path, resources, ui::DataPack::BINARY);
}
bool BrowserThemePack::GetTint(int id, color_utils::HSL* hsl) const {
if (tints_) {
for (size_t i = 0; i < kTintTableLength; ++i) {
if (tints_[i].id == id) {
hsl->h = tints_[i].h;
hsl->s = tints_[i].s;
hsl->l = tints_[i].l;
return true;
}
}
}
return false;
}
bool BrowserThemePack::GetColor(int id, SkColor* color) const {
static constexpr auto kOpaqueColors =
base::MakeFixedFlatSet<TP::OverwritableByUserThemeProperty>({
// Background tabs must be opaque since the tabstrip expects to be
// able to render text opaquely atop them.
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE,
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_INACTIVE,
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE_INCOGNITO,
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_INACTIVE_INCOGNITO,
// The frame colors will be used for background tabs when not
// otherwise overridden and thus must be opaque as well.
TP::COLOR_FRAME_ACTIVE,
TP::COLOR_FRAME_INACTIVE,
TP::COLOR_FRAME_ACTIVE_INCOGNITO,
TP::COLOR_FRAME_INACTIVE_INCOGNITO,
// The toolbar is used as the foreground tab color, so it must be
// opaque just like background tabs.
TP::COLOR_TOOLBAR,
});
if (colors_) {
for (size_t i = 0; i < kColorsArrayLength; ++i) {
if (colors_[i].id == id) {
*color = colors_[i].color;
if (base::Contains(kOpaqueColors, id)) {
*color = SkColorSetA(*color, SK_AlphaOPAQUE);
}
return true;
}
}
}
return false;
}
bool BrowserThemePack::GetDisplayProperty(int id, int* result) const {
if (display_properties_) {
for (size_t i = 0; i < kDisplayPropertiesSize; ++i) {
if (display_properties_[i].id == id) {
*result = display_properties_[i].property;
return true;
}
}
}
return false;
}
gfx::Image BrowserThemePack::GetImageNamed(int idr_id) const {
PersistentID prs_id = GetPersistentIDByIDR(idr_id);
if (prs_id == PersistentID::kInvalid) {
return gfx::Image();
}
// Check if the image is cached.
ImageCache::const_iterator image_iter = images_.find(prs_id);
if (image_iter != images_.end()) {
return image_iter->second;
}
ThemeImagePngSource::PngMap png_map;
for (auto scale_factor : scale_factors_) {
scoped_refptr<base::RefCountedMemory> memory =
GetRawData(idr_id, scale_factor);
if (memory.get()) {
png_map[scale_factor] = memory;
}
}
if (!png_map.empty()) {
gfx::ImageSkia image_skia(std::make_unique<ThemeImagePngSource>(png_map),
1.0f);
gfx::Image ret = gfx::Image(image_skia);
images_[prs_id] = ret;
return ret;
}
return gfx::Image();
}
base::RefCountedMemory* BrowserThemePack::GetRawData(
int idr_id,
ui::ResourceScaleFactor scale_factor) const {
base::RefCountedMemory* memory = nullptr;
PersistentID prs_id = GetPersistentIDByIDR(idr_id);
int raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
if (raw_id != -1) {
if (data_pack_.get()) {
memory = data_pack_->GetStaticMemory(raw_id);
} else {
auto it = image_memory_.find(raw_id);
if (it != image_memory_.end()) {
memory = it->second.get();
}
}
}
return memory;
}
bool BrowserThemePack::HasCustomImage(int idr_id) const {
PersistentID prs_id = GetPersistentIDByIDR(idr_id);
if (prs_id == PersistentID::kInvalid) {
return false;
}
SourceImage* img = source_images_;
for (; img->id != -1; ++img) {
if (img->id == prs_id) {
return true;
}
}
return false;
}
void BrowserThemePack::AddColorMixers(ui::ColorProvider* provider,
const ui::ColorProviderKey& key) const {
ui::ColorMixer& mixer = provider->AddMixer();
// TODO(http://crbug.com/878664): Enable for all cases.
mixer[kColorToolbarBackgroundSubtleEmphasis] = ui::BlendForMinContrast(
kColorToolbar, kColorToolbar, ChooseOmniboxBgBlendTarget(),
kMinOmniboxToolbarContrast);
// A map from theme property IDs to color IDs for use in color mixers.
constexpr struct {
int property_id;
int color_id;
} kThemePropertiesMap[] = {
{TP::COLOR_BOOKMARK_TEXT, kColorBookmarkBarForeground},
{TP::COLOR_CONTROL_BUTTON_BACKGROUND, kColorCaptionButtonBackground},
{TP::COLOR_FRAME_ACTIVE, ui::kColorFrameActive},
{TP::COLOR_FRAME_INACTIVE, ui::kColorFrameInactive},
{TP::COLOR_NTP_BACKGROUND, kColorNewTabPageBackground},
{TP::COLOR_NTP_HEADER, kColorNewTabPageHeader},
{TP::COLOR_NTP_LINK, kColorNewTabPageLink},
{TP::COLOR_NTP_LOGO, kColorNewTabPageLogo},
{TP::COLOR_NTP_SECTION_BORDER, kColorNewTabPageSectionBorder},
{TP::COLOR_NTP_TEXT, kColorNewTabPageText},
{TP::COLOR_OMNIBOX_TEXT, kColorOmniboxText},
{TP::COLOR_OMNIBOX_BACKGROUND, kColorToolbarBackgroundSubtleEmphasis},
{TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE,
kColorTabBackgroundInactiveFrameActive},
{TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_INACTIVE,
kColorTabBackgroundInactiveFrameInactive},
{TP::COLOR_TAB_FOREGROUND_ACTIVE_FRAME_ACTIVE,
kColorTabForegroundActiveFrameActive},
{TP::COLOR_TAB_FOREGROUND_ACTIVE_FRAME_INACTIVE,
kColorTabForegroundActiveFrameInactive},
{TP::COLOR_TAB_FOREGROUND_INACTIVE_FRAME_ACTIVE,
kColorTabForegroundInactiveFrameActive},
{TP::COLOR_TAB_FOREGROUND_INACTIVE_FRAME_INACTIVE,
kColorTabForegroundInactiveFrameInactive},
{TP::COLOR_TAB_THROBBER_SPINNING, kColorTabThrobber},
{TP::COLOR_TAB_THROBBER_WAITING, kColorTabThrobberPreconnect},
{TP::COLOR_TOOLBAR, kColorToolbar},
{TP::COLOR_TOOLBAR_BUTTON_ICON, kColorToolbarButtonIcon},
{TP::COLOR_TOOLBAR_BUTTON_ICON_HOVERED, kColorToolbarButtonIconHovered},
{TP::COLOR_TOOLBAR_BUTTON_ICON_PRESSED, kColorToolbarButtonIconPressed},
{TP::COLOR_TOOLBAR_TEXT, kColorToolbarText},
{TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_ACTIVE,
kColorWindowControlButtonBackgroundActive},
{TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_INACTIVE,
kColorWindowControlButtonBackgroundInactive}};
SkColor color;
for (const auto& entry : kThemePropertiesMap) {
if (GetColor(entry.property_id, &color)) {
mixer[entry.color_id] = {color};
}
}
if (GetColor(TP::COLOR_TOOLBAR_BUTTON_ICON, &color)) {
mixer[kColorBookmarkFavicon] = {kColorToolbarButtonIcon};
}
if (GetColor(TP::COLOR_BOOKMARK_TEXT, &color)) {
mixer[kColorBookmarkFolderIcon] =
ui::DeriveDefaultIconColor(kColorBookmarkBarForeground);
}
int result = 0;
if (GetDisplayProperty(TP::SHOULD_FILL_BACKGROUND_TAB_COLOR, &result) &&
result == 0) {
mixer[kColorNewTabButtonBackgroundFrameActive] = {SK_ColorTRANSPARENT};
mixer[kColorNewTabButtonBackgroundFrameInactive] = {SK_ColorTRANSPARENT};
}
}
// private:
void BrowserThemePack::AdjustThemePack() {
CropImages(&images_);
// Create toolbar image, and generate toolbar color from image where relevant.
// This must be done after reading colors from JSON (so they can be used for
// compositing the image).
CreateToolbarImageAndColors(&images_);
// Create frame images, and generate frame colors from images where relevant.
// This must be done after reading colors from JSON (so they can be used for
// compositing the image).
CreateFrameImagesAndColors(&images_);
// Generate any missing frame colors from tints. This must be done after
// generating colors from the frame images, so only colors with no matching
// images are generated.
GenerateFrameColorsFromTints();
// Set frame and toolbar related elements' colors (e.g. status bubble,
// info bar, download shelf) to frame or toolbar color.
SetFrameAndToolbarRelatedColors();
// Generate background color information for window control buttons. This
// must be done after frame colors are set, since they are used when
// determining window control button colors.
GenerateWindowControlButtonColor(&images_);
// Create the tab background images, and generate colors where relevant. This
// must be done after all frame colors are set, since they are used when
// creating these.
CreateTabBackgroundImagesAndColors(&images_);
// Make sure the |images_on_file_thread_| has bitmaps for supported
// scale factors before passing to FILE thread.
images_on_file_thread_ = images_;
for (auto& image : images_on_file_thread_) {
gfx::ImageSkia* image_skia =
const_cast<gfx::ImageSkia*>(image.second.ToImageSkia());
image_skia->MakeThreadSafe();
}
// Set ThemeImageSource on |images_| to resample the source
// image if a caller of BrowserThemePack::GetImageNamed() requests an
// ImageSkiaRep for a scale factor not specified by the theme author.
// Callers of BrowserThemePack::GetImageNamed() to be able to retrieve
// ImageSkiaReps for all supported scale factors.
for (auto& image : images_) {
const gfx::ImageSkia source_image_skia = image.second.AsImageSkia();
auto source = std::make_unique<ThemeImageSource>(source_image_skia);
gfx::ImageSkia image_skia(std::move(source), source_image_skia.size());
image.second = gfx::Image(image_skia);
}
// Generate raw images (for new-tab-page attribution and background) for
// any missing scale from an available scale image.
for (auto kPreloadID : kPreloadIDs) {
GenerateRawImageForAllSupportedScales(kPreloadID);
}
// Generates missing NTP related colors. Should be called after theme images
// are prepared.
GenerateMissingNtpColors();
}
void BrowserThemePack::InitEmptyPack() {
InitHeader();
InitTints();
InitColors();
InitDisplayProperties();
}
void BrowserThemePack::InitEmptyPackFromColors() {
DCHECK(!is_valid());
InitEmptyPack();
// Init `source_images_` only here as other code paths initialize it
// differently.
InitSourceImages();
}
void BrowserThemePack::InitHeader() {
header_ = new BrowserThemePackHeader;
header_->version = kThemePackVersion;
// TODO(erg): Need to make this endian safe on other computers. Prerequisite
// is that ui::DataPack removes this same check.
#if defined(__BYTE_ORDER)
// Linux check
static_assert(__BYTE_ORDER == __LITTLE_ENDIAN,
"datapack assumes little endian");
#elif defined(__BIG_ENDIAN__)
// Mac check
#error DataPack assumes little endian
#endif
header_->little_endian = 1;
}
void BrowserThemePack::InitTints() {
tints_ = new TintEntry[kTintTableLength];
for (size_t i = 0; i < kTintTableLength; ++i) {
tints_[i].id = -1;
tints_[i].h = -1;
tints_[i].s = -1;
tints_[i].l = -1;
}
}
void BrowserThemePack::InitColors() {
colors_ = new ColorPair[kColorsArrayLength];
for (size_t i = 0; i < kColorsArrayLength; ++i) {
colors_[i].id = -1;
colors_[i].color = SkColorSetRGB(0, 0, 0);
}
}
void BrowserThemePack::InitDisplayProperties() {
display_properties_ = new DisplayPropertyPair[kDisplayPropertiesSize];
for (size_t i = 0; i < kDisplayPropertiesSize; ++i) {
display_properties_[i].id = -1;
display_properties_[i].property = 0;
}
}
void BrowserThemePack::InitSourceImages() {
source_images_ = new SourceImage[1];
source_images_[0].id = -1;
}
void BrowserThemePack::FinalizePackFromColors() {
AdjustThemePack();
// The BrowserThemePack is now in a consistent state.
is_valid_ = true;
}
void BrowserThemePack::SetHeaderId(const Extension* extension) {
DCHECK(header_);
const std::string& id = extension->id();
memcpy(header_->theme_id, id.c_str(), crx_file::id_util::kIdSize);
}
void BrowserThemePack::SetTintsFromJSON(const base::Value::Dict* tints_value) {
DCHECK(tints_);
if (!tints_value) {
return;
}
// Parse the incoming data from |tints_value| into an intermediary structure.
std::map<int, color_utils::HSL> temp_tints;
for (const auto [key, value] : *tints_value) {
if (!value.is_list()) {
continue;
}
const base::Value::List& tint_list = value.GetList();
if (tint_list.size() != 3) {
continue;
}
std::optional<double> h = tint_list[0].GetIfDouble();
std::optional<double> s = tint_list[1].GetIfDouble();
std::optional<double> l = tint_list[2].GetIfDouble();
if (!h || !s || !l) {
continue;
}
color_utils::HSL hsl = {*h, *s, *l};
MakeHSLShiftValid(&hsl);
int id = GetIntForString(key, kTintTable, kTintTableLength);
if (id != -1) {
temp_tints[id] = hsl;
}
}
// Copy data from the intermediary data structure to the array.
size_t count = 0;
for (std::map<int, color_utils::HSL>::const_iterator it = temp_tints.begin();
it != temp_tints.end() && count < kTintTableLength; ++it, ++count) {
tints_[count].id = it->first;
tints_[count].h = it->second.h;
tints_[count].s = it->second.s;
tints_[count].l = it->second.l;
}
}
void BrowserThemePack::SetColorsFromJSON(
const base::Value::Dict* colors_value) {
DCHECK(colors_);
std::map<int, SkColor> temp_colors;
if (colors_value) {
ReadColorsFromJSON(*colors_value, &temp_colors);
}
// Copy data from the intermediary data structure to the array.
size_t count = 0;
for (std::map<int, SkColor>::const_iterator it = temp_colors.begin();
it != temp_colors.end() && count < kOverwritableColorTableLength;
++it, ++count) {
colors_[count].id = it->first;
colors_[count].color = it->second;
}
}
void BrowserThemePack::ReadColorsFromJSON(const base::Value::Dict& colors_value,
std::map<int, SkColor>* temp_colors) {
// Parse the incoming data from |colors_value| into an intermediary structure.
for (const auto [key, value] : colors_value) {
if (!value.is_list()) {
continue;
}
const base::Value::List& color_list = value.GetList();
if (!(color_list.size() == 3 || color_list.size() == 4)) {
continue;
}
SkColor color = SK_ColorWHITE;
std::optional<int> r = color_list[0].GetIfInt();
std::optional<int> g = color_list[1].GetIfInt();
std::optional<int> b = color_list[2].GetIfInt();
if (!(r.has_value() && r.value() >= 0 && r.value() <= 255 &&
g.has_value() && g.value() >= 0 && g.value() <= 255 &&
b.has_value() && b.value() >= 0 && b.value() <= 255)) {
continue;
}
if (color_list.size() == 4) {
bool alpha_valid = false;
if (color_list[3].is_int()) {
int alpha_int = color_list[3].GetInt();
if (alpha_int == 0 || alpha_int == 1) {
color = SkColorSetARGB(alpha_int ? 255 : 0, *r, *g, *b);
alpha_valid = true;
}
} else if (color_list[3].is_double()) {
double alpha = color_list[3].GetDouble();
if (alpha >= 0 && alpha <= 1) {
color =
SkColorSetARGB(base::ClampRound<U8CPU>(alpha * 255), *r, *g, *b);
alpha_valid = true;
}
}
if (!alpha_valid) {
continue;
}
} else {
color = SkColorSetRGB(*r, *g, *b);
}
if (key == "ntp_section") {
// We no longer use ntp_section, but to support legacy
// themes we still need to use it as a fallback for
// ntp_header.
if (!temp_colors->count(TP::COLOR_NTP_HEADER)) {
(*temp_colors)[TP::COLOR_NTP_HEADER] = color;
}
} else {
int id = GetIntForString(key, kOverwritableColorTable,
kOverwritableColorTableLength);
if (id != -1) {
(*temp_colors)[id] = color;
}
}
}
}
void BrowserThemePack::SetDisplayPropertiesFromJSON(
const base::Value::Dict* display_properties_value) {
DCHECK(display_properties_);
if (!display_properties_value) {
return;
}
std::map<int, int> temp_properties;
for (const auto [key, value] : *display_properties_value) {
int property_id =
GetIntForString(key, kDisplayProperties, kDisplayPropertiesSize);
switch (property_id) {
case TP::NTP_BACKGROUND_ALIGNMENT: {
if (value.is_string()) {
temp_properties[TP::NTP_BACKGROUND_ALIGNMENT] =
TP::StringToAlignment(value.GetString());
}
break;
}
case TP::NTP_BACKGROUND_TILING: {
if (value.is_string()) {
temp_properties[TP::NTP_BACKGROUND_TILING] =
TP::StringToTiling(value.GetString());
}
break;
}
case TP::NTP_LOGO_ALTERNATE: {
if (value.is_int()) {
temp_properties[TP::NTP_LOGO_ALTERNATE] = value.GetInt();
}
break;
}
}
}
// Copy data from the intermediary data structure to the array.
size_t count = 0;
for (std::map<int, int>::const_iterator it = temp_properties.begin();
it != temp_properties.end() && count < kDisplayPropertiesSize;
++it, ++count) {
display_properties_[count].id = it->first;
display_properties_[count].property = it->second;
}
}
void BrowserThemePack::ParseImageNamesFromJSON(
const base::Value::Dict* images_value,
const base::FilePath& images_path,
FilePathMap* file_paths) const {
if (!images_value) {
return;
}
for (const auto [key, value] : *images_value) {
if (value.is_dict()) {
for (const auto [inner_key, inner_value] : value.GetDict()) {
ui::ResourceScaleFactor scale_factor = ui::kScaleFactorNone;
if (GetScaleFactorFromManifestKey(inner_key, &scale_factor) &&
inner_value.is_string()) {
AddFileAtScaleToMap(key, scale_factor,
images_path.AppendASCII(inner_value.GetString()),
file_paths);
}
}
} else if (value.is_string()) {
AddFileAtScaleToMap(key, ui::k100Percent,
images_path.AppendASCII(value.GetString()),
file_paths);
}
}
}
void BrowserThemePack::AddFileAtScaleToMap(const std::string& image_name,
ui::ResourceScaleFactor scale_factor,
const base::FilePath& image_path,
FilePathMap* file_paths) const {
PersistentID id = GetPersistentIDByName(image_name);
if (id != PersistentID::kInvalid) {
(*file_paths)[id][scale_factor] = image_path;
}
}
void BrowserThemePack::BuildSourceImagesArray(const FilePathMap& file_paths) {
source_images_ = new SourceImage[file_paths.size() + 1];
std::ranges::transform(
file_paths, source_images_.get(),
[](const auto& pair) { return SourceImage{pair.first}; });
source_images_[file_paths.size()].id = -1;
}
bool BrowserThemePack::LoadRawBitmapsTo(const FilePathMap& file_paths,
ImageCache* image_cache) {
// Themes should be loaded on the file thread, not the UI thread.
// http://crbug.com/61838
base::ScopedAllowBlocking scoped_allow_blocking;
for (const auto& entry : file_paths) {
PersistentID prs_id = entry.first;
// Some images need to go directly into |image_memory_|. No modification is
// necessary or desirable.
const bool is_copyable = base::Contains(kPreloadIDs, prs_id);
gfx::ImageSkia image_skia;
for (int pass = 0; pass < 2; ++pass) {
// Two passes: In the first pass, we process only scale factor
// 100% and in the second pass all other scale factors. We
// process scale factor 100% first because the first image added
// in image_skia.AddRepresentation() determines the DIP size for
// all representations.
for (const auto& s2f : entry.second) {
ui::ResourceScaleFactor scale_factor = s2f.first;
if ((pass == 0 && scale_factor != ui::k100Percent) ||
(pass == 1 && scale_factor == ui::k100Percent)) {
continue;
}
scoped_refptr<base::RefCountedMemory> raw_data(
ReadFileData(s2f.second));
if (!raw_data.get() || !raw_data->size()) {
LOG(ERROR) << "Could not load theme image" << " prs_id=" << prs_id
<< " scale_factor_enum=" << scale_factor
<< " file=" << s2f.second.value()
<< (raw_data.get() ? " (zero size)" : " (read error)");
return false;
}
if (is_copyable) {
int raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
image_memory_[raw_id] = raw_data;
} else {
SkBitmap bitmap = gfx::PNGCodec::Decode(*raw_data);
if (!bitmap.isNull()) {
image_skia.AddRepresentation(gfx::ImageSkiaRep(
bitmap, ui::GetScaleForResourceScaleFactor(scale_factor)));
} else {
// The image is either broken or a different format.
return false;
}
}
}
}
if (!is_copyable && !image_skia.isNull()) {
(*image_cache)[prs_id] = gfx::Image(image_skia);
}
}
return true;
}
void BrowserThemePack::CropImages(ImageCache* images) const {
for (const auto& image_to_crop : kImagesToCrop) {
auto it = images->find(image_to_crop.prs_id);
if (it == images->end()) {
continue;
}
gfx::ImageSkia image_skia = it->second.AsImageSkia();
(*images)[image_to_crop.prs_id] =
gfx::Image(gfx::ImageSkiaOperations::ExtractSubset(
image_skia,
gfx::Rect(0, 0, image_skia.width(), image_to_crop.max_height)));
}
}
void BrowserThemePack::SetFrameAndToolbarRelatedColors() {
// Propagate the user-specified toolbar color to similar elements.
SkColor toolbar_color;
if (GetColor(TP::COLOR_TOOLBAR, &toolbar_color)) {
// If the toolbar color is set but the text color is not, ensure it has
// sufficient contrast.
SkColor toolbar_text_color =
TP::GetDefaultColor(TP::COLOR_TOOLBAR_TEXT, false);
toolbar_text_color =
color_utils::BlendForMinContrast(toolbar_text_color, toolbar_color)
.color;
SetColorIfUnspecified(TP::COLOR_TOOLBAR_TEXT, toolbar_text_color);
} else {
toolbar_color = TP::GetDefaultColor(TP::COLOR_TOOLBAR, false);
}
SkColor toolbar_button_icon_color;
color_utils::HSL button_tint;
if (GetColor(TP::COLOR_TOOLBAR_BUTTON_ICON, &toolbar_button_icon_color)) {
SetColor(TP::COLOR_TOOLBAR_BUTTON_ICON_HOVERED, toolbar_button_icon_color);
SetColor(TP::COLOR_TOOLBAR_BUTTON_ICON_PRESSED, toolbar_button_icon_color);
SetColor(TP::COLOR_TAB_THROBBER_SPINNING, toolbar_button_icon_color);
SetColor(TP::COLOR_TAB_THROBBER_WAITING, toolbar_button_icon_color);
} else if (GetTint(TP::TINT_BUTTONS, &button_tint)) {
// Duplicate how COLOR_TOOLBAR_BUTTON_ICON will be computed.
// TODO(pkasting): Should this code be shared with
// ThemeHelper::GetDefaultColor() somehow?
const SkColor button_color =
color_utils::HSLShift(gfx::kGoogleGrey700, button_tint);
SetColor(TP::COLOR_TAB_THROBBER_SPINNING, button_color);
SetColor(TP::COLOR_TAB_THROBBER_WAITING, button_color);
}
SkColor toolbar_text_color;
if (GetColor(TP::COLOR_TOOLBAR_TEXT, &toolbar_text_color)) {
SetColorIfUnspecified(TP::COLOR_BOOKMARK_TEXT, toolbar_text_color);
SetColorIfUnspecified(TP::COLOR_TAB_FOREGROUND_ACTIVE_FRAME_ACTIVE,
toolbar_text_color);
}
SkColor tab_foreground_color;
if (GetColor(TP::COLOR_TAB_FOREGROUND_ACTIVE_FRAME_ACTIVE,
&tab_foreground_color)) {
SetColor(TP::COLOR_TAB_FOREGROUND_ACTIVE_FRAME_INACTIVE,
tab_foreground_color);
}
SkColor omnibox_background_color;
if (GetColor(TP::COLOR_OMNIBOX_BACKGROUND, &omnibox_background_color)) {
omnibox_background_color = color_utils::GetResultingPaintColor(
omnibox_background_color, toolbar_color);
SetColor(TP::COLOR_OMNIBOX_BACKGROUND, omnibox_background_color);
} else {
// TODO(pkasting): This should be shared with the omnibox color mixer.
omnibox_background_color = gfx::kGoogleGrey100;
}
SkColor omnibox_text_color;
if (GetColor(TP::COLOR_OMNIBOX_TEXT, &omnibox_text_color)) {
SetColor(TP::COLOR_OMNIBOX_TEXT,
color_utils::GetResultingPaintColor(omnibox_text_color,
omnibox_background_color));
}
}
void BrowserThemePack::CreateToolbarImageAndColors(ImageCache* images) {
ImageCache temp_output;
constexpr PersistentID kSrcImageId = PRS::kToolbar;
const auto image_it = images->find(kSrcImageId);
if (image_it == images->end()) {
return;
}
auto image = image_it->second.AsImageSkia();
constexpr int kToolbarColorId = TP::COLOR_TOOLBAR;
SkColor toolbar_color;
if (!GetColor(kToolbarColorId, &toolbar_color)) {
toolbar_color = TP::GetDefaultColor(kToolbarColorId, false);
}
// Generate a composite image by drawing the toolbar image on top of the
// specified toolbar color (if any).
color_utils::HSL hsl_shift{-1, -1, -1};
gfx::ImageSkia overlay;
auto source = std::make_unique<TabBackgroundImageSource>(
toolbar_color, image, overlay, hsl_shift, 0);
gfx::Size dest_size = image.size();
const gfx::Image dest_image(gfx::ImageSkia(std::move(source), dest_size));
temp_output[kSrcImageId] = dest_image;
SetColorIfUnspecified(kToolbarColorId,
ComputeImageColor(dest_image, dest_size.height()));
MergeImageCaches(temp_output, images);
}
void BrowserThemePack::CreateFrameImagesAndColors(ImageCache* images) {
static constexpr struct FrameValues {
PersistentID prs_id;
int tint_id;
std::optional<int> color_id;
} kFrameValues[] = {
{PRS::kFrame, TP::TINT_FRAME, TP::COLOR_FRAME_ACTIVE},
{PRS::kFrameInactive, TP::TINT_FRAME_INACTIVE, TP::COLOR_FRAME_INACTIVE},
{PRS::kFrameOverlay, TP::TINT_FRAME, std::nullopt},
{PRS::kFrameOverlayInactive, TP::TINT_FRAME_INACTIVE, std::nullopt},
{PRS::kFrameIncognito, TP::TINT_FRAME_INCOGNITO,
TP::COLOR_FRAME_ACTIVE_INCOGNITO},
{PRS::kFrameIncognitoInactive, TP::TINT_FRAME_INCOGNITO_INACTIVE,
TP::COLOR_FRAME_INACTIVE_INCOGNITO},
};
// Create all the output images in a separate cache and move them back into
// the input images because there can be name collisions.
ImageCache temp_output;
for (const auto& frame_values : kFrameValues) {
PersistentID src_id = frame_values.prs_id;
// If the theme doesn't provide an image, attempt to fall back to one it
// does.
if (!images->count(src_id)) {
// Fall back from inactive overlay to active overlay.
if (src_id == PRS::kFrameOverlayInactive) {
src_id = PRS::kFrameOverlay;
}
// Fall back from inactive incognito to active incognito.
if (src_id == PRS::kFrameIncognitoInactive) {
src_id = PRS::kFrameIncognito;
}
// For all non-overlay images, fall back to PRS_THEME_FRAME as a last
// resort.
if (!images->count(src_id) && src_id != PRS::kFrameOverlay) {
src_id = PRS::kFrame;
}
}
// Note that if the original ID and all the fallbacks are absent, the caller
// will rely on the frame colors instead.
const auto image = images->find(src_id);
if (image != images->end()) {
const gfx::Image dest_image(
gfx::ImageSkiaOperations::CreateHSLShiftedImage(
*image->second.ToImageSkia(),
GetTintInternal(frame_values.tint_id)));
temp_output[frame_values.prs_id] = dest_image;
if (frame_values.color_id) {
SetColorIfUnspecified(
frame_values.color_id.value(),
ComputeImageColor(dest_image, kTallestFrameHeight));
}
}
}
MergeImageCaches(temp_output, images);
}
void BrowserThemePack::GenerateFrameColorsFromTints() {
SkColor frame;
if (!GetColor(TP::COLOR_FRAME_ACTIVE, &frame)) {
frame = TP::GetDefaultColor(TP::COLOR_FRAME_ACTIVE, false);
SetColor(TP::COLOR_FRAME_ACTIVE,
HSLShift(frame, GetTintInternal(TP::TINT_FRAME)));
}
SetColorIfUnspecified(
TP::COLOR_FRAME_INACTIVE,
HSLShift(frame, GetTintInternal(TP::TINT_FRAME_INACTIVE)));
SetColorIfUnspecified(
TP::COLOR_FRAME_ACTIVE_INCOGNITO,
HSLShift(frame, GetTintInternal(TP::TINT_FRAME_INCOGNITO)));
SetColorIfUnspecified(
TP::COLOR_FRAME_INACTIVE_INCOGNITO,
HSLShift(frame, GetTintInternal(TP::TINT_FRAME_INCOGNITO_INACTIVE)));
}
void BrowserThemePack::GenerateWindowControlButtonColor(ImageCache* images) {
static constexpr struct ControlBGValue {
// The color to compute and store.
int color_id;
// The frame color to use as the base of this button background.
int frame_color_id;
} kControlButtonBackgroundMap[] = {
{TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_ACTIVE,
TP::COLOR_FRAME_ACTIVE},
{TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_INACTIVE,
TP::COLOR_FRAME_INACTIVE},
{TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_INCOGNITO_ACTIVE,
TP::COLOR_FRAME_ACTIVE_INCOGNITO},
{TP::COLOR_WINDOW_CONTROL_BUTTON_BACKGROUND_INCOGNITO_INACTIVE,
TP::COLOR_FRAME_INACTIVE_INCOGNITO},
};
// Get data related to the control button background image and color first,
// since they are shared by all variants.
gfx::ImageSkia bg_image;
ImageCache::const_iterator bg_img_it =
images->find(PRS::kWindowControlBackground);
if (bg_img_it != images->end()) {
bg_image = bg_img_it->second.AsImageSkia();
}
SkColor button_bg_color = SK_ColorTRANSPARENT;
SkAlpha button_bg_alpha = SK_AlphaTRANSPARENT;
if (GetColor(TP::COLOR_CONTROL_BUTTON_BACKGROUND, &button_bg_color)) {
button_bg_alpha = SkColorGetA(button_bg_color);
}
button_bg_alpha =
WindowFrameUtil::CalculateWindowsCaptionButtonBackgroundAlpha(
button_bg_alpha);
// Determine what portion of the image to use in our calculations (we won't
// use more along the X-axis than the width of the caption buttons). This
// should theoretically be the maximum of the size of the caption button area
// on the glass frame and opaque frame, but it would be rather complicated to
// determine the size of the opaque frame's caption button area at pack
// processing time (as it is determined by the size of icons, which we don't
// have easy access to here), so we use the glass frame area as an
// approximation.
gfx::Size dest_size = WindowFrameUtil::GetWindowsCaptionButtonAreaSize();
// To get an accurate sampling, all we need to do is get a representative
// image that is at MOST the size of the caption button area. In the case of
// an image that is smaller - we only need to sample an area the size of the
// provided image (trying to take tiling into account would be overkill).
if (!bg_image.isNull()) {
dest_size.SetToMin(bg_image.size());
}
for (const ControlBGValue& bg_pair : kControlButtonBackgroundMap) {
SkColor frame_color;
GetColor(bg_pair.frame_color_id, &frame_color);
SkColor base_color =
color_utils::AlphaBlend(button_bg_color, frame_color, button_bg_alpha);
if (bg_image.isNull()) {
SetColor(bg_pair.color_id, base_color);
continue;
}
auto source = std::make_unique<ControlButtonBackgroundImageSource>(
base_color, bg_image, dest_size);
const gfx::Image dest_image(gfx::ImageSkia(std::move(source), dest_size));
SetColorIfUnspecified(bg_pair.color_id,
ComputeImageColor(dest_image, dest_size.height()));
}
}
void BrowserThemePack::CreateTabBackgroundImagesAndColors(ImageCache* images) {
static constexpr struct TabValues {
// The background image to create/update.
PersistentID tab_id;
// For inactive images, the corresponding active image. If the active
// images are customized and the inactive ones are not, the inactive ones
// will be based on the active ones.
std::optional<PersistentID> fallback_tab_id;
// The frame image to use as the base of this tab background image.
PersistentID frame_id;
// The frame color to use as the base of this tab background image.
int frame_color_id;
// The color to compute and store for this image, if not present.
int color_id;
} kTabBackgroundMap[] = {
{PRS::kTabBackground, std::nullopt, PRS::kFrame, TP::COLOR_FRAME_ACTIVE,
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE},
{PRS::kTabBackgroundInactive, PRS::kTabBackground, PRS::kFrameInactive,
TP::COLOR_FRAME_INACTIVE,
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_INACTIVE},
{PRS::kTabBackgroundIncognito, std::nullopt, PRS::kFrameIncognito,
TP::COLOR_FRAME_ACTIVE_INCOGNITO,
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_ACTIVE_INCOGNITO},
{PRS::kTabBackgroundIncognitoInactive, PRS::kTabBackgroundIncognito,
PRS::kFrameIncognitoInactive, TP::COLOR_FRAME_INACTIVE_INCOGNITO,
TP::COLOR_TAB_BACKGROUND_INACTIVE_FRAME_INACTIVE_INCOGNITO},
};
ImageCache temp_output;
for (const auto& entry : kTabBackgroundMap) {
ImageCache::const_iterator tab_it = images->find(entry.tab_id);
// Inactive images should be based on the active ones if the active ones
// were customized.
if (tab_it == images->end() && entry.fallback_tab_id) {
tab_it = images->find(*entry.fallback_tab_id);
}
// Generate background tab images when provided with custom frame or
// background tab images; in the former case the theme author may want the
// background tabs to appear to tint the frame, and in the latter case the
// provided background tab image may have transparent regions, which must be
// made opaque by overlaying atop the original frame.
const ImageCache::const_iterator frame_it = images->find(entry.frame_id);
if (frame_it != images->end() || tab_it != images->end()) {
SkColor frame_color;
GetColor(entry.frame_color_id, &frame_color);
gfx::ImageSkia image_to_tint;
if (frame_it != images->end()) {
image_to_tint = (frame_it->second).AsImageSkia();
}
gfx::ImageSkia overlay;
if (tab_it != images->end()) {
overlay = tab_it->second.AsImageSkia();
}
auto source = std::make_unique<TabBackgroundImageSource>(
frame_color, image_to_tint, overlay,
GetTintInternal(TP::TINT_BACKGROUND_TAB), TP::kFrameHeightAboveTabs);
gfx::Size dest_size = image_to_tint.size();
dest_size.SetToMax(overlay.size());
dest_size.set_height(kTallestTabHeight);
const gfx::Image dest_image(gfx::ImageSkia(std::move(source), dest_size));
temp_output[entry.tab_id] = dest_image;
SetColorIfUnspecified(entry.color_id,
ComputeImageColor(dest_image, kTallestTabHeight));
}
}
MergeImageCaches(temp_output, images);
}
void BrowserThemePack::GenerateMissingNtpColors() {
gfx::Image image = GetImageNamed(IDR_THEME_NTP_BACKGROUND);
bool has_background_image = !image.IsEmpty();
// Opaquify background color.
SkColor background_color;
bool has_background_color =
GetColor(TP::COLOR_NTP_BACKGROUND, &background_color);
if (has_background_color) {
background_color = color_utils::GetResultingPaintColor(
background_color, TP::GetDefaultColor(TP::COLOR_NTP_BACKGROUND, false));
SetColor(TP::COLOR_NTP_BACKGROUND, background_color);
}
// Calculate NTP text color based on NTP background.
SkColor text_color;
if (!GetColor(TP::COLOR_NTP_TEXT, &text_color)) {
if (has_background_image) {
background_color = ComputeImageColor(image, image.Height());
}
if (has_background_image || has_background_color) {
SetColor(TP::COLOR_NTP_TEXT,
color_utils::GetColorWithMaxContrast(background_color));
}
}
// Calculate logo alternate, if not specified.
int logo_alternate = 0;
if (!GetDisplayProperty(TP::NTP_LOGO_ALTERNATE, &logo_alternate)) {
logo_alternate =
has_background_image ||
(has_background_color && !IsColorGrayscale(background_color));
SetDisplayProperty(TP::NTP_LOGO_ALTERNATE, logo_alternate);
}
// For themes that use alternate logo and no NTP background image is present,
// set logo color in the same hue as NTP background.
if (logo_alternate == 1 && !has_background_image && has_background_color) {
SkColor logo_color = color_utils::IsDark(background_color)
? SK_ColorWHITE
: GetContrastingColor(background_color,
/*luminosity_change=*/0.3f);
SetColor(TP::COLOR_NTP_LOGO, logo_color);
}
// Calculate NTP section border color.
SkColor header_color;
if (GetColor(TP::COLOR_NTP_HEADER, &header_color)) {
SetColor(TP::COLOR_NTP_SECTION_BORDER, SkColorSetA(header_color, 0x50));
}
}
void BrowserThemePack::RepackImages(const ImageCache& images,
RawImages* reencoded_images) const {
for (const auto& image : images) {
gfx::ImageSkia image_skia = *image.second.ToImageSkia();
std::vector<gfx::ImageSkiaRep> image_reps = image_skia.image_reps();
DCHECK(!image_reps.empty())
<< "No image reps for resource " << image.first << ".";
for (const auto& rep : image_reps) {
std::optional<std::vector<uint8_t>> bitmap_data =
gfx::PNGCodec::EncodeBGRASkBitmap(rep.GetBitmap(),
/*discard_transparency=*/false);
CHECK(bitmap_data) << "Image file for resource " << image.first
<< " could not be encoded.";
int raw_id = GetRawIDByPersistentID(
image.first, ui::GetSupportedResourceScaleFactor(rep.scale()));
(*reencoded_images)[raw_id] = base::MakeRefCounted<base::RefCountedBytes>(
std::move(bitmap_data).value());
}
}
}
void BrowserThemePack::MergeImageCaches(const ImageCache& source,
ImageCache* destination) const {
for (const auto& it : source) {
(*destination)[it.first] = it.second;
}
}
void BrowserThemePack::AddRawImagesTo(const RawImages& images,
RawDataForWriting* out) const {
for (const auto& pair : images) {
(*out)[pair.first] = base::as_string_view(*pair.second);
}
}
color_utils::HSL BrowserThemePack::GetTintInternal(int id) const {
color_utils::HSL hsl;
if (GetTint(id, &hsl)) {
return hsl;
}
int original_id = id;
if (id == TP::TINT_FRAME_INCOGNITO) {
original_id = TP::TINT_FRAME;
} else if (id == TP::TINT_FRAME_INCOGNITO_INACTIVE) {
original_id = TP::TINT_FRAME_INACTIVE;
}
return TP::GetDefaultTint(original_id, original_id != id);
}
int BrowserThemePack::GetRawIDByPersistentID(
PersistentID prs_id,
ui::ResourceScaleFactor scale_factor) const {
if (prs_id == PersistentID::kInvalid) {
return -1;
}
for (size_t i = 0; i < scale_factors_.size(); ++i) {
if (scale_factors_[i] == scale_factor) {
return ((PersistentID::kMaxValue + 1) * i) + prs_id;
}
}
return -1;
}
bool BrowserThemePack::GetScaleFactorFromManifestKey(
const std::string& key,
ui::ResourceScaleFactor* scale_factor) const {
int percent = 0;
if (base::StringToInt(key, &percent)) {
float scale = static_cast<float>(percent) / 100.0f;
for (auto i : scale_factors_) {
if (fabs(ui::GetScaleForResourceScaleFactor(i) - scale) < 0.001) {
*scale_factor = i;
return true;
}
}
}
return false;
}
void BrowserThemePack::GenerateRawImageForAllSupportedScales(
PersistentID prs_id) {
// Compute (by scaling) bitmaps for |prs_id| for any scale factors
// for which the theme author did not provide a bitmap. We compute
// the bitmaps using the highest scale factor that theme author
// provided.
// Note: We use only supported scale factors. For example, if scale
// factor 2x is supported by the current system, but 1.8x is not and
// if the theme author did not provide an image for 2x but one for
// 1.8x, we will not use the 1.8x image here. Here we will only use
// images provided for scale factors supported by the current system.
// See if any image is missing. If not, we're done.
bool image_missing = false;
for (auto& scale_factor : scale_factors_) {
int raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
if (image_memory_.find(raw_id) == image_memory_.end()) {
image_missing = true;
break;
}
}
if (!image_missing) {
return;
}
// Find available scale factor with highest scale.
ui::ResourceScaleFactor available_scale_factor = ui::kScaleFactorNone;
for (auto& scale_factor : scale_factors_) {
int raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
if ((available_scale_factor == ui::kScaleFactorNone ||
(ui::GetScaleForResourceScaleFactor(scale_factor) >
ui::GetScaleForResourceScaleFactor(available_scale_factor))) &&
image_memory_.find(raw_id) != image_memory_.end()) {
available_scale_factor = scale_factor;
}
}
// If no scale factor is available, we're done.
if (available_scale_factor == ui::kScaleFactorNone) {
return;
}
// Get bitmap for the available scale factor.
int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor);
RawImages::const_iterator it = image_memory_.find(available_raw_id);
SkBitmap available_bitmap = gfx::PNGCodec::Decode(*it->second);
if (available_bitmap.isNull()) {
// The image is either broken or a different format.
return;
}
// Fill in all missing scale factors by scaling the available bitmap.
for (auto& scale_factor : scale_factors_) {
int scaled_raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
if (image_memory_.find(scaled_raw_id) != image_memory_.end()) {
continue;
}
SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
available_bitmap, available_scale_factor, scale_factor);
std::optional<std::vector<uint8_t>> bitmap_data =
gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap,
/*discard_transparency=*/false);
if (!bitmap_data) {
NOTREACHED() << "Unable to encode theme image for prs_id=" << prs_id
<< " for scale_factor=" << scale_factor;
}
image_memory_[scaled_raw_id] = base::MakeRefCounted<base::RefCountedBytes>(
std::move(bitmap_data).value());
}
}
|