File: app_shim_manager_mac.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1981 lines) | stat: -rw-r--r-- 78,127 bytes parent folder | download | duplicates (3)
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
// Copyright 2013 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/apps/app_shim/app_shim_manager_mac.h"

#include <CoreFoundation/CoreFoundation.h>

#include <algorithm>
#include <optional>
#include <set>
#include <utility>

#include "apps/app_lifetime_monitor_factory.h"
#include "base/apple/bundle_locations.h"
#include "base/apple/foundation_util.h"
#include "base/apple/osstatus_logging.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/barrier_closure.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/sha1.h"
#include "base/logging.h"
#include "base/mac/code_signature.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/types/expected.h"
#include "base/types/expected_macros.h"
#include "chrome/browser/app_controller_mac.h"
#include "chrome/browser/apps/app_shim/app_shim_host_bootstrap_mac.h"
#include "chrome/browser/apps/app_shim/app_shim_host_mac.h"
#include "chrome/browser/apps/app_shim/app_shim_listener.h"
#include "chrome/browser/apps/app_shim/app_shim_termination_manager.h"
#include "chrome/browser/apps/app_shim/code_signature_mac.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/notifications/mac/notification_platform_bridge_mac.h"
#include "chrome/browser/notifications/mac/notification_utils.h"
#include "chrome/browser/profiles/avatar_menu.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/profiles/profile_picker.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/web_applications/os_integration/mac/app_shim_registry.h"
#include "chrome/browser/web_applications/os_integration/mac/web_app_shortcut_mac.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/mac/app_mode_common.h"
#include "chrome/services/mac_notifications/public/mojom/mac_notifications.mojom.h"
#include "components/crash/core/common/crash_key.h"
#include "components/crx_file/id_util.h"
#include "content/public/browser/browser_context.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/base/filename_util.h"

namespace {

// A crash key that is used when dumping because of errors when building and
// verifying the app shim requirement.
crash_reporter::CrashKeyString<256> app_shim_requirement_crash_key(
    "AppShimRequirement");

// This function logs the status and error_details using OSSTATUS_LOG(). It also
// calls base::debug::DumpWithoutCrashing() using app_shim_requirement_crash_key
// as a crash key. The status and error_details are appended to the crash key.
void DumpOSStatusError(OSStatus status, std::string error_details) {
  OSSTATUS_LOG(ERROR, status) << error_details;
  crash_reporter::ScopedCrashKeyString crash_key_value(
      &app_shim_requirement_crash_key,
      base::StringPrintf("%s: %s (%d)", error_details.c_str(),
                         logging::DescriptionFromOSStatus(status).c_str(),
                         status));
  base::debug::DumpWithoutCrashing();
}

// This function is similar to DumpOSStatusError(), however it operates without
// an OSStatus.
void DumpError(std::string error_details) {
  LOG(ERROR) << error_details;
  crash_reporter::ScopedCrashKeyString crash_key_value(
      &app_shim_requirement_crash_key, error_details);
  base::debug::DumpWithoutCrashing();
}

// Creates a requirement for the app shim based on the framework bundle's
// designated requirement.
//
// Returns a non-null requirement or the reason why the requirement could not
// be created.
base::expected<base::apple::ScopedCFTypeRef<SecRequirementRef>,
               apps::MissingRequirementReason>
CreateAppShimRequirement() {
  ASSIGN_OR_RETURN(auto framework_requirement_string,
                   apps::FrameworkBundleDesignatedRequirementString());

  base::apple::ScopedCFTypeRef<CFStringRef> app_shim_requirement_string =
      apps::AppShimManager::
          BuildAppShimRequirementStringFromFrameworkRequirementString(
              framework_requirement_string.get());
  if (!app_shim_requirement_string) {
    return base::unexpected(apps::MissingRequirementReason::Error);
  }

  return apps::RequirementFromString(app_shim_requirement_string.get());
}

// Returns whether |app_shim_audit_token|'s code signature is trusted:
// - True if the framework bundle is unsigned (there's nothing to verify).
// - True if |app_shim_audit_token| satisfies the constructed designated
// requirement tailored for the app shim based on the framework bundle's
// requirement.
// - False otherwise (|app_shim_audit_token| does not satisfy the constructed
// designated requirement).
bool IsAcceptablyCodeSignedLegacy(audit_token_t app_shim_audit_token) {
  static base::NoDestructor<
      base::expected<base::apple::ScopedCFTypeRef<SecRequirementRef>,
                     apps::MissingRequirementReason>>
      app_shim_requirement(CreateAppShimRequirement());
  if (!app_shim_requirement->has_value()) {
    switch (app_shim_requirement->error()) {
      case apps::MissingRequirementReason::NoOrAdHocSignature:
        // App shim validation is not required because framework bundle is not
        // code-signed or is ad-hoc code-signed.
        return true;
      case apps::MissingRequirementReason::Error:
        // Framework bundle is code-signed however we were unable to create the
        // app shim requirement. Deny.
        // apps::AppShimManager::BuildAppShimRequirementStringFromFrameworkRequirementString
        // already did the base::debug::DumpWithoutCrashing, possibly on a
        // previous call. We can return false here without any additional
        // explanation.
        return false;
    }
  }

  OSStatus status = base::mac::ProcessIsSignedAndFulfillsRequirement(
      app_shim_audit_token, app_shim_requirement->value().get());
  if (status != errSecSuccess) {
    if (status == errSecCSReqFailed &&
        AppShimRegistry::Get()->HasSavedAnyCdHashes()) {
      // errSecCSReqFailed is most likely a result of opening an ad-hoc signed
      // app shim after leaving the ad-hoc signing experiment group.
      // Log the error but skip `DumpWithoutCrashing`.
      OSSTATUS_LOG(ERROR, status) << "SecCodeCheckValidity";
    } else {
      DumpOSStatusError(status, "SecCodeCheckValidity");
    }
    return false;
  }
  return true;
}

// Returns whether |app_shim_code|'s code directory hash matches the value
// that was saved when the app was signed.
bool VerifyCodeDirectoryHash(
    base::apple::ScopedCFTypeRef<SecCodeRef> app_shim_code) {
  base::apple::ScopedCFTypeRef<CFDictionaryRef> app_shim_info;
  OSStatus status = SecCodeCopySigningInformation(
      app_shim_code.get(), kSecCSSigningInformation,
      app_shim_info.InitializeInto());
  if (status != errSecSuccess) {
    DumpOSStatusError(status, "SecCodeCopySigningInformation");
    return false;
  }

  CFDataRef cd_hash = base::apple::GetValueFromDictionary<CFDataRef>(
      app_shim_info.get(), kSecCodeInfoUnique);

  CFDictionaryRef info_plist =
      base::apple::GetValueFromDictionary<CFDictionaryRef>(app_shim_info.get(),
                                                           kSecCodeInfoPList);
  if (!info_plist) {
    return false;
  }

  CFStringRef app_id = base::apple::GetValueFromDictionary<CFStringRef>(
      info_plist, CFSTR("CrAppModeShortcutID"));
  if (!app_id) {
    return false;
  }

  return AppShimRegistry::Get()->VerifyCdHashForApp(
      base::SysCFStringRefToUTF8(app_id), base::apple::CFDataToSpan(cd_hash));
}

// Returns whether |app_shim_audit_token|'s code signature is trusted. The
// verification consists of:
//  - verifying the signature is valid.
//  - verifying the code directory hash in the signature matches the value
//    stored for this app at signing time.
bool IsAcceptablyAdHocCodeSigned(audit_token_t app_shim_audit_token) {
  base::apple::ScopedCFTypeRef<CFDataRef> audit_token_cf(CFDataCreate(
      nullptr, reinterpret_cast<const UInt8*>(&app_shim_audit_token),
      sizeof(audit_token_t)));
  const void* app_shim_attribute_keys[] = {kSecGuestAttributeAudit};
  const void* app_shim_attribute_values[] = {audit_token_cf.get()};
  base::apple::ScopedCFTypeRef<CFDictionaryRef> app_shim_attributes(
      CFDictionaryCreate(
          nullptr, app_shim_attribute_keys, app_shim_attribute_values,
          std::size(app_shim_attribute_keys), &kCFTypeDictionaryKeyCallBacks,
          &kCFTypeDictionaryValueCallBacks));
  base::apple::ScopedCFTypeRef<SecCodeRef> app_shim_code;
  OSStatus status = SecCodeCopyGuestWithAttributes(
      nullptr, app_shim_attributes.get(), kSecCSDefaultFlags,
      app_shim_code.InitializeInto());
  if (status != errSecSuccess) {
    DumpOSStatusError(status, "SecCodeCopyGuestWithAttributes");
    return false;
  }
  status =
      SecCodeCheckValidity(app_shim_code.get(), kSecCSDefaultFlags, nullptr);
  if (status != errSecSuccess) {
    DumpOSStatusError(status, "SecCodeCheckValidity");
    return false;
  }

  return VerifyCodeDirectoryHash(app_shim_code);
}

bool ProfileMenuItemComparator(const chrome::mojom::ProfileMenuItemPtr& a,
                               const chrome::mojom::ProfileMenuItemPtr& b) {
  return a->menu_index < b->menu_index;
}

// Used by tests to be informed when launching an app shim has finished.
base::OnceClosure& GetShimStartupDoneCallback() {
  static base::NoDestructor<base::OnceClosure> instance;
  return *instance;
}

base::OnceClosure TakeShimStartupDoneCallbackOrDoNothing() {
  if (GetShimStartupDoneCallback()) {
    return std::move(GetShimStartupDoneCallback());
  }
  return base::DoNothing();
}

}  // namespace

