summaryrefslogtreecommitdiffstats
path: root/src/webenginewidgets/api/qwebengineview.cpp
blob: 5fdd995b8dfd02161816e9df89d4d9a468cd44ff (plain)
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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWebEngine module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qwebenginenotificationpresenter_p.h"
#include "qwebengineview.h"
#include "qwebengineview_p.h"
#include "render_widget_host_view_qt_delegate_client.h"
#include "render_widget_host_view_qt_delegate_item.h"
#include "qwebengine_accessible.h"
#include "ui/autofillpopupwidget_p.h"

#include <QtWebEngineCore/private/qwebenginepage_p.h>
#include <QtWebEngineCore/qwebenginecontextmenurequest.h>
#include <QtWebEngineCore/qwebenginehistory.h>
#include <QtWebEngineCore/qwebenginehttprequest.h>
#include <QtWebEngineCore/qwebengineprofile.h>

#include "autofill_popup_controller.h"
#include "color_chooser_controller.h"
#include "web_contents_adapter.h"

#include <QContextMenuEvent>
#include <QToolTip>
#include <QVBoxLayout>
#include <QKeyEvent>
#include <QIcon>
#include <QStyle>
#include <QGuiApplication>
#include <QQuickWidget>

#if QT_CONFIG(action)
#include <QAction>
#endif

#if QT_CONFIG(colordialog)
#include <QColorDialog>
#endif

#if QT_CONFIG(filedialog)
#include <QFileDialog>
#include <QStandardPaths>
#include "file_picker_controller.h"
#endif

#if QT_CONFIG(inputdialog)
#include <QInputDialog>
#endif

#if QT_CONFIG(menu)
#include <QMenu>
#endif

#if QT_CONFIG(messagebox)
#include <QMessageBox>
#endif

#if QT_CONFIG(webengine_printing_and_pdf)
#include "printing/printer_worker.h"

#include <QPrinter>
#include <QThread>
#endif

namespace QtWebEngineCore {
class WebEngineQuickWidget : public QQuickWidget, public WidgetDelegate
{
public:
    WebEngineQuickWidget(RenderWidgetHostViewQtDelegateItem *widget, QWidget *parent)
        : QQuickWidget(parent)
        , m_contentItem(widget)
    {
        setFocusPolicy(Qt::StrongFocus);
        setMouseTracking(true);
        setAttribute(Qt::WA_AcceptTouchEvents);
        setAttribute(Qt::WA_OpaquePaintEvent);
        setAttribute(Qt::WA_AlwaysShowToolTips);

        QQuickItem *root = new QQuickItem(); // Indirection so we don't delete m_contentItem
        setContent(QUrl(), nullptr, root);
        root->setFlags(QQuickItem::ItemHasContents);
        root->setFocus(true);
        root->setVisible(true);
        m_contentItem->setParentItem(root);

        connectRemoveParentBeforeParentDelete();
    }
    ~WebEngineQuickWidget() override
    {
        if (m_contentItem) {
            m_contentItem->setWidgetDelegate(nullptr);
            m_contentItem->setParentItem(nullptr);
        }
    }

    void InitAsPopup(const QRect &screenRect) override
    {
        setAttribute(Qt::WA_ShowWithoutActivating);
        setFocusPolicy(Qt::NoFocus);
        setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);

        setGeometry(screenRect);
        raise();
        m_contentItem->show();
        show();
    }

    void Bind(WebContentsAdapterClient *client) override
    {
        BindPage(static_cast<QWebEnginePagePrivate *>(client)->q_func());
    }

    void BindPage(QWebEnginePage *page)
    {
        if (m_pageDestroyedConnection)
            QObject::disconnect(m_pageDestroyedConnection);
        QWebEngineViewPrivate::bindPageAndWidget(page, this);
        m_pageDestroyedConnection = QObject::connect(page, &QObject::destroyed, this, &WebEngineQuickWidget::UnbindPage);
    }

    void UnbindPage()
    {
        Unbind();
        // In case we are not bound to a WebContentsAdapterClient that would destroy us:
        if (!parent())
            Destroy();
    }

    void Unbind() override
    {
        if (m_pageDestroyedConnection) {
            QObject::disconnect(m_pageDestroyedConnection);
            m_pageDestroyedConnection = {};
        }
        QWebEngineViewPrivate::bindPageAndWidget(nullptr, this);
    }

    void Destroy() override
    {
        deleteLater();
    }

    bool ActiveFocusOnPress() override
    {
        return true;
    }

    void SetInputMethodEnabled(bool enabled) override
    {
        QQuickWidget::setAttribute(Qt::WA_InputMethodEnabled, enabled);
    }
    void SetInputMethodHints(Qt::InputMethodHints hints) override
    {
        QQuickWidget::setInputMethodHints(hints);
    }
    void SetClearColor(const QColor &color) override
    {
        QQuickWidget::setClearColor(color);
        // QQuickWidget is usually blended by punching holes into widgets
        // above it to simulate the visual stacking order. If we want it to be
        // transparent we have to throw away the proper stacking order and always
        // blend the complete normal widgets backing store under it.
        bool isTranslucent = color.alpha() < 255;
        setAttribute(Qt::WA_AlwaysStackOnTop, isTranslucent);
        setAttribute(Qt::WA_OpaquePaintEvent, !isTranslucent);
        update();
    }
    void MoveWindow(const QPoint &screenPos) override
    {
        QQuickWidget::move(screenPos);
    }
    void Resize(int width, int height) override
    {
        QQuickWidget::resize(width, height);
    }
    QWindow *Window() override
    {
        if (const QWidget *root = QQuickWidget::window())
            return root->windowHandle();
        return nullptr;
    }

protected:
    void closeEvent(QCloseEvent *event) override
    {
        QQuickWidget::closeEvent(event);

        // If a close event was received from the window manager (e.g. when moving the parent window,
        // clicking outside the popup area)
        // make sure to notify the Chromium WebUI popup and its underlying
        // RenderWidgetHostViewQtDelegate instance to be closed.
        if (m_contentItem && m_contentItem->m_isPopup)
            m_contentItem->m_client->closePopup();
    }
    void showEvent(QShowEvent *event) override
    {
        QQuickWidget::showEvent(event);
        // We don't have a way to catch a top-level window change with QWidget
        // but a widget will most likely be shown again if it changes, so do
        // the reconnection at this point.
        for (const QMetaObject::Connection &c : qAsConst(m_windowConnections))
            disconnect(c);
        m_windowConnections.clear();
        if (QWindow *w = Window()) {
            m_windowConnections.append(connect(w, SIGNAL(xChanged(int)), m_contentItem, SLOT(onWindowPosChanged())));
            m_windowConnections.append(connect(w, SIGNAL(yChanged(int)), m_contentItem, SLOT(onWindowPosChanged())));
        }
    }
    void resizeEvent(QResizeEvent *event) override
    {
        QQuickWidget::resizeEvent(event);
        if (m_contentItem) { // FIXME: Not sure why we need to set m_contentItem size manually
            m_contentItem->setSize(event->size());
            m_contentItem->onWindowPosChanged();
        }
    }
    QVariant inputMethodQuery(Qt::InputMethodQuery query) const override
    {
        if (m_contentItem)
            return m_contentItem->inputMethodQuery(query);
        return QVariant();
    }
    bool event(QEvent *event) override;

