File: startup_browser_creator.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 (1777 lines) | stat: -rw-r--r-- 73,951 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
// 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.

#include "chrome/browser/ui/startup/startup_browser_creator.h"

#include <stddef.h>

#include <optional>
#include <set>
#include <string>
#include <utility>

#include "apps/switches.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/statistics_recorder.h"
#include "base/scoped_multi_source_observation.h"
#include "base/strings/string_tokenizer.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/trace_event/trace_event.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/platform_apps/app_load_service.h"
#include "chrome/browser/apps/platform_apps/platform_app_launch.h"
#include "chrome/browser/ash/floating_workspace/floating_workspace_service_factory.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/extensions/startup_helper.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/nuke_profile_directory_utils.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_manager.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/sessions/exit_type_service.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/startup/launch_mode_recorder.h"
#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
#include "chrome/browser/ui/startup/startup_tab_provider.h"
#include "chrome/browser/ui/startup/startup_types.h"
#include "chrome/browser/ui/startup/web_app_startup_utils.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/web_applications/web_app_ui_manager_impl.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/util.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "components/url_formatter/url_fixer.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/common/content_switches.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/switches.h"
#include "printing/buildflags/buildflags.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_switches.h"
#include "chrome/browser/ash/app_mode/app_launch_utils.h"
#include "chrome/browser/ash/app_mode/kiosk_app_types.h"
#include "chrome/browser/ash/app_mode/kiosk_controller.h"
#include "chrome/browser/ash/app_restore/full_restore_service.h"
#include "chrome/browser/ash/app_restore/full_restore_service_factory.h"
#include "chrome/browser/ash/floating_workspace/floating_workspace_service.h"
#include "chrome/browser/ash/floating_workspace/floating_workspace_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_helper.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "components/user_manager/user_manager.h"
#else
#include "chrome/browser/extensions/api/messaging/native_messaging_launch_from_native.h"
#include "chrome/browser/ui/profiles/profile_picker.h"
#endif

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
#include "chrome/browser/ui/startup/first_run_service.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/web_applications/os_integration/mac/app_shim_registry.h"
#endif

#if BUILDFLAG(IS_WIN)
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/notifications/notification_platform_bridge_win.h"
#include "chrome/browser/notifications/win/notification_launch_id.h"
#include "chrome/browser/ui/startup/credential_provider_signin_dialog_win.h"
#include "chrome/browser/ui/webui/settings/reset_settings_handler.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/credential_provider/common/gcp_strings.h"
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#include "chrome/browser/headless/headless_mode_util.h"
#include "chrome/browser/ui/startup/web_app_info_recorder_utils.h"
#include "components/headless/policy/headless_mode_policy.h"
#endif

#if !BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_installation_manager.h"
#endif

using content::BrowserThread;
using content::ChildProcessSecurityPolicy;

namespace {

// Keeps track on which profiles have been launched.
class ProfileLaunchObserver : public ProfileObserver,
                              public BrowserListObserver {
 public:
  ProfileLaunchObserver() { BrowserList::AddObserver(this); }
  ProfileLaunchObserver(const ProfileLaunchObserver&) = delete;
  ProfileLaunchObserver& operator=(const ProfileLaunchObserver&) = delete;
  ~ProfileLaunchObserver() override { BrowserList::RemoveObserver(this); }

  // BrowserListObserver:
  void OnBrowserAdded(Browser* browser) override {
    opened_profiles_.insert(browser->profile());
    MaybeActivateProfile();
  }

  // ProfileObserver:
  void OnProfileWillBeDestroyed(Profile* profile) override {
    observed_profiles_.RemoveObservation(profile);
    launched_profiles_.erase(profile);
    opened_profiles_.erase(profile);
    if (profile == profile_to_activate_) {
      profile_to_activate_ = nullptr;
    }
    // If this profile was the last launched one without an opened window,
    // then we may be ready to activate |profile_to_activate_|.
    MaybeActivateProfile();
  }

  // Returns true if `profile` has been launched by
  // StartupBrowserCreator::LaunchBrowser() and has at least one open window.
  bool HasBeenLaunchedAndBrowserOpen(const Profile* profile) const {
    return base::Contains(opened_profiles_, profile) &&
           base::Contains(launched_profiles_, profile);
  }

  void AddLaunched(Profile* profile) {
    if (!observed_profiles_.IsObservingSource(profile)) {
      observed_profiles_.AddObservation(profile);
    }
    launched_profiles_.insert(profile);
    if (chrome::FindBrowserWithProfile(profile)) {
      // A browser may get opened before we get initialized (e.g., in tests),
      // so we never see the OnBrowserAdded() for it.
      opened_profiles_.insert(profile);
    }
  }

  void Clear() {
    launched_profiles_.clear();
    opened_profiles_.clear();
  }

  bool activated_profile() { return activated_profile_; }

  void set_profile_to_activate(Profile* profile) {
    if (!observed_profiles_.IsObservingSource(profile)) {
      observed_profiles_.AddObservation(profile);
    }
    profile_to_activate_ = profile;
    MaybeActivateProfile();
  }

 private:
  void MaybeActivateProfile() {
    if (!profile_to_activate_) {
      return;
    }
    // Check that browsers have been opened for all the launched profiles.
    // Note that browsers opened for profiles that were not added as launched
    // profiles are simply ignored.
    auto i = launched_profiles_.begin();
    for (; i != launched_profiles_.end(); ++i) {
      if (opened_profiles_.find(*i) == opened_profiles_.end()) {
        return;
      }
    }
    // Asynchronous post to give a chance to the last window to completely
    // open and activate before trying to activate |profile_to_activate_|.
    content::GetUIThreadTaskRunner({})->PostTask(
        FROM_HERE, base::BindOnce(&ProfileLaunchObserver::ActivateProfile,
                                  base::Unretained(this)));
    // Avoid posting more than once before ActivateProfile gets called.
    observed_profiles_.RemoveAllObservations();
    BrowserList::RemoveObserver(this);
  }

  void ActivateProfile() {
    // We need to test again, in case the profile got deleted in the mean time.
    if (profile_to_activate_) {
      Browser* browser = chrome::FindBrowserWithProfile(profile_to_activate_);
      // |profile| may never get launched, e.g., if it only had
      // incognito Windows and one of them was used to exit Chrome.
      // So it won't have a browser in that case.
      if (browser) {
        browser->window()->Activate();
      }
      // No need try to activate this profile again.
      profile_to_activate_ = nullptr;
    }
    // Assign true here, even if no browser was actually activated, so that
    // the test can stop waiting, and fail gracefully when needed.
    activated_profile_ = true;
  }

  // These are the profiles that get launched by
  // StartupBrowserCreator::LaunchBrowser.
  std::set<raw_ptr<const Profile, SetExperimental>> launched_profiles_;
  // These are the profiles for which at least one browser window has been
  // opened. This is needed to know when it is safe to activate
  // |profile_to_activate_|, otherwise, new browser windows being opened will
  // be activated on top of it.
  std::set<raw_ptr<const Profile, SetExperimental>> opened_profiles_;
  // This is null until the profile to activate has been chosen. This value
  // should only be set once all profiles have been launched, otherwise,
  // activation may not happen after the launch of newer profiles.
  raw_ptr<Profile, DanglingUntriaged> profile_to_activate_ = nullptr;
  // Set once we attempted to activate a profile. We only get one shot at this.
  bool activated_profile_ = false;
  base::ScopedMultiSourceObservation<Profile, ProfileObserver>
      observed_profiles_{this};
};

base::LazyInstance<ProfileLaunchObserver>::DestructorAtExit
    profile_launch_observer = LAZY_INSTANCE_INITIALIZER;

// Dumps the current set of the browser process's histograms to |output_file|.
// The file is overwritten if it exists. This function should only be called in
// the blocking pool.
void DumpBrowserHistograms(const base::FilePath& output_file) {
  std::string output_string(
      base::StatisticsRecorder::ToJSON(base::JSON_VERBOSITY_LEVEL_FULL));

  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
                                                base::BlockingType::MAY_BLOCK);
  base::WriteFile(output_file, output_string);
}

