File: lens_overlay_controller.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 (2856 lines) | stat: -rw-r--r-- 112,222 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
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
// Copyright 2024 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/lens/lens_overlay_controller.h"

#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/numerics/checked_math.h"
#include "base/process/kill.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/lens/core/mojom/geometry.mojom.h"
#include "chrome/browser/lens/core/mojom/lens_side_panel.mojom.h"
#include "chrome/browser/lens/core/mojom/overlay_object.mojom.h"
#include "chrome/browser/lens/core/mojom/text.mojom.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/task_manager/web_contents_tags.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/browser/ui/lens/lens_help_menu_utils.h"
#include "chrome/browser/ui/lens/lens_overlay_entry_point_controller.h"
#include "chrome/browser/ui/lens/lens_overlay_event_handler.h"
#include "chrome/browser/ui/lens/lens_overlay_image_helper.h"
#include "chrome/browser/ui/lens/lens_overlay_languages_controller.h"
#include "chrome/browser/ui/lens/lens_overlay_proto_converter.h"
#include "chrome/browser/ui/lens/lens_overlay_query_controller.h"
#include "chrome/browser/ui/lens/lens_overlay_side_panel_coordinator.h"
#include "chrome/browser/ui/lens/lens_overlay_theme_utils.h"
#include "chrome/browser/ui/lens/lens_overlay_untrusted_ui.h"
#include "chrome/browser/ui/lens/lens_overlay_url_builder.h"
#include "chrome/browser/ui/lens/lens_preselection_bubble.h"
#include "chrome/browser/ui/lens/lens_search_contextualization_controller.h"
#include "chrome/browser/ui/lens/lens_search_controller.h"
#include "chrome/browser/ui/lens/lens_search_feature_flag_utils.h"
#include "chrome/browser/ui/lens/lens_searchbox_controller.h"
#include "chrome/browser/ui/lens/lens_session_metrics_logger.h"
#include "chrome/browser/ui/lens/page_content_type_conversions.h"
#include "chrome/browser/ui/search/omnibox_utils.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/user_education/browser_user_education_interface.h"
#include "chrome/browser/ui/views/side_panel/side_panel.h"
#include "chrome/browser/ui/views/side_panel/side_panel_coordinator.h"
#include "chrome/browser/ui/views/side_panel/side_panel_enums.h"
#include "chrome/browser/ui/views/side_panel/side_panel_ui.h"
#include "chrome/browser/ui/views/side_panel/side_panel_util.h"
#include "chrome/browser/ui/webui/webui_embedding_context.h"
#include "chrome/common/chrome_render_frame.mojom.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/branded_strings.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/find_in_page/find_tab_helper.h"
#include "components/lens/lens_features.h"
#include "components/lens/lens_overlay_metrics.h"
#include "components/lens/lens_overlay_mime_type.h"
#include "components/lens/lens_overlay_permission_utils.h"
#include "components/omnibox/browser/lens_suggest_inputs_utils.h"
#include "components/optimization_guide/content/browser/page_content_proto_provider.h"
#include "components/optimization_guide/content/browser/page_context_eligibility.h"
#include "components/permissions/permission_request_manager.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/tabs/public/tab_interface.h"
#include "components/viz/common/frame_timing_details.h"
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/child_process_termination_info.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/download_request_utils.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents_user_data.h"
#include "content/public/browser/web_ui.h"
#include "net/base/network_change_notifier.h"
#include "pdf/buildflags.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/lens_server_proto/lens_overlay_selection_type.pb.h"
#include "third_party/lens_server_proto/lens_overlay_service_deps.pb.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/base/window_open_disposition_utils.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/controls/webview/web_contents_set_background_color.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/native_widget.h"

#if BUILDFLAG(ENABLE_PDF)
#include "components/pdf/browser/pdf_document_helper.h"
#include "pdf/mojom/pdf.mojom.h"
#endif  // BUILDFLAG(ENABLE_PDF)

void* kLensOverlayPreselectionWidgetIdentifier =
    &kLensOverlayPreselectionWidgetIdentifier;

namespace {

// Timeout for the fadeout animation. This is purposely set to be twice the
// duration of the fade out animation on the WebUI JS because there is a delay
// between us notifying the WebUI, and the WebUI receiving our event.
constexpr base::TimeDelta kFadeoutAnimationTimeout = base::Milliseconds(300);

// The amount of time to wait for a reflow after closing the side panel before
// taking a screenshot.
constexpr base::TimeDelta kReflowWaitTimeout = base::Milliseconds(200);

// Copy the objects of a vector into another without transferring
// ownership.
std::vector<lens::mojom::OverlayObjectPtr> CopyObjects(
    const std::vector<lens::mojom::OverlayObjectPtr>& objects) {
  std::vector<lens::mojom::OverlayObjectPtr> objects_copy(objects.size());
  std::transform(
      objects.begin(), objects.end(), objects_copy.begin(),
      [](const lens::mojom::OverlayObjectPtr& obj) { return obj->Clone(); });
  return objects_copy;
}

// Given a BGR bitmap, converts into a RGB bitmap instead. Returns empty bitmap
// if creation fails.
SkBitmap CreateRgbBitmap(const SkBitmap& bgr_bitmap) {
  // Convert bitmap from color type `kBGRA_8888_SkColorType` into a new Bitmap
  // with color type `kRGBA_8888_SkColorType` which will allow the bitmap to
  // render properly in the WebUI.
  sk_sp<SkColorSpace> srgb_color_space =
      bgr_bitmap.colorSpace()->makeSRGBGamma();
  SkImageInfo rgb_info = bgr_bitmap.info()
                             .makeColorType(kRGBA_8888_SkColorType)
                             .makeColorSpace(SkColorSpace::MakeSRGB());
  SkBitmap rgb_bitmap;
  rgb_bitmap.setInfo(rgb_info);
  rgb_bitmap.allocPixels(rgb_info);
  if (rgb_bitmap.writePixels(bgr_bitmap.pixmap())) {
    return rgb_bitmap;
  }

  // Bitmap creation failed.
  return SkBitmap();
}

// Converts a JSON string array to a vector.
std::vector<std::string> JSONArrayToVector(const std::string& json_array) {
  std::optional<base::Value> json_value = base::JSONReader::Read(json_array);

  if (!json_value) {
    return {};
  }

  base::Value::List* entries = json_value->GetIfList();
  if (!entries) {
    return {};
  }

  std::vector<std::string> result;
  result.reserve(entries->size());
  for (const base::Value& entry : *entries) {
    const std::string* filter = entry.GetIfString();
    if (filter) {
      result.emplace_back(*filter);
    }
  }
  return result;
}

LensOverlayController* GetLensOverlayControllerFromTabInterface(
    tabs::TabInterface* tab_interface) {
  return tab_interface
             ? tab_interface->GetTabFeatures()->lens_overlay_controller()
             : nullptr;
}

}  // namespace

LensOverlayController::LensOverlayController(
    tabs::TabInterface* tab,
    LensSearchController* lens_search_controller,
    variations::VariationsClient* variations_client,
    signin::IdentityManager* identity_manager,
    PrefService* pref_service,
    syncer::SyncService* sync_service,
    ThemeService* theme_service)
    : tab_(tab),
      lens_search_controller_(lens_search_controller),
      variations_client_(variations_client),
      identity_manager_(identity_manager),
      pref_service_(pref_service),
      sync_service_(sync_service),
      theme_service_(theme_service),
      gen204_controller_(
          std::make_unique<lens::LensOverlayGen204Controller>()) {
  InitializeTutorialIPHUrlMatcher();

  // Listen to WebContents events
  tab_contents_observer_ = std::make_unique<UnderlyingWebContentsObserver>(
      tab_->GetContents(), this);
}

LensOverlayController::~LensOverlayController() {
  tab_contents_observer_.reset();
  state_ = State::kOff;
}

DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(LensOverlayController, kOverlayId);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(LensOverlayController,
                                      kOverlaySidePanelWebViewId);

// static.
LensOverlayController* LensOverlayController::FromWebUIWebContents(
    content::WebContents* webui_web_contents) {
  return GetLensOverlayControllerFromTabInterface(
      webui::GetTabInterface(webui_web_contents));
}

// static.
LensOverlayController* LensOverlayController::FromTabWebContents(
    content::WebContents* tab_web_contents) {
  return GetLensOverlayControllerFromTabInterface(
      tabs::TabInterface::GetFromContents(tab_web_contents));
}

void LensOverlayController::TriggerOverlayFadeOutAnimation(
    base::OnceClosure callback) {
  if (state_ == State::kOff || IsOverlayClosing()) {
    return;
  }

  // Notify the overlay so it can do any animations or cleanup. The page_ is not
  // guaranteed to exist if CloseUIAsync is called during the setup process.
  if (page_) {
    page_->NotifyOverlayClosing();
  }

  // Set a short 200ms timeout to give the fade out time to transition.
  base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, std::move(callback), kFadeoutAnimationTimeout);
}

void LensOverlayController::CloseUI(
    lens::LensOverlayDismissalSource dismissal_source) {
  if (state_ == State::kOff) {
    return;
  }

  state_ = State::kClosing;

  // Closes preselection toast if it exists.
  ClosePreselectionBubble();

  // Notify the query controller to loose references to this classes data before
  // it gets cleaned up to prevent dangling ptrs.
  lens_overlay_query_controller_->ResetPageContentData();
  lens_overlay_query_controller_ = nullptr;

  // A permission prompt may be suspended if the overlay was showing when the
  // permission was queued. Restore the suspended prompt if possible.
  // TODO(crbug.com/331940245): Refactor to be decoupled from
  // PermissionPromptFactory
  content::WebContents* contents = tab_->GetContents();
  CHECK(contents);
  auto* permission_request_manager =
      permissions::PermissionRequestManager::FromWebContents(contents);
  if (permission_request_manager &&
      permission_request_manager->CanRestorePrompt()) {
    permission_request_manager->RestorePrompt();
  }

  results_side_panel_coordinator_ = nullptr;
  pre_initialization_objects_.reset();
  pre_initialization_text_.reset();

  side_panel_shown_subscription_ = base::CallbackListSubscription();
  side_panel_coordinator_ = nullptr;

  // Re-enable mouse and keyboard events to the tab contents web view.
  auto* contents_web_view = tab_->GetBrowserWindowInterface()->GetWebView();
  CHECK(contents_web_view);
  contents_web_view->SetEnabled(true);

  if (overlay_web_view_) {
    // Remove render frame observer.
    overlay_web_view_->GetWebContents()
        ->GetPrimaryMainFrame()
        ->GetProcess()
        ->RemoveObserver(this);
  }

  initialization_data_.reset();

  tab_contents_view_observer_.Reset();
  omnibox_tab_helper_observer_.Reset();
  find_tab_observer_.Reset();
  receiver_.reset();
  page_.reset();
  languages_controller_.reset();
  scoped_tab_modal_ui_.reset();
  pending_region_.reset();
  fullscreen_observation_.Reset();
  immersive_mode_observer_.Reset();
  lens_overlay_blur_layer_delegate_.reset();
#if BUILDFLAG(IS_MAC)
  pref_change_registrar_.Reset();
#endif  // BUILDFLAG(IS_MAC)

  // Notify the searchbox controller to reset its handlers before the overlay
  // is cleaned up. This is needed to prevent a dangling ptr.
  GetLensSearchboxController()->ResetOverlaySearchboxHandler();

  // Cleanup all of the lens overlay related views. The overlay view is owned by
  // the browser view and is reused for each Lens overlay session. Clean it up
  // so it is ready for the next invocation.
  if (overlay_view_) {
    overlay_view_->RemoveChildViewT(
        std::exchange(preselection_widget_anchor_, nullptr));
    overlay_view_->RemoveChildViewT(std::exchange(overlay_web_view_, nullptr));
    MaybeHideSharedOverlayView();
    overlay_view_ = nullptr;
  }

  lens_selection_type_ = lens::UNKNOWN_SELECTION_TYPE;
  should_send_screenshot_on_init_ = false;

  state_ = State::kOff;

  // Update the entrypoints now that the controller is closed.
  UpdateEntryPointsState();
}

// static
const std::u16string LensOverlayController::GetFilenameForURL(const GURL& url) {
  if (!url.has_host() || url.HostIsIPAddress()) {
    return u"screenshot.png";
  }

  return base::ASCIIToUTF16(base::StrCat({"screenshot_", url.host(), ".png"}));
}

void LensOverlayController::BindOverlay(
    mojo::PendingReceiver<lens::mojom::LensPageHandler> receiver,
    mojo::PendingRemote<lens::mojom::LensPage> page) {
  if (state_ != State::kStartingWebUI) {
    return;
  }
  receiver_.Bind(std::move(receiver));
  page_.Bind(std::move(page));

  InitializeOverlay(/*initialization_data=*/nullptr);
}

uint64_t LensOverlayController::GetInvocationTimeSinceEpoch() {
  return invocation_time_since_epoch_.InMillisecondsSinceUnixEpoch();
}

views::View* LensOverlayController::GetOverlayViewForTesting() {
  return overlay_view_.get();
}

views::WebView* LensOverlayController::GetOverlayWebViewForTesting() {
  return overlay_web_view_.get();
}

void LensOverlayController::SendText(lens::mojom::TextPtr text) {
  if (!page_) {
    // Store the text to send once the page is bound.
    pre_initialization_text_ = std::move(text);
    return;
  }
  page_->TextReceived(std::move(text));
}