    void connectRemoveParentBeforeParentDelete();
    void removeParentBeforeParentDelete();

private:
    friend QWebEngineViewPrivate;
    QPointer<RenderWidgetHostViewQtDelegateItem> m_contentItem; // deleted by core
    QMetaObject::Connection m_parentDestroyedConnection;
    QMetaObject::Connection m_pageDestroyedConnection;
    QList<QMetaObject::Connection> m_windowConnections;
};

void WebEngineQuickWidget::connectRemoveParentBeforeParentDelete()
{
    disconnect(m_parentDestroyedConnection);

    if (QWidget *parent = parentWidget()) {
        m_parentDestroyedConnection = connect(parent, &QObject::destroyed,
                                              this,
                                              &WebEngineQuickWidget::removeParentBeforeParentDelete);
    } else {
        m_parentDestroyedConnection = QMetaObject::Connection();
    }
}

void WebEngineQuickWidget::removeParentBeforeParentDelete()
{
    // Unset the parent, because parent is being destroyed, but the owner of this
    // WebEngineQuickWidget is actually a RenderWidgetHostViewQt instance.
    setParent(nullptr);

    // If this widget represents a popup window, make sure to close it, so that if the popup was the
    // last visible top level window, the application event loop can quit if it deems it necessarry.
    if (m_contentItem && m_contentItem->m_isPopup)
        close();
}

bool WebEngineQuickWidget::event(QEvent *event)
{
    bool handled = false;

    // Track parent to make sure we don't get deleted.
    if (event->type() == QEvent::ParentChange)
        connectRemoveParentBeforeParentDelete();

    if (!m_contentItem)
        return QQuickWidget::event(event);

    // Mimic QWidget::event() by ignoring mouse, keyboard, touch and tablet events if the widget is
    // disabled.
    if (!isEnabled()) {
        switch (event->type()) {
        case QEvent::TabletPress:
        case QEvent::TabletRelease:
        case QEvent::TabletMove:
        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease:
        case QEvent::MouseButtonDblClick:
        case QEvent::MouseMove:
        case QEvent::TouchBegin:
        case QEvent::TouchUpdate:
        case QEvent::TouchEnd:
        case QEvent::TouchCancel:
        case QEvent::ContextMenu:
        case QEvent::KeyPress:
        case QEvent::KeyRelease:
#if QT_CONFIG(wheelevent)
        case QEvent::Wheel:
#endif
            return false;
        default:
            break;
        }
    }

    switch (event->type()) {
    case QEvent::FocusIn:
    case QEvent::FocusOut:
        // We forward focus events later, once they have made it to the content item.
        return QQuickWidget::event(event);
    case QEvent::DragEnter:
    case QEvent::DragLeave:
    case QEvent::DragMove:
    case QEvent::Drop:
    case QEvent::HoverEnter:
    case QEvent::HoverLeave:
    case QEvent::HoverMove:
        // Let the parent handle these events.
        return false;
    default:
        break;
    }

    switch (event->type()) {
        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease:
        case QEvent::MouseButtonDblClick:
        case QEvent::MouseMove:
            // Don't forward mouse events synthesized by the system, which are caused by genuine touch
            // events. Chromium would then process for e.g. a mouse click handler twice, once due to the
            // system synthesized mouse event, and another time due to a touch-to-gesture-to-mouse
            // transformation done by Chromium.
            // Only allow them for popup type, since QWidgetWindow will ignore them for Qt::Popup flag,
            // which is expected to get input through synthesized mouse events (either by system or Qt)
            if (!m_contentItem->m_isPopup &&
                    static_cast<QMouseEvent *>(event)->source() == Qt::MouseEventSynthesizedBySystem) {
                Q_ASSERT(!windowFlags().testFlag(Qt::Popup));
                return true;
            }
            break;
        default:
            break;
    }

    if (event->type() == QEvent::MouseButtonDblClick) {
        // QWidget keeps the Qt4 behavior where the DblClick event would replace the Press event.
        // QtQuick is different by sending both the Press and DblClick events for the second press
        // where we can simply ignore the DblClick event.
        QMouseEvent *dblClick = static_cast<QMouseEvent *>(event);
        QMouseEvent press(QEvent::MouseButtonPress, dblClick->position(), dblClick->scenePosition(),
                          dblClick->globalPosition(), dblClick->button(), dblClick->buttons(),
                          dblClick->modifiers(), dblClick->source());
        press.setTimestamp(dblClick->timestamp());
        handled = m_contentItem->m_client->forwardEvent(&press);
    } else
        handled = m_contentItem->m_client->forwardEvent(event);

    if (!handled)
        return QQuickWidget::event(event);
    event->accept();
    return true;
}

} // namespace QtWebEngineCore

QT_BEGIN_NAMESPACE

void QWebEngineViewPrivate::pageChanged(QWebEnginePage *oldPage, QWebEnginePage *newPage)
{
    Q_Q(QWebEngineView);

    if (oldPage) {
        oldPage->setVisible(false);
        QObject::disconnect(oldPage, &QWebEnginePage::titleChanged, q, &QWebEngineView::titleChanged);
        QObject::disconnect(oldPage, &QWebEnginePage::urlChanged, q, &QWebEngineView::urlChanged);
        QObject::disconnect(oldPage, &QWebEnginePage::iconUrlChanged, q, &QWebEngineView::iconUrlChanged);
        QObject::disconnect(oldPage, &QWebEnginePage::iconChanged, q, &QWebEngineView::iconChanged);
        QObject::disconnect(oldPage, &QWebEnginePage::loadStarted, q, &QWebEngineView::loadStarted);
        QObject::disconnect(oldPage, &QWebEnginePage::loadProgress, q, &QWebEngineView::loadProgress);
        QObject::disconnect(oldPage, &QWebEnginePage::loadFinished, q, &QWebEngineView::loadFinished);
        QObject::disconnect(oldPage, &QWebEnginePage::selectionChanged, q, &QWebEngineView::selectionChanged);
        QObject::disconnect(oldPage, &QWebEnginePage::renderProcessTerminated, q, &QWebEngineView::renderProcessTerminated);
    }

    if (newPage) {
        QObject::connect(newPage, &QWebEnginePage::titleChanged, q, &QWebEngineView::titleChanged);
        QObject::connect(newPage, &QWebEnginePage::urlChanged, q, &QWebEngineView::urlChanged);
        QObject::connect(newPage, &QWebEnginePage::iconUrlChanged, q, &QWebEngineView::iconUrlChanged);
        QObject::connect(newPage, &QWebEnginePage::iconChanged, q, &QWebEngineView::iconChanged);
        QObject::connect(newPage, &QWebEnginePage::loadStarted, q, &QWebEngineView::loadStarted);
        QObject::connect(newPage, &QWebEnginePage::loadProgress, q, &QWebEngineView::loadProgress);
        QObject::connect(newPage, &QWebEnginePage::loadFinished, q, &QWebEngineView::loadFinished);
        QObject::connect(newPage, &QWebEnginePage::selectionChanged, q, &QWebEngineView::selectionChanged);
        QObject::connect(newPage, &QWebEnginePage::renderProcessTerminated, q, &QWebEngineView::renderProcessTerminated);
        newPage->setVisible(q->isVisible());
    }

    auto oldUrl = oldPage ? oldPage->url() : QUrl();
    auto newUrl = newPage ? newPage->url() : QUrl();
    if (oldUrl != newUrl)
        Q_EMIT q->urlChanged(newUrl);

    auto oldTitle = oldPage ? oldPage->title() : QString();
    auto newTitle = newPage ? newPage->title() : QString();
    if (oldTitle != newTitle)
        Q_EMIT q->titleChanged(newTitle);

    auto oldIcon = oldPage ? oldPage->iconUrl() : QUrl();
    auto newIcon = newPage ? newPage->iconUrl() : QUrl();
    if (oldIcon != newIcon) {
        Q_EMIT q->iconUrlChanged(newIcon);
        Q_EMIT q->iconChanged(newPage ? newPage->icon() : QIcon());
    }

    if ((oldPage && oldPage->hasSelection()) || (newPage && newPage->hasSelection()))
        Q_EMIT q->selectionChanged();
}