namespace apps {

bool AppShimManager::AppShimObserver::OnNotificationAction(
    mac_notifications::mojom::NotificationActionInfoPtr& info) {
  return true;
}

void SetMacShimStartupDoneCallbackForTesting(base::OnceClosure callback) {
  DCHECK(!GetShimStartupDoneCallback());
  GetShimStartupDoneCallback() = std::move(callback);
}

base::OnceClosure TakeShimStartupDoneCallbackForTesting() {
  return std::move(GetShimStartupDoneCallback());
}

// The state for an individual (app, Profile) pair. This includes the
// AppShimHost.
struct AppShimManager::ProfileState {
  ProfileState(AppShimManager::AppState* in_app_state,
               std::unique_ptr<AppShimHost> in_single_profile_host);
  ProfileState(const ProfileState&) = delete;
  ProfileState& operator=(const ProfileState&) = delete;
  ~ProfileState() = default;

  AppShimHost* GetHost() const;

  // Weak, owns |this|.
  const raw_ptr<AppShimManager::AppState> app_state;

  // The AppShimHost for apps that are not multi-profile.
  const std::unique_ptr<AppShimHost> single_profile_host;

  // All browser instances for this (app, Profile) pair.
  std::set<Browser*> browsers;

  // The current BadgeValue for this (app, Profile) pair.
  std::optional<badging::BadgeManager::BadgeValue> badge;
};

// The state for an individual app. This includes the state for all
// profiles that are using the app.
struct AppShimManager::AppState {
  AppState(const webapps::AppId& app_id,
           std::unique_ptr<AppShimHost> multi_profile_host)
      : app_id(app_id), multi_profile_host(std::move(multi_profile_host)) {}
  AppState(const AppState&) = delete;
  AppState& operator=(const AppState&) = delete;
  ~AppState() = default;

  bool IsMultiProfile() const;

  // Return true if the app state should be deleted (e.g, because all profiles
  // have closed).
  bool ShouldDeleteAppState() const;

  // Mark the last-active profiles in AppShimRegistry, so that they will re-open
  // when the app is started next. Does nothing if this isn't a multi-profile
  // app, or if `did_save_last_active_profiles_on_terminate` is true.
  void MaybeSaveLastActiveProfiles() const;

  const std::string app_id;

  // Multi-profile apps share the same shim process across multiple profiles.
  const std::unique_ptr<AppShimHost> multi_profile_host;

  // The profile state for the profiles currently running this app.
  std::map<Profile*, std::unique_ptr<ProfileState>> profiles;

  // When an app is terminated, we only want to save the last active profiles
  // once. This field is set to true when a clean shutdown has already saved
  // last active profiles, to prevent the code that exists to handle unclean
  // shutdowns from overwriting the last active profiles. In case of a clean
  // shutdown some browser windows/profiles might have already closed by the
  // time OnShimProcessDisconnected runs.
  bool did_save_last_active_profiles_on_terminate = false;

  // Sometimes, for example when we have a pending notification permission
  // prompt, we want to keep alive an app shim process even though no windows
  // are open. This counter keep tracks of the number of outstanding
  // ScopedAppShimKeepAlive instances.
  int keep_alive_count = 0;
};

AppShimManager::ProfileState::ProfileState(
    AppShimManager::AppState* in_app_state,
    std::unique_ptr<AppShimHost> in_single_profile_host)
    : app_state(in_app_state),
      single_profile_host(std::move(in_single_profile_host)) {
  // Assert that the ProfileState and AppState agree about whether or not this
  // is a multi-profile shim.
  DCHECK_NE(!!single_profile_host, !!app_state->multi_profile_host);
}

AppShimHost* AppShimManager::ProfileState::GetHost() const {
  if (app_state->multi_profile_host)
    return app_state->multi_profile_host.get();
  return single_profile_host.get();
}

bool AppShimManager::AppState::IsMultiProfile() const {
  return multi_profile_host.get();
}

bool AppShimManager::AppState::ShouldDeleteAppState() const {
  // The new behavior for multi-profile apps is to not close the app based on
  // which windows are open. Rather, the app must be explicitly closed via
  // the Quit menu, which will terminate the app (and the browser will be
  // notified of the closed mojo pipe). The app is closed automatically when
  // it has been uninstalled for all profiles.
  // https://crbug.com/1080729 for new behavior.
  // https://crbug.com/1139254,1132223 for closing when profiles close.
  if (IsMultiProfile() &&
      base::FeatureList::IsEnabled(features::kAppShimNewCloseBehavior)) {
    // This might get called late enough during shutdown for ProfileManager to
    // no longer exist. GetInstalledProfilesForApp requires ProfileManager to
    // still exist, so if we're shutting down, just return true.
    if (g_browser_process->IsShuttingDown()) {
      return true;
    }
    return profiles.empty() &&
           AppShimRegistry::Get()->GetInstalledProfilesForApp(app_id).empty();
  }

  // The old behavior, and the behavior for single-profile apps, is to close
  // only when all profiles are closed.
  return profiles.empty() && keep_alive_count == 0;
}

void AppShimManager::AppState::MaybeSaveLastActiveProfiles() const {
  if (!IsMultiProfile() || did_save_last_active_profiles_on_terminate) {
    return;
  }
  std::set<base::FilePath> last_active_profile_paths;
  for (auto iter_profile = profiles.begin(); iter_profile != profiles.end();
       ++iter_profile) {
    last_active_profile_paths.insert(iter_profile->first->GetPath());
  }
  AppShimRegistry::Get()->SaveLastActiveProfilesForApp(
      app_id, last_active_profile_paths);
}

class ScopedAppShimKeepAlive {
 public:
  ScopedAppShimKeepAlive(AppShimManager* manager, const webapps::AppId& app_id);
  ~ScopedAppShimKeepAlive();

  ScopedAppShimKeepAlive(const ScopedAppShimKeepAlive&) = delete;
  ScopedAppShimKeepAlive& operator=(const ScopedAppShimKeepAlive&) = delete;

