File: arc_session_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (2094 lines) | stat: -rw-r--r-- 78,801 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/session/arc_session_manager.h"

#include <string>
#include <utility>

#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/task/task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "chrome/browser/ash/app_list/arc/arc_app_launcher.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ash/app_list/arc/arc_fast_app_reinstall_starter.h"
#include "chrome/browser/ash/app_list/arc/arc_pai_starter.h"
#include "chrome/browser/ash/app_list/arc/intent.h"
#include "chrome/browser/ash/arc/arc_demo_mode_delegate_impl.h"
#include "chrome/browser/ash/arc/arc_migration_guide_notification.h"
#include "chrome/browser/ash/arc/arc_mount_provider.h"
#include "chrome/browser/ash/arc/arc_optin_uma.h"
#include "chrome/browser/ash/arc/arc_support_host.h"
#include "chrome/browser/ash/arc/arc_ui_availability_reporter.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/auth/arc_auth_service.h"
#include "chrome/browser/ash/arc/optin/arc_terms_of_service_default_negotiator.h"
#include "chrome/browser/ash/arc/optin/arc_terms_of_service_oobe_negotiator.h"
#include "chrome/browser/ash/arc/policy/arc_policy_util.h"
#include "chrome/browser/ash/arc/session/arc_provisioning_result.h"
#include "chrome/browser/ash/guest_os/public/guest_os_service.h"
#include "chrome/browser/ash/guest_os/public/guest_os_service_factory.h"
#include "chrome/browser/ash/login/demo_mode/demo_components.h"
#include "chrome/browser/ash/login/demo_mode/demo_session.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/webui/ash/diagnostics_dialog/diagnostics_dialog.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/components/dbus/session_manager/session_manager_client.h"
#include "chromeos/ash/components/install_attributes/install_attributes.h"
#include "chromeos/ash/components/memory/swap_configuration.h"
#include "chromeos/ash/experiences/arc/app/arc_app_constants.h"
#include "chromeos/ash/experiences/arc/arc_features.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "chromeos/ash/experiences/arc/metrics/arc_metrics_constants.h"
#include "chromeos/ash/experiences/arc/metrics/arc_metrics_service.h"
#include "chromeos/ash/experiences/arc/metrics/stability_metrics_manager.h"
#include "chromeos/ash/experiences/arc/session/arc_data_remover.h"
#include "chromeos/ash/experiences/arc/session/arc_instance_mode.h"
#include "chromeos/ash/experiences/arc/session/arc_management_transition.h"
#include "chromeos/ash/experiences/arc/session/arc_session.h"
#include "chromeos/ash/experiences/arc/session/arc_session_runner.h"
#include "chromeos/ash/experiences/arc/session/serial_number_util.h"
#include "components/account_id/account_id.h"
#include "components/exo/wm_helper.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "components/session_manager/core/session_manager.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "ui/display/types/display_constants.h"

// Enable VLOG level 1.
#undef ENABLED_VLOG_LEVEL
#define ENABLED_VLOG_LEVEL 1