lens::mojom::OverlayThemePtr LensOverlayController::CreateTheme(
    lens::PaletteId palette_id) {
  CHECK(base::Contains(lens::kPaletteColors, palette_id));
  const auto& palette = lens::kPaletteColors.at(palette_id);
  auto theme = lens::mojom::OverlayTheme::New();
  theme->primary = palette.at(lens::ColorId::kPrimary);
  theme->shader_layer_1 = palette.at(lens::ColorId::kShaderLayer1);
  theme->shader_layer_2 = palette.at(lens::ColorId::kShaderLayer2);
  theme->shader_layer_3 = palette.at(lens::ColorId::kShaderLayer3);
  theme->shader_layer_4 = palette.at(lens::ColorId::kShaderLayer4);
  theme->shader_layer_5 = palette.at(lens::ColorId::kShaderLayer5);
  theme->scrim = palette.at(lens::ColorId::kScrim);
  theme->surface_container_highest_light =
      palette.at(lens::ColorId::kSurfaceContainerHighestLight);
  theme->surface_container_highest_dark =
      palette.at(lens::ColorId::kSurfaceContainerHighestDark);
  theme->selection_element = palette.at(lens::ColorId::kSelectionElement);
  return theme;
}

void LensOverlayController::SendObjects(
    std::vector<lens::mojom::OverlayObjectPtr> objects) {
  if (!page_) {
    // Store the objects to send once the page is bound.
    pre_initialization_objects_ = std::move(objects);
    return;
  }
  page_->ObjectsReceived(std::move(objects));
}

void LensOverlayController::NotifyResultsPanelOpened() {
  if (page_) {
    page_->NotifyResultsPanelOpened();
  }
}

void LensOverlayController::TriggerCopy() {
  // This prevents a race condition where the overlay is closed as a keyboard
  // event is being processed.
  if (!page_) {
    return;
  }
  page_->OnCopyCommand();
}

bool LensOverlayController::IsOverlayShowing() const {
  return state_ == State::kStartingWebUI || state_ == State::kOverlay ||
         state_ == State::kOverlayAndResults;
}

bool LensOverlayController::IsOverlayActive() const {
  return IsOverlayShowing() || state_ == State::kLivePageAndResults;
}

bool LensOverlayController::IsOverlayInitializing() {
  return state_ == State::kStartingWebUI || state_ == State::kScreenshot ||
         state_ == State::kClosingOpenedSidePanel;
}

bool LensOverlayController::IsOverlayClosing() {
  return state_ == State::kClosing;
}

bool LensOverlayController::IsScreenshotPossible(
    content::RenderWidgetHostView* view) {
  return view && view->IsSurfaceAvailableForCopy();
}

tabs::TabInterface* LensOverlayController::GetTabInterface() {
  return tab_;
}

void LensOverlayController::IssueLensRegionRequestForTesting(
    lens::mojom::CenterRotatedBoxPtr region,
    bool is_click) {
  IssueLensRegionRequest(std::move(region), is_click);
}

void LensOverlayController::IssueTextSelectionRequestForTesting(
    const std::string& text_query,
    int selection_start_index,
    int selection_end_index,
    bool is_translate) {
  IssueTextSelectionRequest(text_query, selection_start_index,
                            selection_end_index, is_translate);
}

void LensOverlayController::
    RecordUkmAndTaskCompletionForLensOverlayInteractionForTesting(
        lens::mojom::UserAction user_action) {
  RecordUkmAndTaskCompletionForLensOverlayInteraction(user_action);
}

void LensOverlayController::RecordSemanticEventForTesting(
    lens::mojom::SemanticEvent event) {
  RecordLensOverlaySemanticEvent(event);
}

void LensOverlayController::IssueSearchBoxRequestForTesting(
    base::Time query_start_time,
    const std::string& search_box_text,
    AutocompleteMatchType::Type match_type,
    bool is_zero_prefix_suggestion,
    std::map<std::string, std::string> additional_query_params) {
  IssueSearchBoxRequest(query_start_time, search_box_text, match_type,
                        is_zero_prefix_suggestion, additional_query_params);
}

void LensOverlayController::IssueTranslateSelectionRequestForTesting(
    const std::string& text_query,
    const std::string& content_language,
    int selection_start_index,
    int selection_end_index) {
  IssueTranslateSelectionRequest(text_query, content_language,
                                 selection_start_index, selection_end_index);
}

void LensOverlayController::IssueMathSelectionRequestForTesting(
    const std::string& query,
    const std::string& formula,
    int selection_start_index,
    int selection_end_index) {
  IssueMathSelectionRequest(query, formula, selection_start_index,
                            selection_end_index);
}

void LensOverlayController::IssueTranslateFullPageRequestForTesting(
    const std::string& source_language,
    const std::string& target_language) {
  IssueTranslateFullPageRequest(source_language, target_language);
}

void LensOverlayController::IssueEndTranslateModeRequestForTesting() {
  IssueEndTranslateModeRequest();
}

void LensOverlayController::IssueTranslateFullPageRequest(
    const std::string& source_language,
    const std::string& target_language) {
  // Remove the selection thumbnail, if it exists.
  GetLensSearchboxController()->SetSearchboxThumbnail(std::string());
  ClearRegionSelection();
  // Set the coachmark text.
  if (preselection_widget_) {
    // This cast is safe since we know the widget delegate will always be a
    // `lens::LensPreselectionBubble`.
    auto* bubble_view = static_cast<lens::LensPreselectionBubble*>(
        preselection_widget_->widget_delegate());
    bubble_view->SetLabelText(
        IDS_LENS_OVERLAY_INITIAL_TOAST_MESSAGE_SELECT_TEXT);
  }
  // Set the translate options on initialization data in case we need to
  // re-enable translate mode later.
  initialization_data_->translate_options_ =
      lens::TranslateOptions(source_language, target_language);

  lens_overlay_query_controller_->SendFullPageTranslateQuery(source_language,
                                                             target_language);
  MaybeLaunchSurvey();
}

void LensOverlayController::IssueEndTranslateModeRequest() {
  // Reset the coachmark text back to default.
  if (preselection_widget_) {
    // This cast is safe since we know the widget delegate will always be a
    // `lens::LensPreselectionBubble`.
    auto* bubble_view = static_cast<lens::LensPreselectionBubble*>(
        preselection_widget_->widget_delegate());
    bubble_view->SetLabelText(IDS_LENS_OVERLAY_INITIAL_TOAST_MESSAGE);
  }
  lens_selection_type_ = lens::UNKNOWN_SELECTION_TYPE;
  initialization_data_->selected_text_.reset();
  initialization_data_->translate_options_.reset();
  lens_overlay_query_controller_->SendEndTranslateModeQuery();
}

void LensOverlayController::NotifyOverlayInitialized() {
  // Now that the overlay is actually showing, it is safe to start doing a Lens
  // request without showing the page reflowing.
  if (pending_region_) {
    // If there is a pending region (i.e. for image right click)
    // use INJECTED_IMAGE as the selection type.
    IssueLensRequest(/*query_start_time=*/invocation_time_since_epoch_,
                     std::move(pending_region_), lens::INJECTED_IMAGE,
                     pending_region_bitmap_);
    pending_region_bitmap_.reset();
  }
}

void LensOverlayController::CopyText(const std::string& text) {
  ui::ScopedClipboardWriter clipboard_writer(ui::ClipboardBuffer::kCopyPaste);
  clipboard_writer.WriteText(base::UTF8ToUTF16(text));
}

void LensOverlayController::CopyImage(lens::mojom::CenterRotatedBoxPtr region) {
  if (initialization_data_->initial_screenshot_.drawsNothing()) {
    return;
  }

  SkBitmap cropped = lens::CropBitmapToRegion(
      initialization_data_->initial_screenshot_, std::move(region));
  ui::ScopedClipboardWriter clipboard_writer(ui::ClipboardBuffer::kCopyPaste);
  clipboard_writer.WriteImage(cropped);
}

void LensOverlayController::RecordUkmAndTaskCompletionForLensOverlayInteraction(
    lens::mojom::UserAction user_action) {
  ukm::SourceId source_id =
      tab_->GetContents()->GetPrimaryMainFrame()->GetPageUkmSourceId();
  ukm::builders::Lens_Overlay_Overlay_UserAction(source_id)
      .SetUserAction(static_cast<int64_t>(user_action))
      .Record(ukm::UkmRecorder::Get());
  lens_overlay_query_controller_->SendTaskCompletionGen204IfEnabled(
      user_action);
}

void LensOverlayController::RecordLensOverlaySemanticEvent(
    lens::mojom::SemanticEvent event) {
  lens_overlay_query_controller_->SendSemanticEventGen204IfEnabled(event);
}

void LensOverlayController::SaveAsImage(
    lens::mojom::CenterRotatedBoxPtr region) {
  SkBitmap cropped = lens::CropBitmapToRegion(
      initialization_data_->initial_screenshot_, std::move(region));
  const GURL data_url = GURL(webui::GetBitmapDataUrl(cropped));
  content::DownloadManager* download_manager =
      tab_->GetBrowserWindowInterface()->GetProfile()->GetDownloadManager();
  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("lens_overlay_save", R"(
      semantics {
        sender: "Lens Overlay"
        description:
          "The user may capture a selection of the current screenshot in the "
          "Lens overlay via a button in the overlay. The resulting image is "
          "saved from a data URL to the disk on the local client."
        trigger: "User clicks 'Save as image' in the Lens Overlay after "
           "activating the Lens Overlay and making a selection on the "
           "screenshot."
        data: "A capture of a portion of a screenshot of the current page."
        destination: LOCAL
        last_reviewed: "2024-08-23"
        user_data {
          type: WEB_CONTENT
        }
        internal {
          contacts {
            owners: "//chrome/browser/ui/lens/OWNERS"
          }
        }
      }
      policy {
        cookies_allowed: NO
        setting:
          "No user-visible setting for this feature. Configured via Finch."
        policy_exception_justification:
          "This is not a network request."
      })");
  std::unique_ptr<download::DownloadUrlParameters> params =
      content::DownloadRequestUtils::CreateDownloadForWebContentsMainFrame(
          overlay_web_view_->GetWebContents(), data_url, traffic_annotation);
  params->set_prompt(true);
  params->set_suggested_name(
      GetFilenameForURL(tab_->GetContents()->GetLastCommittedURL()));
  download_manager->DownloadUrl(std::move(params));
}

void LensOverlayController::MaybeShowTranslateFeaturePromo() {
  auto* tracker = ui::ElementTracker::GetElementTracker();
  translate_button_shown_subscription_ =
      tracker->AddElementShownInAnyContextCallback(
          kLensOverlayTranslateButtonElementId,
          base::BindRepeating(
              &LensOverlayController::TryShowTranslateFeaturePromo,
              weak_factory_.GetWeakPtr()));
}

void LensOverlayController::MaybeCloseTranslateFeaturePromo(
    bool feature_engaged) {
  if (auto* const interface =
          BrowserUserEducationInterface::MaybeGetForWebContentsInTab(
              tab_->GetContents())) {
    if (!interface->IsFeaturePromoActive(
            feature_engagement::kIPHLensOverlayTranslateButtonFeature)) {
      // Do nothing if feature promo is not active.
      return;
    }

    if (feature_engaged) {
      interface->NotifyFeaturePromoFeatureUsed(
          feature_engagement::kIPHLensOverlayTranslateButtonFeature,
          FeaturePromoFeatureUsedAction::kClosePromoIfPresent);
    } else {
      interface->AbortFeaturePromo(
          feature_engagement::kIPHLensOverlayTranslateButtonFeature);
    }
  }
}

void LensOverlayController::FetchSupportedLanguages(
    FetchSupportedLanguagesCallback callback) {
  CHECK(languages_controller_);
  languages_controller_->SendGetSupportedLanguagesRequest(std::move(callback));
}

void LensOverlayController::TryShowTranslateFeaturePromo(
    ui::TrackedElement* element) {
  if (!element) {
    return;
  }

  if (auto* const interface =
          BrowserUserEducationInterface::MaybeGetForWebContentsInTab(
              tab_->GetContents())) {
    interface->MaybeShowFeaturePromo(
        feature_engagement::kIPHLensOverlayTranslateButtonFeature);
  }
}

std::string LensOverlayController::GetInvocationSourceString() {
  return lens::InvocationSourceToString(invocation_source_);
}

content::WebContents*
LensOverlayController::GetSidePanelWebContentsForTesting() {
  if (!results_side_panel_coordinator_) {
    return nullptr;
  }
  return results_side_panel_coordinator_->GetSidePanelWebContents();
}

const GURL& LensOverlayController::GetPageURLForTesting() {
  return lens_search_controller_->GetPageURL();
}

SessionID LensOverlayController::GetTabIdForTesting() {
  return GetLensSearchboxController()->GetTabId();
}

metrics::OmniboxEventProto::PageClassification
LensOverlayController::GetPageClassificationForTesting() {
  return GetPageClassification();
}

const std::string& LensOverlayController::GetThumbnailForTesting() {
  return GetLensSearchboxController()->GetThumbnail();
}

void LensOverlayController::OnTextModifiedForTesting() {
  GetLensSearchboxController()->OnTextModified();
}

void LensOverlayController::OnThumbnailRemovedForTesting() {
  GetLensSearchboxController()->OnThumbnailRemoved();
}

void LensOverlayController::OnFocusChangedForTesting(bool focused) {
  GetLensSearchboxController()->OnFocusChanged(focused);
}

void LensOverlayController::OnZeroSuggestShownForTesting() {
  OnZeroSuggestShown();
}

void LensOverlayController::OpenSidePanelForTesting() {
  MaybeOpenSidePanel();
}

const lens::proto::LensOverlaySuggestInputs&
LensOverlayController::GetLensSuggestInputsForTesting() {
  return GetLensSearchboxController()->GetLensSuggestInputs();
}

bool LensOverlayController::IsUrlEligibleForTutorialIPHForTesting(
    const GURL& url) {
  return IsUrlEligibleForTutorialIPH(url);
}