 private:
  base::WeakPtr<AppShimManager> manager_;
  const webapps::AppId app_id_;
};

ScopedAppShimKeepAlive::ScopedAppShimKeepAlive(AppShimManager* manager,
                                               const webapps::AppId& app_id)
    : manager_(manager->weak_factory_.GetWeakPtr()), app_id_(app_id) {
  auto app = manager_->apps_.find(app_id_);
  CHECK(app != manager_->apps_.end());
  app->second->keep_alive_count++;
}

ScopedAppShimKeepAlive::~ScopedAppShimKeepAlive() {
  if (manager_) {
    auto app = manager_->apps_.find(app_id_);
    if (app != manager_->apps_.end()) {
      CHECK_GT(app->second->keep_alive_count, 0);
      app->second->keep_alive_count--;
    }
  }
}

AppShimManager::AppShimManager(std::unique_ptr<Delegate> delegate)
    : delegate_(std::move(delegate)),
      profile_manager_(g_browser_process->profile_manager()),
      weak_factory_(this) {
  AppShimHostBootstrap::SetClient(this);
  if (profile_manager_)
    profile_manager_->AddObserver(this);
  BrowserList::AddObserver(this);
}

AppShimManager::~AppShimManager() {
  BrowserList::RemoveObserver(this);
  AppShimHostBootstrap::SetClient(nullptr);
}

void AppShimManager::OnProfileManagerDestroying() {
  avatar_menu_.reset();
  if (profile_manager_)
    profile_manager_->RemoveObserver(this);
  profile_manager_ = nullptr;
  weak_factory_.InvalidateWeakPtrs();
}

AppShimHost* AppShimManager::FindHost(Profile* profile,
                                      const webapps::AppId& app_id) {
  auto found_app = apps_.find(app_id);
  if (found_app == apps_.end())
    return nullptr;
  AppState* app_state = found_app->second.get();
  auto found_profile = app_state->profiles.find(profile);
  if (found_profile == app_state->profiles.end())
    return nullptr;
  ProfileState* profile_state = found_profile->second.get();
  return profile_state->GetHost();
}

bool AppShimManager::HasNonBookmarkAppWindowsOpen() {
  return delegate_->HasNonBookmarkAppWindowsOpen();
}

void AppShimManager::UpdateAppBadge(
    Profile* profile,
    const webapps::AppId& app_id,
    const std::optional<badging::BadgeManager::BadgeValue>& badge) {
  // TODO(crbug.com/40761338): Support updating the app badge for apps
  // that aren't currently running.
  auto found_app = apps_.find(app_id);
  if (found_app == apps_.end()) {
    return;
  }
  AppState* app_state = found_app->second.get();
  DCHECK(app_state);
  auto found_profile = app_state->profiles.find(profile);
  if (found_profile == app_state->profiles.end()) {
    return;
  }
  ProfileState* profile_state = found_profile->second.get();
  DCHECK(profile_state);

  profile_state->badge = badge;
  UpdateApplicationBadge(profile_state);
}

mojo::Remote<mac_notifications::mojom::MacNotificationProvider>
AppShimManager::LaunchNotificationProvider(const webapps::AppId& app_id) {
  CHECK(web_app::UseNotificationAttributionForWebAppShims());

  mojo::Remote<mac_notifications::mojom::MacNotificationProvider> remote;
  auto bind_provider = base::BindOnce(
      [](mojo::PendingReceiver<
             mac_notifications::mojom::MacNotificationProvider> receiver,
         base::WeakPtr<AppShimManager> manager, AppShimHost* host) {
        if (!host) {
          LOG(ERROR) << "Failed to launch app shim for notifications";
          if (manager) {
            manager->dummy_notification_provider_receivers_.Add(
                manager.get(), std::move(receiver));
          }
          return;
        }
        host->GetAppShim()->BindNotificationProvider(std::move(receiver));
      },
      remote.BindNewPipeAndPassReceiver(), weak_factory_.GetWeakPtr());

  auto found_app = apps_.find(app_id);
  if (found_app == apps_.end()) {
    // To check or display a notification associated with a specific app, calls
    // to the notifications API need to happen from within that app. If we don't
    // already have a running app shim, launch a new one, but launch it in
    // "background" mode, so to the user it isn't noticeable that this is
    // happening.
    LaunchShimInBackgroundMode(app_id, std::move(bind_provider));
    return remote;
  }

  AppState* app_state = found_app->second.get();
  CHECK(app_state->IsMultiProfile());
  AppShimHost* shim = app_state->multi_profile_host.get();
  std::move(bind_provider).Run(shim);
  return remote;
}

void AppShimManager::ShowNotificationPermissionRequest(
    const webapps::AppId& app_id,
    RequestNotificationPermissionCallback callback) {
  CHECK(web_app::UseNotificationAttributionForWebAppShims());

  if (notification_permission_result_for_testing_.has_value()) {
    std::move(callback).Run(*notification_permission_result_for_testing_);
    return;
  }

  auto request_permission = base::BindOnce(
      [](base::WeakPtr<AppShimManager> manager, const webapps::AppId& app_id,
         RequestNotificationPermissionCallback callback, AppShimHost* host) {
        if (!host) {
          LOG(ERROR)
              << "Failed to launch app shim for notifications permissions";
          std::move(callback).Run(mac_notifications::mojom::
                                      RequestPermissionResult::kRequestFailed);
          return;
        }
        std::unique_ptr<ScopedAppShimKeepAlive> keep_alive;
        if (manager) {
          keep_alive =
              std::make_unique<ScopedAppShimKeepAlive>(manager.get(), app_id);
        }
        // Wrap callback with default invoke to correctly report a failure if
        // the app shim fails to launch.
        host->GetAppShim()->RequestNotificationPermission(
            mojo::WrapCallbackWithDefaultInvokeIfNotRun(
                std::move(callback).Then(base::OnceClosure(
                    base::DoNothingWithBoundArgs(std::move(keep_alive)))),
                mac_notifications::mojom::RequestPermissionResult::
                    kRequestFailed));
      },
      weak_factory_.GetWeakPtr(), app_id, std::move(callback));

  auto found_app = apps_.find(app_id);
  if (found_app == apps_.end()) {
    LaunchShimInBackgroundMode(app_id, std::move(request_permission));
    return;
  }

  AppState* app_state = found_app->second.get();
  CHECK(app_state->IsMultiProfile());
  std::move(request_permission).Run(app_state->multi_profile_host.get());
}

Profile* AppShimManager::ProfileForBackgroundShimLaunch(
    const webapps::AppId& app_id) {
  if (profile_manager_) {
    for (Profile* p : profile_manager_->GetLoadedProfiles()) {
      if (!p->IsRegularProfile()) {
        continue;
      }
      if (delegate_->AppIsInstalled(p, app_id)) {
        return p;
      }
    }
  }
  return nullptr;
}

void AppShimManager::LaunchShimInBackgroundMode(
    const webapps::AppId& app_id,
    base::OnceCallback<void(AppShimHost*)> callback) {
  // A shim can only be launched through an active profile, so find a profile
  // through which to do the launch. This method should only be called for
  // multi-profile apps, for which an arbitrary profile is good enough.
  Profile* profile = ProfileForBackgroundShimLaunch(app_id);

  if (!profile) {
    LOG(ERROR) << "Failed to find loaded profile with " << app_id
               << " installed";
    std::move(callback).Run(nullptr);
    return;
  }

  CHECK(delegate_->AppIsMultiProfile(profile, app_id));
  auto* profile_state = GetOrCreateProfileState(profile, app_id);
  std::move(callback).Run(profile_state->GetHost());
  profile_state->GetHost()->LaunchShim(web_app::ShimLaunchMode::kBackground);
}

void AppShimManager::BindNotificationService(
    mojo::PendingReceiver<mac_notifications::mojom::MacNotificationService>
        service,
    mojo::PendingRemote<mac_notifications::mojom::MacNotificationActionHandler>
        handler) {
  // Dummy MacNotificationProvider implementation. The notifications code that
  // ends up calling LaunchNotificationProvider expects to always get a
  // bound/connected MacNotificationProvider remote, so if we don't have an
  // app shim process to connect to, instead a remote bound to this is returned.
}

void AppShimManager::OnNotificationAction(
    mac_notifications::mojom::NotificationActionInfoPtr info) {
  if (!app_shim_observer_ || app_shim_observer_->OnNotificationAction(info)) {
    ProcessMacNotificationResponse(
        mac_notifications::NotificationStyle::kAppShim, std::move(info),
        notification_action_handler_receivers_.current_context());
  }

  auto it = bootstraps_pending_notification_actions_.find(
      notification_action_handler_receivers_.current_receiver());
  if (it != bootstraps_pending_notification_actions_.end()) {
    // ProcessMacNotificationResponse posts a task to the UI thread to handle
    // the response. OnShimProcessConnectedForRegisterOnly needs to run after
    // that task, so post a task here as well.
    content::GetUIThreadTaskRunner({})->PostTask(
        FROM_HERE,
        base::BindOnce(&AppShimManager::OnShimProcessConnectedForRegisterOnly,
                       base::Unretained(this), std::move(it->second)));
    bootstraps_pending_notification_actions_.erase(it);
  }
}

void AppShimManager::UpdateApplicationBadge(ProfileState* profile_state) {
  if (profile_state->single_profile_host &&
      profile_state->single_profile_host->GetAppShim()) {
    profile_state->single_profile_host->GetAppShim()->SetBadgeLabel(
        profile_state->badge
            ? badging::GetBadgeString(profile_state->badge.value())
            : "");
  } else if (profile_state->app_state->multi_profile_host &&
             profile_state->app_state->multi_profile_host->GetAppShim()) {
    std::optional<badging::BadgeManager::BadgeValue> combined_badge;
    for (const auto& [profile, state] : profile_state->app_state->profiles) {
      if (state->badge) {
        if (!combined_badge) {
          combined_badge.emplace();
        }
        if (state->badge->has_value()) {
          // Number badge, add to combined badge.
          if (!combined_badge->has_value()) {
            combined_badge->emplace(0);
          }
          combined_badge->value() += state->badge->value();
        }
      }
    }
    profile_state->app_state->multi_profile_host->GetAppShim()->SetBadgeLabel(
        combined_badge ? badging::GetBadgeString(combined_badge.value()) : "");
  }
}

AppShimHost* AppShimManager::GetHostForRemoteCocoaBrowser(Browser* browser) {
  const std::string app_id =
      web_app::GetAppIdFromApplicationName(browser->app_name());
  if (!delegate_->AppUsesRemoteCocoa(browser->profile(), app_id))
    return nullptr;
  auto* profile_state = GetOrCreateProfileState(browser->profile(), app_id);
  if (!profile_state)
    return nullptr;
  return profile_state->GetHost();
}

bool AppShimManager::BrowserUsesRemoteCocoa(Browser* browser) {
  const std::string app_id =
      web_app::GetAppIdFromApplicationName(browser->app_name());
  if (web_app::AppShimCreationAndLaunchDisabledForTest()) {
    return false;
  }
  return delegate_->AppUsesRemoteCocoa(browser->profile(), app_id);
}

void AppShimManager::OnShimLaunchRequested(
    AppShimHost* host,
    web_app::LaunchShimUpdateBehavior update_behavior,
    web_app::ShimLaunchMode launch_mode,
    apps::ShimLaunchedCallback launched_callback,
    apps::ShimTerminatedCallback terminated_callback) {
  // A shim can only be launched through an active profile, so find a profile
  // through which to do the launch. For multi-profile apps, select one
  // arbitrarily. For non-multi-profile apps, select the specified profile.
  Profile* profile = nullptr;
  {
    auto found_app = apps_.find(host->GetAppId());
    CHECK(found_app != apps_.end());
    AppState* app_state = found_app->second.get();
    if (app_state->IsMultiProfile()) {
      // It is possible for `profiles` to be empty if the profile was closed
      // while an initial launch attempt took place (and then failed, triggering
      // a second launch attempt). In that case, simply fail the second launch
      // as well.
      if (app_state->profiles.empty()) {
        LOG(ERROR)
            << "Attempting to launch shim for which no profiles are loaded.";
        std::move(terminated_callback).Run();
        return;
      }
      DCHECK(!app_state->profiles.empty());
      profile = app_state->profiles.begin()->first;
    } else {
      profile = ProfileForPath(host->GetProfilePath());
    }
  }

  // If `update_behavior` was set to possible recreate shims, it can happen that
  // the app got uninstalled while an initial launch attempt took place (and
  // failed). So check first if the app is still installed.
  // TODO(mek): Rather than this workaround, we should make sure to destroy
  // AppShimHost and terminate app shims when an app is uninstalled.
  if (web_app::RecreateShimsRequested(update_behavior) &&
      (!delegate_->AppIsInstalled(profile, host->GetAppId()) ||
       !AppShimRegistry::Get()->IsAppInstalledInProfile(host->GetAppId(),
                                                        profile->GetPath()))) {
    LOG(ERROR)
        << "Attempting to launch shim for an app that is no longer installed.";
    std::move(terminated_callback).Run();
    return;
  }

  delegate_->LaunchShim(profile, host->GetAppId(), update_behavior, launch_mode,
                        std::move(launched_callback),
                        std::move(terminated_callback));
}

void AppShimManager::OnShimProcessConnected(
    std::unique_ptr<AppShimHostBootstrap> bootstrap) {
  DCHECK(crx_file::id_util::IdIsValid(bootstrap->GetAppId()));
  if (app_shim_observer_) {
    app_shim_observer_->OnShimProcessConnected(bootstrap->GetAppShimPid());
  }

  auto notification_action_handler = bootstrap->TakeNotificationActionHandler();
  std::optional<mojo::ReceiverId> notification_action_receiver_id;
  if (web_app::UseNotificationAttributionForWebAppShims() &&
      notification_action_handler) {
    notification_action_receiver_id =
        notification_action_handler_receivers_.Add(
            this, std::move(notification_action_handler),
            bootstrap->GetAppId());
  }

  switch (bootstrap->GetLaunchType()) {
    case chrome::mojom::AppShimLaunchType::kNormal: {
      const base::FilePath profile_path = bootstrap->GetProfilePath();
      LoadAndLaunchAppParams params;
      params.app_id = bootstrap->GetAppId();
      params.files = bootstrap->GetLaunchFiles();
      params.urls = bootstrap->GetLaunchUrls();
      params.login_item_restore_state = bootstrap->GetLoginItemRestoreState();
      LoadAndLaunchAppCallback launch_callback = base::BindOnce(
          &AppShimManager::OnShimProcessConnectedAndAllLaunchesDone,
          weak_factory_.GetWeakPtr(), std::move(bootstrap));
      LoadAndLaunchApp(profile_path, params, std::move(launch_callback));
      break;
    }
    case chrome::mojom::AppShimLaunchType::kNotificationAction:
      if (web_app::UseNotificationAttributionForWebAppShims() &&
          notification_action_receiver_id.has_value()) {
        // Wait for the notification action to be handled before finishing up
        // the connection process to ensure Chrome and the App Shim stay alive
        // long enough.
        bootstraps_pending_notification_actions_.emplace(
            *notification_action_receiver_id, std::move(bootstrap));
        break;
      }
      [[fallthrough]];
    case chrome::mojom::AppShimLaunchType::kRegisterOnly:
      OnShimProcessConnectedForRegisterOnly(std::move(bootstrap));
      break;
  }
}

void AppShimManager::OnShimProcessConnectedForRegisterOnly(
    std::unique_ptr<AppShimHostBootstrap> bootstrap) {
  const webapps::AppId& app_id = bootstrap->GetAppId();
  DCHECK(bootstrap->GetLaunchType() ==
             chrome::mojom::AppShimLaunchType::kRegisterOnly ||
         bootstrap->GetLaunchType() ==
             chrome::mojom::AppShimLaunchType::kNotificationAction)
      << bootstrap->GetLaunchType();

  // Create a ProfileState the specified profile (if there is one). We should
  // not do this (if there exists no ProfileState, then the shim should just
  // exit), but many tests assume this behavior, and need to be updated.
  Profile* profile = ProfileForPath(bootstrap->GetProfilePath());
  bool app_installed = delegate_->AppIsInstalled(profile, app_id);
  if (profile && app_installed && delegate_->AppCanCreateHost(profile, app_id))
    GetOrCreateProfileState(profile, app_id);

  // Because this was a register-only launch, it must have been launched by
  // Chrome, and so there should probably still exist the ProfileState through
  // which the launch was originally done.
  ProfileState* profile_state = nullptr;
  auto found_app = apps_.find(app_id);
  if (found_app != apps_.end()) {
    AppState* app_state = found_app->second.get();
    if (app_state->IsMultiProfile()) {
      // While generally `profiles` should never be empty, sometimes we keep
      // alive app shims even when no profiles have windows open for the app
      // (for example when we have a pending notification permission request).
      if (!app_state->profiles.empty()) {
        profile_state = app_state->profiles.begin()->second.get();
      }
    } else {
      auto found_profile = app_state->profiles.find(profile);
      if (found_profile != app_state->profiles.end()) {
        profile_state = found_profile->second.get();
      }
    }
  }

  OnShimProcessConnectedAndAllLaunchesDone(
      std::move(bootstrap), profile_state,
      profile_state
          ? chrome::mojom::AppShimLaunchResult::kSuccess
          : chrome::mojom::AppShimLaunchResult::kSuccessAndDisconnect);
}

void AppShimManager::LoadAndLaunchAppForTesting(const webapps::AppId& app_id) {
  LoadAndLaunchAppParams params;
  params.app_id = app_id;
  LoadAndLaunchApp(/*profile_path=*/base::FilePath(), params,
                   base::DoNothing());
}

void AppShimManager::LoadAndLaunchApp(
    const base::FilePath& profile_path,
    const LoadAndLaunchAppParams& params,
    LoadAndLaunchAppCallback launch_callback) {
  // Before anything else, if this launch includes files or urls we need to
  // determine which profiles are capable of handling those files or urls.
  std::map<base::FilePath, int> profiles_with_handlers =
      GetProfilesWithMatchingHandlers(params);

  // Check to see if the app is already running for a profile compatible with
  // |profile_path|. If so, early-out.
  if (LoadAndLaunchApp_TryExistingProfileStates(
          profile_path, params, profiles_with_handlers, &launch_callback)) {
    // If we used an existing profile, |launch_callback| should have been run.
    DCHECK(!launch_callback);
    DCHECK(!GetShimStartupDoneCallback());
    return;
  }

  // Retrieve the list of last-active profiles. If there are no last-active
  // profiles (which is rare -- e.g, when the last-active profiles were
  // removed), then use all profiles for which the app is installed.
  std::set<base::FilePath> last_active_profile_paths =
      AppShimRegistry::Get()->GetLastActiveProfilesForApp(params.app_id);
  if (last_active_profile_paths.empty()) {
    last_active_profile_paths =
        AppShimRegistry::Get()->GetInstalledProfilesForApp(params.app_id);
  }

  // If a non-empty `profile_path` was specified, use that as first preferred
  // profile. If this is a file or protocol handler launch subsequently use the
  // best match from `profiles_with_handlers`. Otherwise append all profiles
  // from `last_active_profile_paths` to the list of profiles to launch.
  std::vector<base::FilePath> profile_paths_to_launch;
  if (!profile_path.empty())
    profile_paths_to_launch.push_back(profile_path);
  if (!profiles_with_handlers.empty()) {
    int best_score = 0;
    base::FilePath best_path;
    for (const auto& [profile, score] : profiles_with_handlers) {
      if (score > best_score) {
        best_score = score;
        best_path = profile;
      }
    }
    DCHECK(!best_path.empty());
    profile_paths_to_launch.push_back(best_path);
  } else {
    profile_paths_to_launch.insert(profile_paths_to_launch.end(),
                                   last_active_profile_paths.begin(),
                                   last_active_profile_paths.end());
  }

  // Attempt load all of the profiles in |profile_paths_to_launch|, and once
  // they're loaded (or have failed to load), call
  // OnShimProcessConnectedAndProfilesToLaunchLoaded.
  base::OnceClosure callback =
      base::BindOnce(&AppShimManager::LoadAndLaunchApp_OnProfilesAndAppReady,
                     weak_factory_.GetWeakPtr(), profile_paths_to_launch,
                     /*first_profile_is_from_bootstrap=*/!profile_path.empty(),
                     params, std::move(launch_callback));
  {
    // This will update |callback| to be a chain of callbacks that load the
    // profiles in |profile_paths_to_load|, one by one, using
    // LoadProfileAndApp, and then finally call the initial |callback|. This
    // may end up being async (if some profiles aren't loaded), or may be
    // synchronous (if all profiles happen to already be loaded).
    for (const auto& profile_path_to_launch : profile_paths_to_launch) {
      if (profile_path_to_launch.empty())
        continue;
      LoadProfileAndAppCallback callback_wrapped =
          base::BindOnce([](base::OnceClosure callback_to_wrap,
                            Profile*) { std::move(callback_to_wrap).Run(); },
                         std::move(callback));
      callback = base::BindOnce(
          &AppShimManager::LoadProfileAndApp, weak_factory_.GetWeakPtr(),
          profile_path_to_launch, params.app_id, std::move(callback_wrapped));
    }
  }
  std::move(callback).Run();
}

bool AppShimManager::LoadAndLaunchApp_TryExistingProfileStates(
    const base::FilePath& profile_path,
    const LoadAndLaunchAppParams& params,
    const std::map<base::FilePath, int>& profiles_with_handlers,
    LoadAndLaunchAppCallback* launch_callback) {
  auto found_app = apps_.find(params.app_id);
  if (found_app == apps_.end())
    return false;
  AppState* app_state = found_app->second.get();

  // Search for an existing ProfileState for this app.
  Profile* profile = nullptr;
  ProfileState* profile_state = nullptr;
  if (!profile_path.empty()) {
    // If |profile_path| is populated, then only retrieve that specified
    // profile's ProfileState.
    profile = ProfileForPath(profile_path);
    auto found_profile = app_state->profiles.find(profile);
    if (found_profile == app_state->profiles.end())
      return false;
    profile_state = found_profile->second.get();
  } else {
    // If no profile was specified, select the best option from the open
    // profiles in `profiles_with_handlers`. If there are profiles with handlers
    // yet none of them are currently open don't use an existing profile.
    if (!profiles_with_handlers.empty()) {
      int best_score = 0;
      for (const auto& [it_profile, it_profile_state] : app_state->profiles) {
        auto it = profiles_with_handlers.find(it_profile->GetPath());
        if (it != profiles_with_handlers.end()) {
          int score = it->second;
          if (score > best_score) {
            best_score = score;
            profile = it_profile;
            profile_state = it_profile_state.get();
          }
        }
      }
    } else {
      // If `profiles_with_handlers` is empty, either because `params` does not
      // contains files or urls, or because there are no profiles that can
      // handle the files or urls, select the first open profile encountered.
      // TODO(crbug.com/40570436): This should select the
      // most-recently-used profile, not the first profile encountered.
      auto it = app_state->profiles.begin();
      if (it != app_state->profiles.end()) {
        profile = it->first;
        profile_state = it->second.get();
      }
    }
  }
  if (!profile_state)
    return false;
  DCHECK(profile);

  // Launch the app, if appropriate.
  LoadAndLaunchApp_LaunchIfAppropriate(
      profile, profile_state, params, TakeShimStartupDoneCallbackOrDoNothing());

  std::move(*launch_callback)
      .Run(profile_state, chrome::mojom::AppShimLaunchResult::kSuccess);
  return true;
}

void AppShimManager::LoadAndLaunchApp_OnProfilesAndAppReady(
    const std::vector<base::FilePath>& profile_paths_to_launch,
    bool first_profile_is_from_bootstrap,
    const LoadAndLaunchAppParams& params,
    LoadAndLaunchAppCallback launch_callback) {
  // Launch all of the profiles in |profile_paths_to_launch|. Record the most
  // profile successfully launched in |launched_profile_state|, and the most
  // recent reason for a failure (if any) in |launch_result|.
  ProfileState* launched_profile_state = nullptr;
  auto launch_result = chrome::mojom::AppShimLaunchResult::kProfileNotFound;
  auto barrier = base::BarrierClosure(profile_paths_to_launch.size(),
                                      TakeShimStartupDoneCallbackOrDoNothing());

  for (size_t iter = 0; iter < profile_paths_to_launch.size(); ++iter) {
    base::ScopedClosureRunner launch_finished(barrier);
    const base::FilePath& profile_path = profile_paths_to_launch[iter];
    if (profile_path.empty()) {
      continue;
    }
    if (IsProfileLockedForPath(profile_path)) {
      launch_result = chrome::mojom::AppShimLaunchResult::kProfileLocked;
      continue;
    }
    Profile* profile = ProfileForPath(profile_path);
    if (!profile) {
      launch_result = chrome::mojom::AppShimLaunchResult::kProfileNotFound;
      continue;
    }
    if (!delegate_->AppIsInstalled(profile, params.app_id)) {
      launch_result = chrome::mojom::AppShimLaunchResult::kAppNotFound;
      continue;
    }

    // Create a ProfileState for this app, if appropriate (e.g, not for
    // open-in-a-tab bookmark apps).
    ProfileState* profile_state = nullptr;
    if (delegate_->AppCanCreateHost(profile, params.app_id)) {
      profile_state = GetOrCreateProfileState(profile, params.app_id);
    }

    // Launch the app, if appropriate.
    LoadAndLaunchApp_LaunchIfAppropriate(profile, profile_state, params,
                                         launch_finished.Release());

    // If we successfully created a profile state, save it for |bootstrap| to
    // connect to once all launches are done.
    if (profile_state) {
      launched_profile_state = profile_state;
    } else {
      launch_result = chrome::mojom::AppShimLaunchResult::kSuccessAndDisconnect;
    }

    // If files or urls were specified, only open one new window.
    // If this was the profile specified in the bootstrap, also stop here.
    if (params.HasFilesOrURLs() ||
        (first_profile_is_from_bootstrap && iter == 0)) {
      // Trigger barrier for remaining profiles we didn't launch in.
      for (size_t i = iter + 1; i < profile_paths_to_launch.size(); ++i) {
        barrier.Run();
      }

      break;
    }
  }

  // If we launched any profile, report success.
  if (launched_profile_state)
    launch_result = chrome::mojom::AppShimLaunchResult::kSuccess;

  std::move(launch_callback).Run(launched_profile_state, launch_result);
}

void AppShimManager::OnShimProcessConnectedAndAllLaunchesDone(
    std::unique_ptr<AppShimHostBootstrap> bootstrap,
    ProfileState* profile_state,
    chrome::mojom::AppShimLaunchResult result) {
  if (app_shim_observer_) {
    app_shim_observer_->OnShimProcessConnectedAndAllLaunchesDone(
        bootstrap->GetAppShimPid(), result);
  }

  // If the browser process was launched by the App Shim in hidden mode, the
  // browser process should not stay alive indefinitely after all Browser
  // instances have been closed. Calling ResetKeepAliveWhileHidden() lets
  // the browser process terminate itself when no more Browsers or other
  // ScopedKeepAlives exist.
  //
  // At this point, if chrome was launched by an App Shim we would have finished
  // creating any browser windows or other ScopedKeepAlive instances that
  // resulted from the app shim launch, so now is a good time to stop the
  // browser process from keeping itself alive indefinitely.
  app_controller_mac::ResetKeepAliveWhileHidden();

  // If we failed because the profile was locked, launch the profile manager.
  if (result == chrome::mojom::AppShimLaunchResult::kProfileLocked) {
    LaunchProfilePicker();
  } else {
    // If the app specified a URL, but we tried and failed to launch it, then
    // open that URL in a new browser window.
    if (result != chrome::mojom::AppShimLaunchResult::kSuccess &&
        result != chrome::mojom::AppShimLaunchResult::kSuccessAndDisconnect &&
        bootstrap->GetLaunchType() ==
            chrome::mojom::AppShimLaunchType::kNormal) {
      const GURL& url = bootstrap->GetAppURL();
      if (url.is_valid()) {
        OpenAppURLInBrowserWindow(bootstrap->GetProfilePath(), url);
      }
    }
  }

  // If we failed to find a AppShimHost (in a ProfileState) for |bootstrap|
  // to connect to, then quit the shim. This may not represent an actual
  // failure (e.g, open-in-a-tab bookmarks return kSuccessAndDisconnect).
  if (result != chrome::mojom::AppShimLaunchResult::kSuccess) {
    DCHECK(!profile_state);
    bootstrap->OnFailedToConnectToHost(result);
    return;
  }
  DCHECK(profile_state);
  AppShimHost* host = profile_state->GetHost();
  DCHECK(host);

  // If we already have a host attached (e.g, due to multiple launches racing),
  // close down the app shim that didn't win the race.
  if (host->HasBootstrapConnected()) {
    bootstrap->OnFailedToConnectToHost(
        chrome::mojom::AppShimLaunchResult::kDuplicateHost);
    return;
  }

  // If the connecting shim process doesn't have an acceptable code
  // signature, reject the connection and re-launch the shim. The internal
  // re-launch will likely fail, whereupon the shim will be recreated.
  if (!IsAcceptablyCodeSigned(bootstrap->GetAppShimAuditToken())) {
    LOG(ERROR) << "The attaching app shim's code signature is invalid.";
    bootstrap->OnFailedToConnectToHost(
        chrome::mojom::AppShimLaunchResult::kFailedValidation);
    host->LaunchShim();
    return;
  }

  host->OnBootstrapConnected(std::move(bootstrap));
}

void AppShimManager::LoadAndLaunchApp_LaunchIfAppropriate(
    Profile* profile,
    ProfileState* profile_state,
    const LoadAndLaunchAppParams& params,
    base::OnceClosure launch_finished_callback) {
  // If `params.files`, `params.urls` or `params.override_url` is non-empty,
  // then always do a launch to open the files or URL(s).
  bool do_launch = params.HasFilesOrURLs();

  // Otherwise, only launch if there are no open windows.
  // TODO(https://crbug.com/331931430): This code should ignore browsers that
  // are closing (where IsBrowserClosing() returns true), but doing so is
  // tricky.
  if (!do_launch) {
    bool had_windows = delegate_->ShowAppWindows(profile, params.app_id);
    if (!had_windows && profile_state && !profile_state->browsers.empty()) {
      // Try to activate the most recently used open window.
      BrowserList* browsers = BrowserList::GetInstance();
      Browser* browser = nullptr;
      for (auto it = browsers->begin_browsers_ordered_by_activation();
           it != browsers->end_browsers_ordered_by_activation(); ++it) {
        if ((*it)->profile() != profile) {
          continue;
        }
        if (!web_app::AppBrowserController::IsForWebApp(*it, params.app_id)) {
          continue;
        }
        browser = *it;
        break;
      }

      // If iterating the browsers by activation order didn't find any matching
      // windows fall back to showing an arbitrary one from our ProfileState
      // instead.
      if (!browser) {
        browser = *(profile_state->browsers.begin());
      }

      browser->window()->Show();
      had_windows = true;
    }

    if (!had_windows) {
      do_launch = true;
    }
  }

  if (do_launch) {
    delegate_->LaunchApp(profile, params.app_id, params.files, params.urls,
                         params.override_url, params.login_item_restore_state,
                         std::move(launch_finished_callback));
  } else {
    std::move(launch_finished_callback).Run();
  }
}

// static
AppShimManager* AppShimManager::Get() {
  // This will only return nullptr in certain unit tests that do not initialize
  // the app shim host manager.
  return g_browser_process->platform_part()->app_shim_manager();
}

void AppShimManager::LoadProfileAndApp(const base::FilePath& profile_path,
                                       const webapps::AppId& app_id,
                                       LoadProfileAndAppCallback callback) {
  // Run |profile_loaded_callback| when the profile is loaded (be that now, or
  // after having to asynchronously load the profile).
  auto profile_loaded_callback = base::BindOnce(
      &AppShimManager::LoadProfileAndApp_OnProfileLoaded,
      weak_factory_.GetWeakPtr(), profile_path, app_id, std::move(callback));
  if (auto* profile = ProfileForPath(profile_path))
    std::move(profile_loaded_callback).Run(profile);
  else
    LoadProfileAsync(profile_path, std::move(profile_loaded_callback));
}

void AppShimManager::LoadProfileAndApp_OnProfileLoaded(
    const base::FilePath& profile_path,
    const webapps::AppId& app_id,
    LoadProfileAndAppCallback callback,
    Profile* profile) {
  // It may be that the profile fails to load.
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!profile) {
    LOG(ERROR) << "Failed to load profile from " << profile_path.value() << ".";
    std::move(callback).Run(nullptr);
    return;
  }
  // Run |registry_ready_callback| when the WebAppProvider is ready (be that
  // now, or after a callback). Failing to do so will result in apps not
  // launching.
  // https://crbug.com/1094419.
  auto registry_ready_callback = base::BindOnce(
      &AppShimManager::LoadProfileAndApp_OnProfileAppRegistryReady,
      weak_factory_.GetWeakPtr(), profile_path, app_id, std::move(callback));
  WaitForAppRegistryReadyAsync(profile, std::move(registry_ready_callback));
}