namespace arc {

namespace {

// Weak pointer.  This class is owned by ArcServiceLauncher.
ArcSessionManager* g_arc_session_manager = nullptr;

// Allows the session manager to skip creating UI in unit tests.
bool g_ui_enabled = true;

constexpr const char kArcSaltPath[] = "/var/lib/misc/arc_salt";

constexpr const char kArcPrepareHostGeneratedDirJobName[] =
    "arc_2dprepare_2dhost_2dgenerated_2ddir";

// Maximum amount of time we'll wait for ARC to finish booting up. Once this
// timeout expires, keep ARC running in case the user wants to file feedback,
// but present the UI to try again.
constexpr base::TimeDelta kArcSignInTimeout = base::Minutes(5);

// Updates UMA with user cancel only if error is not currently shown.
void MaybeUpdateOptInCancelUMA(const ArcSupportHost* support_host) {
  if (!support_host ||
      support_host->ui_page() == ArcSupportHost::UIPage::NO_PAGE ||
      support_host->ui_page() == ArcSupportHost::UIPage::ERROR) {
    return;
  }

  UpdateOptInCancelUMA(OptInCancelReason::USER_CANCEL);
}

// Returns true if launching the Play Store on OptIn succeeded is needed.
// Launch Play Store app, except for the following cases:
// * When Opt-in verification is disabled (for tests);
// * In case ARC is enabled from OOBE.
// * In Public Session mode, because Play Store will be hidden from users
//   and only apps configured by policy should be installed.
// * When ARC is managed, and user does not go through OOBE opt-in,
//   because the whole OptIn flow should happen as seamless as possible for
//   the user.
// Some tests require the Play Store to be shown and forces this using chromeos
// switch kArcForceShowPlayStoreApp.
bool ShouldLaunchPlayStoreApp(Profile* profile,
                              bool oobe_or_assistant_wizard_start) {
  if (!IsPlayStoreAvailable()) {
    return false;
  }

  if (oobe_or_assistant_wizard_start) {
    return false;
  }

  if (ShouldShowOptInForTesting()) {
    return true;
  }

  if (IsRobotOrOfflineDemoAccountMode()) {
    return false;
  }

  if (IsArcOptInVerificationDisabled()) {
    return false;
  }

  if (ShouldStartArcSilentlyForManagedProfile(profile)) {
    return false;
  }

  return true;
}

// Defines the conditions that require UI to present eventual error conditions
// to the end user.
//
// Don't show UI for MGS sessions in demo mode because the only one UI must be
// the demo app. In case of error the UI will be useless as well, because
// in typical use case there will be no one nearby the demo device, who can
// do some action to solve the problem be means of UI.
// All other managed sessions will be attended by a user and require an error
// UI.
bool ShouldUseErrorDialog() {
  if (!g_ui_enabled) {
    return false;
  }

  if (IsArcOptInVerificationDisabled()) {
    return false;
  }

  if (ash::DemoSession::IsDeviceInDemoMode()) {
    return false;
  }

  return true;
}

void ResetStabilityMetrics() {
  // TODO(shaochuan): Make this an event observable by StabilityMetricsManager
  // and eliminate this null check.
  auto* stability_metrics_manager = StabilityMetricsManager::Get();
  if (!stability_metrics_manager) {
    return;
  }
  stability_metrics_manager->ResetMetrics();
}

void SetArcEnabledStateMetric(bool enabled) {
  // TODO(shaochuan): Make this an event observable by StabilityMetricsManager
  // and eliminate this null check.
  auto* stability_metrics_manager = StabilityMetricsManager::Get();
  if (!stability_metrics_manager) {
    return;
  }
  stability_metrics_manager->SetArcEnabledState(enabled);
}

int GetSignInErrorCode(const arc::mojom::ArcSignInError* sign_in_error) {
  if (!sign_in_error) {
    return 0;
  }

#define IF_ERROR_RETURN_CODE(name, type)                          \
  if (sign_in_error->is_##name()) {                               \
    return static_cast<std::underlying_type_t<arc::mojom::type>>( \
        sign_in_error->get_##name());                             \
  }

  IF_ERROR_RETURN_CODE(cloud_provision_flow_error, CloudProvisionFlowError)
  IF_ERROR_RETURN_CODE(general_error, GeneralSignInError)
  IF_ERROR_RETURN_CODE(check_in_error, GMSCheckInError)
  IF_ERROR_RETURN_CODE(sign_in_error, GMSSignInError)
#undef IF_ERROR_RETURN_CODE

  LOG(ERROR) << "Unknown sign-in error "
             << std::underlying_type_t<arc::mojom::ArcSignInError::Tag>(
                    sign_in_error->which())
             << ".";

  return -1;
}

ArcSupportHost::Error GetCloudProvisionFlowError(
    mojom::CloudProvisionFlowError cloud_provision_flow_error) {
  switch (cloud_provision_flow_error) {
    case mojom::CloudProvisionFlowError::ERROR_ENROLLMENT_TOKEN_INVALID:
      return ArcSupportHost::Error::
          SIGN_IN_CLOUD_PROVISION_FLOW_ENROLLMENT_TOKEN_INVALID;

    case mojom::CloudProvisionFlowError::ERROR_DEVICE_QUOTA_EXCEEDED:
      return ArcSupportHost::Error::
          SIGN_IN_CLOUD_PROVISION_FLOW_DOMAIN_JOIN_FAIL_ERROR;

    case mojom::CloudProvisionFlowError::ERROR_NETWORK_UNAVAILABLE:
      return ArcSupportHost::Error::SIGN_IN_CLOUD_PROVISION_FLOW_NETWORK_ERROR;

    case mojom::CloudProvisionFlowError::ERROR_USER_CANCEL:
      return ArcSupportHost::Error::
          SIGN_IN_CLOUD_PROVISION_FLOW_INTERRUPTED_ERROR;

    case mojom::CloudProvisionFlowError::ERROR_NO_ACCOUNT_IN_WORK_PROFILE:
      return ArcSupportHost::Error::
          SIGN_IN_CLOUD_PROVISION_FLOW_ACCOUNT_MISSING_ERROR;

    case mojom::CloudProvisionFlowError::ERROR_ACCOUNT_NOT_READY:
    case mojom::CloudProvisionFlowError::ERROR_ACCOUNT_NOT_ALLOWLISTED:
    case mojom::CloudProvisionFlowError::ERROR_DPC_SUPPORT:
    case mojom::CloudProvisionFlowError::ERROR_ENTERPRISE_INVALID:
      return ArcSupportHost::Error::
          SIGN_IN_CLOUD_PROVISION_FLOW_PERMANENT_ERROR;

    case mojom::CloudProvisionFlowError::ERROR_ACCOUNT_OTHER:
    case mojom::CloudProvisionFlowError::ERROR_ADD_ACCOUNT_FAILED:
    case mojom::CloudProvisionFlowError::ERROR_CHECKIN_FAILED:
    case mojom::CloudProvisionFlowError::ERROR_INVALID_POLICY_STATE:
    case mojom::CloudProvisionFlowError::ERROR_INVALID_SETUP_ACTION:
    case mojom::CloudProvisionFlowError::ERROR_JSON:
    case mojom::CloudProvisionFlowError::ERROR_MANAGED_PROVISIONING_FAILED:
    case mojom::CloudProvisionFlowError::
        ERROR_OAUTH_TOKEN_AUTHENTICATOR_EXCEPTION:
    case mojom::CloudProvisionFlowError::ERROR_OAUTH_TOKEN_IO_EXCEPTION:
    case mojom::CloudProvisionFlowError::
        ERROR_OAUTH_TOKEN_OPERATION_CANCELED_EXCEPTION:
    case mojom::CloudProvisionFlowError::ERROR_OAUTH_TOKEN:
    case mojom::CloudProvisionFlowError::ERROR_OTHER:
    case mojom::CloudProvisionFlowError::ERROR_QUARANTINE:
    case mojom::CloudProvisionFlowError::ERROR_REMOVE_ACCOUNT_FAILED:
    case mojom::CloudProvisionFlowError::ERROR_REQUEST_ANDROID_ID_FAILED:
    case mojom::CloudProvisionFlowError::ERROR_SERVER_TRANSIENT_ERROR:
    case mojom::CloudProvisionFlowError::ERROR_SERVER:
    case mojom::CloudProvisionFlowError::ERROR_TIMEOUT:
    default:
      return ArcSupportHost::Error::
          SIGN_IN_CLOUD_PROVISION_FLOW_TRANSIENT_ERROR;
  }
}

ArcSupportHost::Error GetSupportHostError(const ArcProvisioningResult& result) {
  if (result.gms_sign_in_error() ==
      mojom::GMSSignInError::GMS_SIGN_IN_NETWORK_ERROR) {
    return ArcSupportHost::Error::SIGN_IN_NETWORK_ERROR;
  }

  if (result.gms_sign_in_error() ==
      mojom::GMSSignInError::GMS_SIGN_IN_BAD_AUTHENTICATION) {
    return ArcSupportHost::Error::SIGN_IN_BAD_AUTHENTICATION_ERROR;
  }

  if (result.gms_sign_in_error()) {
    return ArcSupportHost::Error::SIGN_IN_GMS_SIGNIN_ERROR;
  }

  if (result.gms_check_in_error()) {
    return ArcSupportHost::Error::SIGN_IN_GMS_CHECKIN_ERROR;
  }

  if (result.cloud_provision_flow_error()) {
    return GetCloudProvisionFlowError(
        result.cloud_provision_flow_error().value());
  }

  if (result.general_error() ==
      mojom::GeneralSignInError::CHROME_SERVER_COMMUNICATION_ERROR) {
    return ArcSupportHost::Error::SERVER_COMMUNICATION_ERROR;
  }

  if (result.general_error() ==
      mojom::GeneralSignInError::NO_NETWORK_CONNECTION) {
    return ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR;
  }

  if (result.general_error() == mojom::GeneralSignInError::ARC_DISABLED) {
    return ArcSupportHost::Error::ANDROID_MANAGEMENT_REQUIRED_ERROR;
  }

  if (result.stop_reason() == ArcStopReason::LOW_DISK_SPACE) {
    return ArcSupportHost::Error::LOW_DISK_SPACE_ERROR;
  }

  return ArcSupportHost::Error::SIGN_IN_UNKNOWN_ERROR;
}

bool ShouldShowNetworkTests(const ArcProvisioningResult& result) {
  if (result.gms_sign_in_error() ==
          mojom::GMSSignInError::GMS_SIGN_IN_TIMEOUT ||
      result.gms_sign_in_error() ==
          mojom::GMSSignInError::GMS_SIGN_IN_SERVICE_UNAVAILABLE ||
      result.gms_sign_in_error() ==
          mojom::GMSSignInError::GMS_SIGN_IN_NETWORK_ERROR) {
    return true;
  }

  if (result.gms_check_in_error() ==
          mojom::GMSCheckInError::GMS_CHECK_IN_FAILED ||
      result.gms_check_in_error() ==
          mojom::GMSCheckInError::GMS_CHECK_IN_TIMEOUT) {
    return true;
  }

  if (result.cloud_provision_flow_error() ==
          mojom::CloudProvisionFlowError::ERROR_SERVER_TRANSIENT_ERROR ||
      result.cloud_provision_flow_error() ==
          mojom::CloudProvisionFlowError::ERROR_TIMEOUT ||
      result.cloud_provision_flow_error() ==
          mojom::CloudProvisionFlowError::ERROR_NETWORK_UNAVAILABLE ||
      result.cloud_provision_flow_error() ==
          mojom::CloudProvisionFlowError::ERROR_SERVER) {
    return true;
  }

  if (result.general_error() ==
          mojom::GeneralSignInError::GENERIC_PROVISIONING_TIMEOUT ||
      result.general_error() ==
          mojom::GeneralSignInError::NO_NETWORK_CONNECTION ||
      result.general_error() ==
          mojom::GeneralSignInError::CHROME_SERVER_COMMUNICATION_ERROR) {
    return true;
  }
  return false;
}

ArcSessionManager::ExpansionResult ReadSaltInternal() {
  DCHECK(arc::IsArcVmEnabled());

  // For ARCVM, read |kArcSaltPath| if that exists.
  std::optional<std::string> salt =
      ReadSaltOnDisk(base::FilePath(kArcSaltPath));
  if (!salt) {
    return ArcSessionManager::ExpansionResult{{}, false};
  }
  return ArcSessionManager::ExpansionResult{std::move(*salt), true};
}

// Inform ArcMetricsServices about the starting time of ARC provisioning.
void ReportProvisioningStartTime(const base::TimeTicks& start_time,
                                 Profile* profile) {
  ArcMetricsService* metrics_service =
      ArcMetricsService::GetForBrowserContext(profile);
  // metrics_service might be null in unit tests.
  if (metrics_service) {
    auto account_type_suffix = GetHistogramNameByUserType("", profile);
    metrics_service->ReportProvisioningStartTime(start_time,
                                                 account_type_suffix);
  }
}

// Returns whether ARCVM /data migration is in progress and should be resumed.
bool ArcVmDataMigrationIsInProgress(PrefService* prefs) {
  if (!base::FeatureList::IsEnabled(kEnableArcVmDataMigration)) {
    return false;
  }
  return GetArcVmDataMigrationStatus(prefs) ==
         ArcVmDataMigrationStatus::kStarted;
}

// The result status of deferring ARC activation until user session start up
// task completion, used for UMA.
enum class DeferArcActivationResult {
  // Decided to defer, and the prediction succeeded, i.e. no activation
  // happens during user session start up.
  kDeferSucceeded = 0,

  // Decided to defer, but the prediction failed, i.e. an activation happens
  // during user session start up.
  kDeferFailed = 1,

  // Decided not to defer, and the prediction succeeded, i.e. an activation
  // happens during user session start up.
  kNotDeferSucceeded = 2,

  // Decided not to defer, and the prediction failed, i.e. no activation
  // happens during user session start up.
  kNotDeferFailed = 3,

  kMaxValue = kNotDeferFailed,
};

enum class DeferArcActivationCategory {
  // ARC activation is deferred until the user session start up task completion.
  kDeferred = 0,

  // ARC activation is not deferred, because the user is suspected to activate
  // ARC very soon.
  kNotDeferred = 1,

  // ARC is already activated, or the user session start up tasks are already
  // completed. Thus, it was out of scope to decide deferring.
  kNotTarget = 2,

  kMaxValue = kNotTarget,
};

// Using 1ms as minimum for common practice.
// The delay will be up to 20 seconds, because of the timer in the tracker.
// Using 25 secs just in case for additional buffer. The number of buckets are
// linearly extrapolated from the common one.
void UmaHistogramDeferActivationTimes(const std::string& name,
                                      base::TimeDelta elapsed) {
  base::UmaHistogramCustomTimes(name, elapsed, base::Milliseconds(1),
                                base::Seconds(25), 125);
}

}  // namespace

// This class is used to track statuses on OptIn flow. It is created in case ARC
// is activated, and it needs to OptIn. Once started OptInFlowResult::STARTED is
// recorded via UMA. If it finishes successfully OptInFlowResult::SUCCEEDED is
// recorded. Optional OptInFlowResult::SUCCEEDED_AFTER_RETRY is recorded in this
// case if an error occurred during OptIn flow, and user pressed Retry. In case
// the user cancels OptIn flow before it was completed then
// OptInFlowResult::CANCELED is recorded and if an error occurred optional
// OptInFlowResult::CANCELED_AFTER_ERROR. If a shutdown happens during the OptIn
// nothing is recorded, except initial OptInFlowResult::STARTED.
// OptInFlowResult::STARTED = OptInFlowResult::SUCCEEDED +
// OptInFlowResult::CANCELED + cases happened during the shutdown.
class ArcSessionManager::ScopedOptInFlowTracker {
 public:
  ScopedOptInFlowTracker() {
    UpdateOptInFlowResultUMA(OptInFlowResult::STARTED);
  }

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

  ~ScopedOptInFlowTracker() {
    if (shutdown_) {
      return;
    }

    UpdateOptInFlowResultUMA(success_ ? OptInFlowResult::SUCCEEDED
                                      : OptInFlowResult::CANCELED);
    if (error_) {
      UpdateOptInFlowResultUMA(success_
                                   ? OptInFlowResult::SUCCEEDED_AFTER_RETRY
                                   : OptInFlowResult::CANCELED_AFTER_ERROR);
    }
  }

  // Tracks error occurred during the OptIn flow.
  void TrackError() {
    DCHECK(!success_ && !shutdown_);
    error_ = true;
  }

  // Tracks that OptIn finished successfully.
  void TrackSuccess() {
    DCHECK(!success_ && !shutdown_);
    success_ = true;
  }

  // Tracks that OptIn was not completed before shutdown.
  void TrackShutdown() {
    DCHECK(!success_ && !shutdown_);
    shutdown_ = true;
  }