void QWebEngineViewPrivate::widgetChanged(QtWebEngineCore::WebEngineQuickWidget *oldWidget,
                                          QtWebEngineCore::WebEngineQuickWidget *newWidget)
{
    Q_Q(QWebEngineView);

    if (oldWidget) {
        q->layout()->removeWidget(oldWidget);
        oldWidget->hide();
#if QT_CONFIG(accessibility)
        if (!QtWebEngineCore::closingDown())
            QAccessible::deleteAccessibleInterface(
                    QAccessible::uniqueId(QAccessible::queryAccessibleInterface(oldWidget)));
#endif
    }

    if (newWidget) {
        Q_ASSERT(!QtWebEngineCore::closingDown());
#if QT_CONFIG(accessibility)
        QAccessible::deleteAccessibleInterface(QAccessible::uniqueId(QAccessible::queryAccessibleInterface(newWidget)));
        QAccessible::registerAccessibleInterface(new QtWebEngineCore::RenderWidgetHostViewQtDelegateWidgetAccessible(newWidget, q));
#endif
        q->layout()->addWidget(newWidget);
        q->setFocusProxy(newWidget);
        newWidget->show();
    }
}

void QWebEngineViewPrivate::contextMenuRequested(QWebEngineContextMenuRequest *request)
{
#if QT_CONFIG(action)
    m_contextRequest = request;
    switch (q_ptr->contextMenuPolicy()) {
    case Qt::DefaultContextMenu: {
        QContextMenuEvent event(QContextMenuEvent::Mouse, request->position(),
                                q_ptr->mapToGlobal(request->position()));
        q_ptr->contextMenuEvent(&event);
        return;
    }
    case Qt::CustomContextMenu:
        Q_EMIT q_ptr->customContextMenuRequested(request->position());
        return;
    case Qt::ActionsContextMenu:
        if (q_ptr->actions().count()) {
            QContextMenuEvent event(QContextMenuEvent::Mouse, request->position(),
                                    q_ptr->mapToGlobal(request->position()));
            QMenu::exec(q_ptr->actions(), event.globalPos(), 0, q_ptr);
        }
        return;
    case Qt::PreventContextMenu:
    case Qt::NoContextMenu:
        return;
    }

    Q_UNREACHABLE();
#else
    Q_UNUSED(request);
#endif // QT_CONFIG(action)
}

QStringList QWebEngineViewPrivate::chooseFiles(QWebEnginePage::FileSelectionMode mode,
                                               const QStringList &oldFiles,
                                               const QStringList &acceptedMimeTypes)
{
#if QT_CONFIG(filedialog)
    Q_Q(QWebEngineView);
    const QStringList &filter =
            QtWebEngineCore::FilePickerController::nameFilters(acceptedMimeTypes);
    QStringList ret;
    QString str;
    switch (static_cast<QtWebEngineCore::FilePickerController::FileChooserMode>(mode)) {
    case QtWebEngineCore::FilePickerController::OpenMultiple:
        ret = QFileDialog::getOpenFileNames(q, QString(), QString(),
                                            filter.join(QStringLiteral(";;")), nullptr,
                                            QFileDialog::HideNameFilterDetails);
        break;
    case QtWebEngineCore::FilePickerController::UploadFolder:
        str = QFileDialog::getExistingDirectory(q, QWebEngineView::tr("Select folder to upload"));
        if (!str.isNull())
            ret << str;
        break;
    case QtWebEngineCore::FilePickerController::Save:
        str = QFileDialog::getSaveFileName(
                q, QString(),
                (QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)
                 + oldFiles.first()));
        if (!str.isNull())
            ret << str;
        break;
    case QtWebEngineCore::FilePickerController::Open:
        str = QFileDialog::getOpenFileName(q, QString(), oldFiles.first(),
                                           filter.join(QStringLiteral(";;")), nullptr,
                                           QFileDialog::HideNameFilterDetails);
        if (!str.isNull())
            ret << str;
        break;
    }
    return ret;
#else
    Q_UNUSED(mode);
    Q_UNUSED(oldFiles);
    Q_UNUSED(acceptedMimeTypes);

    return QStringList();
#endif // QT_CONFIG(filedialog)
}

void QWebEngineViewPrivate::showColorDialog(
        QSharedPointer<QtWebEngineCore::ColorChooserController> controller)
{
#if QT_CONFIG(colordialog)
    Q_Q(QWebEngineView);
    QColorDialog *dialog = new QColorDialog(controller.data()->initialColor(), q);

    QColorDialog::connect(dialog, SIGNAL(colorSelected(QColor)), controller.data(),
                          SLOT(accept(QColor)));
    QColorDialog::connect(dialog, SIGNAL(rejected()), controller.data(), SLOT(reject()));

    // Delete when done
    QColorDialog::connect(dialog, SIGNAL(colorSelected(QColor)), dialog, SLOT(deleteLater()));
    QColorDialog::connect(dialog, SIGNAL(rejected()), dialog, SLOT(deleteLater()));

    dialog->open();
#else
    Q_UNUSED(controller);
#endif
}

