forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebView.cpp
More file actions
946 lines (805 loc) · 28 KB
/
WebView.cpp
File metadata and controls
946 lines (805 loc) · 28 KB
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
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
* Copyright (C) 2017 Sony Interactive Entertainment Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WebView.h"
#include "APIPageConfiguration.h"
#include "DrawingAreaProxyCoordinatedGraphics.h"
#include "Logging.h"
#include "NativeWebKeyboardEvent.h"
#include "NativeWebMouseEvent.h"
#include "NativeWebWheelEvent.h"
#include "WKAPICast.h"
#include "WebContextMenuProxyWin.h"
#include "WebEditCommandProxy.h"
#include "WebEventFactory.h"
#include "WebKitDLL.h"
#include "WebPageGroup.h"
#include "WebPageProxy.h"
#include "WebProcessPool.h"
#include <Commctrl.h>
#include <WebCore/BitmapInfo.h>
#include <WebCore/Cursor.h>
#include <WebCore/Editor.h>
#include <WebCore/FloatRect.h>
#include <WebCore/HWndDC.h>
#include <WebCore/IntRect.h>
#include <WebCore/NotImplemented.h>
#include <WebCore/Region.h>
#include <WebCore/WindowMessageBroadcaster.h>
#include <wtf/FileSystem.h>
#include <wtf/SoftLinking.h>
#include <wtf/text/StringBuffer.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/WTFString.h>
#if ENABLE(REMOTE_INSPECTOR)
#include "RemoteInspectorProtocolHandler.h"
#endif
#if USE(CAIRO)
#include <cairo-win32.h>
#include <cairo.h>
#endif
#if USE(GRAPHICS_LAYER_WC)
#include "DrawingAreaProxyWC.h"
#endif
namespace WebKit {
using namespace WebCore;
static const LPCWSTR kWebKit2WebViewWindowClassName = L"WebKit2WebViewWindowClass";
static const int kMaxToolTipWidth = 250;
enum {
UpdateActiveStateTimer = 1,
};
LRESULT CALLBACK WebView::WebViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LONG_PTR longPtr = ::GetWindowLongPtr(hWnd, 0);
if (WebView* webView = reinterpret_cast<WebView*>(longPtr))
return webView->wndProc(hWnd, message, wParam, lParam);
if (message == WM_CREATE) {
LPCREATESTRUCT createStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
// Associate the WebView with the window.
::SetWindowLongPtr(hWnd, 0, (LONG_PTR)createStruct->lpCreateParams);
return 0;
}
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
LRESULT WebView::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = 0;
bool handled = true;
switch (message) {
case WM_CLOSE:
m_page->tryClose();
break;
case WM_DESTROY:
m_isBeingDestroyed = true;
close();
break;
case WM_ERASEBKGND:
lResult = 1;
break;
case WM_PAINT:
lResult = onPaintEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_PRINTCLIENT:
lResult = onPrintClientEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_MOUSEACTIVATE:
setWasActivatedByMouseEvent(true);
handled = false;
break;
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
case WM_MOUSELEAVE:
lResult = onMouseEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_MOUSEWHEEL:
lResult = onWheelEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_HSCROLL:
lResult = onHorizontalScroll(hWnd, message, wParam, lParam, handled);
break;
case WM_VSCROLL:
lResult = onVerticalScroll(hWnd, message, wParam, lParam, handled);
break;
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
case WM_SYSCHAR:
case WM_CHAR:
case WM_SYSKEYUP:
case WM_KEYUP:
lResult = onKeyEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_SIZE:
lResult = onSizeEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_WINDOWPOSCHANGED:
lResult = onWindowPositionChangedEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_SETFOCUS:
lResult = onSetFocusEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_KILLFOCUS:
lResult = onKillFocusEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_TIMER:
lResult = onTimerEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_SHOWWINDOW:
lResult = onShowWindowEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_SETCURSOR:
lResult = onSetCursor(hWnd, message, wParam, lParam, handled);
break;
case WM_MENUCOMMAND:
lResult = onMenuCommand(hWnd, message, wParam, lParam, handled);
break;
case WM_COMMAND:
SendMessage(GetParent(hWnd), message, wParam, lParam);
break;
default:
handled = false;
break;
}
if (!handled)
lResult = ::DefWindowProc(hWnd, message, wParam, lParam);
return lResult;
}
bool WebView::registerWebViewWindowClass()
{
static bool haveRegisteredWindowClass = false;
if (haveRegisteredWindowClass)
return true;
haveRegisteredWindowClass = true;
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_DBLCLKS;
wcex.lpfnWndProc = WebView::WebViewWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(WebView*);
wcex.hInstance = instanceHandle();
wcex.hIcon = 0;
wcex.hCursor = ::LoadCursor(0, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = kWebKit2WebViewWindowClassName;
wcex.hIconSm = 0;
return !!::RegisterClassEx(&wcex);
}
WebView::WebView(RECT rect, const API::PageConfiguration& configuration, HWND parentWindow)
: m_pageClient(makeUniqueWithoutRefCountedCheck<PageClientImpl>(*this))
{
registerWebViewWindowClass();
m_window = ::CreateWindowExW(0, kWebKit2WebViewWindowClassName, 0, WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE,
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, parentWindow ? parentWindow : HWND_MESSAGE, 0, instanceHandle(), this);
ASSERT(::IsWindow(m_window));
// We only check our window style, and not ::IsWindowVisible, because m_isVisible only tracks
// this window's visibility status, while ::IsWindowVisible takes our ancestors' visibility
// status into account. <http://webkit.org/b/54104>
ASSERT(m_isVisible == static_cast<bool>(::GetWindowLong(m_window, GWL_STYLE) & WS_VISIBLE));
auto pageConfiguration = configuration.copy();
WebProcessPool& processPool = pageConfiguration->processPool();
m_page = processPool.createWebPage(*m_pageClient, WTFMove(pageConfiguration));
m_page->initializeWebPage();
IntSize windowSize(rect.right - rect.left, rect.bottom - rect.top);
if (m_page->drawingArea())
m_page->drawingArea()->setSize(windowSize);
#if ENABLE(REMOTE_INSPECTOR)
m_page->setURLSchemeHandlerForScheme(RemoteInspectorProtocolHandler::create(*m_page), "inspector"_s);
#endif
// FIXME: Initializing the tooltip window here matches WebKit win, but seems like something
// we could do on demand to save resources.
initializeToolTipWindow();
// Initialize the top level parent window and register it with the WindowMessageBroadcaster.
windowAncestryDidChange();
}
WebView::~WebView()
{
// Tooltip window needs to be explicitly destroyed since it isn't a WS_CHILD.
if (::IsWindow(m_toolTipWindow))
::DestroyWindow(m_toolTipWindow);
}
void WebView::initialize()
{
if (shouldInitializeTrackPointHack()) {
// If we detected a registry key belonging to a TrackPoint driver, then create fake
// scrollbars, so the WebView will receive WM_VSCROLL and WM_HSCROLL messages.
// We create an invisible vertical scrollbar and an invisible horizontal scrollbar to allow
// for receiving both types of messages.
::CreateWindow(TEXT("SCROLLBAR"), TEXT("FAKETRACKPOINTHSCROLLBAR"), WS_CHILD | WS_VISIBLE | SBS_HORZ, 0, 0, 0, 0, m_window, 0, instanceHandle(), 0);
::CreateWindow(TEXT("SCROLLBAR"), TEXT("FAKETRACKPOINTVSCROLLBAR"), WS_CHILD | WS_VISIBLE | SBS_VERT, 0, 0, 0, 0, m_window, 0, instanceHandle(), 0);
}
}
void WebView::setParentWindow(HWND parentWindow)
{
if (m_window) {
// If the host window hasn't changed, bail.
if (::GetParent(m_window) == parentWindow)
return;
if (parentWindow)
::SetParent(m_window, parentWindow);
else if (!m_isBeingDestroyed) {
// Turn the WebView into a message-only window so it will no longer be a child of the
// old parent window and will be hidden from screen. We only do this when
// isBeingDestroyed() is false because doing this while handling WM_DESTROY can leave
// m_window in a weird state (see <http://webkit.org/b/29337>).
::SetParent(m_window, HWND_MESSAGE);
}
}
windowAncestryDidChange();
}
static HWND findTopLevelParentWindow(HWND window)
{
if (!window)
return 0;
HWND current = window;
for (HWND parent = GetParent(current); current; current = parent, parent = GetParent(parent)) {
if (!parent || !(GetWindowLongPtr(current, GWL_STYLE) & (WS_POPUP | WS_CHILD)))
return current;
}
ASSERT_NOT_REACHED();
return 0;
}
void WebView::windowAncestryDidChange()
{
HWND newTopLevelParentWindow;
if (m_window)
newTopLevelParentWindow = findTopLevelParentWindow(m_window);
else {
// There's no point in tracking active state changes of our parent window if we don't have
// a window ourselves.
newTopLevelParentWindow = 0;
}
if (newTopLevelParentWindow == m_topLevelParentWindow)
return;
if (m_topLevelParentWindow)
WindowMessageBroadcaster::removeListener(m_topLevelParentWindow, this);
m_topLevelParentWindow = newTopLevelParentWindow;
if (m_topLevelParentWindow)
WindowMessageBroadcaster::addListener(m_topLevelParentWindow, this);
updateActiveState();
}
LRESULT WebView::onMouseEvent(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
{
NativeWebMouseEvent mouseEvent = NativeWebMouseEvent(hWnd, message, wParam, lParam, m_wasActivatedByMouseEvent);
setWasActivatedByMouseEvent(false);
switch (message) {
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
::SetFocus(m_window);
::SetCapture(m_window);
break;
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
::ReleaseCapture();
break;
case WM_MOUSEMOVE:
startTrackingMouseLeave();
break;
case WM_MOUSELEAVE:
stopTrackingMouseLeave();
break;
case WM_LBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
break;
default:
ASSERT_NOT_REACHED();
}
m_page->handleMouseEvent(mouseEvent);
handled = true;
return 0;
}
LRESULT WebView::onWheelEvent(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
{
NativeWebWheelEvent wheelEvent(hWnd, message, wParam, lParam);
if (wheelEvent.controlKey()) {
// We do not want WebKit to handle Control + Wheel, this should be handled by the client application
// to zoom the page.
handled = false;
return 0;
}
m_page->handleNativeWheelEvent(wheelEvent);
handled = true;
return 0;
}
LRESULT WebView::onHorizontalScroll(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
{
ScrollDirection direction;
ScrollGranularity granularity;
switch (LOWORD(wParam)) {
case SB_LINELEFT:
granularity = ScrollGranularity::Line;
direction = ScrollDirection::ScrollLeft;
break;
case SB_LINERIGHT:
granularity = ScrollGranularity::Line;
direction = ScrollDirection::ScrollRight;
break;
case SB_PAGELEFT:
granularity = ScrollGranularity::Document;
direction = ScrollDirection::ScrollLeft;
break;
case SB_PAGERIGHT:
granularity = ScrollGranularity::Document;
direction = ScrollDirection::ScrollRight;
break;
default:
handled = false;
return 0;
}
m_page->scrollBy(direction, granularity);
handled = true;
return 0;
}
LRESULT WebView::onVerticalScroll(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
{
ScrollDirection direction;
ScrollGranularity granularity;
switch (LOWORD(wParam)) {
case SB_LINEDOWN:
granularity = ScrollGranularity::Line;
direction = ScrollDirection::ScrollDown;
break;
case SB_LINEUP:
granularity = ScrollGranularity::Line;
direction = ScrollDirection::ScrollUp;
break;
case SB_PAGEDOWN:
granularity = ScrollGranularity::Document;
direction = ScrollDirection::ScrollDown;
break;
case SB_PAGEUP:
granularity = ScrollGranularity::Document;
direction = ScrollDirection::ScrollUp;
break;
default:
handled = false;
return 0;
}
m_page->scrollBy(direction, granularity);
handled = true;
return 0;
}
LRESULT WebView::onKeyEvent(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
{
Vector<MSG> pendingCharEvents;
if (message == WM_KEYDOWN) {
MSG msg;
// WM_SYSCHAR events should not be removed, because WebKit is using WM_SYSCHAR for access keys and they can't be canceled.
while (PeekMessage(&msg, hWnd, WM_CHAR, WM_DEADCHAR, PM_REMOVE)) {
if (msg.message == WM_CHAR)
pendingCharEvents.append(msg);
}
}
m_page->handleKeyboardEvent(NativeWebKeyboardEvent(hWnd, message, wParam, lParam, WTFMove(pendingCharEvents)));
// We claim here to always have handled the event. If the event is not in fact handled, we will
// find out later in didNotHandleKeyEvent.
handled = true;
return 0;
}
static void drawPageBackground(HDC dc, const WebPageProxy* page, const RECT& rect)
{
auto& backgroundColor = page->backgroundColor();
if (!backgroundColor || backgroundColor.value().isVisible())
return;
::FillRect(dc, &rect, reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1));
}
void WebView::paint(HDC hdc, const IntRect& dirtyRect)
{
if (dirtyRect.isEmpty())
return;
m_page->endPrinting();
if (m_page->drawingArea()) {
auto painter = [&](auto drawingArea) {
// FIXME: We should port WebKit1's rect coalescing logic here.
Region unpaintedRegion;
#if USE(CAIRO)
cairo_surface_t* surface = cairo_win32_surface_create(hdc);
cairo_t* context = cairo_create(surface);
drawingArea->paint(context, dirtyRect, unpaintedRegion);
cairo_destroy(context);
cairo_surface_destroy(surface);
#endif
auto unpaintedRects = unpaintedRegion.rects();
for (auto& rect : unpaintedRects)
drawPageBackground(hdc, m_page.get(), rect);
};
switch (m_page->drawingArea()->type()) {
#if USE(GRAPHICS_LAYER_WC)
case DrawingAreaType::WC:
painter(static_cast<DrawingAreaProxyWC*>(m_page->drawingArea()));
break;
#endif
case DrawingAreaType::CoordinatedGraphics:
painter(static_cast<DrawingAreaProxyCoordinatedGraphics*>(m_page->drawingArea()));
break;
default:
ASSERT_NOT_REACHED();
}
} else
drawPageBackground(hdc, m_page.get(), dirtyRect);
}
LRESULT WebView::onPaintEvent(HWND hWnd, UINT message, WPARAM, LPARAM, bool& handled)
{
// Update child windows now so that any areas of our window they reveal will be included in the
// invalid region that ::BeginPaint sees.
PAINTSTRUCT paintStruct;
HDC hdc = ::BeginPaint(m_window, &paintStruct);
paint(hdc, paintStruct.rcPaint);
::EndPaint(m_window, &paintStruct);
handled = true;
return 0;
}
LRESULT WebView::onPrintClientEvent(HWND hWnd, UINT, WPARAM wParam, LPARAM, bool& handled)
{
HDC hdc = reinterpret_cast<HDC>(wParam);
RECT winRect;
::GetClientRect(hWnd, &winRect);
paint(hdc, winRect);
handled = true;
return 0;
}
LRESULT WebView::onSizeEvent(HWND hwnd, UINT, WPARAM, LPARAM lParam, bool& handled)
{
int width = LOWORD(lParam);
int height = HIWORD(lParam);
IntSize windowSize(width, height);
if (m_page && m_page->drawingArea()) {
// FIXME specify correctly layerPosition.
m_page->drawingArea()->setSize(windowSize, m_nextResizeScrollOffset);
m_nextResizeScrollOffset = IntSize();
}
handled = true;
return 0;
}
LRESULT WebView::onWindowPositionChangedEvent(HWND, UINT, WPARAM, LPARAM lParam, bool& handled)
{
if (reinterpret_cast<WINDOWPOS*>(lParam)->flags & SWP_SHOWWINDOW)
updateActiveStateSoon();
handled = false;
return 0;
}
LRESULT WebView::onSetFocusEvent(HWND, UINT, WPARAM, LPARAM lParam, bool& handled)
{
m_page->activityStateDidChange(ActivityState::IsFocused);
handled = true;
return 0;
}
LRESULT WebView::onKillFocusEvent(HWND, UINT, WPARAM, LPARAM lParam, bool& handled)
{
m_page->activityStateDidChange(ActivityState::IsFocused);
handled = true;
return 0;
}
LRESULT WebView::onTimerEvent(HWND hWnd, UINT, WPARAM wParam, LPARAM, bool& handled)
{
switch (wParam) {
case UpdateActiveStateTimer:
::KillTimer(hWnd, UpdateActiveStateTimer);
updateActiveState();
break;
}
handled = true;
return 0;
}
LRESULT WebView::onShowWindowEvent(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
{
// lParam is 0 when the message is sent because of a ShowWindow call.
// FIXME: Since we don't get notified when an ancestor window is hidden or shown, we will keep
// painting even when we have a hidden ancestor. <http://webkit.org/b/54104>
if (!lParam)
setIsVisible(wParam);
handled = false;
return 0;
}
LRESULT WebView::onSetCursor(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
{
if (!m_lastCursorSet) {
handled = false;
return 0;
}
::SetCursor(m_lastCursorSet);
return 0;
}
LRESULT WebView::onMenuCommand(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, bool& handled)
{
#if ENABLE(CONTEXT_MENUS)
auto hMenu = reinterpret_cast<HMENU>(lParam);
auto index = static_cast<unsigned>(wParam);
MENUITEMINFO menuItemInfo;
menuItemInfo.cbSize = sizeof(menuItemInfo);
menuItemInfo.cch = 0;
menuItemInfo.fMask = MIIM_STRING;
::GetMenuItemInfo(hMenu, index, TRUE, &menuItemInfo);
menuItemInfo.cch++;
Vector<WCHAR> buffer(menuItemInfo.cch);
menuItemInfo.dwTypeData = buffer.data();
menuItemInfo.fMask |= MIIM_ID;
::GetMenuItemInfo(hMenu, index, TRUE, &menuItemInfo);
String title(buffer.data(), menuItemInfo.cch);
ContextMenuAction action = static_cast<ContextMenuAction>(menuItemInfo.wID);
bool enabled = !(menuItemInfo.fState & MFS_DISABLED);
bool checked = menuItemInfo.fState & MFS_CHECKED;
WebContextMenuItemData item(ContextMenuItemType::Action, action, WTFMove(title), enabled, checked);
m_page->contextMenuItemSelected(item);
handled = true;
#else
UNUSED_PARAM(hWnd);
UNUSED_PARAM(message);
UNUSED_PARAM(wParam);
UNUSED_PARAM(lParam);
handled = false;
#endif
return 0;
}
void WebView::updateActiveState()
{
m_page->activityStateDidChange(ActivityState::WindowIsActive);
}
void WebView::updateActiveStateSoon()
{
// This function is called while processing the WM_NCACTIVATE message.
// While processing WM_NCACTIVATE when we are being deactivated, GetActiveWindow() will
// still return our window. If we were to call updateActiveState() in that case, we would
// wrongly think that we are still the active window. To work around this, we update our
// active state after a 0-delay timer fires, at which point GetActiveWindow() will return
// the newly-activated window.
::SetTimer(m_window, UpdateActiveStateTimer, 0, 0);
}
static bool initCommonControls()
{
static bool haveInitialized = false;
if (haveInitialized)
return true;
INITCOMMONCONTROLSEX init;
init.dwSize = sizeof(init);
init.dwICC = ICC_TREEVIEW_CLASSES;
haveInitialized = !!::InitCommonControlsEx(&init);
return haveInitialized;
}
void WebView::initializeToolTipWindow()
{
if (!initCommonControls())
return;
m_toolTipWindow = ::CreateWindowEx(WS_EX_TRANSPARENT, TOOLTIPS_CLASS, 0, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
m_window, 0, 0, 0);
if (!m_toolTipWindow)
return;
TOOLINFO info { };
info.cbSize = sizeof(info);
info.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
info.uId = reinterpret_cast<UINT_PTR>(m_window);
::SendMessage(m_toolTipWindow, TTM_ADDTOOL, 0, reinterpret_cast<LPARAM>(&info));
::SendMessage(m_toolTipWindow, TTM_SETMAXTIPWIDTH, 0, kMaxToolTipWidth);
::SetWindowPos(m_toolTipWindow, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
void WebView::startTrackingMouseLeave()
{
if (m_trackingMouseLeave)
return;
m_trackingMouseLeave = true;
TRACKMOUSEEVENT trackMouseEvent;
trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
trackMouseEvent.dwFlags = TME_LEAVE;
trackMouseEvent.hwndTrack = m_window;
::TrackMouseEvent(&trackMouseEvent);
}
void WebView::stopTrackingMouseLeave()
{
if (!m_trackingMouseLeave)
return;
m_trackingMouseLeave = false;
TRACKMOUSEEVENT trackMouseEvent;
trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
trackMouseEvent.dwFlags = TME_LEAVE | TME_CANCEL;
trackMouseEvent.hwndTrack = m_window;
::TrackMouseEvent(&trackMouseEvent);
}
bool WebView::shouldInitializeTrackPointHack()
{
static bool shouldCreateScrollbars;
static bool hasRunTrackPointCheck;
if (hasRunTrackPointCheck)
return shouldCreateScrollbars;
hasRunTrackPointCheck = true;
const wchar_t* trackPointKeys[] = {
L"Software\\Lenovo\\TrackPoint",
L"Software\\Lenovo\\UltraNav",
L"Software\\Alps\\Apoint\\TrackPoint",
L"Software\\Synaptics\\SynTPEnh\\UltraNavUSB",
L"Software\\Synaptics\\SynTPEnh\\UltraNavPS2"
};
for (size_t i = 0; i < std::size(trackPointKeys); ++i) {
HKEY trackPointKey;
int readKeyResult = ::RegOpenKeyExW(HKEY_CURRENT_USER, trackPointKeys[i], 0, KEY_READ, &trackPointKey);
::RegCloseKey(trackPointKey);
if (readKeyResult == ERROR_SUCCESS) {
shouldCreateScrollbars = true;
return shouldCreateScrollbars;
}
}
return shouldCreateScrollbars;
}
void WebView::close()
{
if (m_window) {
// We can't check IsWindow(m_window) here, because that will return true even while
// we're already handling WM_DESTROY. So we check !m_isBeingDestroyed instead.
if (!m_isBeingDestroyed)
DestroyWindow(m_window);
// Either we just destroyed m_window, or it's in the process of being destroyed. Either
// way, we clear it out to make sure we don't try to use it later.
m_window = 0;
}
setParentWindow(0);
m_page->close();
}
HCURSOR WebView::cursorToShow() const
{
if (!m_page->hasRunningProcess())
return 0;
// We only show the override cursor if the default (arrow) cursor is showing.
static HCURSOR arrowCursor = ::LoadCursor(0, IDC_ARROW);
if (m_overrideCursor && m_webCoreCursor == arrowCursor)
return m_overrideCursor;
return m_webCoreCursor;
}
void WebView::setCursor(const WebCore::Cursor& cursor)
{
if (!cursor.platformCursor()->nativeCursor())
return;
m_webCoreCursor = cursor.platformCursor()->nativeCursor();
updateNativeCursor();
}
void WebView::updateNativeCursor()
{
m_lastCursorSet = cursorToShow();
if (!m_lastCursorSet)
return;
::SetCursor(m_lastCursorSet);
}
void WebView::setOverrideCursor(HCURSOR overrideCursor)
{
m_overrideCursor = overrideCursor;
updateNativeCursor();
}
void WebView::setIsInWindow(bool isInWindow)
{
m_isInWindow = isInWindow;
if (m_page)
m_page->activityStateDidChange(ActivityState::IsInWindow);
}
void WebView::setIsVisible(bool isVisible)
{
m_isVisible = isVisible;
if (m_page)
m_page->activityStateDidChange(ActivityState::IsVisible);
}
bool WebView::isWindowActive()
{
HWND activeWindow = ::GetActiveWindow();
return (activeWindow && m_topLevelParentWindow == findTopLevelParentWindow(activeWindow));
}
bool WebView::isFocused()
{
return ::GetFocus() == m_window;
}
bool WebView::isVisible()
{
return m_isVisible;
}
bool WebView::isInWindow()
{
return m_isInWindow;
}
void WebView::setScrollOffsetOnNextResize(const IntSize& scrollOffset)
{
// The next time we get a WM_SIZE message, scroll by the specified amount in onSizeEvent().
m_nextResizeScrollOffset = scrollOffset;
}
void WebView::setViewNeedsDisplay(const WebCore::Region& region)
{
const RECT r = region.bounds();
::InvalidateRect(m_window, &r, true);
}
void WebView::didCommitLoadForMainFrame(bool useCustomRepresentation)
{
}
double WebView::customRepresentationZoomFactor()
{
return 1;
}
void WebView::setCustomRepresentationZoomFactor(double)
{
}
void WebView::findStringInCustomRepresentation(const String&, FindOptions, unsigned)
{
}
void WebView::countStringMatchesInCustomRepresentation(const String&, FindOptions, unsigned)
{
}
HWND WebView::nativeWindow()
{
return m_window;
}
// WebCore::WindowMessageListener
void WebView::windowReceivedMessage(HWND, UINT message, WPARAM wParam, LPARAM)
{
switch (message) {
case WM_NCACTIVATE:
updateActiveStateSoon();
break;
case WM_SETTINGCHANGE:
break;
}
}
static Vector<wchar_t> truncatedString(const String& string)
{
// Truncate tooltip texts because multiline mode of tooltip control does word-wrapping very slowly
auto maxLength = 1024;
auto buffer = string.wideCharacters();
if (buffer.size() > maxLength) {
buffer[maxLength - 4] = L'.';
buffer[maxLength - 3] = L'.';
buffer[maxLength - 2] = L'.';
buffer[maxLength - 1] = L'\0';
}
return buffer;
}
void WebView::setToolTip(const String& toolTip)
{
if (!m_toolTipWindow)
return;
if (!toolTip.isEmpty()) {
TOOLINFO info { };
info.cbSize = sizeof(info);
info.uFlags = TTF_IDISHWND;
info.uId = reinterpret_cast<UINT_PTR>(nativeWindow());
auto toolTipCharacters = truncatedString(toolTip); // Retain buffer long enough to make the SendMessage call
info.lpszText = toolTipCharacters.data();
::SendMessage(m_toolTipWindow, TTM_UPDATETIPTEXT, 0, reinterpret_cast<LPARAM>(&info));
}
::SendMessage(m_toolTipWindow, TTM_ACTIVATE, !toolTip.isEmpty(), 0);
}
void WebView::setUsesOffscreenRendering(bool enabled)
{
m_usesOffscreenRendering = enabled;
}
bool WebView::usesOffscreenRendering() const
{
return m_usesOffscreenRendering;
}
} // namespace WebKit