 private:
  bool error_ = false;
  bool success_ = false;
  bool shutdown_ = false;
};

ArcSessionManager::ArcSessionManager(
    std::unique_ptr<ArcSessionRunner> arc_session_runner,
    std::unique_ptr<AdbSideloadingAvailabilityDelegateImpl>
        adb_sideloading_availability_delegate)
    : arc_session_runner_(std::move(arc_session_runner)),
      adb_sideloading_availability_delegate_(
          std::move(adb_sideloading_availability_delegate)),
      android_management_checker_factory_(
          ArcRequirementChecker::GetDefaultAndroidManagementCheckerFactory()),
      attempt_user_exit_callback_(base::BindRepeating(chrome::AttemptUserExit)),
      attempt_restart_callback_(base::BindRepeating(chrome::AttemptRestart)) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!g_arc_session_manager);
  g_arc_session_manager = this;
  arc_session_runner_->AddObserver(this);
  arc_session_runner_->SetDemoModeDelegate(
      std::make_unique<ArcDemoModeDelegateImpl>());
  if (ash::SessionManagerClient::Get()) {
    ash::SessionManagerClient::Get()->AddObserver(this);
  }
  ResetStabilityMetrics();
  ash::ConciergeClient::Get()->AddVmObserver(this);
}

ArcSessionManager::~ArcSessionManager() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  ash::ConciergeClient::Get()->RemoveVmObserver(this);

  if (ash::SessionManagerClient::Get()) {
    ash::SessionManagerClient::Get()->RemoveObserver(this);
  }

  internal_state_ = InternalState::kDestroying;
  Shutdown();
  DCHECK(arc_session_runner_);
  arc_session_runner_->RemoveObserver(this);

  DCHECK_EQ(this, g_arc_session_manager);
  g_arc_session_manager = nullptr;
}

// static
ArcSessionManager* ArcSessionManager::Get() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return g_arc_session_manager;
}

// static
void ArcSessionManager::SetUiEnabledForTesting(bool enable) {
  g_ui_enabled = enable;
  ArcRequirementChecker::SetUiEnabledForTesting(enable);
}

// static
void ArcSessionManager::SetArcTermsOfServiceOobeNegotiatorEnabledForTesting(
    bool enable) {
  ArcRequirementChecker::SetArcTermsOfServiceOobeNegotiatorEnabledForTesting(
      enable);
}

// static
void ArcSessionManager::EnableCheckAndroidManagementForTesting(bool enable) {
  ArcRequirementChecker::EnableCheckAndroidManagementForTesting(enable);
}

void ArcSessionManager::OnSessionStopped(ArcStopReason reason,
                                         bool restarting) {
  if (restarting) {
    DCHECK_EQ(state_, State::ACTIVE);
    // If ARC is being restarted, here do nothing, and just wait for its
    // next run.
    return;
  }

  DCHECK(state_ == State::ACTIVE || state_ == State::STOPPING) << state_;
  state_ = State::STOPPED;

  if (arc_sign_in_timer_.IsRunning()) {
    OnProvisioningFinished(ArcProvisioningResult(reason));
  }

  for (auto& observer : observer_list_) {
    observer.OnArcSessionStopped(reason);
  }

  MaybeStartArcDataRemoval();
}

void ArcSessionManager::OnSessionRestarting() {
  for (auto& observer : observer_list_) {
    observer.OnArcSessionRestarting();
  }
}

void ArcSessionManager::OnProvisioningFinished(
    const ArcProvisioningResult& result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // If the Mojo message to notify finishing the provisioning is already sent
  // from the container, it will be processed even after requesting to stop the
  // container. Ignore all |result|s arriving while ARC is disabled, in order to
  // avoid popping up an error message triggered below. This code intentionally
  // does not support the case of re-enabling.
  if (!enable_requested_) {
    LOG(WARNING) << "Provisioning result received after ARC was disabled. "
                 << "Ignoring result " << result << ".";
    return;
  }

  // Due asynchronous nature of stopping the ARC instance,
  // OnProvisioningFinished may arrive after setting the |State::STOPPED| state
  // and |State::Active| is not guaranteed to be set here.
  // prefs::kArcDataRemoveRequested also can be active for now.

  const bool provisioning_successful = result.is_success();
  if (provisioning_reported_) {
    // We don't expect success ArcProvisioningResult to be reported twice
    // or reported after an error.
    DCHECK(!provisioning_successful);
    // TODO(khmel): Consider changing LOG to NOTREACHED once we guaranty that
    // no double message can happen in production.
    LOG(WARNING) << "Provisioning result was already reported. Ignoring "
                 << "additional result " << result << ".";
    return;
  }
  provisioning_reported_ = true;
  if (scoped_opt_in_tracker_ && !provisioning_successful) {
    scoped_opt_in_tracker_->TrackError();
  }

  if (result.general_error() ==
      mojom::GeneralSignInError::CHROME_SERVER_COMMUNICATION_ERROR) {
    // TODO(poromov): Consider ARC PublicSession offline mode.
    // Currently ARC session will be exited below, while the main user session
    // will be kept alive without Android apps.
    if (IsRobotOrOfflineDemoAccountMode()) {
      VLOG(1) << "Robot account auth code fetching error";
    }

    // For backwards compatibility, use NETWORK_ERROR for
    // CHROME_SERVER_COMMUNICATION_ERROR case.
    UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR);
  } else if (!sign_in_start_time_.is_null()) {
    DCHECK(profile_);
    arc_sign_in_timer_.Stop();

    UpdateProvisioningTiming(base::TimeTicks::Now() - sign_in_start_time_,
                             provisioning_successful, profile_);
    UpdateProvisioningStatusUMA(GetProvisioningStatus(result), profile_);

    if (result.gms_sign_in_error()) {
      UpdateProvisioningSigninResultUMA(
          GetSigninErrorResult(result.gms_sign_in_error().value()), profile_);
    } else if (result.gms_check_in_error()) {
      UpdateProvisioningCheckinResultUMA(
          GetCheckinErrorResult(result.gms_check_in_error().value()), profile_);
    } else if (result.cloud_provision_flow_error()) {
      UpdateProvisioningDpcResultUMA(
          GetDpcErrorResult(result.cloud_provision_flow_error().value()),
          profile_);
    }

    if (!provisioning_successful) {
      UpdateOptInCancelUMA(OptInCancelReason::PROVISIONING_FAILED);
    }
  }

  PrefService* const prefs = profile_->GetPrefs();
  CHECK(prefs);
  if (provisioning_successful) {
    if (support_host_) {
      support_host_->Close();
    }

    if (scoped_opt_in_tracker_) {
      scoped_opt_in_tracker_->TrackSuccess();
      scoped_opt_in_tracker_.reset();
    }

    bool managed = policy_util::IsAccountManaged(profile_);
    if (managed) {
      UpdateProvisioningDpcResultUMA(ArcProvisioningDpcResult::kSuccess,
                                     profile_);
    } else {
      UpdateProvisioningSigninResultUMA(ArcProvisioningSigninResult::kSuccess,
                                        profile_);
    }
    UpdateProvisioningCheckinResultUMA(ArcProvisioningCheckinResult::kSuccess,
                                       profile_);

    prefs->SetBoolean(prefs::kArcIsManaged, managed);

    if (prefs->HasPrefPath(prefs::kArcSignedIn) &&
        prefs->GetBoolean(prefs::kArcSignedIn)) {
      return;
    }

    prefs->SetBoolean(prefs::kArcSignedIn, true);

    if (ShouldLaunchPlayStoreApp(
            profile_,
            prefs->GetBoolean(prefs::kArcProvisioningInitiatedFromOobe))) {
      playstore_launcher_ = std::make_unique<ArcAppLauncher>(
          profile_, kPlayStoreAppId,
          apps_util::MakeIntentForActivity(
              kPlayStoreActivity, kInitialStartParam, kCategoryLauncher),
          false /* deferred_launch_allowed */, display::kInvalidDisplayId,
          apps::LaunchSource::kFromChromeInternal);
    }

    prefs->ClearPref(prefs::kArcProvisioningInitiatedFromOobe);

    for (auto& observer : observer_list_) {
      observer.OnArcInitialStart();
    }
    return;
  }

  VLOG(1) << "ARC provisioning failed: " << result << ".";

  if (result.stop_reason()) {
    if (prefs->HasPrefPath(prefs::kArcSignedIn)) {
      prefs->SetBoolean(prefs::kArcSignedIn, false);
    }
    VLOG(1) << "ARC stopped unexpectedly";
    ShutdownSession();
  }

  if (result.cloud_provision_flow_error() ||
      // OVERALL_SIGN_IN_TIMEOUT might be an indication that ARC believes it is
      // fully setup, but Chrome does not.
      result.is_timedout() ||
      // Just to be safe, remove data if we don't know the cause.
      result.general_error() == mojom::GeneralSignInError::UNKNOWN_ERROR) {
    VLOG(1) << "ARC provisioning failed permanently. Removing user data";
    RequestArcDataRemoval();
  }

  std::optional<int> error_code;
  ArcSupportHost::Error support_error = GetSupportHostError(result);
  if (support_error == ArcSupportHost::Error::SIGN_IN_UNKNOWN_ERROR) {
    error_code = static_cast<std::underlying_type_t<ProvisioningStatus>>(
        GetProvisioningStatus(result));
  } else if (result.sign_in_error()) {
    error_code = GetSignInErrorCode(result.sign_in_error());
  }

  ShowArcSupportHostError({support_error, error_code} /* error_info */,
                          true /* should_show_send_feedback */,
                          ShouldShowNetworkTests(result));
}

bool ArcSessionManager::IsAllowed() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return profile_ != nullptr;
}

void ArcSessionManager::SetProfile(Profile* profile) {
  if (internal_state_ != InternalState::kNotInitialized &&
      !base::CommandLine::ForCurrentProcess()->HasSwitch(
          ::switches::kTestType)) {
    // This should not be called twice, except in tests.
    base::debug::DumpWithoutCrashing();
  }
  internal_state_ = InternalState::kRunning;

  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!profile_);
  DCHECK(IsArcAllowedForProfile(profile));
  DCHECK(adb_sideloading_availability_delegate_);
  adb_sideloading_availability_delegate_->SetProfile(profile);
  profile_ = profile;
  // RequestEnable() requires |profile_| set, therefore shouldn't have been
  // called at this point.
  SetArcEnabledStateMetric(false);
  session_manager_observation_.Observe(session_manager::SessionManager::Get());
}