bool QWebEngineViewPrivate::showAuthorizationDialog(const QString &title, const QString &message)
{
#if QT_CONFIG(messagebox)
    Q_Q(QWebEngineView);
    return QMessageBox::question(q, title, message, QMessageBox::Yes, QMessageBox::No)
            == QMessageBox::Yes;
#else
    return false;
#endif // QT_CONFIG(messagebox)
}

void QWebEngineViewPrivate::javaScriptAlert(const QUrl &url, const QString &msg)
{
#if QT_CONFIG(messagebox)
    Q_Q(QWebEngineView);
    QMessageBox::information(q, QStringLiteral("Javascript Alert - %1").arg(url.toString()),
                             msg.toHtmlEscaped());
#else
    Q_UNUSED(msg);
#endif // QT_CONFIG(messagebox)
}

bool QWebEngineViewPrivate::javaScriptConfirm(const QUrl &url, const QString &msg)
{
#if QT_CONFIG(messagebox)
    Q_Q(QWebEngineView);
    return (QMessageBox::information(q,
                                     QStringLiteral("Javascript Confirm - %1").arg(url.toString()),
                                     msg.toHtmlEscaped(), QMessageBox::Ok, QMessageBox::Cancel)
            == QMessageBox::Ok);
#else
    Q_UNUSED(msg);
    return false;
#endif // QT_CONFIG(messagebox)
}

bool QWebEngineViewPrivate::javaScriptPrompt(const QUrl &url, const QString &msg,
                                             const QString &defaultValue, QString *result)
{
#if QT_CONFIG(inputdialog)
    Q_Q(QWebEngineView);
    bool ret = false;
    if (result)
        *result = QInputDialog::getText(
                q, QStringLiteral("Javascript Prompt - %1").arg(url.toString()),
                msg.toHtmlEscaped(), QLineEdit::Normal, defaultValue.toHtmlEscaped(), &ret);
    return ret;
#else
    Q_UNUSED(msg);
    Q_UNUSED(defaultValue);
    Q_UNUSED(result);
    return false;
#endif // QT_CONFIG(inputdialog)
}

void QWebEngineViewPrivate::focusContainer()
{
    Q_Q(QWebEngineView);
    q->activateWindow();
    q->setFocus();
}

void QWebEngineViewPrivate::unhandledKeyEvent(QKeyEvent *event)
{
    Q_Q(QWebEngineView);
    if (q->parentWidget())
        QGuiApplication::sendEvent(q->parentWidget(), event);
}

bool QWebEngineViewPrivate::passOnFocus(bool reverse)
{
    Q_Q(QWebEngineView);
    return q->focusNextPrevChild(!reverse);
}

#if QT_CONFIG(accessibility)
static QAccessibleInterface *webAccessibleFactory(const QString &, QObject *object)
{
    if (QWebEngineView *v = qobject_cast<QWebEngineView*>(object))
        return new QWebEngineViewAccessible(v);
    return nullptr;
}
#endif // QT_CONFIG(accessibility)

QWebEngineViewPrivate::QWebEngineViewPrivate()
    : page(nullptr)
    , m_dragEntered(false)
    , m_ownsPage(false)
    , m_contextRequest(nullptr)
{
#if QT_CONFIG(accessibility)
    QAccessible::installFactory(&webAccessibleFactory);
#endif // QT_CONFIG(accessibility)
}

QWebEngineViewPrivate::~QWebEngineViewPrivate() = default;

void QWebEngineViewPrivate::bindPageAndView(QWebEnginePage *page, QWebEngineView *view)
{
    QWebEngineViewPrivate *v =
            page ? static_cast<QWebEngineViewPrivate *>(page->d_func()->view) : nullptr;
    auto oldView = v ? v->q_func() : nullptr;
    auto oldPage = view ? view->d_func()->page : nullptr;

    bool ownNewPage = false;
    bool deleteOldPage = false;

    // Change pointers first.

    if (page && oldView != view) {
        if (oldView) {
            ownNewPage = oldView->d_func()->m_ownsPage;
            oldView->d_func()->page = nullptr;
            oldView->d_func()->m_ownsPage = false;
        }
        page->d_func()->view = view ? view->d_func() : nullptr;
    }

    if (view && oldPage != page) {
        if (oldPage) {
            if (oldPage->d_func())
                oldPage->d_func()->view = nullptr;
            deleteOldPage = view->d_func()->m_ownsPage;
        }
        view->d_func()->m_ownsPage = ownNewPage;
        view->d_func()->page = page;
    }

    // Then notify.

    auto widget = page ? static_cast<QtWebEngineCore::WebEngineQuickWidget *>(page->d_func()->widget) : nullptr;
    auto oldWidget = (oldPage && oldPage->d_func()) ? static_cast<QtWebEngineCore::WebEngineQuickWidget *>(oldPage->d_func()->widget) : nullptr;

    // New page/widget moving away from oldView
    if (page && oldView != view && oldView) {
        oldView->d_func()->pageChanged(page, nullptr);
        if (widget)
            oldView->d_func()->widgetChanged(widget, nullptr);
    }

    // New page/widget moving into new view
    if (view && oldPage != page) {
        if (oldPage && oldPage->d_func())
            view->d_func()->pageChanged(oldPage, page);
        else
            view->d_func()->pageChanged(nullptr, page);
        if (!widget && page && page->d_func()->item) {
            widget = new QtWebEngineCore::WebEngineQuickWidget(page->d_func()->item, nullptr);
            widget->BindPage(page);
        }
        if (oldWidget != widget)
            view->d_func()->widgetChanged(oldWidget, widget);
    }
    if (deleteOldPage)
        delete oldPage;
}

void QWebEngineViewPrivate::bindPageAndWidget(
        QWebEnginePage *page, QtWebEngineCore::WebEngineQuickWidget *widget)
{
    auto oldPage = (widget && widget->m_contentItem) ? widget->m_contentItem->m_page : nullptr;
    auto oldWidget = page ? static_cast<QtWebEngineCore::WebEngineQuickWidget *>(page->d_func()->widget) : nullptr;

    // Change pointers first.

    if (widget && oldPage != page) {
        if (oldPage && oldPage->d_func()) {
            oldPage->d_func()->widget = nullptr;
            oldPage->d_func()->item = nullptr;
        }
        if (widget->m_contentItem)
            widget->m_contentItem->m_page = page;
    }

    if (page && oldWidget != widget) {
        if (oldWidget && oldWidget->m_contentItem)
            oldWidget->m_contentItem->m_page = nullptr;
        page->d_func()->widget = widget;
        page->d_func()->item = widget->m_contentItem;
    }

    // Then notify.

    if (widget && oldPage != page && oldPage && oldPage->d_func()) {
        if (auto oldView = oldPage->d_func()->view)
            static_cast<QWebEngineViewPrivate *>(oldView)->widgetChanged(widget, nullptr);
    }

    if (page && oldWidget != widget) {
        if (auto view = page->d_func()->view)
            static_cast<QWebEngineViewPrivate *>(view)->widgetChanged(oldWidget, widget);
    }
}