void LensOverlayController::ShowUI(
    lens::LensOverlayInvocationSource invocation_source,
    lens::LensOverlayQueryController* lens_overlay_query_controller) {
  // If UI is already showing or in the process of showing, do nothing.
  if (state_ != State::kOff) {
    return;
  }

  // The UI should only show if the tab is in the foreground or if the tab web
  // contents is not in a crash state.
  if (!tab_->IsActivated() || tab_->GetContents()->IsCrashed()) {
    return;
  }

  // If a different tab-modal is showing, do nothing.
  if (!tab_->CanShowModalUI()) {
    return;
  }

  // Increment the counter for the number of times the Lens Overlay has been
  // started.
  int lens_overlay_start_count =
      pref_service_->GetInteger(prefs::kLensOverlayStartCount);
  pref_service_->SetInteger(prefs::kLensOverlayStartCount,
                            lens_overlay_start_count + 1);

  // Store reference for later use.
  invocation_source_ = invocation_source;
  lens_overlay_query_controller_ = lens_overlay_query_controller;

  // Grab reference to the side panel coordinator it not already done so.
  if (!results_side_panel_coordinator_) {
    results_side_panel_coordinator_ =
        lens_search_controller_->lens_overlay_side_panel_coordinator();
  }

  Profile* profile =
      Profile::FromBrowserContext(tab_->GetContents()->GetBrowserContext());
  side_panel_coordinator_ =
      tab_->GetBrowserWindowInterface()->GetFeatures().side_panel_coordinator();
  CHECK(side_panel_coordinator_);

  // Create the languages controller.
  languages_controller_ =
      std::make_unique<lens::LensOverlayLanguagesController>(profile);

  // Setup observer to be notified of side panel opens and closes.
  side_panel_shown_subscription_ =
      side_panel_coordinator_->RegisterSidePanelShown(
          base::BindRepeating(&LensOverlayController::OnSidePanelDidOpen,
                              weak_factory_.GetWeakPtr()));

  if (find_in_page::FindTabHelper* const find_tab_helper =
          find_in_page::FindTabHelper::FromWebContents(tab_->GetContents())) {
    find_tab_observer_.Observe(find_tab_helper);
  }

  if (!omnibox_tab_helper_observer_.IsObserving()) {
    if (auto* helper = OmniboxTabHelper::FromWebContents(tab_->GetContents())) {
      omnibox_tab_helper_observer_.Observe(helper);
    }
  }

  // This is safe because we checked if another modal was showing above.
  scoped_tab_modal_ui_ = tab_->ShowModalUI();
  fullscreen_observation_.Observe(tab_->GetBrowserWindowInterface()
                                      ->GetExclusiveAccessManager()
                                      ->fullscreen_controller());

  // The preselection widget can cover top Chrome in immersive fullscreen.
  // Observer the reveal state to hide the widget when top Chrome is shown.
  immersive_mode_observer_.Observe(
      tab_->GetBrowserWindowInterface()->GetImmersiveModeController());

#if BUILDFLAG(IS_MAC)
  // Add observer to listen for changes in the always show toolbar state,
  // since that requires the preselection bubble to rerender to show properly.
  pref_change_registrar_.Init(pref_service_);
  pref_change_registrar_.Add(
      prefs::kShowFullscreenToolbar,
      base::BindRepeating(
          &LensOverlayController::CloseAndReshowPreselectionBubble,
          base::Unretained(this)));
#endif  // BUILDFLAG(IS_MAC)

  NotifyUserEducationAboutOverlayUsed();

  // Establish data required for session metrics.
  invocation_time_ = base::TimeTicks::Now();
  invocation_time_since_epoch_ = base::Time::Now();
  ocr_dom_similarity_recorded_in_session_ = false;

  // This should be the last thing called in ShowUI, so if something goes wrong
  // in capturing the screenshot, the state gets cleaned up correctly.
  if (side_panel_coordinator_->IsSidePanelShowing()) {
    // Close the currently opened side panel synchronously. Postpone the
    // screenshot for a fixed time to allow reflow.
    state_ = State::kClosingOpenedSidePanel;
    side_panel_coordinator_->Close(/*suppress_animations=*/true);
    base::SingleThreadTaskRunner::GetCurrentDefault()
        ->PostNonNestableDelayedTask(
            FROM_HERE,
            base::BindOnce(&LensOverlayController::FinishedWaitingForReflow,
                           weak_factory_.GetWeakPtr()),
            kReflowWaitTimeout);
  } else {
    CaptureScreenshot();
  }
}

void LensOverlayController::IssueContextualSearchRequest(
    const GURL& destination_url,
    lens::LensOverlayQueryController* lens_overlay_query_controller,
    AutocompleteMatchType::Type match_type,
    bool is_zero_prefix_suggestion,
    lens::LensOverlayInvocationSource invocation_source) {
  // Ignore the request if the overlay is off or closing.
  if (IsOverlayClosing()) {
    return;
  }

  // If the overlay is off, turn it on so the request can be fulfilled.
  if (state_ == State::kOff) {
    // TODO(crbug.com/403573362): This is a temporary fix to unblock
    // prototyping. Since this flow goes straight to the side panel results with
    // not overlay UI, this flow does a lot of unnecessary work. There should be
    // a new flow that can contextualize without the overlay UI being
    // initialized.
    // Set the query controller if it is not already set. This happens in cases
    // when a contextual request is made but the overlay is not shown.
    lens_overlay_query_controller_ = lens_overlay_query_controller;
    CHECK(lens_overlay_query_controller_);
    GetContextualizationController()->StartContextualization(
        invocation_source,
        base::BindOnce(
            &LensOverlayController::OnPageContextUpdatedForSuggestion,
            weak_factory_.GetWeakPtr(), destination_url, match_type,
            is_zero_prefix_suggestion, invocation_source));
    return;
  }

  if (IsOverlayInitializing()) {
    // Hold the request until the overlay has finished initializing.
    pending_contextual_search_request_ =
        base::BindOnce(&LensOverlayController::IssueContextualSearchRequest,
                       weak_factory_.GetWeakPtr(), destination_url,
                       lens_overlay_query_controller, match_type,
                       is_zero_prefix_suggestion, invocation_source);
    return;
  } else if (state_ != State::kOff) {
    // If the state is not off or initializing, the Lens sessions should already
    // be initialized. This means the page could have changed since the last
    // contextualization so it should be updated before issuing the contextual
    // search request.
    CHECK(lens_overlay_query_controller_);
    GetContextualizationController()->TryUpdatePageContextualization(
        base::BindOnce(
            &LensOverlayController::OnPageContextUpdatedForSuggestion,
            weak_factory_.GetWeakPtr(), destination_url, match_type,
            is_zero_prefix_suggestion, invocation_source));
    return;
  }

  // TODO(crbug.com/401583049): Revisit if this should go through the
  // OnSuggestionAccepted flow or if there should be a more direct contextual
  // search flow.
  GetLensSearchboxController()->OnSuggestionAccepted(
      destination_url, match_type, is_zero_prefix_suggestion);
}

void LensOverlayController::ShowUIWithPendingRegion(
    lens::LensOverlayQueryController* lens_overlay_query_controller,
    lens::LensOverlayInvocationSource invocation_source,
    lens::mojom::CenterRotatedBoxPtr region,
    const SkBitmap& region_bitmap) {
  pending_region_ = std::move(region);
  pending_region_bitmap_ = region_bitmap;
  ShowUI(invocation_source, lens_overlay_query_controller);
  // Overrides value set in ShowUI since invoking lens overlay with a pending
  // region is considered a search.
  GetLensSessionMetricsLogger()->OnSearchPerformed();
}

std::string LensOverlayController::GetVsridForNewTab() {
  return lens_overlay_query_controller_->GetVsridForNewTab();
}

void LensOverlayController::SetTranslateMode(
    std::optional<lens::TranslateOptions> translate_options) {
  if (!page_) {
    return;
  }

  if (translate_options.has_value()) {
    page_->SetTranslateMode(translate_options->source_language,
                            translate_options->target_language);
  } else {
    // If the overlay was previously in translate mode, send a
    // request to end translate mode so the WebUI can update its state.
    if (initialization_data_->translate_options_.has_value()) {
      IssueEndTranslateModeRequest();
      results_side_panel_coordinator_->SetSidePanelIsLoadingResults(true);
    }
    // Disable translate mode by setting source and target languages to empty
    // strings. This is a no-op if translate mode is already disabled.
    page_->SetTranslateMode(std::string(), std::string());
  }
  // Store the latest translate options.
  initialization_data_->translate_options_ = translate_options;
}

void LensOverlayController::SetTextSelection(int32_t selection_start_index,
                                             int32_t selection_end_index) {
  page_->SetTextSelection(selection_start_index, selection_end_index);
  initialization_data_->selected_text_ =
      std::make_pair(selection_start_index, selection_end_index);
}

void LensOverlayController::SetPostRegionSelection(
    lens::mojom::CenterRotatedBoxPtr box) {
  page_->SetPostRegionSelection(box->Clone());
  initialization_data_->selected_region_ = std::move(box);
}

void LensOverlayController::SetAdditionalSearchQueryParams(
    std::map<std::string, std::string> additional_search_query_params) {
  initialization_data_->additional_search_query_params_ =
      additional_search_query_params;
}

void LensOverlayController::ClearTextSelection() {
  if (!IsOverlayShowing()) {
    return;
  }
  if (initialization_data_->selected_text_.has_value()) {
    initialization_data_->selected_text_.reset();
    page_->ClearTextSelection();
  }
}

void LensOverlayController::ClearRegionSelection() {
  if (!IsOverlayShowing()) {
    return;
  }
  GetLensSearchboxController()->SetSearchboxThumbnail("");
  lens_selection_type_ = lens::UNKNOWN_SELECTION_TYPE;
  initialization_data_->selected_region_.reset();
  initialization_data_->selected_region_bitmap_.reset();
  page_->ClearRegionSelection();
}

void LensOverlayController::OnSearchboxFocusChanged(bool focused) {
  if (!focused) {
    return;
  }

  if (IsContextualSearchbox()) {
    GetLensSessionMetricsLogger()->OnSearchboxFocused();

    if (state() == State::kLivePageAndResults) {
      // If the live page is showing and the searchbox becomes focused, showing
      // intent to issue a new query, upload the new page content for
      // contextualization.
      GetContextualizationController()->TryUpdatePageContextualization(
          base::BindOnce(&LensOverlayController::NotifyPageContentUpdated,
                         weak_factory_.GetWeakPtr()));
    }
  }
}

void LensOverlayController::OnZeroSuggestShown() {
  if (!IsContextualSearchbox()) {
    return;
  }

  GetLensSessionMetricsLogger()->OnZeroSuggestShown(
      /*is_initial_query=*/state() == State::kOverlay);
}

void LensOverlayController::IssueLensRequest(
    base::Time query_start_time,
    lens::mojom::CenterRotatedBoxPtr region,
    lens::LensOverlaySelectionType selection_type,
    std::optional<SkBitmap> region_bytes) {
  CHECK(initialization_data_);
  CHECK(region);
  GetLensSearchboxController()->SetSearchboxInputText(std::string());
  initialization_data_->selected_region_ = region.Clone();
  initialization_data_->selected_text_.reset();
  initialization_data_->additional_search_query_params_.clear();
  lens_selection_type_ = selection_type;
  if (region_bytes) {
    initialization_data_->selected_region_bitmap_ = region_bytes.value();
  } else {
    initialization_data_->selected_region_bitmap_.reset();
  }

  if (GetContextualizationController()->GetCurrentPageContextEligibility()) {
    lens_overlay_query_controller_->SendRegionSearch(
        query_start_time, region.Clone(), selection_type,
        initialization_data_->additional_search_query_params_, region_bytes);
  }
  MaybeOpenSidePanel();
  GetLensSessionMetricsLogger()->RecordTimeToFirstInteraction(
      lens::LensOverlayFirstInteractionType::kRegionSelect);

  // TODO(crbug.com/428208291): The overlay can be in the live page and results
  // state and this could be a query coming from the back stack which would make
  // setting this to kOverlayAndResults incorrect. Check if the overlay is
  // currently in that state to determine if this should be set to
  // kOverlayAndResults or not. This should be fixed by moving the functionality
  // to make Lens requests to a more appropriate location.
  if (state_ != State::kLivePageAndResults) {
    state_ = State::kOverlayAndResults;
    MaybeLaunchSurvey();
  }
}

void LensOverlayController::IssueMultimodalRequest(
    base::Time query_start_time,
    lens::mojom::CenterRotatedBoxPtr region,
    const std::string& text_query,
    lens::LensOverlaySelectionType selection_type,
    std::optional<SkBitmap> region_bitmap) {
  if (GetContextualizationController()->GetCurrentPageContextEligibility()) {
    lens_overlay_query_controller_->SendMultimodalRequest(
        query_start_time, std::move(region), text_query, selection_type,
        initialization_data_->additional_search_query_params_, region_bitmap);
  }
}