void ArcSessionManager::SetUserInfo() {
  DCHECK(profile_);
  DCHECK(arc_session_runner_);

  const AccountId account(multi_user_util::GetAccountIdFromProfile(profile_));
  const cryptohome::Identification cryptohome_id(account);
  const std::string user_id_hash =
      ash::ProfileHelper::GetUserIdHashFromProfile(profile_);

  std::string serialno = GetSerialNumber();
  arc_session_runner_->SetUserInfo(cryptohome_id, user_id_hash, serialno);
}

void ArcSessionManager::TrimVmMemory(TrimVmMemoryCallback callback,
                                     int page_limit) {
  arc_session_runner_->TrimVmMemory(std::move(callback), page_limit);
}

std::string ArcSessionManager::GetSerialNumberForKeyMint() {
  DCHECK(arc::IsArcVmEnabled());
  if (!arc_salt_on_disk_.has_value()) {
    arc_salt_on_disk_ = ReadSaltOnDisk(base::FilePath(kArcSaltPath));
  }
  return GetSerialNumber();
}

std::string ArcSessionManager::GetSerialNumber() const {
  DCHECK(profile_);
  DCHECK(arc_salt_on_disk_);

  const AccountId account(multi_user_util::GetAccountIdFromProfile(profile_));
  const std::string user_id_hash =
      ash::ProfileHelper::GetUserIdHashFromProfile(profile_);

  std::string serialno;
  // ARC container doesn't need the serial number.
  if (arc::IsArcVmEnabled()) {
    const std::string chromeos_user =
        cryptohome::CreateAccountIdentifierFromAccountId(account).account_id();
    serialno = GetOrCreateSerialNumber(g_browser_process->local_state(),
                                       chromeos_user, *arc_salt_on_disk_);
  }
  return serialno;
}

void ArcSessionManager::Initialize() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile_);

  DCHECK_EQ(state_, State::NOT_INITIALIZED);
  state_ = State::STOPPED;

  // If ExpandPropertyFilesAndReadSaltInternal() takes time to finish,
  // Initialize() may be called before it finishes. In that case,
  // SetUserInfo() is called in OnExpandPropertyFilesAndReadSalt().
  if (arc_salt_on_disk_) {
    VLOG(1) << "Calling SetUserInfo() in ArcSessionManager::Initialize";
    SetUserInfo();
  }

  // Create the support host at initialization. Note that, practically,
  // ARC support Chrome app is rarely used (only opt-in and re-auth flow).
  // So, it may be better to initialize it lazily.
  // TODO(hidehiko): Revisit to think about lazy initialization.
  if (ShouldUseErrorDialog()) {
    DCHECK(!support_host_);
    support_host_ = std::make_unique<ArcSupportHost>(profile_);
    support_host_->SetErrorDelegate(this);
  }
  auto* prefs = profile_->GetPrefs();
  const cryptohome::Identification cryptohome_id(
      multi_user_util::GetAccountIdFromProfile(profile_));
  data_remover_ = std::make_unique<ArcDataRemover>(prefs, cryptohome_id);

  if (ArcVmDataMigrationIsInProgress(prefs)) {
    const int auto_resume_count =
        prefs->GetInteger(prefs::kArcVmDataMigrationAutoResumeCount);
    if (auto_resume_count <= kArcVmDataMigrationMaxAutoResumeCount) {
      // |auto_resume_count| == kArcVmDataMigrationMaxAutoResumeCount means that
      // this is the first ARC session in which auto-resume is disabled.
      // Report to UMA and increment the pref value so that we can track the
      // number of users who hit the maximum number of auto-resumes.
      base::UmaHistogramExactLinear("Arc.VmDataMigration.AutoResumeCount",
                                    auto_resume_count,
                                    kArcVmDataMigrationMaxAutoResumeCount);
      prefs->SetInteger(prefs::kArcVmDataMigrationAutoResumeCount,
                        auto_resume_count + 1);
      if (auto_resume_count < kArcVmDataMigrationMaxAutoResumeCount) {
        VLOG(1) << "ARCVM /data migration is in progress. Restarting Chrome "
                   "session to resume the migration. Auto-resume count: "
                << auto_resume_count;
        attempt_restart_callback_.Run();
        return;
      }
    }
    LOG(WARNING) << "Skipping auto-resume of ARCVM /data migration, because it "
                    "has reached the maximum number of retries";
  }

  // Chrome may be shut down before completing ARC data removal.
  // For such a case, start removing the data now, if necessary.
  MaybeStartArcDataRemoval();
}

void ArcSessionManager::Shutdown() {
  VLOG(1) << "Shutting down session manager";

  // We expect this is called twice, once from ArcServiceLauncher
  // then the internal state is switched to kRunning -> kShutdown,
  // followed by the one from dtor, then the state is kDestroying.
  // All cases should happen after stopping RunLoop.
  bool expected = true;
  if (base::RunLoop::IsRunningOnCurrentThread()) {
    LOG(ERROR) << "Shutdown is called while message loop is running";
    expected = false;
  }
  switch (internal_state_) {
    case InternalState::kNotInitialized:
    case InternalState::kRunning:
      // If the device is shutdown on login screen, ArcSessionManager state is
      // kNotInitialized. Otherwise, i.e., if it's shut down from a user session
      // the internal state should be kRunning.
      internal_state_ = InternalState::kShutdown;
      break;
    case InternalState::kShutdown:
      LOG(ERROR) << "Unexpected state: kShutdown";
      expected = false;
      break;
    case InternalState::kDestroying:
      // Do nothing.
      break;
  }
  if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
          ::switches::kTestType) &&
      !expected) {
    base::debug::DumpWithoutCrashing();
  }

  enable_requested_ = false;
  ResetArcState();
  session_manager_observation_.Reset();
  arc_session_runner_->OnShutdown();
  data_remover_.reset();
  if (support_host_) {
    support_host_->SetErrorDelegate(nullptr);
    support_host_->Close();
    support_host_.reset();
  }
  pai_starter_.reset();
  fast_app_reinstall_starter_.reset();
  arc_ui_availability_reporter_.reset();
  profile_ = nullptr;
  state_ = State::NOT_INITIALIZED;
  if (scoped_opt_in_tracker_) {
    scoped_opt_in_tracker_->TrackShutdown();
    scoped_opt_in_tracker_.reset();
  }
  for (auto& observer : observer_list_) {
    observer.OnShutdown();
  }
}

void ArcSessionManager::ShutdownSession() {
  ResetArcState();
  switch (state_) {
    case State::NOT_INITIALIZED:
      // Ignore in NOT_INITIALIZED case. This is called in initial SetProfile
      // invocation.
      // TODO(hidehiko): Remove this along with the clean up.
    case State::STOPPED:
      // Currently, ARC is stopped. Do nothing.
    case State::REMOVING_DATA_DIR:
      // When data removing is done, |state_| will be set to STOPPED.
      // Do nothing here.
    case State::CHECKING_DATA_MIGRATION_NECESSITY:
      // Checking whether /data migration is necessary. |state_| will be set to
      // STOPPED when the check is done. Do nothing.
    case State::STOPPING:
      // Now ARC is stopping. Do nothing here.
      VLOG(1) << "Skipping session shutdown because state is: " << state_;
      break;
    case State::CHECKING_REQUIREMENTS:
    case State::READY:
      // We need to kill the mini-container that might be running here.
      arc_session_runner_->RequestStop();
      // While RequestStop is asynchronous, ArcSessionManager is agnostic to the
      // state of the mini-container, so we can set it's state_ to STOPPED
      // immediately.
      state_ = State::STOPPED;
      break;
    case State::ACTIVE:
      // Request to stop the ARC. |state_| will be set to STOPPED eventually.
      // Set state before requesting the runner to stop in order to prevent the
      // case when |OnSessionStopped| can be called inline and as result
      // |state_| might be changed.
      state_ = State::STOPPING;
      arc_session_runner_->RequestStop();
      break;
  }
}

void ArcSessionManager::ResetArcState() {
  pre_start_time_ = base::TimeTicks();
  start_time_ = base::TimeTicks();
  arc_sign_in_timer_.Stop();
  playstore_launcher_.reset();
  requirement_checker_.reset();
}

void ArcSessionManager::AddObserver(ArcSessionManagerObserver* observer) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  observer_list_.AddObserver(observer);
  if (property_files_expansion_result_) {
    observer->OnPropertyFilesExpanded(*property_files_expansion_result_);
  }
}

void ArcSessionManager::RemoveObserver(ArcSessionManagerObserver* observer) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  observer_list_.RemoveObserver(observer);
}

void ArcSessionManager::NotifyArcPlayStoreEnabledChanged(bool enabled) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  for (auto& observer : observer_list_) {
    observer.OnArcPlayStoreEnabledChanged(enabled);
  }
}

// This is the special method to support enterprise mojo API.
// TODO(hidehiko): Remove this.
void ArcSessionManager::StopAndEnableArc() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  reenable_arc_ = true;
  StopArc();
}

void ArcSessionManager::OnArcSignInTimeout() {
  LOG(ERROR) << "Timed out waiting for first sign in.";
  OnProvisioningFinished(ArcProvisioningResult(ChromeProvisioningTimeout()));
}

void ArcSessionManager::CancelAuthCode() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (state_ == State::NOT_INITIALIZED) {
    NOTREACHED();
  }

  // If ARC failed to boot normally, stop ARC. Otherwise, ARC is booting
  // normally and the instance should not be stopped.
  if (state_ != State::CHECKING_REQUIREMENTS &&
      (!support_host_ ||
       support_host_->ui_page() != ArcSupportHost::UIPage::ERROR)) {
    return;
  }

  MaybeUpdateOptInCancelUMA(support_host_.get());
  VLOG(1) << "Auth cancelled. Stopping ARC. state: " << state_;
  StopArc();
  SetArcPlayStoreEnabledForProfile(profile_, false);
}