QIcon QWebEngineViewPrivate::webActionIcon(QWebEnginePage::WebAction action)
{
    Q_Q(QWebEngineView);
    QIcon icon;
    QStyle *style = q->style();

    switch (action) {
    case QWebEnginePage::Back:
        icon = style->standardIcon(QStyle::SP_ArrowBack);
        break;
    case QWebEnginePage::Forward:
        icon = style->standardIcon(QStyle::SP_ArrowForward);
        break;
    case QWebEnginePage::Stop:
        icon = style->standardIcon(QStyle::SP_BrowserStop);
        break;
    case QWebEnginePage::Reload:
        icon = style->standardIcon(QStyle::SP_BrowserReload);
        break;
    case QWebEnginePage::ReloadAndBypassCache:
        icon = style->standardIcon(QStyle::SP_BrowserReload);
        break;
    default:
        break;
    }
    return icon;
}

QWebEnginePage *QWebEngineViewPrivate::createPageForWindow(QWebEnginePage::WebWindowType type)
{
    Q_Q(QWebEngineView);
    QWebEngineView *newView = q->createWindow(type);
    if (newView)
        return newView->page();
    return nullptr;
}

void QWebEngineViewPrivate::setToolTip(const QString &toolTipText)
{
    Q_Q(QWebEngineView);
    if (toolTipText.isEmpty()) {
        // Avoid duplicate events.
        if (!q->toolTip().isEmpty())
            q->setToolTip(QString());
        // Force to hide tooltip because QWidget's default handler
        // doesn't hide on empty text.
        if (!QToolTip::text().isEmpty())
            QToolTip::hideText();
    } else if (toolTipText != q->toolTip()) {
        q->setToolTip(toolTipText);
    }

}

bool QWebEngineViewPrivate::isEnabled() const
{
    Q_Q(const QWebEngineView);
    return q->isEnabled();
}

QObject *QWebEngineViewPrivate::accessibilityParentObject()
{
    Q_Q(QWebEngineView);
    return q;
}

void QWebEngineViewPrivate::didPrintPage(QPrinter *&currentPrinter, QSharedPointer<QByteArray> result)
{
#if QT_CONFIG(webengine_printing_and_pdf)
    Q_Q(QWebEngineView);

    Q_ASSERT(currentPrinter);

    QThread *printerThread = new QThread;
    QObject::connect(printerThread, &QThread::finished, printerThread, &QThread::deleteLater);
    printerThread->start();

    QtWebEngineCore::PrinterWorker *printerWorker = new QtWebEngineCore::PrinterWorker(result, currentPrinter);
    printerWorker->m_deviceResolution = currentPrinter->resolution();
    printerWorker->m_firstPageFirst = currentPrinter->pageOrder() == QPrinter::FirstPageFirst;
    printerWorker->m_documentCopies = currentPrinter->copyCount();
    printerWorker->m_collateCopies = currentPrinter->collateCopies();

    QObject::connect(printerWorker, &QtWebEngineCore::PrinterWorker::resultReady, q, [q, &currentPrinter](bool success) {
        currentPrinter = nullptr;
        Q_EMIT q->printFinished(success);
    });

    QObject::connect(printerWorker, &QtWebEngineCore::PrinterWorker::resultReady, printerThread, &QThread::quit);
    QObject::connect(printerThread, &QThread::finished, printerWorker, &QtWebEngineCore::PrinterWorker::deleteLater);

    printerWorker->moveToThread(printerThread);
    QMetaObject::invokeMethod(printerWorker, "print");

#else
    Q_UNUSED(currentPrinter);
    Q_UNUSED(result);
#endif
}

void QWebEngineViewPrivate::didPrintPageToPdf(const QString &filePath, bool success)
{
    Q_Q(QWebEngineView);
    Q_EMIT q->pdfPrintingFinished(filePath, success);
}

void QWebEngineViewPrivate::printRequested()
{
    Q_Q(QWebEngineView);
    QTimer::singleShot(0, q, [q]() {
        Q_EMIT q->printRequested();
    });
}

bool QWebEngineViewPrivate::isVisible() const
{
    Q_Q(const QWebEngineView);
    return q->isVisible();
}
QRect QWebEngineViewPrivate::viewportRect() const
{
    Q_Q(const QWebEngineView);
    return q->rect();
}
QtWebEngineCore::RenderWidgetHostViewQtDelegate *
QWebEngineViewPrivate::CreateRenderWidgetHostViewQtDelegate(
        QtWebEngineCore::RenderWidgetHostViewQtDelegateClient *client)
{
    auto *item = new QtWebEngineCore::RenderWidgetHostViewQtDelegateItem(client, false);
    auto *widget = new QtWebEngineCore::WebEngineQuickWidget(item, nullptr);
    item->setWidgetDelegate(widget);
    return item;
}

QtWebEngineCore::RenderWidgetHostViewQtDelegate *
QWebEngineViewPrivate::CreateRenderWidgetHostViewQtDelegateForPopup(
        QtWebEngineCore::RenderWidgetHostViewQtDelegateClient *client)
{
    Q_Q(QWebEngineView);
    auto *item = new QtWebEngineCore::RenderWidgetHostViewQtDelegateItem(client, true);
    auto *widget = new QtWebEngineCore::WebEngineQuickWidget(item, q);
    item->setWidgetDelegate(widget);
    return item;
}

QWebEngineContextMenuRequest *QWebEngineViewPrivate::lastContextMenuRequest() const
{
    return m_contextRequest;
}

void QWebEngineViewPrivate::showAutofillPopup(QtWebEngineCore::AutofillPopupController *controller,
                                              const QRect &bounds, bool autoselectFirstSuggestion)
{
    Q_Q(QWebEngineView);
    if (!m_autofillPopupWidget)
        m_autofillPopupWidget.reset(new QtWebEngineWidgetUI::AutofillPopupWidget(controller, q));
    m_autofillPopupWidget->showPopup(q->mapToGlobal(bounds.bottomLeft()), bounds.width() + 2,
                                     autoselectFirstSuggestion);
    controller->notifyPopupShown();
}

void QWebEngineViewPrivate::hideAutofillPopup()
{
    if (!m_autofillPopupWidget)
        return;

    Q_Q(QWebEngineView);
    QTimer::singleShot(0, q, [this] {
        if (m_autofillPopupWidget) {
            QtWebEngineCore::AutofillPopupController *controller =
                    m_autofillPopupWidget->m_controller;
            m_autofillPopupWidget.reset();
            controller->notifyPopupHidden();
        }
    });
}

/*!
    \fn QWebEngineView::renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode)
    \since 5.6

    This signal is emitted when the render process is terminated with a non-zero exit status.
    \a terminationStatus is the termination status of the process and \a exitCode is the status code
    with which the process terminated.
*/

/*!
    \fn void QWebEngineView::iconChanged(const QIcon &icon)
    \since 5.7

    This signal is emitted when the icon ("favicon") associated with the
    view is changed. The new icon is specified by \a icon.

    \sa icon(), iconUrl(), iconUrlChanged()
*/