// Returns whether |profile_info.profile| can be opened during Chrome startup
// without explicit user action.
bool CanOpenProfileOnStartup(StartupProfileInfo profile_info) {
#if BUILDFLAG(IS_CHROMEOS)
  // On ChromeOS, the user has already chosen and logged into the profile before
  // Chrome starts up.
  DCHECK_NE(profile_info.mode, StartupProfileMode::kProfilePicker);
  return true;
#else
  // Profile picker startups require explicit user action, profile can't be
  // readily opened.
  if (profile_info.mode == StartupProfileMode::kProfilePicker) {
    return false;
  }

  Profile* profile = profile_info.profile;

  // System profiles are not available.
  DCHECK(!profile->IsSystemProfile());

  // Profiles that require signin are not available.
  ProfileAttributesEntry* entry =
      g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetProfileAttributesWithPath(profile->GetPath());
  if (entry && entry->IsSigninRequired()) {
    return false;
  }

  if (profile->IsGuestSession()) {
    // Guest is not available unless a there is already a guest browser open
    // (for example, launching a new browser after clicking on a downloaded file
    // in Guest mode).
    return chrome::GetBrowserCount(
               profile->GetPrimaryOTRProfile(/*create_if_needed=*/false)) > 0;
  }

  return true;
#endif
}