void ArcSessionManager::RequestEnable() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile_);

  if (enable_requested_) {
    VLOG(1) << "ARC is already enabled. Do nothing.";
    return;
  }
  enable_requested_ = true;
  ash::ConfigureSwap(true);
  SetArcEnabledStateMetric(true);

  VLOG(1) << "ARC opt-in. Starting ARC session.";

  // |skipped_terms_of_service_negotiation_| is reset only in case terms are shown.
  // In all other cases it is conidered as skipped.
  skipped_terms_of_service_negotiation_ = true;
  RequestEnableImpl();
}

void ArcSessionManager::OnUserSessionStartUpTaskCompleted() {
  MaybeRecordFirstActivationDuringUserSessionStartUp(false);

  // Allow activation only when it already turns out ARC-On-Demand does not
  // delay the activation.
  if (is_activation_delayed_.has_value() && !is_activation_delayed_.value()) {
    AllowActivation(AllowActivationReason::kUserSessionStartUpTaskCompleted);
  }
}

void ArcSessionManager::AllowActivation(AllowActivationReason reason) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (user_session_start_up_task_timer_.has_value() &&
      reason != AllowActivationReason::kImmediateActivation) {
    base::TimeDelta elapsed =
        user_session_start_up_task_timer_->timer.Elapsed();
    if (user_session_start_up_task_timer_->deferred) {
      if (reason == AllowActivationReason::kUserSessionStartUpTaskCompleted) {
        base::UmaHistogramEnumeration(
            "Arc.DeferActivation.Result",
            DeferArcActivationResult::kDeferSucceeded);
        UmaHistogramDeferActivationTimes(
            "Arc.DeferActivation.Deferred.Success.ElapsedTime", elapsed);
      } else {
        base::UmaHistogramEnumeration("Arc.DeferActivation.Result",
                                      DeferArcActivationResult::kDeferFailed);
        base::UmaHistogramEnumeration(
            "Arc.DeferActivation.Deferred.Failure.Reason", reason);
        UmaHistogramDeferActivationTimes(
            "Arc.DeferActivation.Deferred.Failure.ElapsedTime", elapsed);
      }
    } else {
      if (reason == AllowActivationReason::kUserSessionStartUpTaskCompleted) {
        base::UmaHistogramEnumeration(
            "Arc.DeferActivation.Result",
            DeferArcActivationResult::kNotDeferFailed);
        UmaHistogramDeferActivationTimes(
            "Arc.DeferActivation.NotDeferred.Failure.ElapsedTime", elapsed);
      } else {
        base::UmaHistogramEnumeration(
            "Arc.DeferActivation.Result",
            DeferArcActivationResult::kNotDeferSucceeded);
        base::UmaHistogramEnumeration(
            "Arc.DeferActivation.NotDeferred.Success.Reason", reason);
        UmaHistogramDeferActivationTimes(
            "Arc.DeferActivation.NotDeferred.Success.ElapsedTime", elapsed);
      }
    }
    user_session_start_up_task_timer_.reset();
  }

  // Record the first activation is happening during the user session start up
  // to be referred whether or not to defer ARC for user session start up in
  // following user sessions.
  // ImmediateAction is ignored here. That happens when ARC gets READY and
  // it is decided not to defer ARC, and it should not be considered on deciding
  // whether or not to defer ARC in the following user sessions. Instead,
  // a following activation is recorded, e.g. user's explicit action to launch
  // an ARC app.
  // TODO(hidehiko): Consider excluding non user initiated actions, such as
  // forced by policy.
  if (reason != AllowActivationReason::kImmediateActivation) {
    MaybeRecordFirstActivationDuringUserSessionStartUp(
        reason != AllowActivationReason::kUserSessionStartUpTaskCompleted);
  }

  // First time that ARCVM is allowed in this user session.
  if (!activation_is_allowed_) {
    VLOG(1) << "ARCVM activation is allowed: " << static_cast<int>(reason);
  }

  activation_is_allowed_ = true;
  if (state_ == State::READY) {
    StartArcForRegularBoot();
  }
}

bool ArcSessionManager::IsPlaystoreLaunchRequestedForTesting() const {
  return playstore_launcher_.get();
}

void ArcSessionManager::OnVmStarted(
    const vm_tools::concierge::VmStartedSignal& vm_signal) {
  // When ARCVM starts, register GuestOsMountProvider for Play files.
  if (vm_signal.name() == kArcVmName) {
    if (arcvm_mount_provider_id_.has_value()) {
      // An old instance of ArcMountProvider can remain registered if the
      // previous ARC session did not finish normally and OnVmStopped() was not
      // called (due to concierge crash etc.). Unregister the old instance
      // before registering a new one to prevent multiple registration like
      // b/279378611.
      guest_os::GuestOsServiceFactory::GetForProfile(profile())
          ->MountProviderRegistry()
          ->Unregister(*arcvm_mount_provider_id_);
    }
    arcvm_mount_provider_id_ =
        std::optional<guest_os::GuestOsMountProviderRegistry::Id>(
            guest_os::GuestOsServiceFactory::GetForProfile(profile())
                ->MountProviderRegistry()
                ->Register(std::make_unique<ArcMountProvider>(
                    profile(), vm_signal.vm_info().cid())));
  }
}

void ArcSessionManager::OnVmStopped(
    const vm_tools::concierge::VmStoppedSignal& vm_signal) {
  // When ARCVM stops, unregister GuestOsMountProvider for Play files.
  if (vm_signal.name() == kArcVmName) {
    if (arcvm_mount_provider_id_.has_value()) {
      guest_os::GuestOsServiceFactory::GetForProfile(profile())
          ->MountProviderRegistry()
          ->Unregister(*arcvm_mount_provider_id_);
      arcvm_mount_provider_id_.reset();
    }
  }
}

void ArcSessionManager::RequestEnableImpl() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile_);
  DCHECK(enable_requested_);
  DCHECK(state_ == State::STOPPED || state_ == State::STOPPING ||
         state_ == State::REMOVING_DATA_DIR ||
         state_ == State::CHECKING_DATA_MIGRATION_NECESSITY)
      << state_;

  if (state_ != State::STOPPED) {
    // If the previous invocation of ARC is still running (but currently being
    // stopped) or ARC data removal is in progress, postpone the enabling
    // procedure.
    reenable_arc_ = true;
    return;
  }

  PrefService* const prefs = profile_->GetPrefs();

  // |prefs::kArcProvisioningInitiatedFromOobe| is used to remember
  // |IsArcOobeOptInActive| or |IsArcOptInWizardForAssistantActive| state when
  // ARC start request was made initially. |IsArcOobeOptInActive| or
  // |IsArcOptInWizardForAssistantActive| will be changed by the time when
  // decision to auto-launch the Play Store would be made.
  // |IsArcOobeOptInActive| and |IsArcOptInWizardForAssistantActive| are not
  // preserved on Chrome restart also and in last case
  // |prefs::kArcProvisioningInitiatedFromOobe| is used to remember the state of
  // the initial request.
  // |prefs::kArcProvisioningInitiatedFromOobe| is reset when provisioning is
  // done or ARC is opted out.
  const bool opt_in_start = IsArcOobeOptInActive();
  const bool signed_in = IsArcProvisioned(profile_);
  if (opt_in_start) {
    prefs->SetBoolean(prefs::kArcProvisioningInitiatedFromOobe, true);
  }

  // If it is marked that sign in has been successfully done or if Play Store is
  // not available, then directly start ARC with skipping Play Store ToS.
  // For Kiosk mode, skip ToS because it is very likely that near the device
  // there will be no one who is eligible to accept them.
  // In Public Session mode ARC should be started silently without user
  // interaction. If opt-in verification is disabled, skip negotiation, too.
  // This is for testing purpose.
  const bool should_start_arc_without_user_interaction =
      ShouldArcAlwaysStart() || IsRobotOrOfflineDemoAccountMode() ||
      IsArcOptInVerificationDisabled();
  const bool skip_terms_of_service_negotiation =
      signed_in || should_start_arc_without_user_interaction;
  // When ARC is blocked because of filesystem compatibility, do not proceed
  // to starting ARC nor follow further state transitions.
  if (IsArcBlockedDueToIncompatibleFileSystem(profile_)) {
    // If the next step was the ToS negotiation, show a notification instead.
    // Otherwise, be silent now. Users are notified when clicking ARC app icons.
    if (!skip_terms_of_service_negotiation && g_ui_enabled) {
      arc::ShowArcMigrationGuideNotification(profile_);
    }
    return;
  }

  if (ArcVmDataMigrationIsInProgress(prefs)) {
    VLOG(1) << "Skipping request to enable ARC because ARCVM /data migration "
               "is in progress";
    // Auto-resume should be disabled only when |auto_resume_enabled| is larger
    // than kArcVmDataMigrationMaxAutoResumeCount. This is because the value is
    // incremented in Initialize() when it is smaller than or equal to
    // kArcVmDataMigrationMaxAutoResumeCount. See Initialize() for detail.
    const bool auto_resume_enabled =
        prefs->GetInteger(prefs::kArcVmDataMigrationAutoResumeCount) <=
        kArcVmDataMigrationMaxAutoResumeCount;
    for (auto& observer : observer_list_) {
      observer.OnArcSessionBlockedByArcVmDataMigration(auto_resume_enabled);
    }
    return;
  }

  // ARC might be re-enabled and in this case |arc_ui_availability_reporter_| is
  // already set.
  if (!arc_ui_availability_reporter_) {
    arc_ui_availability_reporter_ = std::make_unique<ArcUiAvailabilityReporter>(
        profile_,
        opt_in_start ? ArcUiAvailabilityReporter::Mode::kOobeProvisioning
        : signed_in  ? ArcUiAvailabilityReporter::Mode::kAlreadyProvisioned
                     : ArcUiAvailabilityReporter::Mode::kInSessionProvisioning);
  }

  if (!pai_starter_ && IsPlayStoreAvailable()) {
    pai_starter_ = ArcPaiStarter::CreateIfNeeded(profile_);
  }

  if (!fast_app_reinstall_starter_ && IsPlayStoreAvailable()) {
    fast_app_reinstall_starter_ = ArcFastAppReinstallStarter::CreateIfNeeded(
        profile_, profile_->GetPrefs());
  }

  if (should_start_arc_without_user_interaction) {
    AllowActivation(AllowActivationReason::kAlwaysStartIsEnabled);
  }

  if (skip_terms_of_service_negotiation) {
    state_ = State::READY;
    if (activation_is_allowed_) {
      StartArcForRegularBoot();
    } else {
      DCHECK(!activation_necessity_checker_);
      activation_necessity_checker_ =
          std::make_unique<ArcActivationNecessityChecker>(
              profile_, adb_sideloading_availability_delegate_.get());
      activation_necessity_checker_->Check(
          base::BindOnce(&ArcSessionManager::OnActivationNecessityChecked,
                         weak_ptr_factory_.GetWeakPtr()));
    }
    return;
  }

  MaybeStartTermsOfServiceNegotiation();
}

