/
WebNetscapePluginView.mm
2499 lines (2067 loc) · 88.9 KB
/
WebNetscapePluginView.mm
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) 2005, 2006, 2007 Apple Inc. All rights reserved.
*
* 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.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE 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 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.
*/
#if ENABLE(NETSCAPE_PLUGIN_API)
#import "WebNetscapePluginView.h"
#import "QuickDrawCompatibility.h"
#import "WebDataSourceInternal.h"
#import "WebDefaultUIDelegate.h"
#import "WebFrameInternal.h"
#import "WebFrameView.h"
#import "WebKitErrorsPrivate.h"
#import "WebKitLogging.h"
#import "WebKitNSStringExtras.h"
#import "WebKitSystemInterface.h"
#import "WebNSDataExtras.h"
#import "WebNSDictionaryExtras.h"
#import "WebNSObjectExtras.h"
#import "WebNSURLExtras.h"
#import "WebNSURLRequestExtras.h"
#import "WebNSViewExtras.h"
#import "WebNetscapeContainerCheckContextInfo.h"
#import "WebNetscapeContainerCheckPrivate.h"
#import "WebNetscapePluginEventHandler.h"
#import "WebNetscapePluginPackage.h"
#import "WebNetscapePluginStream.h"
#import "WebPluginContainerCheck.h"
#import "WebPluginRequest.h"
#import "WebPreferences.h"
#import "WebUIDelegatePrivate.h"
#import "WebViewInternal.h"
#import <Carbon/Carbon.h>
#import <WebCore/CookieJar.h>
#import <WebCore/DocumentLoader.h>
#import <WebCore/Element.h>
#import <WebCore/Frame.h>
#import <WebCore/FrameLoader.h>
#import <WebCore/FrameTree.h>
#import <WebCore/FrameView.h>
#import <WebCore/HTMLPlugInElement.h>
#import <WebCore/Page.h>
#import <WebCore/PluginMainThreadScheduler.h>
#import <WebCore/ScriptController.h>
#import <WebCore/SecurityOrigin.h>
#import <WebCore/SoftLinking.h>
#import <WebCore/WebCoreObjCExtras.h>
#import <WebCore/WebCoreURLResponse.h>
#import <WebCore/npruntime_impl.h>
#import <WebKit/DOMPrivate.h>
#import <WebKit/WebUIDelegate.h>
#import <objc/objc-runtime.h>
#import <runtime/InitializeThreading.h>
#import <runtime/JSLock.h>
#import <wtf/Assertions.h>
#import <wtf/Threading.h>
#import <wtf/text/CString.h>
#define LoginWindowDidSwitchFromUserNotification @"WebLoginWindowDidSwitchFromUserNotification"
#define LoginWindowDidSwitchToUserNotification @"WebLoginWindowDidSwitchToUserNotification"
#define WKNVSupportsCompositingCoreAnimationPluginsBool 74656 /* TRUE if the browser supports hardware compositing of Core Animation plug-ins */
static const int WKNVSilverlightFullscreenPerformanceIssueFixed = 7288546; /* TRUE if Siverlight addressed its underlying bug in <rdar://problem/7288546> */
using namespace WebCore;
using namespace WebKit;
using namespace std;
static inline bool isDrawingModelQuickDraw(NPDrawingModel drawingModel)
{
#ifndef NP_NO_QUICKDRAW
return drawingModel == NPDrawingModelQuickDraw;
#else
return false;
#endif
};
@interface WebNetscapePluginView (Internal)
- (NPError)_createPlugin;
- (void)_destroyPlugin;
- (NSBitmapImageRep *)_printedPluginBitmap;
- (void)_redeliverStream;
- (BOOL)_shouldCancelSrcStream;
@end
static WebNetscapePluginView *currentPluginView = nil;
typedef struct OpaquePortState* PortState;
static const double ThrottledTimerInterval = 0.25;
class PluginTimer : public TimerBase {
public:
typedef void (*TimerFunc)(NPP npp, uint32_t timerID);
PluginTimer(NPP npp, uint32_t timerID, uint32_t interval, NPBool repeat, TimerFunc timerFunc)
: m_npp(npp)
, m_timerID(timerID)
, m_interval(interval)
, m_repeat(repeat)
, m_timerFunc(timerFunc)
{
}
void start(bool throttle)
{
ASSERT(!isActive());
double timeInterval = m_interval / 1000.0;
if (throttle)
timeInterval = max(timeInterval, ThrottledTimerInterval);
if (m_repeat)
startRepeating(timeInterval);
else
startOneShot(timeInterval);
}
private:
virtual void fired()
{
m_timerFunc(m_npp, m_timerID);
if (!m_repeat)
delete this;
}
NPP m_npp;
uint32_t m_timerID;
uint32_t m_interval;
NPBool m_repeat;
TimerFunc m_timerFunc;
};
#ifndef NP_NO_QUICKDRAW
// QuickDraw is not available in 64-bit
typedef struct {
GrafPtr oldPort;
GDHandle oldDevice;
Point oldOrigin;
RgnHandle oldClipRegion;
RgnHandle oldVisibleRegion;
RgnHandle clipRegion;
BOOL forUpdate;
} PortState_QD;
#endif /* NP_NO_QUICKDRAW */
typedef struct {
CGContextRef context;
} PortState_CG;
@class NSTextInputContext;
@interface NSResponder (AppKitDetails)
- (NSTextInputContext *)inputContext;
@end
@interface WebNetscapePluginView (ForwardDeclarations)
- (void)setWindowIfNecessary;
- (NPError)loadRequest:(NSMutableURLRequest *)request inTarget:(const char *)cTarget withNotifyData:(void *)notifyData sendNotification:(BOOL)sendNotification;
@end
@implementation WebNetscapePluginView
+ (void)initialize
{
JSC::initializeThreading();
WTF::initializeMainThreadToProcessMainThread();
#ifndef BUILDING_ON_TIGER
WebCoreObjCFinalizeOnMainThread(self);
#endif
WKSendUserChangeNotifications();
}
#pragma mark EVENTS
// The WindowRef created by -[NSWindow windowRef] has a QuickDraw GrafPort that covers
// the entire window frame (or structure region to use the Carbon term) rather then just the window content.
// We can remove this when <rdar://problem/4201099> is fixed.
- (void)fixWindowPort
{
#ifndef NP_NO_QUICKDRAW
ASSERT(isDrawingModelQuickDraw(drawingModel));
NSWindow *currentWindow = [self currentWindow];
if ([currentWindow isKindOfClass:objc_getClass("NSCarbonWindow")])
return;
float windowHeight = [currentWindow frame].size.height;
NSView *contentView = [currentWindow contentView];
NSRect contentRect = [contentView convertRect:[contentView frame] toView:nil]; // convert to window-relative coordinates
CGrafPtr oldPort;
GetPort(&oldPort);
SetPort(GetWindowPort((WindowRef)[currentWindow windowRef]));
MovePortTo(static_cast<short>(contentRect.origin.x), /* Flip Y */ static_cast<short>(windowHeight - NSMaxY(contentRect)));
PortSize(static_cast<short>(contentRect.size.width), static_cast<short>(contentRect.size.height));
SetPort(oldPort);
#endif
}
#ifndef NP_NO_QUICKDRAW
static UInt32 getQDPixelFormatForBitmapContext(CGContextRef context)
{
UInt32 byteOrder = CGBitmapContextGetBitmapInfo(context) & kCGBitmapByteOrderMask;
if (byteOrder == kCGBitmapByteOrderDefault)
switch (CGBitmapContextGetBitsPerPixel(context)) {
case 16:
byteOrder = kCGBitmapByteOrder16Host;
break;
case 32:
byteOrder = kCGBitmapByteOrder32Host;
break;
}
switch (byteOrder) {
case kCGBitmapByteOrder16Little:
return k16LE555PixelFormat;
case kCGBitmapByteOrder32Little:
return k32BGRAPixelFormat;
case kCGBitmapByteOrder16Big:
return k16BE555PixelFormat;
case kCGBitmapByteOrder32Big:
return k32ARGBPixelFormat;
}
ASSERT_NOT_REACHED();
return 0;
}
static inline void getNPRect(const CGRect& cgr, NPRect& npr)
{
npr.top = static_cast<uint16_t>(cgr.origin.y);
npr.left = static_cast<uint16_t>(cgr.origin.x);
npr.bottom = static_cast<uint16_t>(CGRectGetMaxY(cgr));
npr.right = static_cast<uint16_t>(CGRectGetMaxX(cgr));
}
#endif
static inline void getNPRect(const NSRect& nr, NPRect& npr)
{
npr.top = static_cast<uint16_t>(nr.origin.y);
npr.left = static_cast<uint16_t>(nr.origin.x);
npr.bottom = static_cast<uint16_t>(NSMaxY(nr));
npr.right = static_cast<uint16_t>(NSMaxX(nr));
}
- (PortState)saveAndSetNewPortStateForUpdate:(BOOL)forUpdate
{
ASSERT([self currentWindow] != nil);
// The base coordinates of a window and it's contentView happen to be the equal at a userSpaceScaleFactor
// of 1. For non-1.0 scale factors this assumption is false.
NSView *windowContentView = [[self window] contentView];
NSRect boundsInWindow = [self convertRect:[self bounds] toView:windowContentView];
NSRect visibleRectInWindow = [self convertRect:[self visibleRect] toView:windowContentView];
// Flip Y to convert -[NSWindow contentView] coordinates to top-left-based window coordinates.
float borderViewHeight = [[self currentWindow] frame].size.height;
boundsInWindow.origin.y = borderViewHeight - NSMaxY(boundsInWindow);
visibleRectInWindow.origin.y = borderViewHeight - NSMaxY(visibleRectInWindow);
#ifndef NP_NO_QUICKDRAW
WindowRef windowRef = (WindowRef)[[self currentWindow] windowRef];
ASSERT(windowRef);
// Look at the Carbon port to convert top-left-based window coordinates into top-left-based content coordinates.
if (isDrawingModelQuickDraw(drawingModel)) {
// If drawing with QuickDraw, fix the window port so that it has the same bounds as the NSWindow's
// content view. This makes it easier to convert between AppKit view and QuickDraw port coordinates.
[self fixWindowPort];
::Rect portBounds;
CGrafPtr port = GetWindowPort(windowRef);
GetPortBounds(port, &portBounds);
PixMap *pix = *GetPortPixMap(port);
boundsInWindow.origin.x += pix->bounds.left - portBounds.left;
boundsInWindow.origin.y += pix->bounds.top - portBounds.top;
visibleRectInWindow.origin.x += pix->bounds.left - portBounds.left;
visibleRectInWindow.origin.y += pix->bounds.top - portBounds.top;
}
#endif
window.type = NPWindowTypeWindow;
window.x = (int32_t)boundsInWindow.origin.x;
window.y = (int32_t)boundsInWindow.origin.y;
window.width = static_cast<uint32_t>(NSWidth(boundsInWindow));
window.height = static_cast<uint32_t>(NSHeight(boundsInWindow));
// "Clip-out" the plug-in when:
// 1) it's not really in a window or off-screen or has no height or width.
// 2) window.x is a "big negative number" which is how WebCore expresses off-screen widgets.
// 3) the window is miniaturized or the app is hidden
// 4) we're inside of viewWillMoveToWindow: with a nil window. In this case, superviews may already have nil
// superviews and nil windows and results from convertRect:toView: are incorrect.
if (window.width <= 0 || window.height <= 0 || window.x < -100000 || [self shouldClipOutPlugin]) {
// The following code tries to give plug-ins the same size they will eventually have.
// The specifiedWidth and specifiedHeight variables are used to predict the size that
// WebCore will eventually resize us to.
// The QuickTime plug-in has problems if you give it a width or height of 0.
// Since other plug-ins also might have the same sort of trouble, we make sure
// to always give plug-ins a size other than 0,0.
if (window.width <= 0)
window.width = specifiedWidth > 0 ? specifiedWidth : 100;
if (window.height <= 0)
window.height = specifiedHeight > 0 ? specifiedHeight : 100;
window.clipRect.bottom = window.clipRect.top;
window.clipRect.left = window.clipRect.right;
// Core Animation plug-ins need to be updated (with a 0,0,0,0 clipRect) when
// moved to a background tab. We don't do this for Core Graphics plug-ins as
// older versions of Flash have historical WebKit-specific code that isn't
// compatible with this behavior.
if (drawingModel == NPDrawingModelCoreAnimation)
getNPRect(NSZeroRect, window.clipRect);
} else {
getNPRect(visibleRectInWindow, window.clipRect);
}
// Save the port state, set up the port for entry into the plugin
PortState portState;
switch (drawingModel) {
#ifndef NP_NO_QUICKDRAW
case NPDrawingModelQuickDraw: {
// Set up NS_Port.
::Rect portBounds;
CGrafPtr port = GetWindowPort(windowRef);
GetPortBounds(port, &portBounds);
nPort.qdPort.port = port;
nPort.qdPort.portx = (int32_t)-boundsInWindow.origin.x;
nPort.qdPort.porty = (int32_t)-boundsInWindow.origin.y;
window.window = &nPort;
PortState_QD *qdPortState = (PortState_QD*)malloc(sizeof(PortState_QD));
portState = (PortState)qdPortState;
GetGWorld(&qdPortState->oldPort, &qdPortState->oldDevice);
qdPortState->oldOrigin.h = portBounds.left;
qdPortState->oldOrigin.v = portBounds.top;
qdPortState->oldClipRegion = NewRgn();
GetPortClipRegion(port, qdPortState->oldClipRegion);
qdPortState->oldVisibleRegion = NewRgn();
GetPortVisibleRegion(port, qdPortState->oldVisibleRegion);
RgnHandle clipRegion = NewRgn();
qdPortState->clipRegion = clipRegion;
CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
if (currentContext && WKCGContextIsBitmapContext(currentContext)) {
// We use WKCGContextIsBitmapContext here, because if we just called CGBitmapContextGetData
// on any context, we'd log to the console every time. But even if WKCGContextIsBitmapContext
// returns true, it still might not be a context we need to create a GWorld for; for example
// transparency layers will return true, but return 0 for CGBitmapContextGetData.
void* offscreenData = CGBitmapContextGetData(currentContext);
if (offscreenData) {
// If the current context is an offscreen bitmap, then create a GWorld for it.
::Rect offscreenBounds;
offscreenBounds.top = 0;
offscreenBounds.left = 0;
offscreenBounds.right = CGBitmapContextGetWidth(currentContext);
offscreenBounds.bottom = CGBitmapContextGetHeight(currentContext);
GWorldPtr newOffscreenGWorld;
QDErr err = NewGWorldFromPtr(&newOffscreenGWorld,
getQDPixelFormatForBitmapContext(currentContext), &offscreenBounds, 0, 0, 0,
static_cast<char*>(offscreenData), CGBitmapContextGetBytesPerRow(currentContext));
ASSERT(newOffscreenGWorld);
ASSERT(!err);
if (!err) {
if (offscreenGWorld)
DisposeGWorld(offscreenGWorld);
offscreenGWorld = newOffscreenGWorld;
SetGWorld(offscreenGWorld, NULL);
port = offscreenGWorld;
nPort.qdPort.port = port;
boundsInWindow = [self bounds];
// Generate a QD origin based on the current affine transform for currentContext.
CGAffineTransform offscreenMatrix = CGContextGetCTM(currentContext);
CGPoint origin = {0,0};
CGPoint axisFlip = {1,1};
origin = CGPointApplyAffineTransform(origin, offscreenMatrix);
axisFlip = CGPointApplyAffineTransform(axisFlip, offscreenMatrix);
// Quartz bitmaps have origins at the bottom left, but the axes may be inverted, so handle that.
origin.x = offscreenBounds.left - origin.x * (axisFlip.x - origin.x);
origin.y = offscreenBounds.bottom + origin.y * (axisFlip.y - origin.y);
nPort.qdPort.portx = static_cast<int32_t>(-boundsInWindow.origin.x + origin.x);
nPort.qdPort.porty = static_cast<int32_t>(-boundsInWindow.origin.y - origin.y);
window.x = 0;
window.y = 0;
window.window = &nPort;
// Use the clip bounds from the context instead of the bounds we created
// from the window above.
getNPRect(CGRectOffset(CGContextGetClipBoundingBox(currentContext), -origin.x, origin.y), window.clipRect);
}
}
}
MacSetRectRgn(clipRegion,
window.clipRect.left + nPort.qdPort.portx, window.clipRect.top + nPort.qdPort.porty,
window.clipRect.right + nPort.qdPort.portx, window.clipRect.bottom + nPort.qdPort.porty);
// Clip to the dirty region if drawing to a window. When drawing to another bitmap context, do not clip.
if ([NSGraphicsContext currentContext] == [[self currentWindow] graphicsContext]) {
// Clip to dirty region so plug-in does not draw over already-drawn regions of the window that are
// not going to be redrawn this update. This forces plug-ins to play nice with z-index ordering.
if (forUpdate) {
RgnHandle viewClipRegion = NewRgn();
// Get list of dirty rects from the opaque ancestor -- WebKit does some tricks with invalidation and
// display to enable z-ordering for NSViews; a side-effect of this is that only the WebHTMLView
// knows about the true set of dirty rects.
NSView *opaqueAncestor = [self opaqueAncestor];
const NSRect *dirtyRects;
NSInteger dirtyRectCount, dirtyRectIndex;
[opaqueAncestor getRectsBeingDrawn:&dirtyRects count:&dirtyRectCount];
for (dirtyRectIndex = 0; dirtyRectIndex < dirtyRectCount; dirtyRectIndex++) {
NSRect dirtyRect = [self convertRect:dirtyRects[dirtyRectIndex] fromView:opaqueAncestor];
if (!NSEqualSizes(dirtyRect.size, NSZeroSize)) {
// Create a region for this dirty rect
RgnHandle dirtyRectRegion = NewRgn();
SetRectRgn(dirtyRectRegion, static_cast<short>(NSMinX(dirtyRect)), static_cast<short>(NSMinY(dirtyRect)), static_cast<short>(NSMaxX(dirtyRect)), static_cast<short>(NSMaxY(dirtyRect)));
// Union this dirty rect with the rest of the dirty rects
UnionRgn(viewClipRegion, dirtyRectRegion, viewClipRegion);
DisposeRgn(dirtyRectRegion);
}
}
// Intersect the dirty region with the clip region, so that we only draw over dirty parts
SectRgn(clipRegion, viewClipRegion, clipRegion);
DisposeRgn(viewClipRegion);
}
}
// Switch to the port and set it up.
SetPort(port);
PenNormal();
ForeColor(blackColor);
BackColor(whiteColor);
SetOrigin(nPort.qdPort.portx, nPort.qdPort.porty);
SetPortClipRegion(nPort.qdPort.port, clipRegion);
if (forUpdate) {
// AppKit may have tried to help us by doing a BeginUpdate.
// But the invalid region at that level didn't include AppKit's notion of what was not valid.
// We reset the port's visible region to counteract what BeginUpdate did.
SetPortVisibleRegion(nPort.qdPort.port, clipRegion);
InvalWindowRgn(windowRef, clipRegion);
}
qdPortState->forUpdate = forUpdate;
break;
}
#endif /* NP_NO_QUICKDRAW */
case NPDrawingModelCoreGraphics: {
if (![self canDraw]) {
portState = NULL;
break;
}
ASSERT([NSView focusView] == self);
CGContextRef context = static_cast<CGContextRef>([[NSGraphicsContext currentContext] graphicsPort]);
PortState_CG *cgPortState = (PortState_CG *)malloc(sizeof(PortState_CG));
portState = (PortState)cgPortState;
cgPortState->context = context;
#ifndef NP_NO_CARBON
if (eventModel != NPEventModelCocoa) {
// Update the plugin's window/context
nPort.cgPort.window = windowRef;
nPort.cgPort.context = context;
window.window = &nPort.cgPort;
}
#endif /* NP_NO_CARBON */
// Save current graphics context's state; will be restored by -restorePortState:
CGContextSaveGState(context);
// Clip to the dirty region if drawing to a window. When drawing to another bitmap context, do not clip.
if ([NSGraphicsContext currentContext] == [[self currentWindow] graphicsContext]) {
// Get list of dirty rects from the opaque ancestor -- WebKit does some tricks with invalidation and
// display to enable z-ordering for NSViews; a side-effect of this is that only the WebHTMLView
// knows about the true set of dirty rects.
NSView *opaqueAncestor = [self opaqueAncestor];
const NSRect *dirtyRects;
NSInteger count;
[opaqueAncestor getRectsBeingDrawn:&dirtyRects count:&count];
Vector<CGRect, 16> convertedDirtyRects;
convertedDirtyRects.resize(count);
for (int i = 0; i < count; ++i)
reinterpret_cast<NSRect&>(convertedDirtyRects[i]) = [self convertRect:dirtyRects[i] fromView:opaqueAncestor];
CGContextClipToRects(context, convertedDirtyRects.data(), count);
}
break;
}
case NPDrawingModelCoreAnimation:
// Just set the port state to a dummy value.
portState = (PortState)1;
break;
default:
ASSERT_NOT_REACHED();
portState = NULL;
break;
}
return portState;
}
- (PortState)saveAndSetNewPortState
{
return [self saveAndSetNewPortStateForUpdate:NO];
}
- (void)restorePortState:(PortState)portState
{
ASSERT([self currentWindow]);
ASSERT(portState);
switch (drawingModel) {
#ifndef NP_NO_QUICKDRAW
case NPDrawingModelQuickDraw: {
PortState_QD *qdPortState = (PortState_QD *)portState;
WindowRef windowRef = (WindowRef)[[self currentWindow] windowRef];
CGrafPtr port = GetWindowPort(windowRef);
SetPort(port);
if (qdPortState->forUpdate)
ValidWindowRgn(windowRef, qdPortState->clipRegion);
SetOrigin(qdPortState->oldOrigin.h, qdPortState->oldOrigin.v);
SetPortClipRegion(port, qdPortState->oldClipRegion);
if (qdPortState->forUpdate)
SetPortVisibleRegion(port, qdPortState->oldVisibleRegion);
DisposeRgn(qdPortState->oldClipRegion);
DisposeRgn(qdPortState->oldVisibleRegion);
DisposeRgn(qdPortState->clipRegion);
SetGWorld(qdPortState->oldPort, qdPortState->oldDevice);
break;
}
#endif /* NP_NO_QUICKDRAW */
case NPDrawingModelCoreGraphics: {
ASSERT([NSView focusView] == self);
CGContextRef context = ((PortState_CG *)portState)->context;
ASSERT(!nPort.cgPort.context || (context == nPort.cgPort.context));
CGContextRestoreGState(context);
break;
}
case NPDrawingModelCoreAnimation:
ASSERT(portState == (PortState)1);
break;
default:
ASSERT_NOT_REACHED();
break;
}
}
- (BOOL)sendEvent:(void*)event isDrawRect:(BOOL)eventIsDrawRect
{
if (![self window])
return NO;
ASSERT(event);
if (!_isStarted)
return NO;
ASSERT([_pluginPackage.get() pluginFuncs]->event);
// Make sure we don't call NPP_HandleEvent while we're inside NPP_SetWindow.
// We probably don't want more general reentrancy protection; we are really
// protecting only against this one case, which actually comes up when
// you first install the SVG viewer plug-in.
if (inSetWindow)
return NO;
Frame* frame = core([self webFrame]);
if (!frame)
return NO;
Page* page = frame->page();
if (!page)
return NO;
// Can only send drawRect (updateEvt) to CoreGraphics plugins when actually drawing
ASSERT((drawingModel != NPDrawingModelCoreGraphics) || !eventIsDrawRect || [NSView focusView] == self);
PortState portState = NULL;
if (isDrawingModelQuickDraw(drawingModel) || (drawingModel != NPDrawingModelCoreAnimation && eventIsDrawRect)) {
// In CoreGraphics mode, the port state only needs to be saved/set when redrawing the plug-in view.
// The plug-in is not allowed to draw at any other time.
portState = [self saveAndSetNewPortStateForUpdate:eventIsDrawRect];
// We may have changed the window, so inform the plug-in.
[self setWindowIfNecessary];
}
#if !defined(NDEBUG) && !defined(NP_NO_QUICKDRAW)
// Draw green to help debug.
// If we see any green we know something's wrong.
// Note that PaintRect() only works for QuickDraw plugins; otherwise the current QD port is undefined.
if (isDrawingModelQuickDraw(drawingModel) && eventIsDrawRect) {
ForeColor(greenColor);
const ::Rect bigRect = { -10000, -10000, 10000, 10000 };
PaintRect(&bigRect);
ForeColor(blackColor);
}
#endif
// Temporarily retain self in case the plug-in view is released while sending an event.
[[self retain] autorelease];
BOOL acceptedEvent;
[self willCallPlugInFunction];
{
JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
acceptedEvent = [_pluginPackage.get() pluginFuncs]->event(plugin, event);
}
[self didCallPlugInFunction];
if (portState) {
if ([self currentWindow])
[self restorePortState:portState];
if (portState != (PortState)1)
free(portState);
}
return acceptedEvent;
}
- (void)windowFocusChanged:(BOOL)hasFocus
{
_eventHandler->windowFocusChanged(hasFocus);
}
- (void)sendDrawRectEvent:(NSRect)rect
{
ASSERT(_eventHandler);
CGContextRef context = static_cast<CGContextRef>([[NSGraphicsContext currentContext] graphicsPort]);
_eventHandler->drawRect(context, rect);
}
- (void)stopTimers
{
[super stopTimers];
if (_eventHandler)
_eventHandler->stopTimers();
if (!timers)
return;
HashMap<uint32_t, PluginTimer*>::const_iterator end = timers->end();
for (HashMap<uint32_t, PluginTimer*>::const_iterator it = timers->begin(); it != end; ++it) {
PluginTimer* timer = it->second;
timer->stop();
}
}
- (void)startTimers
{
[super startTimers];
// If the plugin is completely obscured (scrolled out of view, for example), then we will
// send null events at a reduced rate.
_eventHandler->startTimers(_isCompletelyObscured);
if (!timers)
return;
HashMap<uint32_t, PluginTimer*>::const_iterator end = timers->end();
for (HashMap<uint32_t, PluginTimer*>::const_iterator it = timers->begin(); it != end; ++it) {
PluginTimer* timer = it->second;
ASSERT(!timer->isActive());
timer->start(_isCompletelyObscured);
}
}
- (void)focusChanged
{
// We need to null check the event handler here because
// the plug-in view can resign focus after it's been stopped
// and the event handler has been deleted.
if (_eventHandler)
_eventHandler->focusChanged(_hasFocus);
}
- (void)mouseDown:(NSEvent *)theEvent
{
if (!_isStarted)
return;
_eventHandler->mouseDown(theEvent);
}
- (void)mouseUp:(NSEvent *)theEvent
{
if (!_isStarted)
return;
_eventHandler->mouseUp(theEvent);
}
- (void)handleMouseEntered:(NSEvent *)theEvent
{
if (!_isStarted)
return;
// Set cursor to arrow. Plugins often handle cursor internally, but those that don't will just get this default one.
[[NSCursor arrowCursor] set];
_eventHandler->mouseEntered(theEvent);
}
- (void)handleMouseExited:(NSEvent *)theEvent
{
if (!_isStarted)
return;
_eventHandler->mouseExited(theEvent);
// Set cursor back to arrow cursor. Because NSCursor doesn't know about changes that the plugin made, we could get confused about what we think the
// current cursor is otherwise. Therefore we have no choice but to unconditionally reset the cursor when the mouse exits the plugin.
[[NSCursor arrowCursor] set];
}
- (void)handleMouseMoved:(NSEvent *)theEvent
{
if (!_isStarted)
return;
_eventHandler->mouseMoved(theEvent);
}
- (void)mouseDragged:(NSEvent *)theEvent
{
if (!_isStarted)
return;
_eventHandler->mouseDragged(theEvent);
}
- (void)scrollWheel:(NSEvent *)theEvent
{
if (!_isStarted) {
[super scrollWheel:theEvent];
return;
}
if (!_eventHandler->scrollWheel(theEvent))
[super scrollWheel:theEvent];
}
- (void)keyUp:(NSEvent *)theEvent
{
if (!_isStarted)
return;
_eventHandler->keyUp(theEvent);
}
- (void)keyDown:(NSEvent *)theEvent
{
if (!_isStarted)
return;
_eventHandler->keyDown(theEvent);
}
- (void)flagsChanged:(NSEvent *)theEvent
{
if (!_isStarted)
return;
_eventHandler->flagsChanged(theEvent);
}
- (void)sendModifierEventWithKeyCode:(int)keyCode character:(char)character
{
if (!_isStarted)
return;
_eventHandler->syntheticKeyDownWithCommandModifier(keyCode, character);
}
- (void)privateBrowsingModeDidChange
{
if (!_isStarted)
return;
NPBool value = _isPrivateBrowsingEnabled;
[self willCallPlugInFunction];
{
JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
if ([_pluginPackage.get() pluginFuncs]->setvalue)
[_pluginPackage.get() pluginFuncs]->setvalue(plugin, NPNVprivateModeBool, &value);
}
[self didCallPlugInFunction];
}
#pragma mark WEB_NETSCAPE_PLUGIN
- (BOOL)isNewWindowEqualToOldWindow
{
if (window.x != lastSetWindow.x)
return NO;
if (window.y != lastSetWindow.y)
return NO;
if (window.width != lastSetWindow.width)
return NO;
if (window.height != lastSetWindow.height)
return NO;
if (window.clipRect.top != lastSetWindow.clipRect.top)
return NO;
if (window.clipRect.left != lastSetWindow.clipRect.left)
return NO;
if (window.clipRect.bottom != lastSetWindow.clipRect.bottom)
return NO;
if (window.clipRect.right != lastSetWindow.clipRect.right)
return NO;
if (window.type != lastSetWindow.type)
return NO;
switch (drawingModel) {
#ifndef NP_NO_QUICKDRAW
case NPDrawingModelQuickDraw:
if (nPort.qdPort.portx != lastSetPort.qdPort.portx)
return NO;
if (nPort.qdPort.porty != lastSetPort.qdPort.porty)
return NO;
if (nPort.qdPort.port != lastSetPort.qdPort.port)
return NO;
break;
#endif /* NP_NO_QUICKDRAW */
case NPDrawingModelCoreGraphics:
if (nPort.cgPort.window != lastSetPort.cgPort.window)
return NO;
if (nPort.cgPort.context != lastSetPort.cgPort.context)
return NO;
break;
case NPDrawingModelCoreAnimation:
if (window.window != lastSetWindow.window)
return NO;
break;
default:
ASSERT_NOT_REACHED();
break;
}
return YES;
}
-(void)tellQuickTimeToChill
{
#ifndef NP_NO_QUICKDRAW
ASSERT(isDrawingModelQuickDraw(drawingModel));
// Make a call to the secret QuickDraw API that makes QuickTime calm down.
WindowRef windowRef = (WindowRef)[[self window] windowRef];
if (!windowRef) {
return;
}
CGrafPtr port = GetWindowPort(windowRef);
::Rect bounds;
GetPortBounds(port, &bounds);
WKCallDrawingNotification(port, &bounds);
#endif /* NP_NO_QUICKDRAW */
}
- (void)updateAndSetWindow
{
// A plug-in can only update if it's (1) already been started (2) isn't stopped
// and (3) is able to draw on-screen. To meet condition (3) the plug-in must not
// be hidden and be attached to a window. There are two exceptions to this rule:
//
// Exception 1: QuickDraw plug-ins must be manually told when to stop writing
// bits to the window backing store, thus to do so requires a new call to
// NPP_SetWindow() with an empty NPWindow struct.
//
// Exception 2: CoreGraphics plug-ins expect to have their drawable area updated
// when they are moved to a background tab, via a NPP_SetWindow call. This is
// accomplished by allowing -saveAndSetNewPortStateForUpdate to "clip-out" the window's
// clipRect. Flash is curently an exception to this. See 6453738.
//
if (!_isStarted)
return;
#ifdef NP_NO_QUICKDRAW
if (![self canDraw])
return;
#else
if (drawingModel == NPDrawingModelQuickDraw)
[self tellQuickTimeToChill];
else if (drawingModel == NPDrawingModelCoreGraphics && ![self canDraw] && _isFlash) {
// The Flash plug-in does not expect an NPP_SetWindow call from WebKit in this case.
// See Exception 2 above.
return;
}
#endif // NP_NO_QUICKDRAW
BOOL didLockFocus = [NSView focusView] != self && [self lockFocusIfCanDraw];
PortState portState = [self saveAndSetNewPortState];
if (portState) {
[self setWindowIfNecessary];
[self restorePortState:portState];
if (portState != (PortState)1)
free(portState);
} else if (drawingModel == NPDrawingModelCoreGraphics)
[self setWindowIfNecessary];
if (didLockFocus)
[self unlockFocus];
}
- (void)setWindowIfNecessary
{
if (!_isStarted)
return;
if (![self isNewWindowEqualToOldWindow]) {
// Make sure we don't call NPP_HandleEvent while we're inside NPP_SetWindow.
// We probably don't want more general reentrancy protection; we are really
// protecting only against this one case, which actually comes up when
// you first install the SVG viewer plug-in.
NPError npErr;
ASSERT(!inSetWindow);
inSetWindow = YES;
[self willCallPlugInFunction];
{
JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
npErr = [_pluginPackage.get() pluginFuncs]->setwindow(plugin, &window);
}
[self didCallPlugInFunction];
inSetWindow = NO;
#ifndef NDEBUG
switch (drawingModel) {
#ifndef NP_NO_QUICKDRAW
case NPDrawingModelQuickDraw:
LOG(Plugins, "NPP_SetWindow (QuickDraw): %d, port=0x%08x, window.x:%d window.y:%d window.width:%d window.height:%d",
npErr, (int)nPort.qdPort.port, (int)window.x, (int)window.y, (int)window.width, (int)window.height);