QWebEngineView::QWebEngineView(QWidget *parent)
    : QWidget(parent)
    , d_ptr(new QWebEngineViewPrivate)
{
    Q_D(QWebEngineView);
    d->q_ptr = this;
    setAcceptDrops(true);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);
}

/*!
    \since 6.4

    Constructs an empty web view using \a profile with the parent \a parent.

    \note The \a profile object ownership is not taken and it should outlive the view.

    \sa load()
*/

QWebEngineView::QWebEngineView(QWebEngineProfile *profile, QWidget *parent)
    : QWebEngineView(parent)
{
    Q_D(QWebEngineView);
    setPage(new QWebEnginePage(profile, this));
    d->m_ownsPage = true;
}

/*!
    \since 6.4

    Constructs a web view containing \a page with the parent \a parent.

    \note Ownership of \a page is not taken, and it is up to the caller to ensure it is deleted.

    \sa load(), setPage()
*/

QWebEngineView::QWebEngineView(QWebEnginePage *page, QWidget *parent)
    : QWebEngineView(parent)
{
    setPage(page);
}

QWebEngineView::~QWebEngineView()
{
    blockSignals(true);
    QWebEngineViewPrivate::bindPageAndView(nullptr, this);
}

/*!
    \since 6.2

    Returns the view if any, associated with the \a page.

    \sa page(), setPage()
*/
QWebEngineView *QWebEngineView::forPage(const QWebEnginePage *page)
{
    if (!page)
        return nullptr;
    return qobject_cast<QWebEngineView *>(page->d_ptr->accessibilityParentObject());
}

QWebEnginePage* QWebEngineView::page() const
{
    Q_D(const QWebEngineView);
    if (!d->page) {
        QWebEngineView *that = const_cast<QWebEngineView*>(this);
        that->setPage(new QWebEnginePage(that));
        d->m_ownsPage = true;
    }
    return d->page;
}

void QWebEngineView::setPage(QWebEnginePage *newPage)
{
    Q_D(QWebEngineView);
    if (d->page) {
        disconnect(d->m_pageConnection);
        d->m_pageConnection = {};
    }

    QWebEngineViewPrivate::bindPageAndView(newPage, this);
    if (!newPage)
        return;
    d->m_pageConnection = connect(newPage, &QWebEnginePage::_q_aboutToDelete, this,
                                  [newPage]() { QWebEngineViewPrivate::bindPageAndView(newPage, nullptr); });
    auto profile = newPage->profile();
    if (!profile->notificationPresenter())
        profile->setNotificationPresenter(&defaultNotificationPresenter);
}

void QWebEngineView::load(const QUrl& url)
{
    page()->load(url);
}

/*!
    \since 5.9
    Issues the specified \a request and loads the response.

    \sa load(), setUrl(), url(), urlChanged(), QUrl::fromUserInput()
*/
void QWebEngineView::load(const QWebEngineHttpRequest &request)
{
    page()->load(request);
}

void QWebEngineView::setHtml(const QString& html, const QUrl& baseUrl)
{
    page()->setHtml(html, baseUrl);
}

void QWebEngineView::setContent(const QByteArray& data, const QString& mimeType, const QUrl& baseUrl)
{
    page()->setContent(data, mimeType, baseUrl);
}

QWebEngineHistory* QWebEngineView::history() const
{
    return page()->history();
}

QString QWebEngineView::title() const
{
    return page()->title();
}

void QWebEngineView::setUrl(const QUrl &url)
{
    page()->setUrl(url);
}

QUrl QWebEngineView::url() const
{
    return page()->url();
}

QUrl QWebEngineView::iconUrl() const
{
    return page()->iconUrl();
}

/*!
    \property QWebEngineView::icon
    \brief The icon associated with the page currently viewed.
    \since 5.7

    By default, this property contains a null icon.

    \sa iconChanged(), iconUrl(), iconUrlChanged()
*/
QIcon QWebEngineView::icon() const
{
    return page()->icon();
}

bool QWebEngineView::hasSelection() const
{
    return page()->hasSelection();
}

QString QWebEngineView::selectedText() const
{
    return page()->selectedText();
}

#if QT_CONFIG(action)
QAction* QWebEngineView::pageAction(QWebEnginePage::WebAction action) const
{
    return page()->action(action);
}
#endif

void QWebEngineView::triggerPageAction(QWebEnginePage::WebAction action, bool checked)
{
    page()->triggerAction(action, checked);
}

void QWebEngineView::findText(const QString &subString, QWebEnginePage::FindFlags options, const std::function<void(const QWebEngineFindTextResult &)> &resultCallback)
{
    page()->findText(subString, options, resultCallback);
}

/*!
 * \reimp
 */
QSize QWebEngineView::sizeHint() const
{
    // TODO: Remove this override for Qt 6
    return QWidget::sizeHint();
}

QWebEngineSettings *QWebEngineView::settings() const
{
    return page()->settings();
}

void QWebEngineView::stop()
{
    page()->triggerAction(QWebEnginePage::Stop);
}

void QWebEngineView::back()
{
    page()->triggerAction(QWebEnginePage::Back);
}

void QWebEngineView::forward()
{
    page()->triggerAction(QWebEnginePage::Forward);
}

void QWebEngineView::reload()
{
    page()->triggerAction(QWebEnginePage::Reload);
}

QWebEngineView *QWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
    Q_UNUSED(type);
    return nullptr;
}

qreal QWebEngineView::zoomFactor() const
{
    return page()->zoomFactor();
}

void QWebEngineView::setZoomFactor(qreal factor)
{
    page()->setZoomFactor(factor);
}

/*!
 * \reimp
 */
bool QWebEngineView::event(QEvent *ev)
{
    if (ev->type() == QEvent::ContextMenu) {
        if (contextMenuPolicy() == Qt::NoContextMenu) {
            // We forward the contextMenu event to the parent widget
            ev->ignore();
            return false;
        }

        // We swallow spontaneous contextMenu events and synthethize those back later on when we get the
        // HandleContextMenu callback from chromium
        ev->accept();
        return true;
    }

    return QWidget::event(ev);
}

/*!
 * \reimp
 */
#if QT_CONFIG(contextmenu)
void QWebEngineView::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu = createStandardContextMenu();
    menu->popup(event->globalPos());
}
#endif // QT_CONFIG(contextmenu)

/*!
 * \reimp
 */
void QWebEngineView::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    page()->setVisible(true);
}

/*!
 * \reimp
 */
void QWebEngineView::hideEvent(QHideEvent *event)
{
    QWidget::hideEvent(event);
    page()->setVisible(false);
}

/*!
 * \reimp
 */
void QWebEngineView::closeEvent(QCloseEvent *event)
{
    QWidget::closeEvent(event);
    page()->setVisible(false);
    page()->setLifecycleState(QWebEnginePage::LifecycleState::Discarded);
}