void ArcSessionManager::OnActivationNecessityChecked(bool result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(activation_necessity_checker_);

  base::UmaHistogramBoolean("Arc.ArcOnDemand.ActivationIsDelayed", !result);

  activation_necessity_checker_.reset();

  is_activation_delayed_ = !result;
  if (!result) {
    VLOG(1) << "Activation is not allowed yet. Not starting ARC for now.";
    observer_list_.Notify(&ArcSessionManagerObserver::OnArcStartDelayed);
    return;
  }

  // Check whether ARC is expected to be used soon.
  if (base::FeatureList::IsEnabled(
          kDeferArcActivationUntilUserSessionStartUpTaskCompletion)) {
    if (activation_is_allowed_ ||
        session_manager::SessionManager::Get()
            ->IsUserSessionStartUpTaskCompleted() ||
        GetManagementTransition(profile_) !=
            ArcManagementTransition::NO_TRANSITION) {
      // If the activation is already allowed, it is out of the targets to
      // defer. Or, if session start up task is already completed, it does not
      // need to wait activating ARC.
      // If this is running in the process of management transition, e.g. family
      // member to child account, we start ARC now, because it is happening as a
      // part of OOBE flow, and ARC is expected to run on background to complete
      // the transition.
      base::UmaHistogramEnumeration("Arc.DeferActivation.Category",
                                    DeferArcActivationCategory::kNotTarget);
    } else {
      const bool should_defer =
          ShouldDeferArcActivationUntilUserSessionStartUpTaskCompletion(
              profile_->GetPrefs());
      user_session_start_up_task_timer_.emplace(
          UserSessionStartUpTaskTimer{base::ElapsedTimer(), should_defer});
      base::UmaHistogramEnumeration(
          "Arc.DeferActivation.Category",
          should_defer ? DeferArcActivationCategory::kDeferred
                       : DeferArcActivationCategory::kNotDeferred);
      if (should_defer) {
        // Wait for the user session start up task completion to prioritize
        // resources for them.
        VLOG(1) << "ARC activation is deferred until user sesssion start up "
                << "tasks are completed";
        return;
      }
    }
  }

  // In AllowActivation, actual ARC instance is going to be launched,
  // so call it here even if `activation_is_allowed_` checked above is
  // true, intentionally.
  AllowActivation(AllowActivationReason::kImmediateActivation);
}

void ArcSessionManager::RequestDisable(bool remove_arc_data) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile_);
  if (!enable_requested_) {
    VLOG(1) << "ARC is already disabled. "
            << "Killing an instance for login screen (if any).";
    arc_session_runner_->RequestStop();
    return;
  }

  VLOG(1) << "Disabling ARC.";

  skipped_terms_of_service_negotiation_ = false;
  enable_requested_ = false;
  SetArcEnabledStateMetric(false);
  scoped_opt_in_tracker_.reset();
  pai_starter_.reset();
  fast_app_reinstall_starter_.reset();
  arc_ui_availability_reporter_.reset();

  // Reset any pending request to re-enable ARC.
  reenable_arc_ = false;
  StopArc();

  if (remove_arc_data) {
    RequestArcDataRemoval();
  }

  ash::ConfigureSwap(false);
}

void ArcSessionManager::RequestDisable() {
  RequestDisable(false);
}

void ArcSessionManager::RequestDisableWithArcDataRemoval() {
  RequestDisable(true);
}

void ArcSessionManager::RequestArcDataRemoval() {
  if (internal_state_ != InternalState::kRunning &&
      !base::CommandLine::ForCurrentProcess()->HasSwitch(
          ::switches::kTestType)) {
    base::debug::DumpWithoutCrashing();
  }

  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile_);
  DCHECK(data_remover_);
  VLOG(1) << "Scheduling ARC data removal.";

  // TODO(hidehiko): DCHECK the previous state. This is called for four cases;
  // 1) Supporting managed user initial disabled case (Please see also
  //    ArcPlayStoreEnabledPreferenceHandler::Start() for details).
  // 2) Supporting enterprise triggered data removal.
  // 3) One called in OnProvisioningFinished().
  // 4) On request disabling.
  // After the state machine is fixed, 2) should be replaced by
  // RequestDisable() immediately followed by RequestEnable().
  // 3) and 4) are internal state transition. So, as for public interface, 1)
  // should be the only use case, and the |state_| should be limited to
  // STOPPED, then.
  // TODO(hidehiko): Think a way to get rid of 1), too.

  data_remover_->Schedule();
  auto* prefs = profile_->GetPrefs();
  prefs->SetInteger(prefs::kArcManagementTransition,
                    static_cast<int>(ArcManagementTransition::NO_TRANSITION));

  if (ArcVmDataMigrationIsInProgress(prefs)) {
    VLOG(1) << "Skipping ARC /data removal because ARCVM /data migration is "
               "in progress";
    return;
  }

  // To support 1) case above, maybe start data removal.
  if (state_ == State::STOPPED) {
    MaybeStartArcDataRemoval();
  }
}

void ArcSessionManager::MaybeStartTermsOfServiceNegotiation() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile_);
  DCHECK(!requirement_checker_);
  // In Public Session mode, Terms of Service negotiation should be skipped.
  // See also RequestEnableImpl().
  DCHECK(!IsRobotOrOfflineDemoAccountMode());
  // If opt-in verification is disabled, Terms of Service negotiation should
  // be skipped, too. See also RequestEnableImpl().
  DCHECK(!IsArcOptInVerificationDisabled());

  DCHECK_EQ(state_, State::STOPPED);
  state_ = State::CHECKING_REQUIREMENTS;

  // TODO(hidehiko): In kArcSignedIn = true case, this method should never
  // be called. Remove the check.
  // Conceptually, this is starting ToS negotiation, rather than opt-in flow.
  // Move to RequestEnabledImpl.
  if (!scoped_opt_in_tracker_ &&
      !profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) {
    scoped_opt_in_tracker_ = std::make_unique<ScopedOptInFlowTracker>();
  }

  bool is_terms_of_service_negotiation_needed = true;
  if (!IsArcTermsOfServiceNegotiationNeeded(profile_)) {
    if (IsArcStatsReportingEnabled() &&
        !profile_->GetPrefs()->GetBoolean(prefs::kArcTermsAccepted)) {
      // Don't enable stats reporting for users who are not shown the reporting
      // notice during ARC setup.
      profile_->GetPrefs()->SetBoolean(prefs::kArcSkippedReportingNotice, true);
    }
    is_terms_of_service_negotiation_needed = false;
  } else {
    DCHECK(arc_session_runner_);
    // Only set ARC signed in status here before calling StartMiniArc() since
    // we have valid profile available with cryptohome mounted.
    arc_session_runner_->set_arc_signed_in(IsArcProvisioned(profile_));
    // Start the mini-container (or mini-VM) here to save time starting the OS
    // if the user decides to opt-in. Unlike calling StartMiniArc() for ARCVM on
    // login screen, doing so on ToS screen is safe and desirable. The user has
    // already shown the intent to opt-in (or, if this is during OOBE, accepting
    // the ToS is mandatory), and the user's cryptohome has already been
    // mounted. vm_concierge is already running too. For those reasons, calling
    // StartMiniArc() for ARCVM here will actually make its perceived boot time
    // faster.
    StartMiniArc();
  }

  skipped_terms_of_service_negotiation_ =
      !is_terms_of_service_negotiation_needed;
  requirement_checker_ = std::make_unique<ArcRequirementChecker>(
      profile_, support_host_.get(), android_management_checker_factory_);
  requirement_checker_->AddObserver(this);
  requirement_checker_->StartRequirementChecks(
      is_terms_of_service_negotiation_needed,
      base::BindOnce(&ArcSessionManager::OnRequirementChecksDone,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ArcSessionManager::StartArcForTesting() {
  enable_requested_ = true;
  StartArc();
}

void ArcSessionManager::OnArcOptInManagementCheckStarted() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // State::STOPPED appears here in following scenario.
  // Initial provisioning finished with state
  // ProvisioningStatus::ArcStop or
  // ProvisioningStatus::CHROME_SERVER_COMMUNICATION_ERROR.
  // At this moment |prefs::kArcTermsAccepted| is set to true, once user
  // confirmed ToS prior to provisioning flow. Once user presses "Try Again"
  // button, OnRetryClicked calls this immediately.
  DCHECK(state_ == State::CHECKING_REQUIREMENTS || state_ == State::STOPPED)
      << state_;

  for (auto& observer : observer_list_) {
    observer.OnArcOptInManagementCheckStarted();
  }
}

void ArcSessionManager::OnRequirementChecksDone(
    ArcRequirementChecker::RequirementCheckResult result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::CHECKING_REQUIREMENTS);
  DCHECK(requirement_checker_);
  requirement_checker_.reset();

  switch (result) {
    case ArcRequirementChecker::RequirementCheckResult::kOk:
      VLOG(1) << "Starting ARC for first sign in.";
      for (auto& observer : observer_list_) {
        observer.OnArcOptInUserAction();
      }

      StartArc();
      break;
    case ArcRequirementChecker::RequirementCheckResult::
        kTermsOfServicesDeclined:
      // User does not accept the Terms of Service. Disable Google Play Store.
      MaybeUpdateOptInCancelUMA(support_host_.get());
      SetArcPlayStoreEnabledForProfile(profile_, false);
      break;
    case ArcRequirementChecker::RequirementCheckResult::
        kDisallowedByAndroidManagement:
      ShowArcSupportHostError(
          ArcSupportHost::ErrorInfo(
              ArcSupportHost::Error::ANDROID_MANAGEMENT_REQUIRED_ERROR),
          false /* should_show_send_feedback */,
          false /* should_show_run_network_tests */);
      UpdateOptInCancelUMA(OptInCancelReason::ANDROID_MANAGEMENT_REQUIRED);
      break;
    case ArcRequirementChecker::RequirementCheckResult::
        kAndroidManagementCheckError:
      ShowArcSupportHostError(
          ArcSupportHost::ErrorInfo(
              ArcSupportHost::Error::SERVER_COMMUNICATION_ERROR),
          true /* should_show_send_feedback */,
          true /* should_show_run_network_tests */);
      UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR);
      break;
  }
}