void AppShimManager::LoadProfileAndApp_OnProfileAppRegistryReady(
    const base::FilePath& profile_path,
    const webapps::AppId& app_id,
    LoadProfileAndAppCallback callback) {
  // It may be that the profile was destroyed while waiting for the callback to
  // be issued.
  Profile* profile = ProfileForPath(profile_path);
  if (!profile) {
    std::move(callback).Run(nullptr);
    return;
  }
  // Run |app_enabled_callback| once the app is enabled (now or async). Note
  // that this is only relevant for extension-based apps.
  auto app_enabled_callback = base::BindOnce(
      &AppShimManager::LoadProfileAndApp_OnAppEnabled,
      weak_factory_.GetWeakPtr(), profile_path, app_id, std::move(callback));
  if (delegate_->AppIsInstalled(profile, app_id)) {
    std::move(app_enabled_callback).Run();
  } else {
    delegate_->EnableExtension(profile, app_id,
                               std::move(app_enabled_callback));
  }
}

void AppShimManager::LoadProfileAndApp_OnAppEnabled(
    const base::FilePath& profile_path,
    const webapps::AppId& app_id,
    LoadProfileAndAppCallback callback) {
  std::move(callback).Run(ProfileForPath(profile_path));
}

// UMA metric name for result of validating app shim signature.
constexpr const char* kAppShimSignatureValidationResult =
    "Apps.AppShimSignatureValidationResult";