#if QT_CONFIG(draganddrop)
/*!
    \reimp
*/
void QWebEngineView::dragEnterEvent(QDragEnterEvent *e)
{
    Q_D(QWebEngineView);
    e->accept();
    if (d->m_dragEntered)
        d->page->d_ptr->adapter->leaveDrag();
    d->page->d_ptr->adapter->enterDrag(e, mapToGlobal(e->position().toPoint()));
    d->m_dragEntered = true;
}

/*!
    \reimp
*/
void QWebEngineView::dragLeaveEvent(QDragLeaveEvent *e)
{
    Q_D(QWebEngineView);
    if (!d->m_dragEntered)
        return;
    e->accept();
    d->page->d_ptr->adapter->leaveDrag();
    d->m_dragEntered = false;
}

/*!
    \reimp
*/
void QWebEngineView::dragMoveEvent(QDragMoveEvent *e)
{
    Q_D(QWebEngineView);
    if (!d->m_dragEntered)
        return;
    QtWebEngineCore::WebContentsAdapter *adapter = d->page->d_ptr->adapter.data();
    Qt::DropAction dropAction =
            adapter->updateDragPosition(e, mapToGlobal(e->position().toPoint()));
    if (Qt::IgnoreAction == dropAction) {
        e->ignore();
    } else {
        e->setDropAction(dropAction);
        e->accept();
    }
}

/*!
    \reimp
*/
void QWebEngineView::dropEvent(QDropEvent *e)
{
    Q_D(QWebEngineView);
    if (!d->m_dragEntered)
        return;
    e->accept();
    d->page->d_ptr->adapter->endDragging(e, mapToGlobal(e->position().toPoint()));
    d->m_dragEntered = false;
}
#endif // QT_CONFIG(draganddrop)

#if QT_CONFIG(menu)
/*!
  Creates a standard context menu and returns a pointer to it.
*/
QMenu *QWebEngineView::createStandardContextMenu()
{
    Q_D(QWebEngineView);
    QMenu *menu = new QMenu(this);
    QContextMenuBuilder contextMenuBuilder(d->m_contextRequest, this, menu);

    contextMenuBuilder.initMenu();

    menu->setAttribute(Qt::WA_DeleteOnClose, true);

    return menu;
}
#endif // QT_CONFIG(menu)

/*!
  \since 6.2

  Returns additional data about the current context menu. It is only guaranteed to be valid during
  the call to the contextMenuEvent().

  \sa createStandardContextMenu()
*/
QWebEngineContextMenuRequest *QWebEngineView::lastContextMenuRequest() const
{
    Q_D(const QWebEngineView);
    return d->m_contextRequest;
}

/*!
    \fn void QWebEngineView::pdfPrintingFinished(const QString &filePath, bool success)
    \since 6.2

    This signal is emitted when printing the web page into a PDF file has
    finished.
    \a filePath will contain the path the file was requested to be created
    at, and \a success will be \c true if the file was successfully created and
    \c false otherwise.

    \sa printToPdf()
*/

/*!
    Renders the current content of the page into a PDF document and saves it
    in the location specified in \a filePath.
    The page size and orientation of the produced PDF document are taken from
    the values specified in \a layout, while the range of pages printed is
    taken from \a ranges with the default being printing all pages.

    This method issues an asynchronous request for printing the web page into
    a PDF and returns immediately.
    To be informed about the result of the request, connect to the signal
    pdfPrintingFinished().

    If a file already exists at the provided file path, it will be overwritten.
    \since 6.2
    \sa pdfPrintingFinished()
*/
void QWebEngineView::printToPdf(const QString &filePath, const QPageLayout &layout, const QPageRanges &ranges)
{
    page()->printToPdf(filePath, layout, ranges);
}

/*!
    Renders the current content of the page into a PDF document and returns a byte array containing the PDF data
    as parameter to \a resultCallback.
    The page size and orientation of the produced PDF document are taken from the values specified in \a layout,
    while the range of pages printed is taken from \a ranges with the default being printing all pages.

    The \a resultCallback must take a const reference to a QByteArray as parameter. If printing was successful, this byte array
    will contain the PDF data, otherwise, the byte array will be empty.

    \warning We guarantee that the callback (\a resultCallback) is always called, but it might be done
    during page destruction. When QWebEnginePage is deleted, the callback is triggered with an invalid
    value and it is not safe to use the corresponding QWebEnginePage or QWebEngineView instance inside it.

    \since 6.2
*/
void QWebEngineView::printToPdf(const std::function<void(const QByteArray&)> &resultCallback, const QPageLayout &layout, const QPageRanges &ranges)
{
    page()->printToPdf(resultCallback, layout, ranges);
}

/*!
    \fn void QWebEngineView::printRequested()
    \since 6.2

    This signal is emitted when the JavaScript \c{window.print()} method is called.
    Typically, the signal handler can simply call print().

    \sa print()
*/

/*!
    \fn void QWebEngineView::printFinished(bool success)
    \since 6.2

    This signal is emitted when printing requested with print() has finished.
    The parameter \a success is \c true for success or \c false for failure.

    \sa print()
*/

/*!
    Renders the current content of the page into a temporary PDF document, then prints it using \a printer.

    The settings for creating and printing the PDF document will be retrieved from the \a printer
    object.

    When finished the signal printFinished() is emitted with the \c true for success or \c false for failure.

    It is the users responsibility to ensure the \a printer remains valid until printFinished()
    has been emitted.

    \note Printing runs on the browser process, which is by default not sandboxed.

    \note This function rasterizes the result when rendering onto \a printer. Please consider raising
    the default resolution of \a printer to at least 300 DPI or using printToPdf() to produce
    PDF file output more effectively.

    \since 6.2
*/
void QWebEngineView::print(QPrinter *printer)
{
#if QT_CONFIG(webengine_printing_and_pdf)
    if (page()->d_ptr->currentPrinter) {
        qWarning("Cannot print page on printer %ls: Already printing on a device.", qUtf16Printable(printer->printerName()));
        return;
    }

    page()->d_ptr->currentPrinter = printer;
    page()->d_ptr->ensureInitialized();
    page()->d_ptr->adapter->printToPDFCallbackResult(printer->pageLayout(),
                                                     printer->pageRanges(),
                                                     printer->colorMode() == QPrinter::Color,
                                                     false);
#else
    Q_UNUSED(printer);
    Q_EMIT printFinished(false);
#endif
}

#if QT_CONFIG(action)
QContextMenuBuilder::QContextMenuBuilder(QWebEngineContextMenuRequest *request,
                                         QWebEngineView *view, QMenu *menu)
    : QtWebEngineCore::RenderViewContextMenuQt(request), m_view(view), m_menu(menu)
{
    m_view->page()->d_ptr->ensureInitialized();
}

bool QContextMenuBuilder::hasInspector()
{
    return m_view->page()->d_ptr->adapter->hasInspector();
}