void ArcSessionManager::StartBackgroundRequirementChecks() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::ACTIVE);
  DCHECK(!requirement_checker_);

  // We skip Android management check for Public Session mode, because they
  // don't use real google accounts.
  if (IsArcOptInVerificationDisabled() || IsRobotOrOfflineDemoAccountMode()) {
    return;
  }

  requirement_checker_ = std::make_unique<ArcRequirementChecker>(
      profile_, support_host_.get(), android_management_checker_factory_);
  requirement_checker_->StartBackgroundChecks(
      base::BindOnce(&ArcSessionManager::OnBackgroundRequirementChecksDone,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ArcSessionManager::OnBackgroundRequirementChecksDone(
    ArcRequirementChecker::BackgroundCheckResult result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(requirement_checker_);

  requirement_checker_.reset();

  switch (result) {
    case ArcRequirementChecker::BackgroundCheckResult::kNoActionRequired:
      break;
    case ArcRequirementChecker::BackgroundCheckResult::kArcShouldBeDisabled:
      SetArcPlayStoreEnabledForProfile(profile_, false);
      break;
    case ArcRequirementChecker::BackgroundCheckResult::kArcShouldBeRestarted:
      StopAndEnableArc();
      break;
  }
}

void ArcSessionManager::StartArc() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(state_ == State::STOPPED || state_ == State::CHECKING_REQUIREMENTS ||
         state_ == State::READY)
      << state_;
  state_ = State::ACTIVE;

  MaybeStartTimer();

  // ARC must be started only if no pending data removal request exists.
  DCHECK(profile_);
  DCHECK(!profile_->GetPrefs()->GetBoolean(prefs::kArcDataRemoveRequested));

  for (auto& observer : observer_list_) {
    observer.OnArcStarted();
  }

  start_time_ = base::TimeTicks::Now();
  // In case ARC started without mini-ARC |pre_start_time_| is not set.
  if (pre_start_time_.is_null()) {
    pre_start_time_ = start_time_;
  }
  provisioning_reported_ = false;

  std::string locale;
  std::string preferred_languages;
  if (IsArcLocaleSyncDisabled()) {
    // Use fixed locale and preferred languages for auto-tests.
    locale = "en-US";
    preferred_languages = "en-US,en";
    VLOG(1) << "Locale and preferred languages are fixed to " << locale << ","
            << preferred_languages << ".";
  } else {
    GetLocaleAndPreferredLanguages(profile_, &locale, &preferred_languages);
  }

  DCHECK(arc_session_runner_);
  arc_session_runner_->set_default_device_scale_factor(
      exo::GetDefaultDeviceScaleFactor());

  UpgradeParams params;

  const auto* demo_session = ash::DemoSession::Get();
  params.is_demo_session = demo_session && demo_session->started();
  if (params.is_demo_session) {
    DCHECK(demo_session->components()->resources_component_loaded());
    params.demo_session_apps_path =
        demo_session->components()->GetDemoAndroidAppsPath();
  }

  params.management_transition = GetManagementTransition(profile_);
  params.locale = locale;
  // Empty |preferred_languages| is converted to empty array.
  params.preferred_languages = base::SplitString(
      preferred_languages, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);

  user_manager::UserManager* user_manager = user_manager::UserManager::Get();
  DCHECK(user_manager->GetPrimaryUser());
  params.account_id =
      cryptohome::Identification(user_manager->GetPrimaryUser()->GetAccountId())
          .id();

  params.is_account_managed =
      profile_->GetProfilePolicyConnector()->IsManaged();

  arc_session_runner_->set_arc_signed_in(IsArcProvisioned(profile_));
  arc_session_runner_->RequestUpgrade(std::move(params));
}

void ArcSessionManager::StartArcForRegularBoot() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::READY);
  DCHECK(activation_is_allowed_);

  VLOG(1) << "Starting ARC for a regular boot.";
  StartArc();
  // Check Android management in parallel.
  // Note: StartBackgroundRequirementManagementChecks() may call
  // OnBackgroundRequirementChecksDone() synchronously (or asynchronously). In
  // the callback, Google Play Store enabled preference can be set to false if
  // Android management is enabled, and it triggers RequestDisable() via
  // ArcPlayStoreEnabledPreferenceHandler.
  // Thus, StartArc() should be called so that disabling should work even
  // if synchronous call case.
  StartBackgroundRequirementChecks();
}

void ArcSessionManager::RequestStopOnLowDiskSpace() {
  arc_session_runner_->RequestStop();
}

void ArcSessionManager::StopArc() {
  // TODO(hidehiko): This STOPPED guard should be unnecessary. Remove it later.
  // |reenable_arc_| may be set in |StopAndEnableArc| in case enterprise
  // management state is lost.
  if (!reenable_arc_ && state_ != State::STOPPED) {
    profile_->GetPrefs()->SetBoolean(prefs::kArcSignedIn, false);
    profile_->GetPrefs()->SetBoolean(prefs::kArcPaiStarted, false);
    profile_->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, false);
    profile_->GetPrefs()->SetBoolean(prefs::kArcFastAppReinstallStarted, false);
    profile_->GetPrefs()->SetBoolean(prefs::kArcProvisioningInitiatedFromOobe,
                                     false);
    profile_->GetPrefs()->ClearPref(prefs::kArcIsManaged);
  }

  ShutdownSession();
  if (support_host_) {
    support_host_->Close();
  }
}

void ArcSessionManager::MaybeStartArcDataRemoval() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(profile_);

  // Data removal cannot run in parallel with ARC session.
  // LoginScreen instance does not use data directory, so removing should work.
  DCHECK_EQ(state_, State::STOPPED);

  state_ = State::REMOVING_DATA_DIR;
  data_remover_->Run(base::BindOnce(&ArcSessionManager::OnArcDataRemoved,
                                    weak_ptr_factory_.GetWeakPtr()));
}

void ArcSessionManager::OnArcDataRemoved(std::optional<bool> result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::REMOVING_DATA_DIR);
  DCHECK(profile_);

  state_ = State::STOPPED;

  if (result.has_value()) {
    // Regardless of whether it is successfully done or not, notify observers.
    for (auto& observer : observer_list_) {
      observer.OnArcDataRemoved();
    }

    // Note: Currently, we may re-enable ARC even if data removal fails.
    // We may have to avoid it.
  }

  if (!base::FeatureList::IsEnabled(kEnableArcVmDataMigration) ||
      GetArcVmDataMigrationStatus(profile_->GetPrefs()) ==
          ArcVmDataMigrationStatus::kFinished) {
    // No need to check the necessity of ARCVM /data migration.
    MaybeReenableArc();
    return;
  }

  CheckArcVmDataMigrationNecessity(base::BindOnce(
      &ArcSessionManager::MaybeReenableArc, weak_ptr_factory_.GetWeakPtr()));
}

