forked from ariya/phantomjs
/
qwebpage.cpp
4331 lines (3694 loc) · 150 KB
/
qwebpage.cpp
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
/*
Copyright (C) 2008, 2009 Nokia Corporation and/or its subsidiary(-ies)
Copyright (C) 2007 Staikos Computing Services Inc.
Copyright (C) 2007 Apple Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "qwebpage.h"
#include "qwebview.h"
#include "qwebframe.h"
#include "qwebpage_p.h"
#include "qwebframe_p.h"
#include "qwebhistory.h"
#include "qwebhistory_p.h"
#include "qwebinspector.h"
#include "qwebinspector_p.h"
#include "qwebsettings.h"
#include "qwebkitplatformplugin.h"
#include "qwebkitversion.h"
#include "CSSComputedStyleDeclaration.h"
#include "CSSParser.h"
#include "ApplicationCacheStorage.h"
#include "BackForwardListImpl.h"
#include "MemoryCache.h"
#include "Chrome.h"
#include "ChromeClientQt.h"
#include "ClientRect.h"
#include "ContextMenu.h"
#include "ContextMenuClientQt.h"
#include "ContextMenuController.h"
#include "DeviceMotionClientQt.h"
#include "DeviceOrientationClientQt.h"
#include "DocumentLoader.h"
#include "DragClientQt.h"
#include "DragController.h"
#include "DragData.h"
#include "Editor.h"
#include "EditorClientQt.h"
#include "FocusController.h"
#include "FormState.h"
#include "Frame.h"
#include "FrameLoadRequest.h"
#include "FrameLoader.h"
#include "FrameLoader.h"
#include "FrameLoaderClientQt.h"
#include "FrameTree.h"
#include "FrameView.h"
#if ENABLE(CLIENT_BASED_GEOLOCATION)
#include "GeolocationClientMock.h"
#include "GeolocationClientQt.h"
#endif // CLIENT_BASED_GEOLOCATION
#include "GeolocationPermissionClientQt.h"
#include "HTMLFormElement.h"
#include "HTMLFrameOwnerElement.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HashMap.h"
#include "HitTestResult.h"
#include "Image.h"
#include "InspectorClientQt.h"
#include "InspectorController.h"
#include "InspectorServerQt.h"
#include "KURL.h"
#include "LocalizedStrings.h"
#include "Logging.h"
#include "MIMETypeRegistry.h"
#include "NavigationAction.h"
#include "NetworkingContext.h"
#include "NodeList.h"
#include "NotificationPresenterClientQt.h"
#include "NotImplemented.h"
#include "Page.h"
#include "PageClientQt.h"
#include "PageGroup.h"
#include "Pasteboard.h"
#include "PlatformKeyboardEvent.h"
#include "PlatformTouchEvent.h"
#include "PlatformWheelEvent.h"
#include "PluginDatabase.h"
#include "PluginDatabase.h"
#include "PluginPackage.h"
#include "ProgressTracker.h"
#include "QtPlatformPlugin.h"
#include "RefPtr.h"
#include "RenderTextControl.h"
#include "SchemeRegistry.h"
#include "Scrollbar.h"
#include "SecurityOrigin.h"
#include "Settings.h"
#if defined Q_OS_WIN32
#include "SystemInfo.h"
#endif // Q_OS_WIN32
#include "TextIterator.h"
#include "WebPlatformStrategies.h"
#if USE(QTKIT)
#include "WebSystemInterface.h"
#endif
#include "WindowFeatures.h"
#include "WorkerThread.h"
#include "runtime/InitializeThreading.h"
#include "wtf/Threading.h"
#include "DebuggerCallFrame.h"
#include "JavaScriptCallFrame.h"
#include "SourceProvider.h"
#include <QApplication>
#include <QBasicTimer>
#include <QBitArray>
#include <QDebug>
#include <QDesktopWidget>
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QFileDialog>
#include <QHttpRequestHeader>
#include <QInputDialog>
#include <QMessageBox>
#include <QNetworkProxy>
#include <QUndoStack>
#include <QUrl>
#include <QPainter>
#include <QClipboard>
#include <QSslSocket>
#include <QStyle>
#include <QSysInfo>
#include <QTextCharFormat>
#include <QTextDocument>
#include <QTouchEvent>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#if defined(Q_WS_X11)
#include <QX11Info>
#endif
#if USE(QT_MOBILITY_SYSTEMINFO)
#include <qsysteminfo.h>
#endif
using namespace WebCore;
// from text/qfont.cpp
QT_BEGIN_NAMESPACE
extern Q_GUI_EXPORT int qt_defaultDpi();
QT_END_NAMESPACE
bool QWebPagePrivate::drtRun = false;
// Lookup table mapping QWebPage::WebActions to the associated Editor commands
static const char* editorCommandWebActions[] =
{
0, // OpenLink,
0, // OpenLinkInNewWindow,
0, // OpenFrameInNewWindow,
0, // DownloadLinkToDisk,
0, // CopyLinkToClipboard,
0, // OpenImageInNewWindow,
0, // DownloadImageToDisk,
0, // CopyImageToClipboard,
0, // Back,
0, // Forward,
0, // Stop,
0, // Reload,
"Cut", // Cut,
"Copy", // Copy,
"Paste", // Paste,
"Undo", // Undo,
"Redo", // Redo,
"MoveForward", // MoveToNextChar,
"MoveBackward", // MoveToPreviousChar,
"MoveWordForward", // MoveToNextWord,
"MoveWordBackward", // MoveToPreviousWord,
"MoveDown", // MoveToNextLine,
"MoveUp", // MoveToPreviousLine,
"MoveToBeginningOfLine", // MoveToStartOfLine,
"MoveToEndOfLine", // MoveToEndOfLine,
"MoveToBeginningOfParagraph", // MoveToStartOfBlock,
"MoveToEndOfParagraph", // MoveToEndOfBlock,
"MoveToBeginningOfDocument", // MoveToStartOfDocument,
"MoveToEndOfDocument", // MoveToEndOfDocument,
"MoveForwardAndModifySelection", // SelectNextChar,
"MoveBackwardAndModifySelection", // SelectPreviousChar,
"MoveWordForwardAndModifySelection", // SelectNextWord,
"MoveWordBackwardAndModifySelection", // SelectPreviousWord,
"MoveDownAndModifySelection", // SelectNextLine,
"MoveUpAndModifySelection", // SelectPreviousLine,
"MoveToBeginningOfLineAndModifySelection", // SelectStartOfLine,
"MoveToEndOfLineAndModifySelection", // SelectEndOfLine,
"MoveToBeginningOfParagraphAndModifySelection", // SelectStartOfBlock,
"MoveToEndOfParagraphAndModifySelection", // SelectEndOfBlock,
"MoveToBeginningOfDocumentAndModifySelection", //SelectStartOfDocument,
"MoveToEndOfDocumentAndModifySelection", // SelectEndOfDocument,
"DeleteWordBackward", // DeleteStartOfWord,
"DeleteWordForward", // DeleteEndOfWord,
0, // SetTextDirectionDefault,
0, // SetTextDirectionLeftToRight,
0, // SetTextDirectionRightToLeft,
"ToggleBold", // ToggleBold,
"ToggleItalic", // ToggleItalic,
"ToggleUnderline", // ToggleUnderline,
0, // InspectElement,
"InsertNewline", // InsertParagraphSeparator
"InsertLineBreak", // InsertLineSeparator
"SelectAll", // SelectAll
0, // ReloadAndBypassCache,
"PasteAndMatchStyle", // PasteAndMatchStyle
"RemoveFormat", // RemoveFormat
"Strikethrough", // ToggleStrikethrough,
"Subscript", // ToggleSubscript
"Superscript", // ToggleSuperscript
"InsertUnorderedList", // InsertUnorderedList
"InsertOrderedList", // InsertOrderedList
"Indent", // Indent
"Outdent", // Outdent,
"AlignCenter", // AlignCenter,
"AlignJustified", // AlignJustified,
"AlignLeft", // AlignLeft,
"AlignRight", // AlignRight,
0, // StopScheduledPageRefresh,
0, // CopyImageUrlToClipboard,
0 // WebActionCount
};
// Lookup the appropriate editor command to use for WebAction \a action
const char* QWebPagePrivate::editorCommandForWebActions(QWebPage::WebAction action)
{
if ((action > QWebPage::NoWebAction) && (action < int(sizeof(editorCommandWebActions) / sizeof(const char*))))
return editorCommandWebActions[action];
return 0;
}
static inline DragOperation dropActionToDragOp(Qt::DropActions actions)
{
unsigned result = 0;
if (actions & Qt::CopyAction)
result |= DragOperationCopy;
// DragOperationgeneric represents InternetExplorer's equivalent of Move operation,
// hence it should be considered as "move"
if (actions & Qt::MoveAction)
result |= (DragOperationMove | DragOperationGeneric);
if (actions & Qt::LinkAction)
result |= DragOperationLink;
if (result == (DragOperationCopy | DragOperationMove | DragOperationGeneric | DragOperationLink))
result = DragOperationEvery;
return (DragOperation)result;
}
static inline Qt::DropAction dragOpToDropAction(unsigned actions)
{
Qt::DropAction result = Qt::IgnoreAction;
if (actions & DragOperationCopy)
result = Qt::CopyAction;
else if (actions & DragOperationMove)
result = Qt::MoveAction;
// DragOperationgeneric represents InternetExplorer's equivalent of Move operation,
// hence it should be considered as "move"
else if (actions & DragOperationGeneric)
result = Qt::MoveAction;
else if (actions & DragOperationLink)
result = Qt::LinkAction;
return result;
}
QWebPagePrivateDebugger::QWebPagePrivateDebugger(QWebPage* page)
: m_webPage(page)
{
}
QWebPagePrivateDebugger::~QWebPagePrivateDebugger()
{
}
void QWebPagePrivateDebugger::detach(JSC::JSGlobalObject*)
{
}
void QWebPagePrivateDebugger::sourceParsed(JSC::ExecState*, JSC::SourceProvider*, int errorLineNumber, const JSC::UString& errorMessage)
{
}
void QWebPagePrivateDebugger::exception(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineNumber, bool hasHandler)
{
if (!hasHandler) {
reportError(frame.exception());
}
}
void QWebPagePrivateDebugger::reportError(const JSC::JSValue& exception)
{
WTF::RefPtr<WebCore::JavaScriptCallFrame> frame = m_callFrame;
JSC::ExecState* exec = frame->dynamicGlobalObject()->globalExec();
JSC::UString message = exception.toString(exec);
QString qmessage = QString::fromUtf8(message.utf8().data());
QList<QWebPage::JavaScriptFrame> qbacktrace;
JSC::SourceProvider* provider;
QString file;
int line;
QString function;
while (frame) {
provider = reinterpret_cast<JSC::SourceProvider*>(frame->sourceID());
line = frame->line();
file = QString::fromUtf8(provider->url().utf8().data());
function = QString::fromUtf8(frame->functionName().utf8().data());
qbacktrace << QWebPage::JavaScriptFrame(file, line + 1, function);
frame = frame->caller();
}
m_webPage->javaScriptError(QWebPage::JavaScriptError(qmessage, qbacktrace));
}
void QWebPagePrivateDebugger::atStatement(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineNumber)
{
stepOver(frame, sourceID, lineNumber);
}
void QWebPagePrivateDebugger::callEvent(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineNumber)
{
stepIn(frame, sourceID, lineNumber);
}
void QWebPagePrivateDebugger::returnEvent(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber)
{
stepOut();
}
void QWebPagePrivateDebugger::willExecuteProgram(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineNumber)
{
stepIn(frame, sourceID, lineNumber);
}
void QWebPagePrivateDebugger::didExecuteProgram(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber)
{
stepOut();
}
void QWebPagePrivateDebugger::didReachBreakpoint(const JSC::DebuggerCallFrame&, intptr_t sourceID, int lineNumber)
{
}
void QWebPagePrivateDebugger::stepIn(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineNumber)
{
m_callFrame = WebCore::JavaScriptCallFrame::create(frame, m_callFrame, sourceID, textPosition(lineNumber));
}
void QWebPagePrivateDebugger::stepOver(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineNumber)
{
m_callFrame->update(frame, sourceID, textPosition(lineNumber));
}
void QWebPagePrivateDebugger::stepOut()
{
m_callFrame = m_callFrame->caller();
}
QWebPagePrivate::QWebPagePrivate(QWebPage *qq)
: q(qq)
, page(0)
, mainFrame(0)
#ifndef QT_NO_UNDOSTACK
, undoStack(0)
#endif
, insideOpenCall(false)
, m_totalBytes(0)
, m_bytesReceived()
, clickCausedFocus(false)
, networkManager(0)
, forwardUnsupportedContent(false)
, smartInsertDeleteEnabled(true)
, selectTrailingWhitespaceEnabled(false)
, linkPolicy(QWebPage::DontDelegateLinks)
, viewportSize(QSize(0, 0))
, pixelRatio(1)
#ifndef QT_NO_CONTEXTMENU
, currentContextMenu(0)
#endif
, settings(0)
, useFixedLayout(false)
, pluginFactory(0)
, inspectorFrontend(0)
, inspector(0)
, inspectorIsInternalOnly(false)
, m_lastDropAction(Qt::IgnoreAction)
{
WebCore::InitializeLoggingChannelsIfNecessary();
ScriptController::initializeThreading();
WTF::initializeMainThread();
WebCore::SecurityOrigin::setLocalLoadPolicy(WebCore::SecurityOrigin::AllowLocalLoadsForLocalAndSubstituteData);
WebPlatformStrategies::initialize();
#if USE(QTKIT)
InitWebCoreSystemInterface();
#endif
Page::PageClients pageClients;
pageClients.chromeClient = new ChromeClientQt(q);
pageClients.contextMenuClient = new ContextMenuClientQt();
pageClients.editorClient = new EditorClientQt(q);
pageClients.dragClient = new DragClientQt(q);
pageClients.inspectorClient = new InspectorClientQt(q);
#if ENABLE(DEVICE_ORIENTATION)
pageClients.deviceOrientationClient = new DeviceOrientationClientQt(q);
pageClients.deviceMotionClient = new DeviceMotionClientQt(q);
#endif
#if ENABLE(CLIENT_BASED_GEOLOCATION)
if (QWebPagePrivate::drtRun)
pageClients.geolocationClient = new GeolocationClientMock();
else
pageClients.geolocationClient = new GeolocationClientQt(q);
#endif
page = new Page(pageClients);
// By default each page is put into their own unique page group, which affects popup windows
// and visited links. Page groups (per process only) is a feature making it possible to use
// separate settings for each group, so that for instance an integrated browser/email reader
// can use different settings for displaying HTML pages and HTML email. To make QtWebKit work
// as expected out of the box, we use a default group similar to what other ports are doing.
page->setGroupName("Default Group");
#if ENABLE(CLIENT_BASED_GEOLOCATION)
// In case running in DumpRenderTree mode set the controller to mock provider.
if (QWebPagePrivate::drtRun)
static_cast<GeolocationClientMock*>(pageClients.geolocationClient)->setController(page->geolocationController());
#endif
settings = new QWebSettings(page->settings());
history.d = new QWebHistoryPrivate(static_cast<WebCore::BackForwardListImpl*>(page->backForwardList()));
memset(actions, 0, sizeof(actions));
PageGroup::setShouldTrackVisitedLinks(true);
#if ENABLE(NOTIFICATIONS)
NotificationPresenterClientQt::notificationPresenter()->addClient();
#endif
debugger = new QWebPagePrivateDebugger(q);
page->setDebugger(debugger);
}
QWebPagePrivate::~QWebPagePrivate()
{
if (inspector && inspectorIsInternalOnly) {
// Since we have to delete an internal inspector,
// call setInspector(0) directly to prevent potential crashes
setInspector(0);
}
#ifndef QT_NO_CONTEXTMENU
delete currentContextMenu;
#endif
#ifndef QT_NO_UNDOSTACK
delete undoStack;
#endif
delete settings;
delete page;
delete debugger;
if (inspector)
inspector->setPage(0);
#if ENABLE(NOTIFICATIONS)
NotificationPresenterClientQt::notificationPresenter()->removeClient();
#endif
}
WebCore::ViewportArguments QWebPagePrivate::viewportArguments()
{
return page ? page->viewportArguments() : WebCore::ViewportArguments();
}
WebCore::Page* QWebPagePrivate::core(const QWebPage* page)
{
return page->d->page;
}
QWebPagePrivate* QWebPagePrivate::priv(QWebPage* page)
{
return page->d;
}
bool QWebPagePrivate::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
{
if (insideOpenCall
&& frame == mainFrame)
return true;
return q->acceptNavigationRequest(frame, request, type);
}
void QWebPagePrivate::createMainFrame()
{
if (!mainFrame) {
QWebFrameData frameData(page);
mainFrame = new QWebFrame(q, &frameData);
emit q->frameCreated(mainFrame);
}
}
static QWebPage::WebAction webActionForContextMenuAction(WebCore::ContextMenuAction action)
{
switch (action) {
case WebCore::ContextMenuItemTagOpenLink: return QWebPage::OpenLink;
case WebCore::ContextMenuItemTagOpenLinkInNewWindow: return QWebPage::OpenLinkInNewWindow;
case WebCore::ContextMenuItemTagDownloadLinkToDisk: return QWebPage::DownloadLinkToDisk;
case WebCore::ContextMenuItemTagCopyLinkToClipboard: return QWebPage::CopyLinkToClipboard;
case WebCore::ContextMenuItemTagOpenImageInNewWindow: return QWebPage::OpenImageInNewWindow;
case WebCore::ContextMenuItemTagDownloadImageToDisk: return QWebPage::DownloadImageToDisk;
case WebCore::ContextMenuItemTagCopyImageToClipboard: return QWebPage::CopyImageToClipboard;
case WebCore::ContextMenuItemTagCopyImageUrlToClipboard: return QWebPage::CopyImageUrlToClipboard;
case WebCore::ContextMenuItemTagOpenFrameInNewWindow: return QWebPage::OpenFrameInNewWindow;
case WebCore::ContextMenuItemTagCopy: return QWebPage::Copy;
case WebCore::ContextMenuItemTagGoBack: return QWebPage::Back;
case WebCore::ContextMenuItemTagGoForward: return QWebPage::Forward;
case WebCore::ContextMenuItemTagStop: return QWebPage::Stop;
case WebCore::ContextMenuItemTagReload: return QWebPage::Reload;
case WebCore::ContextMenuItemTagCut: return QWebPage::Cut;
case WebCore::ContextMenuItemTagPaste: return QWebPage::Paste;
case WebCore::ContextMenuItemTagDefaultDirection: return QWebPage::SetTextDirectionDefault;
case WebCore::ContextMenuItemTagLeftToRight: return QWebPage::SetTextDirectionLeftToRight;
case WebCore::ContextMenuItemTagRightToLeft: return QWebPage::SetTextDirectionRightToLeft;
case WebCore::ContextMenuItemTagBold: return QWebPage::ToggleBold;
case WebCore::ContextMenuItemTagItalic: return QWebPage::ToggleItalic;
case WebCore::ContextMenuItemTagUnderline: return QWebPage::ToggleUnderline;
case WebCore::ContextMenuItemTagSelectAll: return QWebPage::SelectAll;
#if ENABLE(INSPECTOR)
case WebCore::ContextMenuItemTagInspectElement: return QWebPage::InspectElement;
#endif
default: break;
}
return QWebPage::NoWebAction;
}
#ifndef QT_NO_CONTEXTMENU
QMenu *QWebPagePrivate::createContextMenu(const WebCore::ContextMenu *webcoreMenu,
const QList<WebCore::ContextMenuItem> *items, QBitArray *visitedWebActions)
{
if (!client || !webcoreMenu)
return 0;
QMenu* menu = new QMenu(client->ownerWidget());
for (int i = 0; i < items->count(); ++i) {
const ContextMenuItem &item = items->at(i);
switch (item.type()) {
case WebCore::CheckableActionType: /* fall through */
case WebCore::ActionType: {
QWebPage::WebAction action = webActionForContextMenuAction(item.action());
QAction *a = q->action(action);
if (a) {
ContextMenuItem it(item);
page->contextMenuController()->checkOrEnableIfNeeded(it);
PlatformMenuItemDescription desc = it.releasePlatformDescription();
a->setEnabled(desc.enabled);
a->setChecked(desc.checked);
a->setCheckable(item.type() == WebCore::CheckableActionType);
menu->addAction(a);
visitedWebActions->setBit(action);
}
break;
}
case WebCore::SeparatorType:
menu->addSeparator();
break;
case WebCore::SubmenuType: {
QMenu *subMenu = createContextMenu(webcoreMenu, item.platformSubMenu(), visitedWebActions);
bool anyEnabledAction = false;
QList<QAction *> actions = subMenu->actions();
for (int i = 0; i < actions.count(); ++i) {
if (actions.at(i)->isVisible())
anyEnabledAction |= actions.at(i)->isEnabled();
}
// don't show sub-menus with just disabled actions
if (anyEnabledAction) {
subMenu->setTitle(item.title());
menu->addAction(subMenu->menuAction());
} else
delete subMenu;
break;
}
}
}
return menu;
}
#endif // QT_NO_CONTEXTMENU
#ifndef QT_NO_ACTION
void QWebPagePrivate::_q_webActionTriggered(bool checked)
{
QAction *a = qobject_cast<QAction *>(q->sender());
if (!a)
return;
QWebPage::WebAction action = static_cast<QWebPage::WebAction>(a->data().toInt());
q->triggerAction(action, checked);
}
#endif // QT_NO_ACTION
void QWebPagePrivate::_q_cleanupLeakMessages()
{
#ifndef NDEBUG
// Need this to make leak messages accurate.
memoryCache()->setCapacities(0, 0, 0);
#endif
}
void QWebPagePrivate::updateAction(QWebPage::WebAction action)
{
#ifdef QT_NO_ACTION
Q_UNUSED(action)
#else
QAction *a = actions[action];
if (!a || !mainFrame)
return;
WebCore::FrameLoader *loader = mainFrame->d->frame->loader();
WebCore::Editor *editor = page->focusController()->focusedOrMainFrame()->editor();
bool enabled = a->isEnabled();
bool checked = a->isChecked();
switch (action) {
case QWebPage::Back:
enabled = page->canGoBackOrForward(-1);
break;
case QWebPage::Forward:
enabled = page->canGoBackOrForward(1);
break;
case QWebPage::Stop:
enabled = loader->isLoading();
break;
case QWebPage::Reload:
case QWebPage::ReloadAndBypassCache:
enabled = !loader->isLoading();
break;
#ifndef QT_NO_UNDOSTACK
case QWebPage::Undo:
case QWebPage::Redo:
// those two are handled by QUndoStack
break;
#endif // QT_NO_UNDOSTACK
case QWebPage::SelectAll: // editor command is always enabled
break;
case QWebPage::SetTextDirectionDefault:
case QWebPage::SetTextDirectionLeftToRight:
case QWebPage::SetTextDirectionRightToLeft:
enabled = editor->canEdit();
checked = false;
break;
default: {
// see if it's an editor command
const char* commandName = editorCommandForWebActions(action);
// if it's an editor command, let it's logic determine state
if (commandName) {
Editor::Command command = editor->command(commandName);
enabled = command.isEnabled();
if (enabled)
checked = command.state() != FalseTriState;
else
checked = false;
}
break;
}
}
a->setEnabled(enabled);
if (a->isCheckable())
a->setChecked(checked);
#endif // QT_NO_ACTION
}
void QWebPagePrivate::updateNavigationActions()
{
updateAction(QWebPage::Back);
updateAction(QWebPage::Forward);
updateAction(QWebPage::Stop);
updateAction(QWebPage::Reload);
updateAction(QWebPage::ReloadAndBypassCache);
}
void QWebPagePrivate::updateEditorActions()
{
updateAction(QWebPage::Cut);
updateAction(QWebPage::Copy);
updateAction(QWebPage::Paste);
updateAction(QWebPage::MoveToNextChar);
updateAction(QWebPage::MoveToPreviousChar);
updateAction(QWebPage::MoveToNextWord);
updateAction(QWebPage::MoveToPreviousWord);
updateAction(QWebPage::MoveToNextLine);
updateAction(QWebPage::MoveToPreviousLine);
updateAction(QWebPage::MoveToStartOfLine);
updateAction(QWebPage::MoveToEndOfLine);
updateAction(QWebPage::MoveToStartOfBlock);
updateAction(QWebPage::MoveToEndOfBlock);
updateAction(QWebPage::MoveToStartOfDocument);
updateAction(QWebPage::MoveToEndOfDocument);
updateAction(QWebPage::SelectNextChar);
updateAction(QWebPage::SelectPreviousChar);
updateAction(QWebPage::SelectNextWord);
updateAction(QWebPage::SelectPreviousWord);
updateAction(QWebPage::SelectNextLine);
updateAction(QWebPage::SelectPreviousLine);
updateAction(QWebPage::SelectStartOfLine);
updateAction(QWebPage::SelectEndOfLine);
updateAction(QWebPage::SelectStartOfBlock);
updateAction(QWebPage::SelectEndOfBlock);
updateAction(QWebPage::SelectStartOfDocument);
updateAction(QWebPage::SelectEndOfDocument);
updateAction(QWebPage::DeleteStartOfWord);
updateAction(QWebPage::DeleteEndOfWord);
updateAction(QWebPage::SetTextDirectionDefault);
updateAction(QWebPage::SetTextDirectionLeftToRight);
updateAction(QWebPage::SetTextDirectionRightToLeft);
updateAction(QWebPage::ToggleBold);
updateAction(QWebPage::ToggleItalic);
updateAction(QWebPage::ToggleUnderline);
updateAction(QWebPage::InsertParagraphSeparator);
updateAction(QWebPage::InsertLineSeparator);
updateAction(QWebPage::PasteAndMatchStyle);
updateAction(QWebPage::RemoveFormat);
updateAction(QWebPage::ToggleStrikethrough);
updateAction(QWebPage::ToggleSubscript);
updateAction(QWebPage::ToggleSuperscript);
updateAction(QWebPage::InsertUnorderedList);
updateAction(QWebPage::InsertOrderedList);
updateAction(QWebPage::Indent);
updateAction(QWebPage::Outdent);
updateAction(QWebPage::AlignCenter);
updateAction(QWebPage::AlignJustified);
updateAction(QWebPage::AlignLeft);
updateAction(QWebPage::AlignRight);
}
void QWebPagePrivate::timerEvent(QTimerEvent *ev)
{
int timerId = ev->timerId();
if (timerId == tripleClickTimer.timerId())
tripleClickTimer.stop();
else
q->timerEvent(ev);
}
template<class T>
void QWebPagePrivate::mouseMoveEvent(T* ev)
{
WebCore::Frame* frame = QWebFramePrivate::core(mainFrame);
if (!frame->view())
return;
bool accepted = frame->eventHandler()->mouseMoved(PlatformMouseEvent(ev, 0));
ev->setAccepted(accepted);
}
template<class T>
void QWebPagePrivate::mousePressEvent(T* ev)
{
WebCore::Frame* frame = QWebFramePrivate::core(mainFrame);
if (!frame->view())
return;
RefPtr<WebCore::Node> oldNode;
Frame* focusedFrame = page->focusController()->focusedFrame();
if (Document* focusedDocument = focusedFrame ? focusedFrame->document() : 0)
oldNode = focusedDocument->focusedNode();
if (tripleClickTimer.isActive()
&& (ev->pos() - tripleClick).manhattanLength()
< QApplication::startDragDistance()) {
mouseTripleClickEvent(ev);
return;
}
bool accepted = false;
adjustPointForClicking(ev);
PlatformMouseEvent mev(ev, 1);
// ignore the event if we can't map Qt's mouse buttons to WebCore::MouseButton
if (mev.button() != NoButton)
accepted = frame->eventHandler()->handleMousePressEvent(mev);
ev->setAccepted(accepted);
RefPtr<WebCore::Node> newNode;
focusedFrame = page->focusController()->focusedFrame();
if (Document* focusedDocument = focusedFrame ? focusedFrame->document() : 0)
newNode = focusedDocument->focusedNode();
if (newNode && oldNode != newNode)
clickCausedFocus = true;
}
template<class T>
void QWebPagePrivate::mouseDoubleClickEvent(T *ev)
{
WebCore::Frame* frame = QWebFramePrivate::core(mainFrame);
if (!frame->view())
return;
bool accepted = false;
PlatformMouseEvent mev(ev, 2);
// ignore the event if we can't map Qt's mouse buttons to WebCore::MouseButton
if (mev.button() != NoButton)
accepted = frame->eventHandler()->handleMousePressEvent(mev);
ev->setAccepted(accepted);
tripleClickTimer.start(QApplication::doubleClickInterval(), q);
tripleClick = QPointF(ev->pos()).toPoint();
}
template<class T>
void QWebPagePrivate::mouseTripleClickEvent(T *ev)
{
WebCore::Frame* frame = QWebFramePrivate::core(mainFrame);
if (!frame->view())
return;
bool accepted = false;
PlatformMouseEvent mev(ev, 3);
// ignore the event if we can't map Qt's mouse buttons to WebCore::MouseButton
if (mev.button() != NoButton)
accepted = frame->eventHandler()->handleMousePressEvent(mev);
ev->setAccepted(accepted);
}
void QWebPagePrivate::handleClipboard(QEvent* ev, Qt::MouseButton button)
{
#ifndef QT_NO_CLIPBOARD
if (QApplication::clipboard()->supportsSelection()) {
bool oldSelectionMode = Pasteboard::generalPasteboard()->isSelectionMode();
Pasteboard::generalPasteboard()->setSelectionMode(true);
WebCore::Frame* focusFrame = page->focusController()->focusedOrMainFrame();
if (button == Qt::LeftButton) {
if (focusFrame && (focusFrame->editor()->canCopy() || focusFrame->editor()->canDHTMLCopy())) {
Pasteboard::generalPasteboard()->writeSelection(focusFrame->editor()->selectedRange().get(), focusFrame->editor()->canSmartCopyOrDelete(), focusFrame);
ev->setAccepted(true);
}
} else if (button == Qt::MidButton) {
if (focusFrame && (focusFrame->editor()->canPaste() || focusFrame->editor()->canDHTMLPaste())) {
focusFrame->editor()->paste();
ev->setAccepted(true);
}
}
Pasteboard::generalPasteboard()->setSelectionMode(oldSelectionMode);
}
#endif
}
template<class T>
void QWebPagePrivate::mouseReleaseEvent(T *ev)
{
WebCore::Frame* frame = QWebFramePrivate::core(mainFrame);
if (!frame->view())
return;
bool accepted = false;
adjustPointForClicking(ev);
PlatformMouseEvent mev(ev, 0);
// ignore the event if we can't map Qt's mouse buttons to WebCore::MouseButton
if (mev.button() != NoButton)
accepted = frame->eventHandler()->handleMouseReleaseEvent(mev);
ev->setAccepted(accepted);
handleClipboard(ev, ev->button());
handleSoftwareInputPanel(ev->button(), QPointF(ev->pos()).toPoint());
}
void QWebPagePrivate::handleSoftwareInputPanel(Qt::MouseButton button, const QPoint& pos)
{
Frame* frame = page->focusController()->focusedFrame();
if (!frame)
return;
if (client && client->inputMethodEnabled()
&& frame->document()->focusedNode()
&& button == Qt::LeftButton && qApp->autoSipEnabled()) {
QStyle::RequestSoftwareInputPanel behavior = QStyle::RequestSoftwareInputPanel(
client->ownerWidget()->style()->styleHint(QStyle::SH_RequestSoftwareInputPanel));
if (!clickCausedFocus || behavior == QStyle::RSIP_OnMouseClick) {
HitTestResult result = frame->eventHandler()->hitTestResultAtPoint(frame->view()->windowToContents(pos), false);
if (result.isContentEditable()) {
QEvent event(QEvent::RequestSoftwareInputPanel);
QApplication::sendEvent(client->ownerWidget(), &event);
}
}
}
clickCausedFocus = false;
}
#ifndef QT_NO_CONTEXTMENU
void QWebPagePrivate::contextMenuEvent(const QPoint& globalPos)
{
QMenu *menu = q->createStandardContextMenu();
if (menu) {
menu->exec(globalPos);
delete menu;
}
}
#endif // QT_NO_CONTEXTMENU
/*!
\since 4.5
This function creates the standard context menu which is shown when
the user clicks on the web page with the right mouse button. It is
called from the default contextMenuEvent() handler. The popup menu's
ownership is transferred to the caller.
*/
QMenu *QWebPage::createStandardContextMenu()
{
#ifndef QT_NO_CONTEXTMENU
QMenu *menu = d->currentContextMenu;
d->currentContextMenu = 0;
return menu;
#else
return 0;
#endif
}
#ifndef QT_NO_WHEELEVENT
template<class T>
void QWebPagePrivate::wheelEvent(T *ev)
{
WebCore::Frame* frame = QWebFramePrivate::core(mainFrame);
if (!frame->view())
return;
WebCore::PlatformWheelEvent pev(ev);
bool accepted = frame->eventHandler()->handleWheelEvent(pev);
ev->setAccepted(accepted);
}
#endif // QT_NO_WHEELEVENT
#ifndef QT_NO_SHORTCUT
QWebPage::WebAction QWebPagePrivate::editorActionForKeyEvent(QKeyEvent* event)
{
static struct {
QKeySequence::StandardKey standardKey;
QWebPage::WebAction action;
} editorActions[] = {
{ QKeySequence::Cut, QWebPage::Cut },
{ QKeySequence::Copy, QWebPage::Copy },
{ QKeySequence::Paste, QWebPage::Paste },
{ QKeySequence::Undo, QWebPage::Undo },
{ QKeySequence::Redo, QWebPage::Redo },
{ QKeySequence::MoveToNextChar, QWebPage::MoveToNextChar },
{ QKeySequence::MoveToPreviousChar, QWebPage::MoveToPreviousChar },
{ QKeySequence::MoveToNextWord, QWebPage::MoveToNextWord },
{ QKeySequence::MoveToPreviousWord, QWebPage::MoveToPreviousWord },
{ QKeySequence::MoveToNextLine, QWebPage::MoveToNextLine },
{ QKeySequence::MoveToPreviousLine, QWebPage::MoveToPreviousLine },
{ QKeySequence::MoveToStartOfLine, QWebPage::MoveToStartOfLine },
{ QKeySequence::MoveToEndOfLine, QWebPage::MoveToEndOfLine },
{ QKeySequence::MoveToStartOfBlock, QWebPage::MoveToStartOfBlock },
{ QKeySequence::MoveToEndOfBlock, QWebPage::MoveToEndOfBlock },
{ QKeySequence::MoveToStartOfDocument, QWebPage::MoveToStartOfDocument },
{ QKeySequence::MoveToEndOfDocument, QWebPage::MoveToEndOfDocument },
{ QKeySequence::SelectNextChar, QWebPage::SelectNextChar },
{ QKeySequence::SelectPreviousChar, QWebPage::SelectPreviousChar },
{ QKeySequence::SelectNextWord, QWebPage::SelectNextWord },