bool QContextMenuBuilder::isFullScreenMode()
{
    return m_view->page()->d_ptr->isFullScreenMode();
}

void QContextMenuBuilder::addMenuItem(ContextMenuItem menuItem)
{
    QPointer<QWebEnginePage> thisRef(m_view->page());
    QAction *action = nullptr;

    switch (menuItem) {
    case ContextMenuItem::Back:
        action = thisRef->action(QWebEnginePage::Back);
        break;
    case ContextMenuItem::Forward:
        action = thisRef->action(QWebEnginePage::Forward);
        break;
    case ContextMenuItem::Reload:
        action = thisRef->action(QWebEnginePage::Reload);
        break;
    case ContextMenuItem::Cut:
        action = thisRef->action(QWebEnginePage::Cut);
        break;
    case ContextMenuItem::Copy:
        action = thisRef->action(QWebEnginePage::Copy);
        break;
    case ContextMenuItem::Paste:
        action = thisRef->action(QWebEnginePage::Paste);
        break;
    case ContextMenuItem::Undo:
        action = thisRef->action(QWebEnginePage::Undo);
        break;
    case ContextMenuItem::Redo:
        action = thisRef->action(QWebEnginePage::Redo);
        break;
    case ContextMenuItem::SelectAll:
        action = thisRef->action(QWebEnginePage::SelectAll);
        break;
    case ContextMenuItem::PasteAndMatchStyle:
        action = thisRef->action(QWebEnginePage::PasteAndMatchStyle);
        break;
    case ContextMenuItem::OpenLinkInNewWindow:
        action = thisRef->action(QWebEnginePage::OpenLinkInNewWindow);
        break;
    case ContextMenuItem::OpenLinkInNewTab:
        action = thisRef->action(QWebEnginePage::OpenLinkInNewTab);
        break;
    case ContextMenuItem::CopyLinkToClipboard:
        action = thisRef->action(QWebEnginePage::CopyLinkToClipboard);
        break;
    case ContextMenuItem::DownloadLinkToDisk:
        action = thisRef->action(QWebEnginePage::DownloadLinkToDisk);
        break;
    case ContextMenuItem::CopyImageToClipboard:
        action = thisRef->action(QWebEnginePage::CopyImageToClipboard);
        break;
    case ContextMenuItem::CopyImageUrlToClipboard:
        action = thisRef->action(QWebEnginePage::CopyImageUrlToClipboard);
        break;
    case ContextMenuItem::DownloadImageToDisk:
        action = thisRef->action(QWebEnginePage::DownloadImageToDisk);
        break;
    case ContextMenuItem::CopyMediaUrlToClipboard:
        action = thisRef->action(QWebEnginePage::CopyMediaUrlToClipboard);
        break;
    case ContextMenuItem::ToggleMediaControls:
        action = thisRef->action(QWebEnginePage::ToggleMediaControls);
        break;
    case ContextMenuItem::ToggleMediaLoop:
        action = thisRef->action(QWebEnginePage::ToggleMediaLoop);
        break;
    case ContextMenuItem::DownloadMediaToDisk:
        action = thisRef->action(QWebEnginePage::DownloadMediaToDisk);
        break;
    case ContextMenuItem::InspectElement:
        action = thisRef->action(QWebEnginePage::InspectElement);
        break;
    case ContextMenuItem::ExitFullScreen:
        action = thisRef->action(QWebEnginePage::ExitFullScreen);
        break;
    case ContextMenuItem::SavePage:
        action = thisRef->action(QWebEnginePage::SavePage);
        break;
    case ContextMenuItem::ViewSource:
        action = thisRef->action(QWebEnginePage::ViewSource);
        break;
    case ContextMenuItem::SpellingSuggestions:
        for (int i = 0; i < m_contextData->spellCheckerSuggestions().count() && i < 4; i++) {
            action = new QAction(m_menu);
            QString replacement = m_contextData->spellCheckerSuggestions().at(i);
            QObject::connect(action, &QAction::triggered, [thisRef, replacement] {
                if (thisRef)
                    thisRef->replaceMisspelledWord(replacement);
            });
            action->setText(replacement);
            m_menu->addAction(action);
        }
        return;
    case ContextMenuItem::Separator:
        if (!m_menu->isEmpty())
            m_menu->addSeparator();
        return;
    }
    action->setEnabled(isMenuItemEnabled(menuItem));
    m_menu->addAction(action);
}

bool QContextMenuBuilder::isMenuItemEnabled(ContextMenuItem menuItem)
{
    switch (menuItem) {
    case ContextMenuItem::Back:
        return m_view->page()->d_ptr->adapter->canGoBack();
    case ContextMenuItem::Forward:
        return m_view->page()->d_ptr->adapter->canGoForward();
    case ContextMenuItem::Reload:
        return true;
    case ContextMenuItem::Cut:
        return m_contextData->editFlags() & QWebEngineContextMenuRequest::CanCut;
    case ContextMenuItem::Copy:
        return m_contextData->editFlags() & QWebEngineContextMenuRequest::CanCopy;
    case ContextMenuItem::Paste:
        return m_contextData->editFlags() & QWebEngineContextMenuRequest::CanPaste;
    case ContextMenuItem::Undo:
        return m_contextData->editFlags() & QWebEngineContextMenuRequest::CanUndo;
    case ContextMenuItem::Redo:
        return m_contextData->editFlags() & QWebEngineContextMenuRequest::CanRedo;
    case ContextMenuItem::SelectAll:
        return m_contextData->editFlags() & QWebEngineContextMenuRequest::CanSelectAll;
    case ContextMenuItem::PasteAndMatchStyle:
        return m_contextData->editFlags() & QWebEngineContextMenuRequest::CanPaste;
    case ContextMenuItem::OpenLinkInNewWindow:
    case ContextMenuItem::OpenLinkInNewTab:
    case ContextMenuItem::CopyLinkToClipboard:
    case ContextMenuItem::DownloadLinkToDisk:
    case ContextMenuItem::CopyImageToClipboard:
    case ContextMenuItem::CopyImageUrlToClipboard:
    case ContextMenuItem::DownloadImageToDisk:
    case ContextMenuItem::CopyMediaUrlToClipboard:
    case ContextMenuItem::ToggleMediaControls:
    case ContextMenuItem::ToggleMediaLoop:
    case ContextMenuItem::DownloadMediaToDisk:
    case ContextMenuItem::InspectElement:
    case ContextMenuItem::ExitFullScreen:
    case ContextMenuItem::SavePage:
        return true;
    case ContextMenuItem::ViewSource:
        return m_view->page()->d_ptr->adapter->canViewSource();
    case ContextMenuItem::SpellingSuggestions:
    case ContextMenuItem::Separator:
        return true;
    }
    Q_UNREACHABLE();
}
#endif // QT_CONFIG(action)

QT_END_NAMESPACE

#include "moc_qwebengineview.cpp"