void ArcSessionManager::CheckArcVmDataMigrationNecessity(
    base::OnceClosure callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  DCHECK_EQ(state_, State::STOPPED);
  state_ = State::CHECKING_DATA_MIGRATION_NECESSITY;

  DCHECK(profile_);
  DCHECK(!arc_vm_data_migration_necessity_checker_);
  arc_vm_data_migration_necessity_checker_ =
      std::make_unique<ArcVmDataMigrationNecessityChecker>(profile_);
  arc_vm_data_migration_necessity_checker_->Check(
      base::BindOnce(&ArcSessionManager::OnArcVmDataMigrationNecessityChecked,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void ArcSessionManager::OnArcVmDataMigrationNecessityChecked(
    base::OnceClosure callback,
    std::optional<bool> result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  DCHECK_EQ(state_, State::CHECKING_DATA_MIGRATION_NECESSITY);
  state_ = State::STOPPED;

  DCHECK(profile_);
  DCHECK(arc_vm_data_migration_necessity_checker_);
  arc_vm_data_migration_necessity_checker_.reset();

  // We assume that the migration is needed when |result| has no value, i.e.,
  // when ArcVmDataMigrationNecessityChecker could not determine the necessity.
  if (!result.value_or(true)) {
    VLOG(1) << "No need to perform ARCVM /data migration. Marking the migration"
            << " as finished";
    base::UmaHistogramEnumeration(
        GetHistogramNameByUserType(kArcVmDataMigrationFinishReasonHistogramName,
                                   profile_),
        ArcVmDataMigrationFinishReason::kNoDataToMigrate);
    SetArcVmDataMigrationStatus(profile_->GetPrefs(),
                                ArcVmDataMigrationStatus::kFinished);
  }
  std::move(callback).Run();
}

void ArcSessionManager::MaybeReenableArc() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_EQ(state_, State::STOPPED);
  DCHECK(arc_session_runner_);
  DCHECK(profile_);

  // Whether to use virtio-blk for /data depends on the status of ARCVM /data
  // migration, which can be updated between Initialize() and MaybeReenableArc()
  // by CheckArcVmDataMigrationNecessity(). Hence it should be set here.
  arc_session_runner_->set_use_virtio_blk_data(
      ShouldUseVirtioBlkData(profile_->GetPrefs()));

  if (!reenable_arc_) {
    // Re-enabling is not triggered. Do nothing.
    return;
  }
  DCHECK(enable_requested_);

  // Restart ARC anyway. Let the enterprise reporting instance decide whether
  // the ARC user data wipe is still required or not.
  reenable_arc_ = false;
  VLOG(1) << "Reenable ARC";
  RequestEnableImpl();
}

// Starts a timer to check if provisioning takes too long. The timer will not be
// set if this device was previously provisioned successfully.
void ArcSessionManager::MaybeStartTimer() {
  if (IsArcProvisioned(profile_)) {
    return;
  }

  VLOG(1) << "Setup provisioning timer";
  sign_in_start_time_ = base::TimeTicks::Now();
  ReportProvisioningStartTime(sign_in_start_time_, profile_);
  arc_sign_in_timer_.Start(
      FROM_HERE, kArcSignInTimeout,
      base::BindOnce(&ArcSessionManager::OnArcSignInTimeout,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ArcSessionManager::StartMiniArc() {
  DCHECK(arc_session_runner_);
  pre_start_time_ = base::TimeTicks::Now();
  arc_session_runner_->set_default_device_scale_factor(
      exo::GetDefaultDeviceScaleFactor());
  arc_session_runner_->RequestStartMiniInstance();
}

void ArcSessionManager::OnWindowClosed() {
  CancelAuthCode();
}

void ArcSessionManager::OnRetryClicked() {
  DCHECK(!g_ui_enabled || support_host_);
  DCHECK(!g_ui_enabled ||
         support_host_->ui_page() == ArcSupportHost::UIPage::ERROR);
  DCHECK(!requirement_checker_);

  UpdateOptInActionUMA(OptInActionType::RETRY);

  VLOG(1) << "Retry button clicked";

  if (state_ == State::ACTIVE) {
    // ERROR_WITH_FEEDBACK is set in OnSignInFailed(). In the case, stopping
    // ARC was postponed to contain its internal state into the report.
    // Here, on retry, stop it, then restart.
    if (support_host_) {
      support_host_->ShowArcLoading();
    }
    // In unit tests ShutdownSession may be executed inline and OnSessionStopped
    // is called before |reenable_arc_| is set.
    reenable_arc_ = true;
    ShutdownSession();
  } else {
    // Otherwise, we start ARC once it is stopped now. Usually ARC container is
    // left active after provisioning failure but in case
    // ProvisioningStatus::ARC_STOPPED and
    // ProvisioningStatus::CHROME_SERVER_COMMUNICATION_ERROR failures
    // container is stopped. At this point ToS is already accepted and
    // IsArcTermsOfServiceNegotiationNeeded returns true or ToS needs not to be
    // shown at all. However there is an exception when this does not happen in
    // case an error page is shown when re-opt-in right after opt-out (this is a
    // bug as it should not show an error). When the user click the retry
    // button on this error page, we may start ToS negotiation instead of
    // recreating the instance.
    // TODO(hidehiko): consider removing this case after fixing the bug.
    MaybeStartTermsOfServiceNegotiation();
  }
}

void ArcSessionManager::OnErrorPageShown(bool network_tests_shown) {
}

void ArcSessionManager::OnSendFeedbackClicked() {
  DCHECK(support_host_);
  chrome::OpenFeedbackDialog(nullptr, feedback::kFeedbackSourceArcApp);
}

void ArcSessionManager::OnRunNetworkTestsClicked() {
  DCHECK(support_host_);
  ash::DiagnosticsDialog::ShowDialog(
      ash::DiagnosticsDialog::DiagnosticsPage::kConnectivity,
      support_host_->GetNativeWindow());
}

void ArcSessionManager::SetArcSessionRunnerForTesting(
    std::unique_ptr<ArcSessionRunner> arc_session_runner) {
  DCHECK(arc_session_runner);
  DCHECK(arc_session_runner_);
  arc_session_runner_->RemoveObserver(this);
  arc_session_runner_ = std::move(arc_session_runner);
  arc_session_runner_->AddObserver(this);
}

ArcSessionRunner* ArcSessionManager::GetArcSessionRunnerForTesting() {
  return arc_session_runner_.get();
}

void ArcSessionManager::SetAttemptUserExitCallbackForTesting(
    const base::RepeatingClosure& callback) {
  DCHECK(!callback.is_null());
  attempt_user_exit_callback_ = callback;
}

void ArcSessionManager::SetAttemptRestartCallbackForTesting(
    const base::RepeatingClosure& callback) {
  DCHECK(!callback.is_null());
  attempt_restart_callback_ = callback;
}

void ArcSessionManager::ShowArcSupportHostError(
    ArcSupportHost::ErrorInfo error_info,
    bool should_show_send_feedback,
    bool should_show_run_network_tests) {
  if (support_host_) {
    support_host_->ShowError(error_info, should_show_send_feedback,
                             should_show_run_network_tests);
  }
  for (auto& observer : observer_list_) {
    observer.OnArcErrorShowRequested(error_info);
  }
}

void ArcSessionManager::EmitLoginPromptVisibleCalled() {
  // Since 'login-prompt-visible' Upstart signal starts all Upstart jobs the
  // instance may depend on such as cras, EmitLoginPromptVisibleCalled() is the
  // safe place to start a mini instance.
  if (!IsArcAvailable()) {
    return;
  }

  if (IsArcVmEnabled()) {
    // For ARCVM, don't try to start ARCVM on login screen.
    // Calling StartMiniArc() on login screen for ARCVM does more harm than
    // good. First, the ARCVM boot sequence started by StartMiniArc() stops
    // relatively early in ArcVmClientAdapter which waits for vm_concierge to
    // start (note that vm_concierge does not run on login screen these days.)
    // Because of this, crosvm for ARCVM won't start until the user signs into
    // their user session. Second, after the sign-in, the rest of the mini-ARCVM
    // startup sequence is executed regardless of whether the user has opted
    // into ARC. For opt-out users(*), ARCVM will eventually be stopped, but the
    // stop request may be issued after mini-VM is started. This is a complete
    // waste of resources and may also cause page caches evictions making Chrome
    // UI less responsive.
    // (*) This includes Kiosk mode. See b/197510998 for more info.
    VLOG(1) << "Starting ARCVM on login screen is not supported.";
    return;
  }
  if (!ShouldArcStartManually()) {
    StartMiniArc();
  }
}

void ArcSessionManager::ExpandPropertyFilesAndReadSalt() {
  VLOG(1) << "Started expanding *.prop files";

  // For ARCVM, generate <dest_path>/{combined.prop,fstab}. For ARC, generate
  // <dest_path>/{default,build,vendor_build}.prop.
  const bool is_arcvm = arc::IsArcVmEnabled();

  std::deque<JobDesc> jobs = {
      JobDesc{kArcPrepareHostGeneratedDirJobName,
              UpstartOperation::JOB_STOP_AND_START,
              {std::string("IS_ARCVM=") + (is_arcvm ? "1" : "0")}},
  };

  ConfigureUpstartJobs(std::move(jobs),
                       base::BindOnce(&ArcSessionManager::OnExpandPropertyFiles,
                                      weak_ptr_factory_.GetWeakPtr()));
}

void ArcSessionManager::OnExpandPropertyFiles(bool result) {
  if (!result) {
    LOG(ERROR) << "Failed to expand property files";
    OnExpandPropertyFilesAndReadSalt(
        ArcSessionManager::ExpansionResult{{}, false});
    return;
  }

  if (!arc::IsArcVmEnabled()) {
    OnExpandPropertyFilesAndReadSalt(
        ArcSessionManager::ExpansionResult{{}, true});
    return;
  }

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
      base::BindOnce(&ReadSaltInternal),
      base::BindOnce(&ArcSessionManager::OnExpandPropertyFilesAndReadSalt,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ArcSessionManager::OnExpandPropertyFilesAndReadSalt(
    ExpansionResult result) {
  // ExpandPropertyFilesAndReadSalt() should be called only once.
  DCHECK(!property_files_expansion_result_);

  arc_salt_on_disk_ = result.first;
  property_files_expansion_result_ = result.second;

  // See the comment in Initialize().
  if (profile_) {
    VLOG(1) << "Calling SetUserInfo() in "
            << "ArcSessionManager::OnExpandPropertyFilesAndReadSalt";
    SetUserInfo();
  }

  if (result.second) {
    DCHECK(arc_session_runner_);
    arc_session_runner_->set_arc_signed_in(IsArcProvisioned(profile_));
    arc_session_runner_->ResumeRunner();
  }
  for (auto& observer : observer_list_) {
    observer.OnPropertyFilesExpanded(*property_files_expansion_result_);
  }
}

void ArcSessionManager::StopMiniArcIfNecessary() {
  // This method should only be called before login.
  DCHECK(!profile_);
  DCHECK(arc_session_runner_);
  pre_start_time_ = base::TimeTicks();
  VLOG(1) << "Stopping mini-ARC instance (if any)";
  arc_session_runner_->RequestStop();
}

void ArcSessionManager::MaybeRecordFirstActivationDuringUserSessionStartUp(
    bool value) {
  if (is_first_activation_during_user_session_start_up_recorded_) {
    return;
  }
  is_first_activation_during_user_session_start_up_recorded_ = true;

  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          ash::switches::kLoginUser)) {
    // On browser restart, we don't record the user session start up,
    // because the start up process is different.
    // Theoretically, this is not a pure user login start up, so out of
    // the scope.
    // Practically, start up tasks are considered to be completed
    // quickly as a workaround of the current architecture (b/328339021),
    // so the recording is not reliable.
    return;
  }

  CHECK(profile_);
  RecordFirstActivationDuringUserSessionStartUp(profile_->GetPrefs(), value);
}

std::ostream& operator<<(std::ostream& os,
                         const ArcSessionManager::State& state) {
#define MAP_STATE(name)                \
  case ArcSessionManager::State::name: \
    return os << #name

  switch (state) {
    MAP_STATE(NOT_INITIALIZED);
    MAP_STATE(STOPPED);
    MAP_STATE(CHECKING_REQUIREMENTS);
    MAP_STATE(CHECKING_DATA_MIGRATION_NECESSITY);
    MAP_STATE(REMOVING_DATA_DIR);
    MAP_STATE(READY);
    MAP_STATE(ACTIVE);
    MAP_STATE(STOPPING);
  }

#undef MAP_STATE

  NOTREACHED() << "Invalid value " << static_cast<int>(state);
}

}  // namespace arc