void LensOverlayController::IssueSearchBoxRequest(
    base::Time query_start_time,
    const std::string& search_box_text,
    AutocompleteMatchType::Type match_type,
    bool is_zero_prefix_suggestion,
    std::map<std::string, std::string> additional_query_params) {
  // Log the interaction time here so the time to fetch new page bytes is not
  // intcluded.
  GetLensSessionMetricsLogger()
      ->RecordContextualSearchboxTimeToInteractionAfterNavigation();
  GetLensSessionMetricsLogger()->RecordTimeToFirstInteraction(
      lens::LensOverlayFirstInteractionType::kSearchbox);

  // Do not attempt to contextualize if CSB is disabled, if recontextualization
  // on each query is disabled, if the live page is not being displayed, or if
  // the user is not in the contextual search flow (aka, issues an image request
  // already).
  if (!lens::IsLensOverlayContextualSearchboxEnabled() ||
      !lens::features::ShouldLensOverlayRecontextualizeOnQuery() ||
      state() != State::kLivePageAndResults || !IsContextualSearchbox()) {
    IssueSearchBoxRequestPart2(query_start_time, search_box_text, match_type,
                               is_zero_prefix_suggestion,
                               additional_query_params);
    return;
  }

  // If contextual searchbox is enabled, make sure the page bytes are current
  // prior to issuing the search box request.
  GetContextualizationController()->TryUpdatePageContextualization(
      base::BindOnce(&LensOverlayController::IssueSearchBoxRequestPart2,
                     weak_factory_.GetWeakPtr(), query_start_time,
                     search_box_text, match_type, is_zero_prefix_suggestion,
                     additional_query_params));
}

void LensOverlayController::IssueContextualTextRequest(
    base::Time query_start_time,
    const std::string& text_query,
    lens::LensOverlaySelectionType selection_type) {
  if (GetContextualizationController()->GetCurrentPageContextEligibility()) {
    lens_selection_type_ = selection_type;
    lens_overlay_query_controller_->SendContextualTextQuery(
        query_start_time, text_query, selection_type,
        initialization_data_->additional_search_query_params_);
  }
}

void LensOverlayController::AddOverlayStateToSearchQuery(
    lens::SearchQuery& search_query) {
  // In the case where a query was triggered by a selection on the overlay or
  // use of the searchbox, initialization_data_ and
  // additional_search_query_params_ will have already been set. Record that
  // state in a search query struct.
  if (initialization_data_->selected_region_) {
    search_query.selected_region_ =
        initialization_data_->selected_region_->Clone();
  }
  if (!initialization_data_->selected_region_bitmap_.drawsNothing()) {
    search_query.selected_region_bitmap_ =
        initialization_data_->selected_region_bitmap_;
  }
  if (initialization_data_->selected_text_.has_value()) {
    search_query.selected_text_ = initialization_data_->selected_text_.value();
  }
  if (initialization_data_->translate_options_.has_value()) {
    search_query.translate_options_ =
        initialization_data_->translate_options_.value();
  }
  search_query.lens_selection_type_ = lens_selection_type_;
  search_query.additional_search_query_params_ =
      initialization_data_->additional_search_query_params_;
}

LensOverlayController::OverlayInitializationData::OverlayInitializationData(
    const SkBitmap& screenshot,
    SkBitmap rgb_screenshot,
    lens::PaletteId color_palette,
    GURL page_url,
    std::optional<std::string> page_title)
    : initial_screenshot_(screenshot),
      initial_rgb_screenshot_(std::move(rgb_screenshot)),
      updated_screenshot_(screenshot),
      color_palette_(color_palette),
      page_url_(page_url),
      page_title_(page_title) {}
LensOverlayController::OverlayInitializationData::~OverlayInitializationData() =
    default;

class LensOverlayController::UnderlyingWebContentsObserver
    : public content::WebContentsObserver {
 public:
  UnderlyingWebContentsObserver(content::WebContents* web_contents,
                                LensOverlayController* lens_overlay_controller)
      : content::WebContentsObserver(web_contents),
        lens_overlay_controller_(lens_overlay_controller) {}

  ~UnderlyingWebContentsObserver() override = default;

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

  // content::WebContentsObserver
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override {
    // If the overlay is off, check if we should display IPH.
    if (lens_overlay_controller_->state() == State::kOff) {
      // Only check IPH eligibility if the navigation changed the primary page.
      if (base::FeatureList::IsEnabled(
              feature_engagement::kIPHLensOverlayFeature) &&
          navigation_handle->IsInPrimaryMainFrame() &&
          !navigation_handle->IsSameDocument() &&
          navigation_handle->HasCommitted()) {
        lens_overlay_controller_->MaybeShowDelayedTutorialIPH(
            navigation_handle->GetURL());
      }
      return;
    }

    // If the overlay is open, check if we should close it.
    bool is_user_reload =
        navigation_handle->GetReloadType() != content::ReloadType::NONE &&
        !navigation_handle->IsRendererInitiated();
    // We don't need to close if:
    //   1) The navigation is not for the main page.
    //   2) The navigation hasn't been committed yet.
    //   3) The URL did not change and the navigation wasn't the user reloading
    //      the page.
    if (!navigation_handle->IsInPrimaryMainFrame() ||
        !navigation_handle->HasCommitted() ||
        (navigation_handle->GetPreviousPrimaryMainFrameURL() ==
             navigation_handle->GetURL() &&
         !is_user_reload)) {
      return;
    }
    if (lens_overlay_controller_->state() == State::kLivePageAndResults) {
      lens_overlay_controller_->UpdateNavigationMetrics();
      lens_overlay_controller_->NotifyPageContentUpdated();
      return;
    }
    lens_overlay_controller_->lens_search_controller_->CloseLensSync(
        lens::LensOverlayDismissalSource::kPageChanged);
  }

  void PrimaryMainFrameRenderProcessGone(
      base::TerminationStatus status) override {
    // Exit early if the overlay is off or already closing.
    if (lens_overlay_controller_->state() == State::kOff ||
        lens_overlay_controller_->IsOverlayClosing()) {
      return;
    }

    lens_overlay_controller_->lens_search_controller_->CloseLensSync(
        status == base::TERMINATION_STATUS_NORMAL_TERMINATION
            ? lens::LensOverlayDismissalSource::kPageRendererClosedNormally
            : lens::LensOverlayDismissalSource::
                  kPageRendererClosedUnexpectedly);
  }

 private:
  raw_ptr<LensOverlayController> lens_overlay_controller_;
};

void LensOverlayController::CaptureScreenshot() {
  state_ = State::kScreenshot;

  // Begin the process of grabbing a screenshot.
  content::RenderWidgetHostView* view = tab_->GetContents()
                                            ->GetPrimaryMainFrame()
                                            ->GetRenderViewHost()
                                            ->GetWidget()
                                            ->GetView();

  // During initialization and shutdown a capture may not be possible.
  if (!IsScreenshotPossible(view)) {
    lens_search_controller_->CloseLensSync(
        lens::LensOverlayDismissalSource::kErrorScreenshotCreationFailed);
    return;
  }

  // Side panel is now full closed, take screenshot and open overlay.
  view->CopyFromSurface(
      /*src_rect=*/gfx::Rect(), /*output_size=*/gfx::Size(),
      base::BindPostTask(
          base::SequencedTaskRunner::GetCurrentDefault(),
          base::BindOnce(
              &LensOverlayController::FetchViewportImageBoundingBoxes,
              weak_factory_.GetWeakPtr())));
}

void LensOverlayController::FetchViewportImageBoundingBoxes(
    const SkBitmap& bitmap) {
  content::RenderFrameHost* render_frame_host =
      tab_->GetContents()->GetPrimaryMainFrame();
  mojo::AssociatedRemote<chrome::mojom::ChromeRenderFrame> chrome_render_frame;
  render_frame_host->GetRemoteAssociatedInterfaces()->GetInterface(
      &chrome_render_frame);
  // Bind the InterfacePtr into the callback so that it's kept alive until
  // there's either a connection error or a response.
  auto* frame = chrome_render_frame.get();

  frame->RequestBoundsHintForAllImages(base::BindOnce(
      &LensOverlayController::GetPdfCurrentPage, weak_factory_.GetWeakPtr(),
      std::move(chrome_render_frame), ++screenshot_attempt_id_, bitmap));
}

void LensOverlayController::GetPdfCurrentPage(
    mojo::AssociatedRemote<chrome::mojom::ChromeRenderFrame>
        chrome_render_frame,
    int attempt_id,
    const SkBitmap& bitmap,
    const std::vector<gfx::Rect>& bounds) {
#if BUILDFLAG(ENABLE_PDF)
  if (lens::features::SendPdfCurrentPageEnabled()) {
    pdf::PDFDocumentHelper* pdf_helper =
        pdf::PDFDocumentHelper::MaybeGetForWebContents(tab_->GetContents());
    if (pdf_helper) {
      pdf_helper->GetMostVisiblePageIndex(base::BindOnce(
          &LensOverlayController::DidCaptureScreenshot,
          weak_factory_.GetWeakPtr(), std::move(chrome_render_frame),
          attempt_id, bitmap, bounds));
      return;
    }
  }
#endif  // BUILDFLAG(ENABLE_PDF)

  DidCaptureScreenshot(std::move(chrome_render_frame), attempt_id, bitmap,
                       bounds, /*pdf_current_page=*/std::nullopt);
}

void LensOverlayController::DidCaptureScreenshot(
    mojo::AssociatedRemote<chrome::mojom::ChromeRenderFrame>
        chrome_render_frame,
    int attempt_id,
    const SkBitmap& bitmap,
    const std::vector<gfx::Rect>& all_bounds,
    std::optional<uint32_t> pdf_current_page) {
  // While capturing a screenshot the overlay was cancelled. Do nothing.
  if (state_ == State::kOff || IsOverlayClosing()) {
    return;
  }

  // An id mismatch implies this is not the most recent screenshot attempt.
  if (screenshot_attempt_id_ != attempt_id) {
    return;
  }

  // The documentation for CopyFromSurface claims that the copy can fail, but
  // without providing information about how this can happen.
  // Supposedly IsSurfaceAvailableForCopy() should guard against this case, but
  // this is a multi-process, multi-threaded environment so there may be a
  // TOCTTOU race condition.
  if (bitmap.drawsNothing()) {
    lens_search_controller_->CloseLensSync(
        lens::LensOverlayDismissalSource::kErrorScreenshotCreationFailed);
    return;
  }

  if (lens::features::IsLensOverlayEarlyStartQueryFlowOptimizationEnabled()) {
    // Start the query as soon as the image is ready since it is the only
    // critical asynchronous flow. This optimization parallelizes the query flow
    // with other async startup processes.
    const auto& tab_url = tab_->GetContents()->GetLastCommittedURL();

    // Check if the page is context eligible. This should start the query flow
    // after the eligibility is fetched.
    GetContextualizationController()->IsPageContextEligible(
        tab_url, /*frame_metadata=*/{},
        base::BindOnce(&LensOverlayController::OnPageContextEligibilityFetched,
                       weak_factory_.GetWeakPtr(), bitmap, all_bounds,
                       pdf_current_page));
    return;
  }

  // The following two methods happen async to parallelize the two bottlenecks
  // in our invocation flow.
  CreateInitializationData(bitmap, all_bounds, pdf_current_page);
  ShowOverlay();

  state_ = State::kStartingWebUI;
}

void LensOverlayController::OnPageContextEligibilityFetched(
    const SkBitmap& bitmap,
    const std::vector<gfx::Rect>& all_bounds,
    std::optional<uint32_t> pdf_current_page,
    bool is_page_context_eligible) {
  auto bitmap_to_send = bitmap;
  auto page_url = lens_search_controller_->GetPageURL();
  auto page_title = lens_search_controller_->GetPageTitle();
  if (!is_page_context_eligible) {
    bitmap_to_send = SkBitmap();
    page_url = GURL();
    page_title = "";
  }

  lens_overlay_query_controller_->StartQueryFlow(
      bitmap_to_send, page_url, page_title,
      ConvertSignificantRegionBoxes(all_bounds),
      std::vector<lens::PageContent>(), lens::MimeType::kUnknown,
      pdf_current_page, GetUiScaleFactor(), invocation_time_);

  // The following two methods happen async to parallelize the two bottlenecks
  // in our invocation flow.
  CreateInitializationData(bitmap, all_bounds, pdf_current_page);
  ShowOverlay();

  state_ = State::kStartingWebUI;
}

void LensOverlayController::CreateInitializationData(
    const SkBitmap& screenshot,
    const std::vector<gfx::Rect>& all_bounds,
    std::optional<uint32_t> pdf_current_page) {
  // Create the new RGB bitmap async to prevent the main thread from blocking on
  // the encoding.
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::TaskPriority::USER_BLOCKING},
      base::BindOnce(&CreateRgbBitmap, screenshot),
      base::BindOnce(&LensOverlayController::ContinueCreateInitializationData,
                     weak_factory_.GetWeakPtr(), screenshot, all_bounds,
                     pdf_current_page));
}

void LensOverlayController::ContinueCreateInitializationData(
    const SkBitmap& screenshot,
    const std::vector<gfx::Rect>& all_bounds,
    std::optional<uint32_t> pdf_current_page,
    SkBitmap rgb_screenshot) {
  if (state_ != State::kStartingWebUI || rgb_screenshot.drawsNothing()) {
    // TODO(b/334185985): Handle case when screenshot RGB encoding fails.
    lens_search_controller_->CloseLensSync(
        lens::LensOverlayDismissalSource::kErrorScreenshotEncodingFailed);
    return;
  }

  // Resolve the color palette based on the vibrant screenshot color.
  lens::PaletteId color_palette = lens::PaletteId::kFallback;
  if (lens::features::IsDynamicThemeDetectionEnabled()) {
    std::vector<SkColor> colors;
    for (const auto& pair : lens::kPalettes) {
      colors.emplace_back(pair.first);
    }
    SkColor screenshot_color = lens::ExtractVibrantOrDominantColorFromImage(
        screenshot, lens::features::DynamicThemeMinPopulationPct());
    SkColor theme_color = lens::FindBestMatchedColorOrTransparent(
        colors, screenshot_color, lens::features::DynamicThemeMinChroma());
    if (theme_color != SK_ColorTRANSPARENT) {
      color_palette = lens::kPalettes.at(theme_color);
    }
  }

  auto initialization_data = std::make_unique<OverlayInitializationData>(
      screenshot, std::move(rgb_screenshot), color_palette,
      lens_search_controller_->GetPageURL(),
      lens_search_controller_->GetPageTitle());
  initialization_data->significant_region_boxes_ =
      ConvertSignificantRegionBoxes(all_bounds);
  initialization_data->last_retrieved_most_visible_page_ = pdf_current_page;

  GetContextualizationController()->GetPageContextualization(base::BindOnce(
      &LensOverlayController::StorePageContentAndContinueInitialization,
      weak_factory_.GetWeakPtr(), std::move(initialization_data)));
}