#if !BUILDFLAG(IS_CHROMEOS)
StartupProfileModeReason ShouldShowProfilePickerAtProcessLaunch(
    ProfileManager* profile_manager,
    bool has_command_line_specified_profile_directory,
    const base::CommandLine& command_line) {
  // Skip the profile picker when Chrome is restarted (e.g. after an update) so
  // that the session can be restored.
  if (StartupBrowserCreator::WasRestarted()) {
    return StartupProfileModeReason::kWasRestarted;
  }

  // Don't show the picker if a certain profile (or an incognito window in the
  // default profile) is explicitly requested.
  if (profiles::IsGuestModeRequested(command_line,
                                     g_browser_process->local_state(),
                                     /*show_warning=*/false) ||
      command_line.HasSwitch(switches::kIncognito) ||
      has_command_line_specified_profile_directory) {
    // TODO(crbug.com/40257919): The profile directory and guest mode
    // were already tested in the calling function `GetStartupProfilePath()`.
    // Consolidate these checks.
    return StartupProfileModeReason::kIncognitoModeRequested;
  }

  // Don't show the picker if an app is explicitly requested to open. This URL
  // param should be ideally paired with switches::kProfileDirectory but it's
  // better to err on the side of opening the last profile than to err on the
  // side of not opening the app directly.
  if (command_line.HasSwitch(switches::kApp) ||
      command_line.HasSwitch(switches::kAppId)) {
    return StartupProfileModeReason::kAppRequested;
  }

#if BUILDFLAG(IS_WIN)
  // Don't show the picker if trying to uninstall an app. This URL param should
  // be paired with switches::kProfileDirectory but it's better to err on the
  // side of opening the last profile (and maybe fail uninstalling the app
  // there) than to err on the side of unexpectedly showing the picker UI.
  if (command_line.HasSwitch(switches::kUninstallAppId)) {
    return StartupProfileModeReason::kUninstallApp;
  }

  // Don't show the picker if we want to perform a GCPW Sign In. It will want to
  // only launch an incognito window.
  if (command_line.HasSwitch(credential_provider::kGcpwSigninSwitch)) {
    return StartupProfileModeReason::kGcpwSignin;
  }

  // If the browser is launched due to activation on Windows native
  // notification, the profile id encoded in the notification launch id should
  // be chosen over the profile picker.
  base::FilePath profile_basename =
      NotificationLaunchId::GetNotificationLaunchProfileBaseName(command_line);
  if (!profile_basename.empty()) {
    // TODO(crbug.com/40257919): The notification ID was already tested
    // in the calling function `GetStartupProfilePath()`. Consolidate these
    // checks.
    return StartupProfileModeReason::kNotificationLaunchIdWin2;
  }
#endif  // BUILDFLAG(IS_WIN)

  // Don't show the picker if Chrome should be launched without window. This
  // will also cause a profile to be loaded which Chrome needs for performing
  // background activity.
  if (StartupBrowserCreator::ShouldLoadProfileWithoutWindow(command_line)) {
    return StartupProfileModeReason::kLaunchWithoutWindow;
  }

  return ProfilePicker::GetStartupModeReason();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

// If Incognito or Guest mode are requested by policy or command line returns
// the appropriate private browsing profile. Otherwise returns
// `profile_info.profile`.
Profile* GetPrivateProfileIfRequested(const base::CommandLine& command_line,
                                      StartupProfileInfo profile_info) {
  bool open_guest_profile = profiles::IsGuestModeRequested(
      command_line, g_browser_process->local_state(),
      /* show_warning= */ true);
  if (open_guest_profile) {
    Profile* profile = g_browser_process->profile_manager()->GetProfile(
        ProfileManager::GetGuestProfilePath());
    profile = profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
    return profile;
  }

  if (profile_info.mode == StartupProfileMode::kProfilePicker) {
    // Profile not intended for direct usage, we can exit early.
    return profile_info.profile;
  }

  Profile* profile = profile_info.profile;
  if (IncognitoModePrefs::ShouldLaunchIncognito(command_line,
                                                profile->GetPrefs())) {
    return profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  } else {
    bool expect_incognito = command_line.HasSwitch(switches::kIncognito);
    LOG_IF(WARNING, expect_incognito)
        << "Incognito mode disabled by policy, launching a normal "
        << "browser session.";
  }

  return profile;
}

#if !BUILDFLAG(IS_CHROMEOS)
StartupProfileInfo GetProfilePickerStartupProfileInfo() {
  // We can only show the profile picker if the system profile (where the
  // profile picker lives) also exists (or is creatable).
  // TODO(crbug.com/40205861): Remove unnecessary system profile check here.
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  if (!profile_manager->GetProfile(ProfileManager::GetSystemProfilePath())) {
    return {.profile = nullptr, .mode = StartupProfileMode::kError};
  }

  return {.profile = nullptr, .mode = StartupProfileMode::kProfilePicker};
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

bool IsSilentLaunchEnabled(const base::CommandLine& command_line,
                           const Profile* profile) {
  // This check should have been done in `ProcessCmdLineImpl()` before calling
  // this function. But `LaunchBrowser()` can be called directly, for example by
  // //chrome/browser/ash/login/session/user_session_manager.cc, so it has to be
  // checked again.
  // TODO(crbug.com/40819749): Investigate minimizing duplicate checks.

  if (command_line.HasSwitch(switches::kNoStartupWindow)) {
    return true;
  }

  if (command_line.HasSwitch(switches::kSilentLaunch)) {
    return true;
  }

  if (StartupBrowserCreator::ShouldLoadProfileWithoutWindow(command_line)) {
    return true;
  }

#if BUILDFLAG(IS_CHROMEOS)
  return profile->GetPrefs()->GetBoolean(
      prefs::kStartupBrowserWindowLaunchSuppressed);
#else
  return false;
#endif  // BUILDFLAG(IS_CHROMEOS)
}

bool CanOpenWebApp(Profile* profile) {
  return web_app::AreWebAppsEnabled(profile) &&
         apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile);
}

// Handles the --app switch.
bool MaybeLaunchAppShortcutWindow(const base::CommandLine& command_line,
                                  const base::FilePath& cur_dir,
                                  chrome::startup::IsFirstRun is_first_run,
                                  Profile* profile) {
  if (!profile) {
    return false;
  }

  if (!command_line.HasSwitch(switches::kApp)) {
    return false;
  }

  std::string url_string = command_line.GetSwitchValueASCII(switches::kApp);
  if (url_string.empty()) {
    return false;
  }

#if BUILDFLAG(IS_WIN)  // Fix up Windows shortcuts.
  base::ReplaceSubstringsAfterOffset(&url_string, 0, "\\x", "%");
#endif
  GURL url(url_string);

  // Restrict allowed URLs for --app switch.
  if (!url.is_empty() && url.is_valid()) {
    content::ChildProcessSecurityPolicy* policy =
        content::ChildProcessSecurityPolicy::GetInstance();
    if (policy->IsWebSafeScheme(url.scheme()) ||
        url.SchemeIs(url::kFileScheme)) {
      const content::WebContents* web_contents =
          apps::OpenExtensionAppShortcutWindow(profile, url);
      if (web_contents) {
        web_app::startup::FinalizeWebAppLaunch(
            web_app::startup::OpenMode::kInWindowByUrl, command_line,
            is_first_run, chrome::FindBrowserWithTab(web_contents),
            apps::LaunchContainer::kLaunchContainerWindow);
        return true;
      }
    }
  }
  return false;
}

bool MaybeLaunchExtensionApp(const base::CommandLine& command_line,
                             const base::FilePath& cur_dir,
                             chrome::startup::IsFirstRun is_first_run,
                             Profile* profile) {
  if (!command_line.HasSwitch(switches::kAppId)) {
    return false;
  }

  std::string app_id = command_line.GetSwitchValueASCII(switches::kAppId);
  const extensions::Extension* extension =
      extensions::ExtensionRegistry::Get(profile)->GetInstalledExtension(
          app_id);
  if (!extension) {
    return false;
  }

  LaunchAppWithCallback(
      profile, app_id, command_line, cur_dir,
      base::BindOnce(&web_app::startup::FinalizeWebAppLaunch,
                     web_app::startup::OpenMode::kInWindowByAppId, command_line,
                     is_first_run));
  return true;
}

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused. Updates need to be reflected in
// enum IncognitoForcedStart in tools/metrics/histograms/enums.xml.
enum class IncognitoForcedStart {
  kNoSwitchAndNotForced = 0,
  kSwitchButNotForced = 1,
  kNoSwitchButForced = 2,
  kSwitchAndForced = 3,
  kMaxValue = kSwitchAndForced,
};

void RecordIncognitoForcedStart(bool should_launch_incognito,
                                bool has_incognito_switch) {
  if (has_incognito_switch) {
    base::UmaHistogramEnumeration(
        "Startup.IncognitoForcedStart",
        should_launch_incognito ? IncognitoForcedStart::kSwitchAndForced
                                : IncognitoForcedStart::kSwitchButNotForced);
  } else {
    base::UmaHistogramEnumeration(
        "Startup.IncognitoForcedStart",
        should_launch_incognito ? IncognitoForcedStart::kNoSwitchButForced
                                : IncognitoForcedStart::kNoSwitchAndNotForced);
  }
}

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// Launches a browser by using a dedicated `StartupBrowserCreator`, to avoid
// having to rely on the current instance staying alive while this method is
// bound as a callback.
void OpenNewWindowForFirstRun(const base::CommandLine& command_line,
                              Profile* profile,
                              const base::FilePath& cur_dir,
                              const std::vector<GURL>& first_run_urls,
                              chrome::startup::IsProcessStartup process_startup,
                              chrome::startup::IsFirstRun is_first_run,
                              bool proceed) {
  if (!proceed) {
    return;
  }

  StartupBrowserCreator browser_creator;
  browser_creator.AddFirstRunTabs(first_run_urls);
  browser_creator.LaunchBrowser(command_line, profile, cur_dir, process_startup,
                                is_first_run, /*restore_tabbed_browser=*/true);
}
#endif  // BUILDFLAG(ENABLE_DICE_SUPPORT)

#if BUILDFLAG(IS_CHROMEOS)
// Returns the app id of the kiosk app associated with the current user session.
// Returns nullopt for non-kiosk user sessions, since crash recovery is not
// supported there.
std::optional<ash::KioskAppId> GetAppId(const base::CommandLine& command_line,
                                        Profile* profile) {
  const user_manager::User* user =
      ash::ProfileHelper::Get()->GetUserByProfile(profile);

  if (!user) {
    return std::nullopt;
  }

  switch (user->GetType()) {
    case user_manager::UserType::kKioskChromeApp:
      return ash::KioskAppId::ForChromeApp(
          command_line.GetSwitchValueASCII(::switches::kAppId),
          user->GetAccountId());
    case user_manager::UserType::kKioskWebApp:
      return ash::KioskAppId::ForWebApp(user->GetAccountId());
    case user_manager::UserType::kKioskIWA:
      return ash::KioskAppId::ForIsolatedWebApp(user->GetAccountId());
    case user_manager::UserType::kRegular:
    case user_manager::UserType::kChild:
    case user_manager::UserType::kGuest:
    case user_manager::UserType::kPublicAccount:
      return std::nullopt;
  }
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace

StartupProfileMode StartupProfileModeFromReason(
    StartupProfileModeReason reason) {
  switch (reason) {
    case StartupProfileModeReason::kError:
      return StartupProfileMode::kError;

    case StartupProfileModeReason::kMultipleProfiles:
    case StartupProfileModeReason::kPickerForcedByPolicy:
      return StartupProfileMode::kProfilePicker;

    case StartupProfileModeReason::kGuestModeRequested:
    case StartupProfileModeReason::kProfileDirSwitch:
    case StartupProfileModeReason::kProfileEmailSwitch:
    case StartupProfileModeReason::kIgnoreProfilePicker:
    case StartupProfileModeReason::kCommandLineTabs:
    case StartupProfileModeReason::kPickerNotSupported:
    case StartupProfileModeReason::kWasRestarted:
    case StartupProfileModeReason::kIncognitoModeRequested:
    case StartupProfileModeReason::kAppRequested:
    case StartupProfileModeReason::kUninstallApp:
    case StartupProfileModeReason::kGcpwSignin:
    case StartupProfileModeReason::kLaunchWithoutWindow:
    case StartupProfileModeReason::kNotificationLaunchIdWin1:
    case StartupProfileModeReason::kNotificationLaunchIdWin2:
    case StartupProfileModeReason::kPickerDisabledByPolicy:
    case StartupProfileModeReason::kSingleProfile:
    case StartupProfileModeReason::kInactiveProfiles:
    case StartupProfileModeReason::kUserOptedOut:
      return StartupProfileMode::kBrowserWindow;
  }
}

StartupBrowserCreator::StartupBrowserCreator() = default;

StartupBrowserCreator::~StartupBrowserCreator() {
  // When StartupBrowserCreator finishes doing its job, we should reset
  // was_restarted_read_ so that this browser no longer reads as restarted.
  // In the case where the browser is still running due to PWA or
  // background-extension subsequent startups should not execute restarted
  // behaviors.
  was_restarted_read_ = false;
}

// static
bool StartupBrowserCreator::was_restarted_read_ = false;

// static
bool StartupBrowserCreator::in_synchronous_profile_launch_ = false;

void StartupBrowserCreator::AddFirstRunTabs(const std::vector<GURL>& urls) {
  for (const auto& url : urls) {
    if (url.is_valid()) {
      first_run_tabs_.push_back(url);
    }
  }
}

bool StartupBrowserCreator::Start(const base::CommandLine& cmd_line,
                                  const base::FilePath& cur_dir,
                                  StartupProfileInfo profile_info,
                                  const Profiles& last_opened_profiles) {
  TRACE_EVENT0("startup", "StartupBrowserCreator::Start");
  return ProcessCmdLineImpl(cmd_line, cur_dir,
                            chrome::startup::IsProcessStartup::kYes,
                            profile_info, last_opened_profiles);
}

// static
bool StartupBrowserCreator::InSynchronousProfileLaunch() {
  return in_synchronous_profile_launch_;
}

void StartupBrowserCreator::LaunchBrowser(
    const base::CommandLine& command_line,
    Profile* profile,
    const base::FilePath& cur_dir,
    chrome::startup::IsProcessStartup process_startup,
    chrome::startup::IsFirstRun is_first_run,
    bool restore_tabbed_browser) {
  TRACE_EVENT0("ui", "StartupBrowserCreator::LaunchBrowser");
  SCOPED_UMA_HISTOGRAM_TIMER("Startup.StartupBrowserCreator.LaunchBrowser");

  DCHECK(profile);
#if BUILDFLAG(IS_WIN)
  DCHECK(!command_line.HasSwitch(credential_provider::kGcpwSigninSwitch));
  DCHECK(!command_line.HasSwitch(switches::kNotificationLaunchId));
#endif  // BUILDFLAG(IS_WIN)
  in_synchronous_profile_launch_ =
      process_startup == chrome::startup::IsProcessStartup::kYes;

  profile = GetPrivateProfileIfRequested(
      command_line, {profile, StartupProfileMode::kBrowserWindow});

  if (!IsSilentLaunchEnabled(command_line, profile)) {
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
    auto* fre_service = FirstRunServiceFactory::GetForBrowserContext(profile);
    if (fre_service && fre_service->ShouldOpenFirstRun()) {
      // Show the FRE and let `OpenNewWindowForFirstRun()` handle the browser
      // launch. This `StartupBrowserCreator` will get destroyed when the method
      // returns so the relevant data is copied over and passed to the callback.
      fre_service->OpenFirstRunIfNeeded(base::BindOnce(
          &OpenNewWindowForFirstRun, command_line, profile, cur_dir,
          first_run_tabs_, process_startup, is_first_run));
      return;
    }
#endif

    StartupBrowserCreatorImpl lwp(cur_dir, command_line, this, is_first_run);
    lwp.Launch(profile,
               in_synchronous_profile_launch_
                   ? chrome::startup::IsProcessStartup::kYes
                   : chrome::startup::IsProcessStartup::kNo,
               restore_tabbed_browser);
  }
  in_synchronous_profile_launch_ = false;
  profile_launch_observer.Get().AddLaunched(profile);
}

void StartupBrowserCreator::LaunchBrowserForLastProfiles(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    chrome::startup::IsProcessStartup process_startup,
    chrome::startup::IsFirstRun is_first_run,
    StartupProfileInfo profile_info,
    const Profiles& last_opened_profiles,
    bool restore_tabbed_browser) {
  TRACE_EVENT0("ui", "StartupBrowserCreator::LaunchBrowserForLastProfiles");
  DCHECK_NE(profile_info.mode, StartupProfileMode::kError);

  Profile* profile = profile_info.profile;
  // On Windows, when chrome is launched by notification activation where the
  // kNotificationLaunchId switch is used, always use `profile` which contains
  // the profile id extracted from the notification launch id.
  bool was_windows_notification_launch = false;
#if BUILDFLAG(IS_WIN)
  was_windows_notification_launch =
      command_line.HasSwitch(switches::kNotificationLaunchId);
#endif  // BUILDFLAG(IS_WIN)

  if (profile_info.mode == StartupProfileMode::kProfilePicker) {
#if BUILDFLAG(IS_CHROMEOS)
    NOTREACHED();
#else
    ProfilePicker::Show(ProfilePicker::Params::FromEntryPoint(
        process_startup == chrome::startup::IsProcessStartup::kYes
            ? ProfilePicker::EntryPoint::kOnStartup
            : ProfilePicker::EntryPoint::kNewSessionOnExistingProcess));
    return;
#endif  // BUILDFLAG(IS_CHROMEOS)
  }

  // `last_opened_profiles` will be empty in the following circumstances:
  // - This is the first launch. `profile` is the initial profile.
  // - The user exited the browser by closing all windows for all profiles.
  //   `profile` is the profile which owned the last open window.
  // - Only incognito windows were open when the browser exited.
  //   `profile` is the last used incognito profile. Restoring it will create a
  //   browser window for the corresponding original profile.
  // - All of the last opened profiles fail to initialize.
  if (last_opened_profiles.empty() || was_windows_notification_launch) {
    if (CanOpenProfileOnStartup(profile_info)) {
      Profile* profile_to_open = profile->IsGuestSession()
                                     ? profile->GetPrimaryOTRProfile(
                                           /*create_if_needed=*/true)
                                     : profile;
#if BUILDFLAG(IS_CHROMEOS)
      if (process_startup == chrome::startup::IsProcessStartup::kYes) {
        if (ash::floating_workspace_util::IsFloatingWorkspaceV2Enabled() ||
            ash::floating_workspace_util::IsFloatingSsoEnabled(
                profile_to_open)) {
          // Calling `GetForProfile` here ensures that
          // `FloatingWorkspaceService` is created.
          // TODO(crbug.com/419801387): we can likely remove this call and
          // instead override `ServiceIsCreatedWithBrowserContext` in the
          // factory to conditionally construct the service after profile
          // creation.
          ash::FloatingWorkspaceServiceFactory::GetForProfile(profile_to_open);
        }
        // If floating workspace is responsible for restore, stop here before
        // entering the FullRestoreService code path.
        if (ash::floating_workspace_util::ShouldHandleRestartRestore()) {
          return;
        }
        // If FullRestoreService is available for the profile (i.e. the full
        // restore feature is enabled and the profile is a regular user
        // profile), defer the browser launching to FullRestoreService code.
        auto* full_restore_service =
            ash::full_restore::FullRestoreServiceFactory::GetForProfile(
                profile_to_open);
        if (full_restore_service) {
          full_restore_service->LaunchBrowserWhenReady();
          return;
        }
      }
#endif
      LaunchBrowser(command_line, profile_to_open, cur_dir, process_startup,
                    is_first_run, restore_tabbed_browser);
      return;
    }

    // Show ProfilePicker if `profile` can't be auto opened.
#if BUILDFLAG(IS_CHROMEOS)
    NOTREACHED();
#else
    ProfilePicker::Show(ProfilePicker::Params::FromEntryPoint(
        process_startup == chrome::startup::IsProcessStartup::kYes
            ? ProfilePicker::EntryPoint::kOnStartupNoProfile
            : ProfilePicker::EntryPoint::
                  kNewSessionOnExistingProcessNoProfile));
    return;
#endif  // BUILDFLAG(IS_CHROMEOS)
  }
  ProcessLastOpenedProfiles(command_line, cur_dir, process_startup,
                            is_first_run, profile, last_opened_profiles);
}

// static
bool StartupBrowserCreator::WasRestarted() {
  // Stores the value of the preference kWasRestarted had when it was read.
  static bool was_restarted = false;

  if (!was_restarted_read_) {
    PrefService* pref_service = g_browser_process->local_state();
    was_restarted = pref_service->GetBoolean(prefs::kWasRestarted);
    pref_service->SetBoolean(prefs::kWasRestarted, false);
    was_restarted_read_ = true;
  }
  return was_restarted;
}

// static
SessionStartupPref StartupBrowserCreator::GetSessionStartupPref(
    const base::CommandLine& command_line,
    const Profile* profile) {
  DCHECK(profile);
  const PrefService* prefs = profile->GetPrefs();
  SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs);

  // IsChromeFirstRun() looks for a sentinel file to determine whether the user
  // is starting Chrome for the first time. On Chrome OS, the sentinel is stored
  // in a location shared by all users and the check is meaningless. Query the
  // UserManager instead to determine whether the user is new.
#if BUILDFLAG(IS_CHROMEOS)
  const bool is_first_run =
      user_manager::UserManager::Get()->IsCurrentUserNew();
  // On ChromeOS restarts force the user to login again. The expectation is that
  // after a login the user gets clean state. For this reason we ignore
  // StartupBrowserCreator::WasRestarted(). However
  // StartupBrowserCreator::WasRestarted has to be called in order to correctly
  // update pref values.
  const bool did_restart = false;
  StartupBrowserCreator::WasRestarted();
#else
  const bool is_first_run = first_run::IsChromeFirstRun();
  const bool did_restart = StartupBrowserCreator::WasRestarted();
#endif

  // The pref has an OS-dependent default value. For the first run only, this
  // default is overridden with SessionStartupPref::DEFAULT so that first run
  // behavior (sync promo, welcome page) is consistently invoked.
  // This applies only if the pref is still at its default and has not been
  // set by the user, managed prefs or policy.
  if (is_first_run && SessionStartupPref::TypeIsDefault(prefs)) {
    pref.type = SessionStartupPref::DEFAULT;
  }

  // The switches::kRestoreLastSession command line switch is used to restore
  // sessions after a browser self restart (e.g. after a Chrome upgrade).
  // However, new profiles can be created from a browser process that has this
  // switch so do not set the session pref to SessionStartupPref::LAST for
  // those as there is nothing to restore.
  bool restore_last_session =
      command_line.HasSwitch(switches::kRestoreLastSession);
  if ((restore_last_session || did_restart) && !profile->IsNewProfile()) {
    pref.type = SessionStartupPref::LAST;
  }

  // A browser starting for a profile being unlocked should always restore.
  if (!profile->IsGuestSession()) {
    ProfileAttributesEntry* entry =
        g_browser_process->profile_manager()
            ->GetProfileAttributesStorage()
            .GetProfileAttributesWithPath(profile->GetPath());

    if (entry && entry->IsSigninRequired()) {
      pref.type = SessionStartupPref::LAST;
    }
  }
  if ((pref.ShouldRestoreLastSession() && !pref.ShouldOpenUrls()) &&
      (profile->IsGuestSession() || profile->IsOffTheRecord())) {
    // We don't store session information when incognito. If the user has
    // chosen to restore last session and launched incognito, fallback to
    // default launch behavior.
    pref.type = SessionStartupPref::DEFAULT;
  }

  return pref;
}

// static
void StartupBrowserCreator::ClearLaunchedProfilesForTesting() {
  profile_launch_observer.Get().Clear();
}

// static
void StartupBrowserCreator::RegisterLocalStatePrefs(
    PrefRegistrySimple* registry) {
  registry->RegisterBooleanPref(prefs::kPromotionsEnabled, true);
#if !BUILDFLAG(IS_CHROMEOS)
  registry->RegisterBooleanPref(prefs::kCommandLineFlagSecurityWarningsEnabled,
                                true);
#endif
  registry->RegisterBooleanPref(prefs::kSuppressUnsupportedOSWarning, false);
  registry->RegisterBooleanPref(prefs::kWasRestarted, false);

#if BUILDFLAG(IS_WIN)
  registry->RegisterStringPref(prefs::kShortcutMigrationVersion, std::string());
#endif
}

// static
void StartupBrowserCreator::RegisterProfilePrefs(PrefRegistrySimple* registry) {
#if BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
  // This will be set for newly created profiles, and is used to indicate which
  // users went through onboarding with the current experiment group.
  registry->RegisterStringPref(prefs::kNaviOnboardGroup, "");
#endif  // BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
}

// static
bool StartupBrowserCreator::ShouldLoadProfileWithoutWindow(
    const base::CommandLine& command_line) {
  // Don't open any browser windows if the command line indicates "background
  // mode".
  if (command_line.HasSwitch(switches::kNoStartupWindow)) {
    return true;
  }

  return false;
}

bool StartupBrowserCreator::ProcessCmdLineImpl(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    chrome::startup::IsProcessStartup process_startup,
    StartupProfileInfo profile_info,
    const Profiles& last_opened_profiles) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK_NE(profile_info.mode, StartupProfileMode::kError);
  TRACE_EVENT0("startup", "StartupBrowserCreator::ProcessCmdLineImpl");
  ComputeAndRecordLaunchMode(command_line);

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  if (headless::IsHeadlessMode() &&
      headless::HeadlessModePolicy::IsHeadlessModeDisabled(
          g_browser_process->local_state())) {
    LOG(ERROR) << "Headless mode is disallowed by the system admin.";
    return false;
  }
#endif

  if (process_startup == chrome::startup::IsProcessStartup::kYes &&
      command_line.HasSwitch(switches::kDisablePromptOnRepost)) {
    content::NavigationController::DisablePromptOnRepost();
  }

  chrome::startup::IsFirstRun is_first_run =
      first_run::IsChromeFirstRun() ? chrome::startup::IsFirstRun::kYes
                                    : chrome::startup::IsFirstRun::kNo;

  bool silent_launch = false;
  bool should_launch_incognito =
      // Note: kIncognito and some related flags disable profile picker startups
      // via `ShouldShowProfilePickerAtProcessLaunch()`, so we can use it as
      // a signal here.
      // TODO(http://crbug.com/1293024): Refactor command line processing logic
      // to validate the flag sets and reliably determine the startup mode.
      profile_info.mode != StartupProfileMode::kProfilePicker &&
      IncognitoModePrefs::ShouldLaunchIncognito(
          command_line, profile_info.profile->GetPrefs());
  bool can_use_profile =
      CanOpenProfileOnStartup(profile_info) && !should_launch_incognito;

  RecordIncognitoForcedStart(should_launch_incognito,
                             command_line.HasSwitch(switches::kIncognito));

  // `profile` is never off-the-record. If Incognito or Guest enforcement switch
  // or policy are provided, use the appropriate private browsing profile
  // instead.
  Profile* privacy_safe_profile =
      GetPrivateProfileIfRequested(command_line, profile_info);

  if (command_line.HasSwitch(switches::kValidateCrx)) {
    if (process_startup == chrome::startup::IsProcessStartup::kNo) {
      LOG(ERROR) << "chrome is already running; you must close all running "
                 << "instances before running with the --"
                 << switches::kValidateCrx << " flag";
      return false;
    }
    extensions::StartupHelper helper;
    std::string message;
    std::string error;
    if (helper.ValidateCrx(command_line, &error)) {
      message = std::string("ValidateCrx Success");
    } else {
      message = std::string("ValidateCrx Failure: ") + error;
    }
    printf("%s\n", message.c_str());
    return false;
  }

#if BUILDFLAG(IS_CHROMEOS)

  // The browser will be launched after the user logs in.
  if (command_line.HasSwitch(ash::switches::kLoginManager)) {
    silent_launch = true;
  }

  if (IsRunningInForcedAppMode()) {
    // If we are here, it means the Chrome browser crashed/restarted while in
    // Kiosk mode, since the 'force app mode' switch is only added to the
    // commandline while in a kiosk session.
    Profile* profile = profile_info.profile;

    // Skip browser launch since app mode launches its app window.
    silent_launch = true;

    if (auto app_id = GetAppId(command_line, profile); app_id.has_value()) {
      ash::KioskController::Get().StartSessionAfterCrash(app_id.value(),
                                                         profile);
    } else {
      // If we are here, the user is invalid.
      // We should terminate the session in such cases.
      chrome::AttemptUserExit();
      return false;
    }
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  if (process_startup == chrome::startup::IsProcessStartup::kNo &&
      command_line.HasSwitch(switches::kDumpBrowserHistograms)) {
    // Only handle --dump-browser-histograms from a rendezvous. In this case, do
    // not open a new browser window even if no output file was given.
    base::FilePath output_file(
        command_line.GetSwitchValuePath(switches::kDumpBrowserHistograms));
    if (!output_file.empty()) {
      base::ThreadPool::PostTask(
          FROM_HERE,
          {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
           base::TaskShutdownBehavior::BLOCK_SHUTDOWN},
          base::BindOnce(&DumpBrowserHistograms, output_file));
    }
    silent_launch = true;
  }

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  // Writes open and installed web apps to the specified file without
  // launching a new browser window or tab.
  if (base::FeatureList::IsEnabled(features::kListWebAppsSwitch) &&
      command_line.HasSwitch(switches::kListApps)) {
    base::FilePath output_file(
        command_line.GetSwitchValuePath(switches::kListApps));
    if (!output_file.empty() && output_file.IsAbsolute()) {
      base::FilePath profile_base_name(
          command_line.GetSwitchValuePath(switches::kProfileBaseName));
      chrome::startup::WriteWebAppsToFile(output_file, profile_base_name);
    }
    return true;
  }
#endif  //  BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)

#if !BUILDFLAG(IS_CHROMEOS)
  if (base::FeatureList::IsEnabled(features::kOnConnectNative) &&
      command_line.HasSwitch(switches::kNativeMessagingConnectHost) &&
      command_line.HasSwitch(switches::kNativeMessagingConnectExtension)) {
    // These flags are expected to be set together with `--no-startup-window`
    // and `switches::kProfileDirectory` which suppress the profile picker.
    if (profile_info.mode == StartupProfileMode::kProfilePicker) {
      // TODO(http://crbug.com/1293024): Refactor command line processing logic
      // to validate the flag sets and reliably determine the startup mode.
      NOTREACHED()
          << "Failed to launch a native message host: couldn't pick a profile";
    } else {
      extensions::LaunchNativeMessageHostFromNativeApp(
          command_line.GetSwitchValueASCII(
              switches::kNativeMessagingConnectExtension),
          command_line.GetSwitchValueASCII(
              switches::kNativeMessagingConnectHost),
          command_line.GetSwitchValueASCII(switches::kNativeMessagingConnectId),
          privacy_safe_profile);

      // Chrome's lifetime, if the specified extension and native messaging host
      // are both valid and a connection is established, is prolonged by
      // BackgroundModeManager. If `process_startup` is true,
      // --no-startup-window must be set or a browser window must be created for
      // BackgroundModeManager to start background mode. Without this, nothing
      // will take the first keep-alive and the browser process will not
      // terminate. To avoid this situation, don't set `silent_launch` in
      // response to the native messaging connect switches; require the client
      // to pass --no-startup-window if suppressing the creation of a window is
      // desired.
    }
  }

  if (web_app::IsolatedWebAppInstallationManager::HasIwaInstallSwitch(
          command_line)) {
    if (profile_info.mode == StartupProfileMode::kProfilePicker) {
      auto* profile_manager = g_browser_process->profile_manager();
      LOG(ERROR) << "Command line switches to install IWAs are incompatible "
                    "with the Profile Picker. If you have multiple profiles, "
                    "consider using the --"
                 << switches::kProfileDirectory
                 << " switch to select a profile (it accepts the name of a "
                    "profile directory in "
                 << profile_manager->user_data_dir() << ", such as '"
                 << profile_manager->GetLastUsedProfileDir().BaseName()
                 << "').";
      return false;
    } else {
      web_app::IsolatedWebAppInstallationManager::
          MaybeInstallIwaFromCommandLine(command_line, *privacy_safe_profile);
    }
  }
#endif  //  !BUILDFLAG(IS_CHROMEOS)

  // If --no-startup-window is specified then do not open a new window.
  if (command_line.HasSwitch(switches::kNoStartupWindow)) {
    silent_launch = true;
  }

  // If we don't want to launch a new browser window or tab we are done here.
  if (silent_launch) {
    bool should_block_browser_startup_metrics =
        process_startup == chrome::startup::IsProcessStartup::kYes;
#if BUILDFLAG(IS_CHROMEOS)
    // Login screen is an expected common case. Startup metrics should still be
    // recorded after the user logs in even though this is a `silent_launch`.
    should_block_browser_startup_metrics &=
        !command_line.HasSwitch(ash::switches::kLoginManager);
#endif  // BUILDFLAG(IS_CHROMEOS)
    if (should_block_browser_startup_metrics) {
      startup_metric_utils::GetBrowser().SetNonBrowserUIDisplayed();
    }
    return true;
  }

#if BUILDFLAG(IS_WIN)
  // If --uninstall-app-id is specified, remove the target web app.
  if (command_line.HasSwitch(switches::kUninstallAppId)) {
    // `switches::kUninstallAppId` is expected to be set together with a
    // specific profile dir, which suppresses the profile picker, see
    // `ShouldShowProfilePickerAtProcessLaunch()`.
    // TODO(http://crbug.com/1293024): Refactor command line processing logic to
    // validate the flag sets and reliably determine the startup mode.
    CHECK_EQ(profile_info.mode, StartupProfileMode::kBrowserWindow)
        << "Failed to uninstall app: couldn't pick a profile";
    std::string app_id =
        command_line.GetSwitchValueASCII(switches::kUninstallAppId);

    web_app::WebAppProvider::GetForWebApps(privacy_safe_profile)
        ->ui_manager()
        .AsImpl()
        ->UninstallWebAppFromStartupSwitch(app_id);

    // Return true to allow startup to continue and for the main event loop to
    // run. The process will shut down if no browser windows are open when the
    // uninstall completes thanks to UninstallWebAppFromStartupSwitch's
    // ScopedKeepAlive.
    return true;
  }
#endif  // BUILDFLAG(IS_WIN)

  if (command_line.HasSwitch(extensions::switches::kLoadApps) &&
      can_use_profile) {
    if (!ProcessLoadApps(command_line, cur_dir, privacy_safe_profile)) {
      return false;
    }

    // Return early here to avoid opening a browser window.
    // The exception is when there are no browser windows, since we don't want
    // chrome to shut down.
    // TODO(jackhou): Do this properly once keep-alive is handled by the
    // background page of apps. Tracked at http://crbug.com/175381
    if (chrome::GetBrowserCount(privacy_safe_profile) != 0) {
      return true;
    }
  }

  // Check for --load-and-launch-app.
  if (command_line.HasSwitch(apps::kLoadAndLaunchApp) && can_use_profile) {
    base::CommandLine::StringType path =
        command_line.GetSwitchValueNative(apps::kLoadAndLaunchApp);

    if (!apps::AppLoadService::Get(privacy_safe_profile)
             ->LoadAndLaunch(base::FilePath(path), command_line, cur_dir)) {
      return false;
    }

    // Return early here since we don't want to open a browser window.
    // The exception is when there are no browser windows, since we don't want
    // chrome to shut down.
    // TODO(jackhou): Do this properly once keep-alive is handled by the
    // background page of apps. Tracked at http://crbug.com/175381
    if (chrome::GetBrowserCount(privacy_safe_profile) != 0) {
      return true;
    }
  }

#if BUILDFLAG(IS_WIN)
  if (command_line.HasSwitch(switches::kWinJumplistAction)) {
    // `switches::kWinJumplistAction` is expected to be set together with a
    // URL to open and with a specific profile dir.
    if (profile_info.mode == StartupProfileMode::kBrowserWindow) {
      // Use a non-NULL pointer to indicate JumpList has been used. We re-use
      // chrome::kJumpListIconDirname as the key to the data.
      privacy_safe_profile->SetUserData(
          chrome::kJumpListIconDirname,
          base::WrapUnique(new base::SupportsUserData::Data()));
    } else {
      // TODO(http://crbug.com/1293024): Refactor command line processing logic
      // to validate the flag sets and reliably determine the startup mode.
      DUMP_WILL_BE_NOTREACHED()
          << "Failed start for jumplist action: couldn't pick a profile";
    }
  }

  // If the command line has the kNotificationLaunchId switch, then this
  // call is from notification_helper.exe to process toast activation.
  // Delegate to the notification system; do not open a browser window here.
  if (command_line.HasSwitch(switches::kNotificationLaunchId)) {
    if (NotificationPlatformBridgeWin::HandleActivation(command_line)) {
      return true;
    }
    return false;
  }

  // If being started for credential provider logon purpose, only show the
  // signin page.
  if (command_line.HasSwitch(credential_provider::kGcpwSigninSwitch)) {
    // Having access to an incognito profile for this action (as checked below)
    // requires starting with a regular user profile (non-guest) and suppresses
    // profile picker startups, see `ShouldShowProfilePickerAtProcessLaunch()`.
    // TODO(http://crbug.com/1293024): Refactor command line processing logic to
    // validate the flag sets and reliably determine the startup mode.
    CHECK_EQ(profile_info.mode, StartupProfileMode::kBrowserWindow)
        << "Failed start for GCPW signin: couldn't pick a profile";

    // Use incognito profile since this is a credential provider logon.
    Profile* incognito_profile =
        privacy_safe_profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
    DCHECK(incognito_profile->IsIncognitoProfile());
    // NOTE: All launch urls are ignored when running with --gcpw-signin since
    // this mode only loads Google's sign in page.

    // If GCPW signin dialog fails, returning false here will allow Chrome to
    // exit gracefully during the launch.
    if (!StartGCPWSignin(command_line, incognito_profile)) {
      return false;
    }

    return true;
  }
#endif  // BUILDFLAG(IS_WIN)

  if (command_line.HasSwitch(switches::kAppId)) {
    // `switches::kAppId` presence suppresses the profile picker, see
    // `ShouldShowProfilePickerAtProcessLaunch()`.
    // TODO(http://crbug.com/1293024): Refactor command line processing logic to
    // validate the flag sets and reliably determine the startup mode.
    CHECK_EQ(profile_info.mode, StartupProfileMode::kBrowserWindow)
        << "Failed launch with app: couldn't pick a profile";
    std::string app_id = command_line.GetSwitchValueASCII(switches::kAppId);
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
    // If Chrome Apps are deprecated and |app_id| is a Chrome App, display the
    // deprecation UI instead of launching the app.
    if (apps::OpenDeprecatedApplicationPrompt(privacy_safe_profile, app_id)) {
      return true;
    }
#endif
    // If |app_id| is a disabled or terminated platform app we handle it
    // specially here, otherwise it will be handled below.
    if (apps::OpenExtensionApplicationWithReenablePrompt(
            privacy_safe_profile, app_id, command_line, cur_dir)) {
      return true;
    }
  }

  // TODO(http://crbug.com/1293024): Refactor command line processing logic to
  // validate the flag sets and reliably determine the startup mode.
  // Try a shortcut app launch (--app is present).
  // When running in incognito or guest mode, there typically won't be an
  // AppServiceProxyFactory available. The --app command line switch does not
  // require the AppServiceProxyFactory. This also allows the --app parameter
  // to work in conjunction with the --incognito command line parameter.
  if (MaybeLaunchAppShortcutWindow(command_line, cur_dir, is_first_run,
                                   privacy_safe_profile)) {
    return true;
  }

  // Launch the browser if the profile is unable to open web apps.
  if (!CanOpenWebApp(privacy_safe_profile)) {
    LaunchBrowserForLastProfiles(
        command_line, cur_dir, process_startup, is_first_run, profile_info,
        last_opened_profiles, /*restore_tabbed_browser=*/true);
    return true;
  }

  // Try a platform app launch.
  if (MaybeLaunchExtensionApp(command_line, cur_dir, is_first_run,
                              privacy_safe_profile)) {
    return true;
  }

  // This path is mostly used for Window and Linux.
  // On Chrome OS, app launches are routed through the AppService and
  // WebAppPublisherHelper.
  //
  // On Mac, PWA launch is normally handled in
  // web_app_shim_manager_delegate_mac.cc, but if an app shim for whatever
  // reason fails to dlopen chrome we can still end up here. While in that case
  // the launch behavior here isn't quite the correct behavior for an app launch
  // on Mac OS, this behavior is better than nothing and should result in the
  // app shim getting regenerated to hopefully fix future app launches.
  // TODO(crbug.com/40191242): Some integration tests also rely on this
  // code. Ideally those would be fixed to test the normal app launch path on
  // Mac instead, and this code should be changed to make it harder to
  // accidentally write tests that don't test the normal app launch path.
#if !BUILDFLAG(IS_CHROMEOS)
  // Try a web app launch.
  if (web_app::startup::MaybeHandleWebAppLaunch(
          command_line, cur_dir, privacy_safe_profile, is_first_run)) {
    return true;
  }
#endif

  LaunchBrowserForLastProfiles(command_line, cur_dir, process_startup,
                               is_first_run, profile_info, last_opened_profiles,
                               /*restore_tabbed_browser=*/true);
  return true;
}

void StartupBrowserCreator::ProcessLastOpenedProfiles(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    chrome::startup::IsProcessStartup process_startup,
    chrome::startup::IsFirstRun is_first_run,
    Profile* last_used_profile,
    const Profiles& last_opened_profiles) {
  base::CommandLine command_line_without_urls(command_line.GetProgram());
  for (auto& switch_pair : command_line.GetSwitches()) {
    command_line_without_urls.AppendSwitchNative(switch_pair.first,
                                                 switch_pair.second);
  }

  // Launch the profiles in the order they became active.
  for (Profile* profile : last_opened_profiles) {
    DCHECK(!profile->IsGuestSession());

#if !BUILDFLAG(IS_CHROMEOS)
    // Skip any locked profile.
    if (!CanOpenProfileOnStartup(
            {profile, StartupProfileMode::kBrowserWindow})) {
      continue;
    }

    // Guest profiles should not be reopened on startup. This can happen if
    // the last used profile was a Guest, but other profiles were also open
    // when Chrome was closed. In this case, pick a different open profile
    // to be the active one, since the Guest profile is never added to the
    // list of open profiles.
    if (last_used_profile->IsGuestSession()) {
      last_used_profile = profile;
    }
#endif

    // Don't launch additional profiles which would only open a new tab
    // page. When restarting after an update, all profiles will reopen last
    // open pages.
    SessionStartupPref startup_pref =
        GetSessionStartupPref(command_line, profile);
    if (profile != last_used_profile &&
        startup_pref.type == SessionStartupPref::DEFAULT &&
        !HasPendingUncleanExit(profile)) {
      continue;
    }
    // Only record a launch mode histogram for |last_used_profile|. Pass a
    // null launch_mode_recorder for other profiles.
    LaunchBrowser((profile == last_used_profile) ? command_line
                                                 : command_line_without_urls,
                  profile, cur_dir, process_startup, is_first_run,
                  /*restore_tabbed_browser=*/true);
    // We've launched at least one browser.
    process_startup = chrome::startup::IsProcessStartup::kNo;
  }

// Set the |last_used_profile| to activate if a browser is launched for at
// least one profile. Otherwise, show UserManager.
// Note that this must be done after all profiles have
// been launched so the observer knows about all profiles to wait before
// activation this one.
#if !BUILDFLAG(IS_CHROMEOS)
  if (process_startup == chrome::startup::IsProcessStartup::kYes) {
    ProfilePicker::Show(ProfilePicker::Params::FromEntryPoint(
        ProfilePicker::EntryPoint::kOnStartup));
  } else  // NOLINT
#endif
  {
    profile_launch_observer.Get().set_profile_to_activate(last_used_profile);
  }
}

// static
bool StartupBrowserCreator::ProcessLoadApps(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    Profile* profile) {
  base::CommandLine::StringType path_list =
      command_line.GetSwitchValueNative(extensions::switches::kLoadApps);

  base::StringTokenizerT<base::CommandLine::StringType,
                         base::CommandLine::StringType::const_iterator>
      tokenizer(path_list, FILE_PATH_LITERAL(","));

  if (!tokenizer.GetNext()) {
    return false;
  }

  base::FilePath app_absolute_dir =
      base::MakeAbsoluteFilePath(base::FilePath(tokenizer.token_piece()));
  if (!apps::AppLoadService::Get(profile)->LoadAndLaunch(
          app_absolute_dir, command_line, cur_dir)) {
    return false;
  }

  while (tokenizer.GetNext()) {
    app_absolute_dir =
        base::MakeAbsoluteFilePath(base::FilePath(tokenizer.token_piece()));

    if (!apps::AppLoadService::Get(profile)->Load(app_absolute_dir)) {
      return false;
    }
  }

  return true;
}

// static
void StartupBrowserCreator::ProcessCommandLineWithProfile(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    StartupProfileMode mode,
    Profile* profile) {
  DCHECK_NE(mode, StartupProfileMode::kError);
  if (mode == StartupProfileMode::kBrowserWindow && !profile) {
    LOG(ERROR) << "Failed to load the profile.";
    return;
  }
  Profiles last_opened_profiles;
#if !BUILDFLAG(IS_CHROMEOS)
  // On ChromeOS multiple profiles doesn't apply.
  // If no browser windows are open, i.e. the browser is being kept alive in
  // background mode or for other processing, restore |last_opened_profiles|.
  if (chrome::GetTotalBrowserCount() == 0) {
    last_opened_profiles =
        g_browser_process->profile_manager()->GetLastOpenedProfiles();
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  StartupBrowserCreator startup_browser_creator;
  startup_browser_creator.ProcessCmdLineImpl(
      command_line, cur_dir, chrome::startup::IsProcessStartup::kNo,
      {profile, mode}, last_opened_profiles);
}

// static
void StartupBrowserCreator::ProcessCommandLineAlreadyRunning(
    const base::CommandLine& command_line,
    const base::FilePath& cur_dir,
    const StartupProfilePathInfo& profile_path_info) {
  if (profile_path_info.reason == StartupProfileModeReason::kError) {
    return;
  }

  Profile* profile = nullptr;
  StartupProfileMode mode =
      StartupProfileModeFromReason(profile_path_info.reason);
  bool need_profile = mode == StartupProfileMode::kBrowserWindow;
  if (need_profile) {
    ProfileManager* profile_manager = g_browser_process->profile_manager();
    profile = profile_manager->GetProfileByPath(profile_path_info.path);
    // The profile isn't loaded yet and so needs to be loaded asynchronously.
    if (!profile) {
      profile_manager->CreateProfileAsync(
          profile_path_info.path, base::BindOnce(&ProcessCommandLineWithProfile,
                                                 command_line, cur_dir, mode));
      return;
    }
  }

  ProcessCommandLineWithProfile(command_line, cur_dir, mode, profile);
}

// static
void StartupBrowserCreator::OpenStartupPages(
    Browser* browser,
    chrome::startup::IsProcessStartup process_startup) {
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  chrome::startup::IsFirstRun is_first_run =
      first_run::IsChromeFirstRun() ? chrome::startup::IsFirstRun::kYes
                                    : chrome::startup::IsFirstRun::kNo;
  StartupBrowserCreatorImpl startup_browser_creator_impl(
      base::FilePath(), command_line, is_first_run);
  SessionStartupPref session_startup_pref =
      StartupBrowserCreator::GetSessionStartupPref(command_line,
                                                   browser->profile());
  startup_browser_creator_impl.OpenURLsInBrowser(browser, process_startup,
                                                 session_startup_pref.urls);
}

// static
bool StartupBrowserCreator::ActivatedProfile() {
  return profile_launch_observer.Get().activated_profile();
}

bool HasPendingUncleanExit(Profile* profile) {
  return ExitTypeService::GetLastSessionExitType(profile) ==
             ExitType::kCrashed &&
         !profile_launch_observer.Get().HasBeenLaunchedAndBrowserOpen(
             profile) &&
         !base::CommandLine::ForCurrentProcess()->HasSwitch(
             switches::kHideCrashRestoreBubble);
}

void AddLaunchedProfile(Profile* profile) {
  profile_launch_observer.Get().AddLaunched(profile);
}

StartupProfilePathInfo GetStartupProfilePath(
    const base::FilePath& cur_dir,
    const base::CommandLine& command_line,
    bool ignore_profile_picker) {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  const base::FilePath& user_data_dir = profile_manager->user_data_dir();
// If the browser is launched due to activation on Windows native notification,
// the profile id encoded in the notification launch id should be chosen over
// all others.
#if BUILDFLAG(IS_WIN)
  base::FilePath profile_basename =
      NotificationLaunchId::GetNotificationLaunchProfileBaseName(command_line);
  if (!profile_basename.empty()) {
    return {.path = user_data_dir.Append(profile_basename),
            .reason = StartupProfileModeReason::kNotificationLaunchIdWin1};
  }
#endif  // BUILDFLAG(IS_WIN)

  // If opening in Guest mode is requested, load the default profile so that
  // last opened profile would not trigger a user management dialog.
  if (profiles::IsGuestModeRequested(command_line,
                                     g_browser_process->local_state(),
                                     /* show_warning= */ false)) {
    // TODO(crbug.com/40157821): return a guest profile instead.
    return {.path = profiles::GetDefaultProfileDir(user_data_dir),
            .reason = StartupProfileModeReason::kGuestModeRequested};
  }

  base::FilePath command_line_profile_directory =
      command_line.GetSwitchValuePath(switches::kProfileDirectory);

  if (!command_line_profile_directory.empty() &&
      command_line.HasSwitch(switches::kIgnoreProfileDirectoryIfNotExists)) {
    base::FilePath profile_dir_path =
        user_data_dir.Append(command_line_profile_directory);
    // This is a blocking call to the filesystem, but unfortunately it is
    // required for startup to continue, as the
    // `kIgnoreProfileDirectoryIfNotExists` switch needs to check the file
    // system state to know if the profile directory exists.
    base::ScopedAllowBlocking allow_blocking;
    if (IsProfileDirectoryMarkedForDeletion(profile_dir_path) ||
        !base::DirectoryExists(profile_dir_path)) {
      command_line_profile_directory = base::FilePath();
    }
  }

#if BUILDFLAG(IS_MAC)
  // On Mac OS, when an app shim fails to dlopen chrome, the app shim falls back
  // to trying to launch chrome passing its app-id on the command line. In this
  // situation, the profile directory passed on the command line could be an
  // empty string. When this happens, arbitrarily pick one of the profiles the
  // app is installed in as profile directory. While this isn't necesarilly the
  // right profile to use, it is good enough for the purpose of (indirectly)
  // triggering a rebuild of the app shim, which should resolve whatever
  // problem existed that let to this situation.
  if (command_line_profile_directory.empty() &&
      command_line.HasSwitch(switches::kAppId)) {
    std::string app_id = command_line.GetSwitchValueASCII(switches::kAppId);
    std::set<base::FilePath> profile_paths =
        AppShimRegistry::Get()->GetInstalledProfilesForApp(app_id);
    if (!profile_paths.empty()) {
      command_line_profile_directory = profile_paths.begin()->BaseName();
    }
  }
#endif
  if (!command_line_profile_directory.empty()) {
    return {.path = user_data_dir.Append(command_line_profile_directory),
            .reason = StartupProfileModeReason::kProfileDirSwitch};
  }

  if (command_line.HasSwitch(switches::kProfileEmail)) {
    // Use GetSwitchValueNative() rather than GetSwitchValueASCII() to support
    // non-ASCII email addresses.
    base::CommandLine::StringType email_native =
        command_line.GetSwitchValueNative(switches::kProfileEmail);
    if (!email_native.empty()) {
      std::string email;
#if BUILDFLAG(IS_WIN)
      email = base::WideToUTF8(email_native);
#else
      email = std::move(email_native);
#endif
      base::FilePath profile_dir =
          g_browser_process->profile_manager()->GetProfileDirForEmail(email);
      if (!profile_dir.empty()) {
        return {.path = profile_dir,
                .reason = StartupProfileModeReason::kProfileEmailSwitch};
      }
    }
  }

#if BUILDFLAG(IS_CHROMEOS)
  return {.path = profile_manager->GetLastUsedProfileDir(),
          .reason = StartupProfileModeReason::kPickerNotSupported};
#else
  if (ignore_profile_picker) {
    return {.path = profile_manager->GetLastUsedProfileDir(),
            .reason = StartupProfileModeReason::kIgnoreProfilePicker};
  }

  // Open the picker only if no URLs have been provided to launch Chrome. If
  // URLs are provided or if we aren't able to extract them at this stage (e.g.
  // we need a profile to access search engine preferences and attempt to
  // resolve a query into a URL), open them in the last profile, instead.
  auto has_tabs =
      StartupTabProviderImpl().HasCommandLineTabs(command_line, cur_dir);
  if (has_tabs != CommandLineTabsPresent::kNo) {
    return {.path = profile_manager->GetLastUsedProfileDir(),
            .reason = StartupProfileModeReason::kCommandLineTabs};
  }

  StartupProfileModeReason show_picker_reason =
      ShouldShowProfilePickerAtProcessLaunch(
          profile_manager, !command_line_profile_directory.empty(),
          command_line);

  if (StartupProfileModeFromReason(show_picker_reason) ==
      StartupProfileMode::kProfilePicker) {
    return {.path = base::FilePath(), .reason = show_picker_reason};
  }

  return {.path = profile_manager->GetLastUsedProfileDir(),
          .reason = show_picker_reason};
#endif  // !BUILDFLAG(IS_CHROMEOS)
}

#if !BUILDFLAG(IS_CHROMEOS)
StartupProfileInfo GetStartupProfile(const base::FilePath& cur_dir,
                                     const base::CommandLine& command_line) {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  StartupProfilePathInfo path_info = GetStartupProfilePath(
      cur_dir, command_line, /*ignore_profile_picker=*/false);
  DCHECK_NE(path_info.reason, StartupProfileModeReason::kError);
  StartupProfileMode mode = StartupProfileModeFromReason(path_info.reason);
  base::UmaHistogramEnumeration("ProfilePicker.StartupMode.GetStartupProfile",
                                mode);
  base::UmaHistogramEnumeration("ProfilePicker.StartupReason.GetStartupProfile",
                                path_info.reason);

  switch (mode) {
    case StartupProfileMode::kProfilePicker:
      return GetProfilePickerStartupProfileInfo();
    case StartupProfileMode::kError:
      // No more info to add.
      return {nullptr, StartupProfileMode::kError};
    case StartupProfileMode::kBrowserWindow:
      // Try to acquire a profile below.
      break;
  }

  // NOTE: GetProfile() does synchronous file I/O on the main thread.
  Profile* profile = profile_manager->GetProfile(path_info.path);

  // There are several cases where we should show the profile picker:
  // - if there is no entry in profile attributes storage, which means that the
  //   profile is deleted,
  // - if the profile is locked,
  // - if the profile has failed to load
  // When neither of these is true, we can safely start up with `profile`.
  auto* storage = &profile_manager->GetProfileAttributesStorage();
  ProfileAttributesEntry* entry =
      storage->GetProfileAttributesWithPath(path_info.path);
  if (entry && !entry->IsSigninRequired() && profile) {
    return {profile, StartupProfileMode::kBrowserWindow};
  }

  return GetProfilePickerStartupProfileInfo();
}

StartupProfileInfo GetFallbackStartupProfile() {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  // The only known reason for profiles to fail initialization is being unable
  // to create the profile directory, and this has already happened in
  // GetStartupProfilePath() before calling this function. In this case,
  // creation of new profiles is expected to fail. So only existing profiles are
  // attempted for fallback.

  // If the last used profile could not be initialized, see if any of other last
  // opened profiles can be initialized successfully.
  auto* storage = &profile_manager->GetProfileAttributesStorage();
  for (Profile* profile : ProfileManager::GetLastOpenedProfiles()) {
    // Return any profile that is not locked.
    ProfileAttributesEntry* entry =
        storage->GetProfileAttributesWithPath(profile->GetPath());
    if (!entry || !entry->IsSigninRequired()) {
      return {profile, StartupProfileMode::kBrowserWindow};
    }
  }

  // Couldn't initialize any last opened profiles. Try to show the profile
  // picker.
  StartupProfileInfo profile_picker_info = GetProfilePickerStartupProfileInfo();
  if (profile_picker_info.mode != StartupProfileMode::kError) {
    return profile_picker_info;
  }

  // Couldn't show the profile picker either. Try to open any profile that is
  // not locked.
  for (ProfileAttributesEntry* entry : storage->GetAllProfilesAttributes()) {
    if (!entry->IsSigninRequired()) {
      Profile* profile = profile_manager->GetProfile(entry->GetPath());
      if (profile) {
        return {profile, StartupProfileMode::kBrowserWindow};
      }
    }
  }

  return {nullptr, StartupProfileMode::kError};
}
#endif  // !BUILDFLAG(IS_CHROMEOS)