// Result of validating app shim signature.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class SignatureValidationResult {
  kInvalidSignature = 0,
  kSuccessAdHoc = 1,
  kSuccessLegacy = 2,
  kExpectedAdHocGotLegacy = 3,
  kMaxValue = kExpectedAdHocGotLegacy,
};

// Records the result of validating the app shim code signature to UMA.
void RecordSignatureValidationResult(SignatureValidationResult result) {
  base::UmaHistogramEnumeration(kAppShimSignatureValidationResult, result);
}

bool AppShimManager::IsAcceptablyCodeSigned(audit_token_t audit_token) const {
  static const bool requires_adhoc_signature =
      web_app::UseAdHocSigningForWebAppShims();

  if (requires_adhoc_signature && IsAcceptablyAdHocCodeSigned(audit_token)) {
    RecordSignatureValidationResult(SignatureValidationResult::kSuccessAdHoc);
    return true;
  }

  if (IsAcceptablyCodeSignedLegacy(audit_token)) {
    if (requires_adhoc_signature) {
      RecordSignatureValidationResult(
          SignatureValidationResult::kExpectedAdHocGotLegacy);

      // Returning false to indicate that the signature is invalid will trigger
      // the recreation of the app shim app bundle. This will result in it
      // being re-signed with an ad-hoc signature as expected.
      return false;
    }

    RecordSignatureValidationResult(SignatureValidationResult::kSuccessLegacy);
    return true;
  }

  RecordSignatureValidationResult(SignatureValidationResult::kInvalidSignature);
  return false;
}