void LensOverlayController::StorePageContentAndContinueInitialization(
    std::unique_ptr<OverlayInitializationData> initialization_data,
    std::vector<lens::PageContent> page_contents,
    lens::MimeType primary_content_type,
    std::optional<uint32_t> page_count) {
  initialization_data->page_contents_ = page_contents;
  initialization_data->primary_content_type_ = primary_content_type;
  initialization_data->pdf_page_count_ = page_count;
  InitializeOverlay(std::move(initialization_data));

  // TODO(crbug.com/418825720): Remove this code once the optimization is fully
  // launched as this recording will instead be done in the contextualization
  // controller.
  if (!lens::features::IsLensOverlayEarlyStartQueryFlowOptimizationEnabled()) {
    GetContextualizationController()->SetPageContent(page_contents,
                                                     primary_content_type);
    GetContextualizationController()->RecordDocumentMetrics(page_count);
  }
}

std::vector<lens::mojom::CenterRotatedBoxPtr>
LensOverlayController::ConvertSignificantRegionBoxes(
    const std::vector<gfx::Rect>& all_bounds) {
  std::vector<lens::mojom::CenterRotatedBoxPtr> significant_region_boxes;
  int max_regions = lens::features::GetLensOverlayMaxSignificantRegions();
  if (max_regions == 0) {
    return significant_region_boxes;
  }
  content::RenderFrameHost* render_frame_host =
      tab_->GetContents()->GetPrimaryMainFrame();
  auto view_bounds = render_frame_host->GetView()->GetViewBounds();
  for (auto& image_bounds : all_bounds) {
    // Check the original area of the images against the minimum area.
    if (image_bounds.width() * image_bounds.height() >=
        lens::features::GetLensOverlaySignificantRegionMinArea()) {
      // We only have bounds for images in the main frame of the tab (i.e. not
      // in iframes), so view bounds are identical to tab bounds and can be
      // used for both parameters.
      significant_region_boxes.emplace_back(
          lens::GetCenterRotatedBoxFromTabViewAndImageBounds(
              view_bounds, view_bounds, image_bounds));
    }
  }
  // If an image is outside the viewpoint, the box will have zero area.
  std::erase_if(significant_region_boxes, [](const auto& box) {
    return box->box.height() == 0 || box->box.width() == 0;
  });
  // Sort by descending area.
  std::sort(significant_region_boxes.begin(), significant_region_boxes.end(),
            [](const auto& box1, const auto& box2) {
              return box1->box.height() * box1->box.width() >
                     box2->box.height() * box2->box.width();
            });
  // Treat negative values of max_regions as no limit.
  if (max_regions > 0 &&
      significant_region_boxes.size() > (unsigned long)max_regions) {
    significant_region_boxes.resize(max_regions);
  }

  return significant_region_boxes;
}

void LensOverlayController::SuppressGhostLoader() {
  if (page_) {
    page_->SuppressGhostLoader();
  }
  results_side_panel_coordinator_->SuppressGhostLoader();
}

void LensOverlayController::SetLiveBlur(bool enabled) {
  if (!lens_overlay_blur_layer_delegate_) {
    return;
  }

  if (enabled) {
    lens_overlay_blur_layer_delegate_->StartBackgroundImageCapture();
    return;
  }

  lens_overlay_blur_layer_delegate_->StopBackgroundImageCapture();
}

void LensOverlayController::ShowOverlay() {
  auto* contents_web_view = tab_->GetBrowserWindowInterface()->GetWebView();
  CHECK(contents_web_view);

  // If the view already exists, we just need to reshow it.
  if (overlay_view_) {
    // Restore the state to show the overlay.
    overlay_view_->SetVisible(true);
    preselection_widget_anchor_->SetVisible(true);
    overlay_web_view_->SetVisible(true);

    // Restart the live blur since the view is visible again.
    SetLiveBlur(true);

    // The overlay needs to be focused on show to immediately begin
    // receiving key events.
    overlay_web_view_->RequestFocus();

    // Disable mouse and keyboard inputs to the tab contents web view. Do this
    // after the overlay takes focus. If it is done before, focus will move from
    // the contents web view to another Chrome UI element before the overlay can
    // take focus.
    contents_web_view->SetEnabled(false);
    return;
  }

  // Create the views that will house our UI.
  overlay_view_ = CreateViewForOverlay();
  overlay_view_->SetVisible(true);

  // Sanity check that the overlay view is above the contents web view.
  auto* parent_view = overlay_view_->parent();
  views::View* child_contents_view = contents_web_view;
  // TODO(crbug.com/406794005): Remove this block if overlay_view_ ends up
  // getting reparented such that it always shares a parent with
  // contents_web_view.
  if (base::FeatureList::IsEnabled(features::kSideBySide)) {
    // When split view is enabled, there are two additional layers of
    // hierarchy:
    // BrowserView->MultiContentsView->ContentsContainerView->ContentsWebView
    // vs.
    // BrowserView->ContentsWebView
    // Since the overlay view is parented by BrowserView, to properly pass the
    // check below, we should only compare direct children of BrowserView.
    child_contents_view = child_contents_view->parent()->parent();
  }
  CHECK(parent_view->GetIndexOf(overlay_view_) >
        parent_view->GetIndexOf(child_contents_view));

  // Observe the overlay view to handle resizing the background blur layer.
  tab_contents_view_observer_.Observe(overlay_view_);

  // The overlay needs to be focused on show to immediately begin
  // receiving key events.
  CHECK(overlay_web_view_);
  overlay_web_view_->RequestFocus();

  // Disable mouse and keyboard inputs to the tab contents web view. Do this
  // after the overlay takes focus. If it is done before, focus will move from
  // the contents web view to another Chrome UI element before the overlay can
  // take focus.
  contents_web_view->SetEnabled(false);

  // Listen to the render process housing out overlay.
  overlay_web_view_->GetWebContents()
      ->GetPrimaryMainFrame()
      ->GetProcess()
      ->AddObserver(this);
}

void LensOverlayController::MaybeHideSharedOverlayView() {
  if (!overlay_view_) {
    return;
  }
  for (views::View* child : overlay_view_->children()) {
    if (child->GetVisible()) {
      // If any child is visible, it is being used by another tab so do not hide
      // the overlay view.
      return;
    }
  }
  overlay_view_->SetVisible(false);
}

void LensOverlayController::MaybeOpenSidePanel() {
  results_side_panel_coordinator_->RegisterEntryAndShow();
}

void LensOverlayController::InitializeOverlay(
    std::unique_ptr<OverlayInitializationData> initialization_data) {
  // Initialization data is ready.
  if (initialization_data) {
    // Confirm initialization_data has not already been assigned.
    CHECK(!initialization_data_);
    initialization_data_ = std::move(initialization_data);
  }

  // We can only continue once both the WebUI is bound and the initialization
  // data is processed and ready. If either of those conditions aren't met, we
  // exit early and wait for the other condition to call this method again.
  if (!page_ || !initialization_data_) {
    return;
  }

  // Move the data that was stored prior to initialization into
  // initialization_data_.
  if (pre_initialization_objects_.has_value()) {
    initialization_data_->objects_ =
        std::move(pre_initialization_objects_.value());
    pre_initialization_objects_.reset();
  }
  if (pre_initialization_text_.has_value()) {
    initialization_data_->text_ = std::move(pre_initialization_text_.value());
    pre_initialization_text_.reset();
  }

  InitializeOverlayUI(*initialization_data_);
  base::UmaHistogramBoolean("Lens.Overlay.Shown", true);

  // If the StartQueryFlow optimization is enabled, the page contents will not
  // be sent with the initial image request, so we need to send it here.
  if (lens::IsLensOverlayContextualSearchboxEnabled() &&
      lens::features::IsLensOverlayEarlyStartQueryFlowOptimizationEnabled() &&
      GetContextualizationController()->GetCurrentPageContextEligibility()) {
    // TODO(crbug.com/418856988): Replace this with a call that starts
    // contextualization without the unneeded callback.
    GetContextualizationController()->TryUpdatePageContextualization(
        base::DoNothing());
  }

  // Show the preselection overlay now that the overlay is initialized and ready
  // to be shown.
  if (!pending_region_) {
    ShowPreselectionBubble();
  }

  // Create the blur delegate so it is ready to blur once the view is visible.
  if (lens::features::GetLensOverlayUseBlur()) {
    content::RenderWidgetHost* live_page_widget_host =
        tab_->GetContents()
            ->GetPrimaryMainFrame()
            ->GetRenderViewHost()
            ->GetWidget();
    lens_overlay_blur_layer_delegate_ =
        std::make_unique<lens::LensOverlayBlurLayerDelegate>(
            live_page_widget_host);
  }

  state_ = State::kOverlay;
  lens_search_controller_->NotifyOverlayOpened();

  // Update the entry points state to ensure that the entry points are disabled
  // now that the overlay is showing.
  UpdateEntryPointsState();

  // Only start the query flow again if we don't already have a full image
  // response, unless the early start query flow optimization is enabled.
  if (!initialization_data_->has_full_image_response() &&
      !lens::features::IsLensOverlayEarlyStartQueryFlowOptimizationEnabled()) {
    if (!GetContextualizationController()->GetCurrentPageContextEligibility()) {
      initialization_data_->initial_screenshot_ = SkBitmap();
      initialization_data_->page_url_ = GURL();
      initialization_data_->page_title_ = "";
      should_send_screenshot_on_init_ = true;
    }

    lens_overlay_query_controller_->StartQueryFlow(
        initialization_data_->initial_screenshot_,
        initialization_data_->page_url_, initialization_data_->page_title_,
        std::move(initialization_data_->significant_region_boxes_),
        initialization_data_->page_contents_,
        initialization_data_->primary_content_type_,
        initialization_data_->last_retrieved_most_visible_page_,
        GetUiScaleFactor(), invocation_time_);

#if BUILDFLAG(ENABLE_PDF)
    // TODO(crbug.com/418825720): When StorePageContentAndContinueInitialization
    // is called, the contextualization controller does not update its own
    // contextualization. In this case, the partial PDF text should be sent here
    // if it is available.
    if (initialization_data_->primary_content_type_ == lens::MimeType::kPdf) {
      GetContextualizationController()
          ->FetchVisiblePageIndexAndGetPartialPdfText(
              initialization_data_->pdf_page_count_.value(),
              base::BindOnce(
                  &LensOverlayController::OnPdfPartialPageTextRetrieved,
                  weak_factory_.GetWeakPtr()));
    }
#endif
  }

  // If there is a pending contextual search request, issue it now that the
  // overlay is initialized.
  if (pending_contextual_search_request_) {
    std::move(pending_contextual_search_request_).Run();
  }

  // TODO(b/352622136): We should not start the lens request until the overlay
  // is open to prevent the side panel from opening while the overlay UI is
  // rendering.
  if (pending_region_) {
    // If there is a pending region (i.e. for image right click)
    // use INJECTED_IMAGE as the selection type.
    IssueLensRequest(invocation_time_since_epoch_, std::move(pending_region_),
                     lens::INJECTED_IMAGE, pending_region_bitmap_);
    pending_region_bitmap_.reset();
  }

  GetContextualizationController()->TryCalculateAndRecordOcrDomSimilarity();
}

void LensOverlayController::InitializeOverlayUI(
    const OverlayInitializationData& init_data) {
  // This should only contain LensPage mojo calls and should not affect
  // `state_`.
  CHECK(page_);
  // TODO(b/371593619), it would be more efficent to send all initialization
  // data to the overlay web UI in a single message.
  page_->ThemeReceived(CreateTheme(init_data.color_palette_));

  auto* lens_session_metrics_logger = GetLensSessionMetricsLogger();

  bool should_show_csb = !init_data.page_contents_.empty() &&
                         !init_data.page_contents_.front().bytes_.empty();
  if (should_show_csb) {
    lens_session_metrics_logger->OnContextualSearchboxShown();
  }
  lens_session_metrics_logger->OnInitialPageContentRetrieved(
      /*page_content_type=*/init_data.page_contents_.empty()
          ? lens::MimeType::kUnknown
          : init_data.primary_content_type_);

  page_->ShouldShowContextualSearchBox(should_show_csb);

  // If should show CSB, and the CSB viewport thumbnail is enabled, send it now.
  if (should_show_csb &&
      lens::features::GetVisualSelectionUpdatesEnableCsbThumbnail()) {
    GetLensSearchboxController()->HandleThumbnailCreatedBitmap(
        init_data.initial_screenshot_);
  }

  // Send the initial document type to the overlay web UI.
  NotifyPageContentUpdated();

  page_->ScreenshotDataReceived(init_data.initial_rgb_screenshot_);
  if (!init_data.objects_.empty()) {
    SendObjects(CopyObjects(init_data.objects_));
  }
  if (init_data.text_) {
    SendText(init_data.text_->Clone());
  }
  if (pending_region_) {
    page_->SetPostRegionSelection(pending_region_->Clone());
  }
  if (lens_search_controller_->IsHandshakeComplete()) {
    // Notify the overlay that it is safe to query autocomplete.
    page_->NotifyHandshakeComplete();
  }
}

bool LensOverlayController::IsContextualSearchbox() {
  return lens_search_controller_->lens_searchbox_controller()
      ->IsContextualSearchbox();
}

raw_ptr<views::View> LensOverlayController::CreateViewForOverlay() {
  // Grab the host view for the overlay which is owned by the browser view.
  auto* host_view = tab_->GetBrowserWindowInterface()->LensOverlayView();
  CHECK(host_view);

  // Setup a preselection anchor view. Usually bubbles are anchored to top
  // chrome, but top chrome is not always visible when our overlay is visible.
  // Instead of anchroing to top chrome, we anchor to this view because 1) it
  // always exists when the overlay exists and 2) it is before the WebView in
  // the view hierarchy and therefore will receive focus first when tabbing from
  // top chrome.
  std::unique_ptr<views::View> anchor_view = std::make_unique<views::View>();
  anchor_view->SetFocusBehavior(views::View::FocusBehavior::NEVER);
  preselection_widget_anchor_ = host_view->AddChildView(std::move(anchor_view));

  // Create the web view.
  std::unique_ptr<views::WebView> web_view = std::make_unique<views::WebView>(
      tab_->GetContents()->GetBrowserContext());
  content::WebContents* web_view_contents = web_view->GetWebContents();
  web_view->SetProperty(views::kElementIdentifierKey, kOverlayId);
  views::WebContentsSetBackgroundColor::CreateForWebContentsWithColor(
      web_view_contents, SK_ColorTRANSPARENT);

  // Set the label for the renderer process in Chrome Task Manager.
  task_manager::WebContentsTags::CreateForToolContents(
      web_view_contents, IDS_LENS_OVERLAY_RENDERER_LABEL);

  // As the embedder for the lens overlay WebUI content we must set the
  // appropriate tab interface here.
  webui::SetTabInterface(web_view_contents, GetTabInterface());

  // Set the web contents delegate to this controller so we can handle keyboard
  // events. Allow accelerators (e.g. hotkeys) to work on this web view.
  web_view->set_allow_accelerators(true);
  web_view->GetWebContents()->SetDelegate(this);

  // Load the untrusted WebUI into the web view.
  web_view->LoadInitialURL(GURL(chrome::kChromeUILensOverlayUntrustedURL));

  overlay_web_view_ = host_view->AddChildView(std::move(web_view));
  return host_view;
}

bool LensOverlayController::HandleContextMenu(
    content::RenderFrameHost& render_frame_host,
    const content::ContextMenuParams& params) {
  // We do not want to show the browser context menu on the overlay unless we
  // are in debugging mode. Returning true is equivalent to not showing the
  // context menu.
  return !lens::features::IsLensOverlayDebuggingEnabled();
}

bool LensOverlayController::HandleKeyboardEvent(
    content::WebContents* source,
    const input::NativeWebKeyboardEvent& event) {
  // This can be called before the overlay web view is attached to the overlay
  // view. In that case, the focus manager could be null.
  if (!overlay_web_view_ || !overlay_web_view_->GetFocusManager()) {
    return false;
  }
  return lens_search_controller_->lens_overlay_event_handler()
      ->HandleKeyboardEvent(source, event,
                            overlay_web_view_->GetFocusManager());
}

void LensOverlayController::OnFullscreenStateChanged() {
  // Flag is enabled to allow Lens Overlay in fullscreen no matter what so we
  // can exit early.
  if (lens::features::GetLensOverlayEnableInFullscreen()) {
    return;
  }
  // If there is top chrome we can keep the overlay open.
  if (tab_->GetBrowserWindowInterface()->IsTabStripVisible()) {
    return;
  }
  lens_search_controller_->CloseLensSync(
      lens::LensOverlayDismissalSource::kFullscreened);
}

void LensOverlayController::OnViewBoundsChanged(views::View* observed_view) {
  CHECK(observed_view == overlay_view_);

  // We now want to start the live blur since the screenshot has resized to
  // allow the blur to peek through.
  if (IsOverlayShowing()) {
    SetLiveBlur(true);
  }

  // Set our view to the same bounds as the contents web view so it always
  // covers the tab contents.
  if (lens_overlay_blur_layer_delegate_) {
    // Set the blur to have the same bounds as our view, but since it is in our
    // views local coordinate system, the blur should be positioned at (0,0).
    lens_overlay_blur_layer_delegate_->layer()->SetBounds(
        overlay_view_->GetLocalBounds());
  }
}

#if BUILDFLAG(IS_MAC)
void LensOverlayController::OnWidgetActivationChanged(views::Widget* widget,
                                                      bool active) {
  if (active && preselection_widget_) {
    // On Mac, traversing out of the preselection widget into the browser causes
    // the browser to restore its focus to the wrong place. Thus, when entering
    // the preselection widget, make sure to clear out the browser's native
    // focus. This causes the preselection widget to lose activation, so
    // reactivate it manually.
    tab_->GetBrowserWindowInterface()
        ->TopContainer()
        ->GetWidget()
        ->GetFocusManager()
        ->ClearNativeFocus();
    preselection_widget_->Activate();
  }
}
#endif

void LensOverlayController::OnWidgetDestroying(views::Widget* widget) {
  preselection_widget_ = nullptr;
  preselection_widget_observer_.Reset();
}

void LensOverlayController::OnOmniboxFocusChanged(
    OmniboxFocusState state,
    OmniboxFocusChangeReason reason) {
  if (state_ == LensOverlayController::State::kOverlay) {
    if (state == OMNIBOX_FOCUS_NONE) {
      ShowPreselectionBubble();
    } else {
      HidePreselectionBubble();
    }
  }
}

void LensOverlayController::OnFindEmptyText(
    content::WebContents* web_contents) {
  if (state_ == State::kLivePageAndResults) {
    return;
  }
  lens_search_controller_->CloseLensAsync(
      lens::LensOverlayDismissalSource::kFindInPageInvoked);
}

void LensOverlayController::OnFindResultAvailable(
    content::WebContents* web_contents) {
  if (state_ == State::kLivePageAndResults) {
    return;
  }
  lens_search_controller_->CloseLensAsync(
      lens::LensOverlayDismissalSource::kFindInPageInvoked);
}

void LensOverlayController::OnImmersiveRevealStarted() {
  // The toolbar has began to reveal. If the overlay is showing, hide the
  // preselection bubble to ensure it doesn't cover with the toolbar UI.
  if (IsOverlayShowing()) {
    HidePreselectionBubble();
  }
}

void LensOverlayController::OnImmersiveRevealEnded() {
  // The toolbar is no longer revealed. If the overlay is showing, reshow the
  // preselection bubble to ensure it doesn't cover with the toolbar UI.
  if (IsOverlayShowing()) {
    ShowPreselectionBubble();
  }
}

void LensOverlayController::OnImmersiveFullscreenEntered() {
  // The browser entered immersive fullscreen. If the overlay is showing, call
  // close and reopen the preselection bubble to ensure it respositions
  // correctly.
  if (IsOverlayShowing()) {
    CloseAndReshowPreselectionBubble();
  }
}

void LensOverlayController::OnImmersiveFullscreenExited() {
  // The browser exited immersive fullscreen. If the overlay is showing, call
  // close and reopen the preselection bubble to ensure it respositions
  // correctly.
  if (IsOverlayShowing()) {
    CloseAndReshowPreselectionBubble();
  }
}

void LensOverlayController::OnHandshakeComplete() {
  CHECK(lens_search_controller_->IsHandshakeComplete());
  // Notify the overlay that the handshake is complete if its initialized.
  if (page_) {
    page_->NotifyHandshakeComplete();
  }
}

metrics::OmniboxEventProto::PageClassification
LensOverlayController::GetPageClassification() const {
  return lens_search_controller_->lens_searchbox_controller()
      ->GetPageClassification();
}

float LensOverlayController::GetUiScaleFactor() {
  int device_scale_factor =
      tab_->GetContents()->GetRenderWidgetHostView()->GetDeviceScaleFactor();
  float page_scale_factor =
      zoom::ZoomController::FromWebContents(tab_->GetContents())
          ->GetZoomPercent() /
      100.0f;
  return device_scale_factor * page_scale_factor;
}

void LensOverlayController::OnSidePanelDidOpen() {
  // If a side panel opens that is not ours, we must close the overlay.
  if (side_panel_coordinator_->GetCurrentEntryId() !=
      SidePanelEntry::Id::kLensOverlayResults) {
    lens_search_controller_->CloseLensSync(
        lens::LensOverlayDismissalSource::kUnexpectedSidePanelOpen);
  }
}

void LensOverlayController::FinishedWaitingForReflow() {
  if (state_ == State::kClosingOpenedSidePanel) {
    // This path is invoked after the user invokes the overlay, but we needed
    // to close the side panel before taking a screenshot. The Side panel is
    // now closed so we can now take the screenshot of the page.
    CaptureScreenshot();
  }
}

void LensOverlayController::RenderProcessExited(
    content::RenderProcessHost* host,
    const content::ChildProcessTerminationInfo& info) {
  // Exit early if the overlay is already closing.
  if (IsOverlayClosing()) {
    return;
  }

  // The overlay's primary main frame process has exited, either cleanly or
  // unexpectedly. Close the overlay so that the user does not get into a broken
  // state where the overlay cannot be dismissed. Note that RenderProcessExited
  // can be called during the destruction of a frame in the overlay, so it is
  // important to post a task to close the overlay to avoid double-freeing the
  // overlay's frames. See https://crbug.com/371643466.
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(
          &LensSearchController::CloseLensSync,
          lens_search_controller_->GetWeakPtr(),
          info.status == base::TERMINATION_STATUS_NORMAL_TERMINATION
              ? lens::LensOverlayDismissalSource::kOverlayRendererClosedNormally
              : lens::LensOverlayDismissalSource::
                    kOverlayRendererClosedUnexpectedly));
}

void LensOverlayController::TabForegrounded(tabs::TabInterface* tab) {
  // Ignore the event if the overlay is not backgrounded.
  if (state_ != State::kBackground) {
    // TODO(crbug.com/404941800): This is a temporary DCHECK. This should be a
    // CHECK and the if statement above should be removed once the root cause
    // causing the CHECK(state_ == State::kBackground) to fail is found and
    // fixed.
    DCHECK(state_ == State::kBackground)
        << "State should be kBackground but is instead "
        << static_cast<int>(state_);
    return;
  }

  // If the overlay was backgrounded, restore the previous state.
  if (backgrounded_state_ != State::kLivePageAndResults) {
    ShowOverlay();
  }
  if (backgrounded_state_ != State::kOverlayAndResults &&
      backgrounded_state_ != State::kLivePageAndResults) {
    ShowPreselectionBubble();
  }
  if (lens::IsLensOverlayContextualSearchboxEnabled()) {
    SuppressGhostLoader();
  }

  state_ = backgrounded_state_;
  UpdateEntryPointsState();
}

void LensOverlayController::TabWillEnterBackground(tabs::TabInterface* tab) {
  // If the current tab was already backgrounded, do nothing.
  if (state_ == State::kBackground) {
    DCHECK(state_ != State::kBackground) << "State should not be kBackground.";
    return;
  }

  // If the overlay is active, background it.
  if (IsOverlayActive()) {
    // If the overlay is currently showing, then we should hide the UI.
    if (IsOverlayShowing()) {
      HideOverlay();
    }

    backgrounded_state_ = state_;
    state_ = State::kBackground;
    UpdateEntryPointsState();

    // TODO(crbug.com/335516480): Schedule the UI to be suspended.
  }
}

void LensOverlayController::ActivityRequestedByOverlay(
    ui::mojom::ClickModifiersPtr click_modifiers) {
  // The tab is expected to be in the foreground.
  if (!tab_->IsActivated()) {
    return;
  }
  tab_->GetBrowserWindowInterface()->OpenGURL(
      GURL(lens::features::GetLensOverlayActivityURL()),
      ui::DispositionFromClick(
          click_modifiers->middle_button, click_modifiers->alt_key,
          click_modifiers->ctrl_key, click_modifiers->meta_key,
          click_modifiers->shift_key,
          WindowOpenDisposition::NEW_FOREGROUND_TAB));
}

void LensOverlayController::AddBackgroundBlur() {
  // We do not blur unless the overlay is currently active and the blur delegate
  // was created.
  if (!lens_overlay_blur_layer_delegate_ ||
      (state_ != State::kOverlay && state_ != State::kOverlayAndResults)) {
    return;
  }

  // Add our blur layer to the view.
  overlay_web_view_->SetPaintToLayer();
  overlay_web_view_->layer()->Add(lens_overlay_blur_layer_delegate_->layer());
  overlay_web_view_->layer()->StackAtBottom(
      lens_overlay_blur_layer_delegate_->layer());
  lens_overlay_blur_layer_delegate_->layer()->SetBounds(
      overlay_web_view_->GetLocalBounds());
}

void LensOverlayController::CloseRequestedByOverlayCloseButton() {
  if (lens::features::IsLensOverlayBackToPageEnabled()) {
    lens_search_controller_->HideOverlay(
        lens::LensOverlayDismissalSource::kOverlayCloseButton);
    return;
  }

  lens_search_controller_->CloseLensAsync(
      lens::LensOverlayDismissalSource::kOverlayCloseButton);
}

void LensOverlayController::CloseRequestedByOverlayBackgroundClick() {
  if (lens::features::IsLensOverlayBackToPageEnabled()) {
    lens_search_controller_->HideOverlay(
        lens::LensOverlayDismissalSource::kOverlayBackgroundClick);
    return;
  }

  lens_search_controller_->CloseLensAsync(
      lens::LensOverlayDismissalSource::kOverlayBackgroundClick);
}

void LensOverlayController::FeedbackRequestedByOverlay() {
  lens::FeedbackRequestedByEvent(tab_, ui::EF_NONE);
}

void LensOverlayController::GetOverlayInvocationSource(
    GetOverlayInvocationSourceCallback callback) {
  std::move(callback).Run(GetInvocationSourceString());
}