Profile* AppShimManager::ProfileForPath(const base::FilePath& full_path) {
  if (!profile_manager_)
    return nullptr;
  Profile* profile = profile_manager_->GetProfileByPath(full_path);

  // Use IsValidProfile to check if the profile has been created.
  return profile && profile_manager_->IsValidProfile(profile) ? profile
                                                              : nullptr;
}

void AppShimManager::LoadProfileAsync(
    const base::FilePath& full_path,
    base::OnceCallback<void(Profile*)> callback) {
  profile_manager_->LoadProfileByPath(full_path, false, std::move(callback));
}

void AppShimManager::WaitForAppRegistryReadyAsync(
    Profile* profile,
    base::OnceCallback<void()> callback) {
  auto* provider = web_app::WebAppProvider::GetForWebApps(profile);
  DCHECK(provider);
  if (provider->on_registry_ready().is_signaled())
    std::move(callback).Run();
  else
    provider->on_registry_ready().Post(FROM_HERE, std::move(callback));
}

bool AppShimManager::IsProfileLockedForPath(const base::FilePath& full_path) {
  return profiles::IsProfileLocked(full_path);
}

std::unique_ptr<AppShimHost> AppShimManager::CreateHost(
    AppShimHost::Client* client,
    const base::FilePath& profile_path,
    const webapps::AppId& app_id,
    bool use_remote_cocoa) {
  return std::make_unique<AppShimHost>(client, app_id, profile_path,
                                       use_remote_cocoa);
}

void AppShimManager::OpenAppURLInBrowserWindow(
    const base::FilePath& profile_path,
    const GURL& url) {
  Profile* profile =
      profile_path.empty() ? nullptr : ProfileForPath(profile_path);
  if (!profile)
    profile = profile_manager_->GetLastUsedProfile();
  if (!profile || Browser::GetCreationStatusForProfile(profile) !=
                      Browser::CreationStatus::kOk) {
    return;
  }
  Browser* browser = Browser::Create(
      Browser::CreateParams(Browser::TYPE_NORMAL, profile, true));
  browser->window()->Show();
  NavigateParams params(browser, url, ui::PAGE_TRANSITION_AUTO_BOOKMARK);
  params.tabstrip_add_types = AddTabTypes::ADD_ACTIVE;
  params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
  Navigate(&params);
}

void AppShimManager::LaunchProfilePicker() {
  ProfilePicker::Show(ProfilePicker::Params::FromEntryPoint(
      ProfilePicker::EntryPoint::kProfileLocked));
}

void AppShimManager::MaybeTerminate() {
  apps::AppShimTerminationManager::Get()->MaybeTerminate();
}

void AppShimManager::OnShimProcessDisconnected(AppShimHost* host) {
  const std::string app_id = host->GetAppId();

  auto found_app = apps_.find(app_id);
  CHECK(found_app != apps_.end());
  AppState* app_state = found_app->second.get();
  DCHECK(app_state);

  app_state->MaybeSaveLastActiveProfiles();

  // For multi-profile apps, just delete the AppState, which will take down
  // |host| and all profiles' state.
  if (app_state->IsMultiProfile()) {
    DCHECK_EQ(host, app_state->multi_profile_host.get());
    apps_.erase(found_app);
    if (apps_.empty())
      MaybeTerminate();
    return;
  }

  // For non-RemoteCocoa apps, close all of the windows only if the the shim
  // process has successfully connected (if it never connected, then let the
  // app run as normal).
  bool close_windows =
      !host->UsesRemoteViews() && host->HasBootstrapConnected();

  // Erase the ProfileState, which will delete |host|.
  Profile* profile = ProfileForPath(host->GetProfilePath());
  auto found_profile = app_state->profiles.find(profile);
  CHECK(found_profile != app_state->profiles.end());
  ProfileState* profile_state = found_profile->second.get();
  DCHECK_EQ(host, profile_state->single_profile_host.get());
  app_state->profiles.erase(found_profile);
  host = nullptr;

  // Erase |app_state| if this was the last profile.
  if (app_state->profiles.empty())
    apps_.erase(found_app);

  // Close app windows if we decided to do so above.
  if (close_windows)
    delegate_->CloseAppWindows(profile, app_id);
}

void AppShimManager::OnShimFocus(AppShimHost* host) {
  // This path is only for legacy apps (which are perforce single-profile).
  if (host->UsesRemoteViews())
    return;

  // Legacy apps don't own their own windows, so when we focus the app,
  // what we really want to do is focus the Chrome windows.
  Profile* profile = ProfileForPath(host->GetProfilePath());
  delegate_->ShowAppWindows(profile, host->GetAppId());
}

void AppShimManager::OnShimReopen(AppShimHost* host) {
  if (app_shim_observer_) {
    app_shim_observer_->OnShimReopen(host->GetAppShimPid());
  }
  auto found_app = apps_.find(host->GetAppId());
  CHECK(found_app != apps_.end());
  AppState* app_state = found_app->second.get();
  LoadAndLaunchAppParams params;
  params.app_id = host->GetAppId();
  LoadAndLaunchApp(
      app_state->IsMultiProfile() ? base::FilePath() : host->GetProfilePath(),
      params, base::DoNothing());
}

void AppShimManager::OnShimOpenedFiles(
    AppShimHost* host,
    const std::vector<base::FilePath>& files) {
  auto found_app = apps_.find(host->GetAppId());
  CHECK(found_app != apps_.end());
  AppState* app_state = found_app->second.get();
  LoadAndLaunchAppParams params;
  params.app_id = host->GetAppId();
  params.files = files;
  LoadAndLaunchApp(
      app_state->IsMultiProfile() ? base::FilePath() : host->GetProfilePath(),
      params, base::DoNothing());
  if (app_shim_observer_) {
    app_shim_observer_->OnShimOpenedURLs(host->GetAppShimPid());
  }
}

void AppShimManager::OnShimSelectedProfile(AppShimHost* host,
                                           const base::FilePath& profile_path) {
  LaunchAppInProfile(host->GetAppId(), profile_path);
}