void LensOverlayController::InfoRequestedByOverlay(
    ui::mojom::ClickModifiersPtr click_modifiers) {
  // The tab is expected to be in the foreground.
  if (!tab_->IsActivated()) {
    return;
  }
  tab_->GetBrowserWindowInterface()->OpenGURL(
      GURL(lens::features::GetLensOverlayHelpCenterURL()),
      ui::DispositionFromClick(
          click_modifiers->middle_button, click_modifiers->alt_key,
          click_modifiers->ctrl_key, click_modifiers->meta_key,
          click_modifiers->shift_key,
          WindowOpenDisposition::NEW_FOREGROUND_TAB));
}

void LensOverlayController::IssueLensRegionRequest(
    lens::mojom::CenterRotatedBoxPtr region,
    bool is_click) {
  IssueLensRequest(/*query_start_time=*/base::Time::Now(), std::move(region),
                   is_click ? lens::TAP_ON_EMPTY : lens::REGION_SEARCH,
                   std::nullopt);
}

void LensOverlayController::IssueLensObjectRequest(
    lens::mojom::CenterRotatedBoxPtr region,
    bool is_mask_click) {
  IssueLensRequest(
      /*query_start_time=*/base::Time::Now(), std::move(region),
      is_mask_click ? lens::TAP_ON_REGION_GLEAM : lens::TAP_ON_OBJECT,
      std::nullopt);
}

void LensOverlayController::IssueTextSelectionRequest(const std::string& query,
                                                      int selection_start_index,
                                                      int selection_end_index,
                                                      bool is_translate) {
  initialization_data_->additional_search_query_params_.clear();
  lens_selection_type_ =
      is_translate ? lens::SELECT_TRANSLATED_TEXT : lens::SELECT_TEXT_HIGHLIGHT;

  IssueTextSelectionRequestInner(/*query_start_time=*/base::Time::Now(), query,
                                 selection_start_index, selection_end_index);
}

void LensOverlayController::IssueTranslateSelectionRequest(
    const std::string& query,
    const std::string& content_language,
    int selection_start_index,
    int selection_end_index) {
  initialization_data_->additional_search_query_params_.clear();
  lens::AppendTranslateParamsToMap(
      initialization_data_->additional_search_query_params_, query, "auto");
  lens_selection_type_ = lens::TRANSLATE_CHIP;

  IssueTextSelectionRequestInner(/*query_start_time=*/base::Time::Now(), query,
                                 selection_start_index, selection_end_index);
}

void LensOverlayController::IssueMathSelectionRequest(
    const std::string& query,
    const std::string& formula,
    int selection_start_index,
    int selection_end_index) {
  initialization_data_->additional_search_query_params_.clear();
  lens::AppendStickinessSignalForFormula(
      initialization_data_->additional_search_query_params_, formula);
  lens_selection_type_ = lens::SYMBOLIC_MATH_OBJECT;

  IssueTextSelectionRequestInner(/*query_start_time=*/base::Time::Now(), query,
                                 selection_start_index, selection_end_index);
}

void LensOverlayController::IssueTextSelectionRequestInner(
    base::Time query_start_time,
    const std::string& query,
    int selection_start_index,
    int selection_end_index) {
  initialization_data_->selected_region_.reset();
  initialization_data_->selected_region_bitmap_.reset();
  initialization_data_->selected_text_ =
      std::make_pair(selection_start_index, selection_end_index);

  GetLensSearchboxController()->SetSearchboxInputText(query);
  GetLensSearchboxController()->SetSearchboxThumbnail(std::string());

  lens_overlay_query_controller_->SendTextOnlyQuery(
      query_start_time, query, lens_selection_type_,
      initialization_data_->additional_search_query_params_);
  MaybeOpenSidePanel();
  GetLensSessionMetricsLogger()->RecordTimeToFirstInteraction(
      lens::LensOverlayFirstInteractionType::kTextSelect);
  state_ = State::kOverlayAndResults;
  MaybeLaunchSurvey();
}

void LensOverlayController::ClosePreselectionBubble() {
  if (preselection_widget_) {
    preselection_widget_->Close();
    preselection_widget_ = nullptr;
    preselection_widget_observer_.Reset();
  }
}

void LensOverlayController::ShowPreselectionBubble() {
  // Don't show the preselection bubble if the overlay is not being shown.
  if (state() == State::kOverlayAndResults) {
    return;
  }

#if BUILDFLAG(IS_MAC)
  // On Mac, the kShowFullscreenToolbar pref is used to determine whether the
  // toolbar is always shown. This causes the toolbar to never unreveal, meaning
  // the preselection bubble will never be shown. Check for this case and show
  // the preselection bubble if needed.
  const bool always_show_toolbar =
      pref_service_->GetBoolean(prefs::kShowFullscreenToolbar);
#else
  const bool always_show_toolbar = false;
#endif  // BUILDFLAG(IS_MAC)

  if (!always_show_toolbar && tab_->GetBrowserWindowInterface()
                                  ->GetImmersiveModeController()
                                  ->IsRevealed()) {
    // If the immersive mode controller is revealing top chrome, do not show
    // the preselection bubble. The bubble will be shown once the reveal
    // finishes.
    return;
  }

  if (!preselection_widget_) {
    CHECK(preselection_widget_anchor_);
    // Setup the preselection widget.
    preselection_widget_ = views::BubbleDialogDelegateView::CreateBubble(
        std::make_unique<lens::LensPreselectionBubble>(
            weak_factory_.GetWeakPtr(), preselection_widget_anchor_,
            net::NetworkChangeNotifier::IsOffline(),
            /*exit_clicked_callback=*/
            base::BindRepeating(
                &LensSearchController::CloseLensSync,
                lens_search_controller_->GetWeakPtr(),
                lens::LensOverlayDismissalSource::kPreselectionToastExitButton),
            /*on_cancel_callback=*/
            base::BindOnce(&LensSearchController::CloseLensSync,
                           lens_search_controller_->GetWeakPtr(),
                           lens::LensOverlayDismissalSource::
                               kPreselectionToastEscapeKeyPress)));
    preselection_widget_->SetNativeWindowProperty(
        views::kWidgetIdentifierKey,
        const_cast<void*>(kLensOverlayPreselectionWidgetIdentifier));
    preselection_widget_observer_.Observe(preselection_widget_);
    // Setting the parent allows focus traversal out of the preselection widget.
    preselection_widget_->SetFocusTraversableParent(
        preselection_widget_anchor_->GetWidget()->GetFocusTraversable());
    preselection_widget_->SetFocusTraversableParentView(
        preselection_widget_anchor_);
  }

  // When in fullscreen, top Chrome may cover this widget on Mac. Set the
  // z-order to floating UI element to ensure the widget is above the top
  // Chrome. Only do this if immersive mode is enabled to avoid issues with
  // the preselection widget covering other windows.
  if (tab_->GetBrowserWindowInterface()
          ->GetImmersiveModeController()
          ->IsEnabled()) {
    preselection_widget_->SetZOrderLevel(ui::ZOrderLevel::kFloatingUIElement);
  } else {
    preselection_widget_->SetZOrderLevel(ui::ZOrderLevel::kNormal);
  }

  auto* bubble_view = static_cast<lens::LensPreselectionBubble*>(
      preselection_widget_->widget_delegate());
  bubble_view->SetCanActivate(true);

  // The bubble position is dependent on if top chrome is showing. Resize the
  // bubble to ensure the correct position is used.
  bubble_view->SizeToContents();
  // Show inactive so that the overlay remains active.
  preselection_widget_->ShowInactive();
}

void LensOverlayController::CloseAndReshowPreselectionBubble() {
  // If the preselection bubble is already closed, do not reshow it.
  if (!preselection_widget_) {
    return;
  }
  ClosePreselectionBubble();
  ShowPreselectionBubble();
}

void LensOverlayController::HidePreselectionBubble() {
  if (preselection_widget_) {
    // The preselection bubble remains in the browser's focus order even when it
    // is hidden, for example, when another browser tab is active. This means it
    // remains possible for the bubble to be activated by keyboard input i.e.
    // tabbing into the bubble, which unhides the bubble even on a browser tab
    // where the overlay is not being shown. Prevent this by setting the bubble
    // to non-activatable while it is hidden.
    auto* bubble_view = static_cast<lens::LensPreselectionBubble*>(
        preselection_widget_->widget_delegate());
    bubble_view->SetCanActivate(false);

    preselection_widget_->Hide();
  }
}

void LensOverlayController::IssueSearchBoxRequestPart2(
    base::Time query_start_time,
    const std::string& search_box_text,
    AutocompleteMatchType::Type match_type,
    bool is_zero_prefix_suggestion,
    std::map<std::string, std::string> additional_query_params) {
  // TODO(crbug.com/404941800): Re-add check for state == kOff once the
  // contextualization flow is fully decoupled from the overlay.
  // If the overlay is closing, do not attempt to issue the query.
  if (IsOverlayClosing()) {
    return;
  }
  initialization_data_->additional_search_query_params_ =
      additional_query_params;

  if (initialization_data_->selected_region_.is_null() &&
      GetPageClassification() ==
          metrics::OmniboxEventProto::SEARCH_SIDE_PANEL_SEARCHBOX) {
    // Non-Lens and non-contextual searches should not have a selection type.
    lens_selection_type_ = lens::UNKNOWN_SELECTION_TYPE;
  } else if (is_zero_prefix_suggestion) {
    lens_selection_type_ = lens::MULTIMODAL_SUGGEST_ZERO_PREFIX;
  } else if (match_type == AutocompleteMatchType::Type::SEARCH_WHAT_YOU_TYPED) {
    lens_selection_type_ = lens::MULTIMODAL_SEARCH;
  } else {
    lens_selection_type_ = lens::MULTIMODAL_SUGGEST_TYPEAHEAD;
  }

  if (!GetContextualizationController()->GetCurrentPageContextEligibility()) {
    // Do not send any requests if the page is not context eligible.
  } else if (initialization_data_->selected_region_.is_null() &&
             IsContextualSearchbox()) {
    lens_overlay_query_controller_->SendContextualTextQuery(
        query_start_time, search_box_text, lens_selection_type_,
        initialization_data_->additional_search_query_params_);
    GetLensSessionMetricsLogger()->OnContextualSearchboxQueryIssued(
        is_zero_prefix_suggestion,
        /*is_initial_query=*/state_ == State::kOverlay);
  } else if (initialization_data_->selected_region_.is_null()) {
    lens_overlay_query_controller_->SendTextOnlyQuery(
        query_start_time, search_box_text, lens_selection_type_,
        initialization_data_->additional_search_query_params_);
  } else {
    std::optional<SkBitmap> selected_region_bitmap =
        initialization_data_->selected_region_bitmap_.drawsNothing()
            ? std::nullopt
            : std::make_optional<SkBitmap>(
                  initialization_data_->selected_region_bitmap_);
    lens_overlay_query_controller_->SendMultimodalRequest(
        query_start_time, initialization_data_->selected_region_.Clone(),
        search_box_text, lens_selection_type_,
        initialization_data_->additional_search_query_params_,
        selected_region_bitmap);
  }

  // If we are in the zero state, this request must have come from CSB. In that
  // case, hide the overlay to allow live page to show through.
  if (state_ == State::kOverlay) {
    HideOverlay();
  }

  // If this a search query from the side panel search box with the overlay
  // showing, keep the state as kOverlayAndResults. Else, we are in our
  // contextual flow and the state needs to stay as State::kLivePageAndResults.
  state_ = state_ == State::kOverlayAndResults ? State::kOverlayAndResults
                                               : State::kLivePageAndResults;

  // The searchbox text is set once the URL loads in the results frame, however,
  // adding it here allows the user to see the text query in the searchbox while
  // a long query loads.
  GetLensSearchboxController()->SetSearchboxInputText(search_box_text);

  MaybeOpenSidePanel();
  // Only set the side panel to loading if the page is context eligible because
  // otherwise there will be no results to load.
  bool is_context_eligible =
      GetContextualizationController()->GetCurrentPageContextEligibility();
  results_side_panel_coordinator_->SetSidePanelIsLoadingResults(
      is_context_eligible);
  results_side_panel_coordinator_->SetShowProtectedErrorPage(
      !is_context_eligible);

  MaybeLaunchSurvey();
}

void LensOverlayController::HandleStartQueryResponse(
    std::vector<lens::mojom::OverlayObjectPtr> objects,
    lens::mojom::TextPtr text,
    bool is_error) {
  // TODO(crbug.com/404941800): State can be off temporarily if the user
  // contextualizes via an entry point that does not show the overlay UI. Remove
  // the check for kOff after migrating the contextualization flow since this
  // function should not be called when the overlay is not open.
  if (state_ == State::kOff) {
    return;
  }

  // If the side panel is open, then the error page state can change depending
  // on whether the query succeeded or not. If the side panel is not open, the
  // error page state can only change if the query failed since the first side
  // panel navigation will take care of recording whether the result was shown.
  const bool is_side_panel_open =
      results_side_panel_coordinator_->IsSidePanelBound();
  if (is_side_panel_open &&
      !results_side_panel_coordinator_->IsShowingProtectedErrorPage()) {
    results_side_panel_coordinator_->MaybeSetSidePanelShowErrorPage(
        is_error,
        is_error
            ? lens::mojom::SidePanelResultStatus::kErrorPageShownStartQueryError
            : lens::mojom::SidePanelResultStatus::kResultShown);
  } else if (!is_side_panel_open && is_error) {
    results_side_panel_coordinator_->MaybeSetSidePanelShowErrorPage(
        /*should_show_error_page=*/true,
        lens::mojom::SidePanelResultStatus::kErrorPageShownStartQueryError);
  }

  if (!objects.empty()) {
    SendObjects(std::move(objects));
  }

  // Text can be null if there was no text within the server response.
  if (!text.is_null()) {
    // If the initialization data is not yet ready, SendText will store the text
    // to be attached when ready.
    if (initialization_data_) {
      initialization_data_->text_ = text.Clone();
    }

    SendText(std::move(text));

    // Try and record the OCR DOM similarity since the OCR text is now
    // available.
    GetContextualizationController()->TryCalculateAndRecordOcrDomSimilarity();
  }
}

void LensOverlayController::HandleInteractionURLResponse(
    lens::proto::LensOverlayUrlResponse response) {
  MaybeOpenSidePanel();
  if (lens::features::IsLensSearchSidePanelScrollToAPIEnabled()) {
    results_side_panel_coordinator_->SetLatestPageUrlWithResponse(
        GURL(response.page_url()));
  }
  results_side_panel_coordinator_->LoadURLInResultsFrame(GURL(response.url()));
}

void LensOverlayController::HandleInteractionResponse(
    lens::mojom::TextPtr text) {
  SendText(std::move(text));
}

void LensOverlayController::HandlePageContentUploadProgress(uint64_t position,
                                                            uint64_t total) {
  // If the progress bar is disabled, do not show it.
  if (!lens::features::ShouldShowUploadProgressBar() ||
      !is_upload_progress_bar_shown_ || !IsContextualSearchbox()) {
    return;
  }

  float progress = total > 0 ? static_cast<float>(position) / total : 1.0f;

  // For the first upload handler event received, check if the progress is above
  // the heuristic threshold. If so, do not show the progress bar because it is
  // assumed that the upload will finish quickly, and showing the progress bar
  // would be distracting.
  if (is_first_upload_handler_event_) {
    is_first_upload_handler_event_ = false;
    if (total > 0 &&
        progress > lens::features::GetUploadProgressBarShowHeuristic()) {
      is_upload_progress_bar_shown_ = false;
      return;
    }
  }

  results_side_panel_coordinator_->SetPageContentUploadProgress(
      total > 0 ? static_cast<float>(position) / total : 1.0f);
}

void LensOverlayController::HideOverlay() {
  // Re-enable mouse and keyboard events to the tab contents web view, and take
  // focus before the overlay view is hidden. If it is done after, focus will
  // move from the overlay view to another Chrome UI element before the contents
  // web view can take focus.
  auto* contents_web_view = tab_->GetBrowserWindowInterface()->GetWebView();
  CHECK(contents_web_view);
  contents_web_view->SetEnabled(true);
  contents_web_view->RequestFocus();

  // Hide the overlay view, but keep the web view attached to the overlay view
  // so that the overlay can be re-shown without creating a new web view.
  preselection_widget_anchor_->SetVisible(false);
  overlay_web_view_->SetVisible(false);
  MaybeHideSharedOverlayView();

  SetLiveBlur(false);
  HidePreselectionBubble();
}

void LensOverlayController::HideOverlayAndMaybeSetLivePageState() {
  HideOverlay();

  // If the side panel is open, set the overlay state to kLivePageAndResults.
  if (results_side_panel_coordinator_->IsSidePanelBound()) {
    state_ = State::kLivePageAndResults;
  }
}

void LensOverlayController::MaybeLaunchSurvey() {
  lens_search_controller_->MaybeLaunchSurvey();
}

void LensOverlayController::InitializeTutorialIPHUrlMatcher() {
  if (!base::FeatureList::IsEnabled(
          feature_engagement::kIPHLensOverlayFeature)) {
    return;
  }

  tutorial_iph_url_matcher_ = std::make_unique<url_matcher::URLMatcher>();
  base::MatcherStringPattern::ID id(0);
  url_matcher::util::AddFiltersWithLimit(
      tutorial_iph_url_matcher_.get(), true, &id,
      JSONArrayToVector(
          feature_engagement::kIPHLensOverlayUrlAllowFilters.Get()),
      &iph_url_filters_);
  url_matcher::util::AddFiltersWithLimit(
      tutorial_iph_url_matcher_.get(), false, &id,
      JSONArrayToVector(
          feature_engagement::kIPHLensOverlayUrlBlockFilters.Get()),
      &iph_url_filters_);

  auto force_allow_url_strings = JSONArrayToVector(
      feature_engagement::kIPHLensOverlayUrlForceAllowedUrlMatchPatterns.Get());
  std::vector<base::MatcherStringPattern> force_allow_url_patterns;
  std::vector<const base::MatcherStringPattern*> force_allow_url_pointers;
  force_allow_url_patterns.reserve(force_allow_url_strings.size());
  force_allow_url_pointers.reserve(force_allow_url_strings.size());
  for (const std::string& entry : force_allow_url_strings) {
    force_allow_url_patterns.emplace_back(entry, ++id);
    force_allow_url_pointers.push_back(&force_allow_url_patterns.back());
  }
  forced_url_matcher_ = std::make_unique<url_matcher::RegexSetMatcher>();
  // Pointers will not be referenced after AddPatterns() completes.
  forced_url_matcher_->AddPatterns(force_allow_url_pointers);

  auto allow_strings = JSONArrayToVector(
      feature_engagement::kIPHLensOverlayUrlPathMatchAllowPatterns.Get());
  std::vector<base::MatcherStringPattern> allow_patterns;
  std::vector<const base::MatcherStringPattern*> allow_pointers;
  allow_patterns.reserve(allow_strings.size());
  allow_pointers.reserve(allow_strings.size());
  for (const std::string& entry : allow_strings) {
    allow_patterns.emplace_back(entry, ++id);
    allow_pointers.push_back(&allow_patterns.back());
  }
  page_path_allow_matcher_ = std::make_unique<url_matcher::RegexSetMatcher>();
  // Pointers will not be referenced after AddPatterns() completes.
  page_path_allow_matcher_->AddPatterns(allow_pointers);

  auto block_strings = JSONArrayToVector(
      feature_engagement::kIPHLensOverlayUrlPathMatchBlockPatterns.Get());
  std::vector<base::MatcherStringPattern> block_patterns;
  std::vector<const base::MatcherStringPattern*> block_pointers;
  block_patterns.reserve(block_strings.size());
  block_pointers.reserve(block_strings.size());
  for (const std::string& entry : block_strings) {
    block_patterns.emplace_back(entry, ++id);
    block_pointers.push_back(&block_patterns.back());
  }
  page_path_block_matcher_ = std::make_unique<url_matcher::RegexSetMatcher>();
  // Pointers will not be referenced after AddPatterns() completes.
  page_path_block_matcher_->AddPatterns(block_pointers);
}

void LensOverlayController::MaybeShowDelayedTutorialIPH(const GURL& url) {
  auto* entry_point_controller = tab_->GetBrowserWindowInterface()
                                     ->GetFeatures()
                                     .lens_overlay_entry_point_controller();
  if (!entry_point_controller || !entry_point_controller->IsEnabled()) {
    return;
  }

  // If a tutorial IPH was already queued, cancel it.
  tutorial_iph_timer_.Stop();

  if (IsUrlEligibleForTutorialIPH(url)) {
    tutorial_iph_timer_.Start(
        FROM_HERE, feature_engagement::kIPHLensOverlayDelayTime.Get(),
        base::BindOnce(&LensOverlayController::ShowTutorialIPH,
                       weak_factory_.GetWeakPtr()));
  }
}

void LensOverlayController::UpdateNavigationMetrics() {
  GetLensSessionMetricsLogger()->OnPageNavigation();
}

void LensOverlayController::ClearAllSelections() {
  if (state_ == State::kOff) {
    return;
  }

  if (page_) {
    page_->ClearAllSelections();
  }
  initialization_data_->selected_region_.reset();
  initialization_data_->selected_region_bitmap_.reset();
  initialization_data_->selected_text_.reset();
  if (!IsContextualSearchbox()) {
    lens_selection_type_ = lens::UNKNOWN_SELECTION_TYPE;
  }
}

void LensOverlayController::HandleRegionBitmapCreated(
    const SkBitmap& region_bitmap) {
  // Do not update the selected region bitmap if the overlay is off or if the
  // region bitmap is already set. This is only enabled when the back to page
  // feature is enabled.
  if (state_ == State::kOff ||
      !initialization_data_->selected_region_bitmap_.drawsNothing() ||
      !lens::features::IsLensOverlayBackToPageEnabled()) {
    return;
  }

  initialization_data_->selected_region_bitmap_ = region_bitmap;
}

bool LensOverlayController::IsUrlEligibleForTutorialIPH(const GURL& url) {
  if (!tutorial_iph_url_matcher_) {
    return false;
  }

  // Check if the URL matches any of the allow filters. If it does not,
  // return false immediately as this should not be a shown match.
  auto matches = tutorial_iph_url_matcher_.get()->MatchURL(url);
  if (!matches.size()) {
    return false;
  }

  // Now that the URL is allowed, check if it matches any of the block filters.
  // If it does, return false as to block this URL from showing the IPH.
  for (auto match : matches) {
    // Blocks take precedence over allows.
    if (!iph_url_filters_[match].allow) {
      return false;
    }
  }

  // Now that the URL is an allowed URL, verify the match is not blocked by
  // the block matcher. If it does contain blocked words in its path, return
  // false to prevent the IPH from being shown.
  if (page_path_block_matcher_ && !page_path_block_matcher_->IsEmpty() &&
      page_path_block_matcher_->Match(url.path(), &matches)) {
    return false;
  }

  // Check if the URL matches any of the forced allowed URLs. If it does, return
  // true as this should be a shown match even if the path does not contain an
  // allowlisted pattern (below).
  if (forced_url_matcher_ && !forced_url_matcher_->IsEmpty() &&
      forced_url_matcher_->Match(url.spec(), &matches)) {
    return true;
  }

  // Finally, check if the URL matches any of the allowed patterns. If it
  // doesn't, return false to prevent the IPH from being shown.
  if (page_path_allow_matcher_ && !page_path_allow_matcher_->IsEmpty() &&
      !page_path_allow_matcher_->Match(url.path(), &matches)) {
    return false;
  }

  // Finally if all checks pass, this must be a valid match. I.e.:
  // 1. The URL matches at least one of the allowed URLs.
  // 2. The URL does not match any of the blocked URLs.
  // 3. The URL does not match any of the block path patterns.
  // 4. The URL matches at least one of the allowed path patterns.
  return true;
}

void LensOverlayController::ShowTutorialIPH() {
  if (auto* user_ed =
          tab_->GetBrowserWindowInterface()->GetUserEducationInterface()) {
    user_ed->MaybeShowFeaturePromo(feature_engagement::kIPHLensOverlayFeature);
  }
}

void LensOverlayController::NotifyUserEducationAboutOverlayUsed() {
  if (auto* user_ed =
          tab_->GetBrowserWindowInterface()->GetUserEducationInterface()) {
    user_ed->NotifyFeaturePromoFeatureUsed(
        feature_engagement::kIPHLensOverlayFeature,
        FeaturePromoFeatureUsedAction::kClosePromoIfPresent);
  }
}

void LensOverlayController::NotifyPageContentUpdated() {
  auto page_content_type = lens::StringMimeTypeToMojoPageContentType(
      tab_->GetContents()->GetContentsMimeType());
  if (page_) {
    page_->PageContentTypeChanged(page_content_type);
  }
  results_side_panel_coordinator_->NotifyPageContentUpdated();
}

void LensOverlayController::UpdateEntryPointsState() {
  tab_->GetBrowserWindowInterface()
      ->GetFeatures()
      .lens_overlay_entry_point_controller()
      ->UpdateEntryPointsState(
          /*hide_toolbar_entrypoint=*/false);
}

void LensOverlayController::OnPdfPartialPageTextRetrieved(
    std::vector<std::u16string> pdf_pages_text) {
  initialization_data_->pdf_pages_text_ = std::move(pdf_pages_text);
}

void LensOverlayController::OnPageContextUpdatedForSuggestion(
    const GURL& destination_url,
    AutocompleteMatchType::Type match_type,
    bool is_zero_prefix_suggestion,
    lens::LensOverlayInvocationSource invocation_source) {
  // TODO(crbug.com/404941800): Eventually, this should be a CHECK or removed
  // once the contextualization controller is separated from the overlay. For
  // now, this is required to prevent failures when opening the side panel.
  // `initialization_data_` is used in IssueSearchBoxRequestPart2 to determine
  // what type of query to send (contextual or text only).
  if (state_ == State::kOff) {
    initialization_data_ = std::make_unique<OverlayInitializationData>(
        SkBitmap(), SkBitmap(), lens::PaletteId::kFallback,
        lens_search_controller_->GetPageURL(),
        lens_search_controller_->GetPageTitle());
  }

  // TODO(crbug.com/404941800): Similar to above, this should be a CHECK or
  // removed once the contextualization controller is separated from the
  // overlay. For now, this is required to prevent failures when opening the
  // side panel.
  if (!results_side_panel_coordinator_) {
    results_side_panel_coordinator_ =
        lens_search_controller_->lens_overlay_side_panel_coordinator();
  }

  CHECK(lens_overlay_query_controller_);
  // TODO(crbug.com/404941800): This flow should not start the overlay once
  // contextualization is separated from the overlay.
  GetLensSearchboxController()->OnSuggestionAccepted(
      destination_url, match_type, is_zero_prefix_suggestion);
}

lens::LensSearchboxController*
LensOverlayController::GetLensSearchboxController() {
  return lens_search_controller_->lens_searchbox_controller();
}

lens::LensSearchContextualizationController*
LensOverlayController::GetContextualizationController() {
  return lens_search_controller_->lens_search_contextualization_controller();
}

lens::LensSessionMetricsLogger*
LensOverlayController::GetLensSessionMetricsLogger() {
  return lens_search_controller_->lens_session_metrics_logger();
}