void AppShimManager::LaunchAppInProfile(const webapps::AppId& app_id,
                                        const base::FilePath& profile_path) {
  LoadAndLaunchAppParams params;
  params.app_id = app_id;
  LoadAndLaunchApp(profile_path, params, base::DoNothing());
}

void AppShimManager::OnShimOpenedAppSettings(AppShimHost* host) {
  // Retrieve the list of last-active profiles. If there are no last-active
  // profiles (which is rare -- e.g, when the last-active profiles were
  // removed), then use all profiles for which the app is installed.
  std::set<base::FilePath> last_active_profile_paths =
      AppShimRegistry::Get()->GetLastActiveProfilesForApp(host->GetAppId());
  if (last_active_profile_paths.empty()) {
    last_active_profile_paths =
        AppShimRegistry::Get()->GetInstalledProfilesForApp(host->GetAppId());
  }
  if (last_active_profile_paths.empty()) {
    return;
  }
  // Open settings in the first of these profiles.
  LoadProfileAsync(
      *last_active_profile_paths.begin(),
      base::BindOnce(
          [](const webapps::AppId& app_id, Profile* profile) {
            if (profile) {
              chrome::ShowWebAppSettings(
                  profile, app_id,
                  web_app::AppSettingsPageEntryPoint::kBrowserCommand);
            }
          },
          host->GetAppId()));
}

void AppShimManager::OnShimOpenedUrls(AppShimHost* host,
                                      const std::vector<GURL>& urls) {
  auto found_app = apps_.find(host->GetAppId());
  CHECK(found_app != apps_.end());
  AppState* app_state = found_app->second.get();
  LoadAndLaunchAppParams params;
  params.app_id = host->GetAppId();
  params.urls = urls;
  LoadAndLaunchApp(
      app_state->IsMultiProfile() ? base::FilePath() : host->GetProfilePath(),
      params, base::DoNothing());
  if (app_shim_observer_) {
    app_shim_observer_->OnShimOpenedURLs(host->GetAppShimPid());
  }
}

void AppShimManager::OnShimOpenAppWithOverrideUrl(AppShimHost* host,
                                                  const GURL& override_url) {
  auto found_app = apps_.find(host->GetAppId());
  CHECK(found_app != apps_.end());
  AppState* app_state = found_app->second.get();
  LoadAndLaunchAppParams params;
  params.app_id = host->GetAppId();
  params.override_url = override_url;
  LoadAndLaunchApp(
      app_state->IsMultiProfile() ? base::FilePath() : host->GetProfilePath(),
      params, base::DoNothing());
}

void AppShimManager::OnShimWillTerminate(AppShimHost* host) {
  auto found_app = apps_.find(host->GetAppId());
  CHECK(found_app != apps_.end());
  AppState* app_state = found_app->second.get();
  DCHECK(app_state);

  auto* notification_bridge = static_cast<NotificationPlatformBridgeMac*>(
      g_browser_process->notification_platform_bridge());
  notification_bridge->AppShimWillTerminate(host->GetAppId());

  DCHECK(!app_state->did_save_last_active_profiles_on_terminate);
  app_state->MaybeSaveLastActiveProfiles();
  app_state->did_save_last_active_profiles_on_terminate = true;
}

void AppShimManager::OnNotificationPermissionStatusChanged(
    AppShimHost* host,
    mac_notifications::mojom::PermissionStatus status) {
  AppShimRegistry::Get()->SaveNotificationPermissionStatusForApp(
      host->GetAppId(), status);
}

void AppShimManager::OnProfileAdded(Profile* profile) {
  if (profile->IsOffTheRecord())
    return;

  // The app lifetime monitor service might not be available for some irregular
  // profiles, like the System Profile.
  if (AppLifetimeMonitor* app_lifetime_monitor =
          AppLifetimeMonitorFactory::GetForBrowserContext(profile)) {
    app_lifetime_monitor->AddObserver(this);
  }
}

void AppShimManager::OnProfileMarkedForPermanentDeletion(Profile* profile) {
  if (profile->IsOffTheRecord())
    return;

  // The app lifetime monitor service might not be available for some irregular
  // profiles, like the System Profile.
  if (AppLifetimeMonitor* app_lifetime_monitor =
          AppLifetimeMonitorFactory::GetForBrowserContext(profile)) {
    app_lifetime_monitor->RemoveObserver(this);
  }

  // Close app shims that were kept alive only for this profile.
  for (auto iter_app = apps_.begin(); iter_app != apps_.end();) {
    AppState* app_state = iter_app->second.get();
    if (app_state->ShouldDeleteAppState()) {
      iter_app = apps_.erase(iter_app);
    } else {
      ++iter_app;
    }
  }
}

void AppShimManager::OnAppStart(content::BrowserContext* context,
                                const std::string& app_id) {}

void AppShimManager::OnAppActivated(content::BrowserContext* context,
                                    const std::string& app_id) {
  Profile* profile = Profile::FromBrowserContext(context);
  if (!delegate_->AppIsInstalled(profile, app_id))
    return;
  if (auto* profile_state = GetOrCreateProfileState(profile, app_id))
    profile_state->GetHost()->LaunchShim();
}

void AppShimManager::OnAppDeactivated(content::BrowserContext* context,
                                      const std::string& app_id) {
  Profile* profile = static_cast<Profile*>(context);
  auto found_app = apps_.find(app_id);
  if (found_app != apps_.end()) {
    AppState* app_state = found_app->second.get();
    auto found_profile = app_state->profiles.find(profile);
    if (found_profile != app_state->profiles.end()) {
      if (app_state->profiles.size() == 1) {
        app_state->MaybeSaveLastActiveProfiles();
      }
      app_state->profiles.erase(found_profile);
      if (app_state->ShouldDeleteAppState()) {
        apps_.erase(found_app);
      }
    }
  }

  if (apps_.empty())
    MaybeTerminate();

  // Check the integrity of AppState::profiles across all apps. Include the app
  // ID in the dump, to help pin down the cause.
  //
  // TODO(crbug.com/40217091): Remove this once we're confident this never
  // happens.
  std::string inconsistent_app_ids;
  for (const auto& [id, state] : apps_) {
    if (state->ShouldDeleteAppState()) {
      inconsistent_app_ids += id + " ";
    }
  }
  if (!inconsistent_app_ids.empty())
    DumpError(inconsistent_app_ids);
}

void AppShimManager::OnAppStop(content::BrowserContext* context,
                               const std::string& app_id) {}

void AppShimManager::OnBrowserAdded(Browser* browser) {
  Profile* profile = browser->profile();
  const std::string app_id =
      web_app::GetAppIdFromApplicationName(browser->app_name());
  if (!delegate_->AppUsesRemoteCocoa(profile, app_id))
    return;
  if (auto* profile_state = GetOrCreateProfileState(profile, app_id)) {
    profile_state->browsers.insert(browser);
    if (profile_state->browsers.size() == 1)
      OnAppActivated(browser->profile(), app_id);
  }
}

void AppShimManager::OnBrowserRemoved(Browser* browser) {
  // We can't call OnAppDeactivated() while iterating on |apps_|. It would
  // invalidate the iterator.
  std::vector<std::string> apps_to_deactivate;

  for (const auto& [app_id, app_state] : apps_) {
    for (const auto& [profile, profile_state] : app_state->profiles) {
      auto found = profile_state->browsers.find(browser);
      if (found != profile_state->browsers.end()) {
        // If we have no browser windows open after erasing this window, then
        // close the ProfileState (and potentially the shim as well).
        profile_state->browsers.erase(found);
        if (profile_state->browsers.empty())
          apps_to_deactivate.push_back(app_id);
        break;  // Break to outer loop.
      }
    }
  }

  for (const std::string& app_id : apps_to_deactivate)
    OnAppDeactivated(browser->profile(), app_id);
}

void AppShimManager::OnBrowserSetLastActive(Browser* browser) {
  // Rebuild the profile menu items (to ensure that the checkmark in the menu
  // is next to the new-active item).
  if (avatar_menu_)
    avatar_menu_->ActiveBrowserChanged(browser);
  UpdateAllProfileMenus();

  // Update the application dock menu for the current profile.
  const std::string app_id =
      web_app::GetAppIdFromApplicationName(browser->app_name());
  if (!delegate_->AppUsesRemoteCocoa(browser->profile(), app_id))
    return;
  auto* profile_state = GetOrCreateProfileState(browser->profile(), app_id);
  if (profile_state)
    UpdateApplicationDockMenu(browser->profile(), profile_state);
}

void AppShimManager::OnProfileWillBeDestroyed(Profile* profile) {
  profile_observation_.RemoveObservation(profile);

  // Clean up dangling Profile pointers. This can happen in rare cases, if a
  // Browser is never created for a particular Profile. In those cases,
  // OnBrowserRemoved() never runs, and it doesn't clean up AppState::profiles.
  //
  // Use the same pattern as in OnBrowserRemoved() to avoid invalidating the
  // iterator.
  std::vector<std::string> apps_to_deactivate;

  for (const auto& [app_id, app_state] : apps_) {
    auto found = app_state->profiles.find(profile);
    if (found != app_state->profiles.end()) {
      CHECK(found->second->browsers.empty());
      apps_to_deactivate.push_back(app_id);
    }
  }

  for (const std::string& app_id : apps_to_deactivate)
    OnAppDeactivated(profile, app_id);
}

void AppShimManager::OnAppLaunchCancelled(content::BrowserContext* context,
                                          const std::string& app_id) {
  auto found_app = apps_.find(app_id);
  if (found_app == apps_.end())
    return;

  Profile* profile = static_cast<Profile*>(context);
  AppState* app_state = found_app->second.get();
  auto found_profile = app_state->profiles.find(profile);
  if (found_profile == app_state->profiles.end())
    return;

  // If there are no browser windows open, then close the ProfileState
  // (and potentially the shim as well).
  ProfileState* profile_state = found_profile->second.get();
  if (profile_state->browsers.empty())
    OnAppDeactivated(context, app_id);
}

void AppShimManager::UpdateAllProfileMenus() {
  RebuildProfileMenuItemsFromAvatarMenu();
  for (auto& iter_app : apps_) {
    AppState* app_state = iter_app.second.get();
    if (app_state->IsMultiProfile())
      UpdateAppProfileMenu(app_state);
  }
}

void AppShimManager::RebuildProfileMenuItemsFromAvatarMenu() {
  if (!avatar_menu_) {
    avatar_menu_ = std::make_unique<AvatarMenu>(
        &profile_manager_->GetProfileAttributesStorage(), this, nullptr);
  }
  avatar_menu_->RebuildMenu();
  profile_menu_items_.clear();
  for (size_t i = 0; i < avatar_menu_->GetNumberOfItems(); ++i) {
    auto mojo_item = chrome::mojom::ProfileMenuItem::New();
    const AvatarMenu::Item& item = avatar_menu_->GetItemAt(i);
    mojo_item->name = item.name;
    mojo_item->menu_index = item.menu_index;
    mojo_item->active = item.active;
    mojo_item->profile_path = item.profile_path;
    mojo_item->icon =
        profiles::GetAvatarIconForNSMenu(item.profile_path).ToImageSkia()[0];
    profile_menu_items_.push_back(std::move(mojo_item));
  }
}

void AppShimManager::OnAvatarMenuChanged(AvatarMenu* menu) {
  // Rebuild the profile menu to reflect changes (e.g, added or removed
  // profiles).
  DCHECK_EQ(avatar_menu_.get(), menu);
  UpdateAllProfileMenus();
}

void AppShimManager::UpdateAppProfileMenu(AppState* app_state) {
  DCHECK(app_state->IsMultiProfile());
  // Include in |items| the profiles from |profile_menu_items_| for which this
  // app is installed, sorted by |menu_index|.
  std::vector<chrome::mojom::ProfileMenuItemPtr> items;
  auto installed_profiles =
      AppShimRegistry::Get()->GetInstalledProfilesForApp(app_state->app_id);
  for (const auto& item : profile_menu_items_) {
    if (installed_profiles.count(item->profile_path))
      items.push_back(item->Clone());
  }
  std::sort(items.begin(), items.end(), ProfileMenuItemComparator);

  // Do not show a profile menu unless it has at least 2 entries (that is, the
  // app is available for at least 2 profiles).
  if (items.size() < 2)
    items.clear();

  // Send the profile menu to the app shim process.
  app_state->multi_profile_host->GetAppShim()->UpdateProfileMenu(
      std::move(items));
}

void AppShimManager::UpdateApplicationDockMenu(Profile* profile,
                                               ProfileState* profile_state) {
  AppState* app_state = profile_state->app_state;
  // Send the application dock menu to the app shim process.
  profile_state->GetHost()->GetAppShim()->UpdateApplicationDockMenu(
      delegate_->GetAppShortcutsMenuItemInfos(profile, app_state->app_id));
}

AppShimManager::ProfileState* AppShimManager::GetOrCreateProfileState(
    Profile* profile,
    const webapps::AppId& app_id) {
  if (web_app::AppShimCreationAndLaunchDisabledForTest()) {
    return nullptr;
  }

  const bool is_multi_profile = delegate_->AppIsMultiProfile(profile, app_id);
  const base::FilePath profile_path =
      is_multi_profile ? base::FilePath() : profile->GetPath();
  const bool use_remote_cocoa = delegate_->AppUsesRemoteCocoa(profile, app_id);

  auto found_app = apps_.find(app_id);
  if (found_app == apps_.end()) {
    std::unique_ptr<AppShimHost> multi_profile_host;
    if (is_multi_profile) {
      multi_profile_host =
          CreateHost(this, profile_path, app_id, use_remote_cocoa);
    }
    auto new_app_state =
        std::make_unique<AppState>(app_id, std::move(multi_profile_host));
    found_app =
        apps_.insert(std::make_pair(app_id, std::move(new_app_state))).first;
  }
  AppState* app_state = found_app->second.get();

  // Initialize the profile menu.
  if (is_multi_profile)
    UpdateAppProfileMenu(app_state);

  auto found_profile = app_state->profiles.find(profile);
  if (found_profile == app_state->profiles.end()) {
    std::unique_ptr<AppShimHost> single_profile_host;
    if (!is_multi_profile) {
      single_profile_host =
          CreateHost(this, profile_path, app_id, use_remote_cocoa);
    }
    auto new_profile_state = std::make_unique<ProfileState>(
        app_state, std::move(single_profile_host));
    found_profile =
        app_state->profiles
            .insert(std::make_pair(profile, std::move(new_profile_state)))
            .first;
  }

  // Listen for OnProfileWillBeDestroyed(), but not more than once per Profile.
  // O(n), where n is the number of loaded Profiles (AKA a very small number).
  if (!profile_observation_.IsObservingSource(profile))
    profile_observation_.AddObservation(profile);

  return found_profile->second.get();
}

std::map<base::FilePath, int> AppShimManager::GetProfilesWithMatchingHandlers(
    const LoadAndLaunchAppParams& params) {
  if (!params.HasFilesOrURLs())
    return {};
  std::map<base::FilePath, int> result;

  // Files can be passed both as files or as file:// URLs, so gather all
  // the files from both.
  std::vector<base::FilePath> files = params.files;
  GURL protocol_handler_url;
  for (const GURL& url : params.urls) {
    // Ignore invalid URLs.
    if (!url.is_valid() || !url.has_scheme())
      continue;

    if (url.SchemeIsFile()) {
      base::FilePath file_path;
      if (net::FileURLToFilePath(url, &file_path))
        files.push_back(file_path);
      continue;
    }

    protocol_handler_url = url;
  }

  // For each profile with available handlers, count how many paths and/or
  // URLs those profiles can handle.
  std::map<base::FilePath, AppShimRegistry::HandlerInfo> handlers =
      AppShimRegistry::Get()->GetHandlersForApp(params.app_id);
  for (const auto& [profile, handler_info] : handlers) {
    int count = std::ranges::count_if(
        files, [&handler_info](const base::FilePath& file_path) {
          std::string file_extension =
              base::FilePath(file_path.Extension()).AsUTF8Unsafe();
          return file_extension.length() > 1 &&
                 base::Contains(handler_info.file_handler_extensions,
                                file_extension);
        });

    if (protocol_handler_url.is_valid() &&
        base::Contains(handler_info.protocol_handlers,
                       protocol_handler_url.scheme())) {
      count++;
    }

    if (count > 0)
      result[profile] = count;
  }
  return result;
}

AppShimManager::LoadAndLaunchAppParams::LoadAndLaunchAppParams() = default;

AppShimManager::LoadAndLaunchAppParams::~LoadAndLaunchAppParams() = default;

AppShimManager::LoadAndLaunchAppParams::LoadAndLaunchAppParams(
    const LoadAndLaunchAppParams&) = default;

AppShimManager::LoadAndLaunchAppParams&
AppShimManager::LoadAndLaunchAppParams::operator=(
    const LoadAndLaunchAppParams&) = default;

bool AppShimManager::LoadAndLaunchAppParams::HasFilesOrURLs() const {
  return !files.empty() || !urls.empty() || !override_url.is_empty();
}

base::apple::ScopedCFTypeRef<CFStringRef>
AppShimManager::BuildAppShimRequirementStringFromFrameworkRequirementString(
    CFStringRef framwork_requirement) {
  // Make sure the framework bundle requirement is in the expected format.
  // It should start with 'identifier "' and have at least 2 quotes. This allows
  // us to easily find the end of the "identifier" portion of the requirement so
  // we can swap in the desired app shim identifier leaving rest of the
  // requirement unmodified.
  CFIndex len = CFStringGetLength(framwork_requirement);
  base::apple::ScopedCFTypeRef<CFArrayRef> quote_ranges(
      CFStringCreateArrayWithFindResults(nullptr, framwork_requirement,
                                         CFSTR("\""), CFRangeMake(0, len), 0));
  if (!CFStringHasPrefix(framwork_requirement, CFSTR("identifier \"")) ||
      !quote_ranges || CFArrayGetCount(quote_ranges.get()) < 2) {
    DumpError("Framework bundle requirement is malformed.");
    return base::apple::ScopedCFTypeRef<CFStringRef>(nullptr);
  }

  // Get the index of the second quote.
  CFIndex second_quote_index =
      static_cast<const CFRange*>(CFArrayGetValueAtIndex(quote_ranges.get(), 1))
          ->location;

  // Make sure there is something to read after the second quote.
  if (second_quote_index + 1 >= len) {
    DumpError("Framework bundle requirement is too short");
    return base::apple::ScopedCFTypeRef<CFStringRef>(nullptr);
  }

  // Build the app shim requirement. Keep the data from the framework bundle
  // requirement starting after second quote.
  base::apple::ScopedCFTypeRef<CFStringRef> right_of_second_quote(
      CFStringCreateWithSubstring(
          nullptr, framwork_requirement,
          CFRangeMake(second_quote_index + 1, len - second_quote_index - 1)));
  base::apple::ScopedCFTypeRef<CFMutableStringRef> shim_requirement_string(
      CFStringCreateMutableCopy(nullptr, 0,
                                CFSTR("identifier \"app_mode_loader\"")));
  CFStringAppend(shim_requirement_string.get(), right_of_second_quote.get());
  return shim_requirement_string;
}

}  // namespace apps