forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsObjectFrame.cpp
2180 lines (1859 loc) · 68.1 KB
/
nsObjectFrame.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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// vim:set ts=2 sts=2 sw=2 et cin:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* rendering objects for replaced elements implemented by a plugin */
#include "nsObjectFrame.h"
#include "mozilla/BasicEvents.h"
#ifdef XP_WIN
// This is needed for DoublePassRenderingEvent.
#include "mozilla/plugins/PluginMessageUtils.h"
#endif
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsPresContext.h"
#include "nsIPresShell.h"
#include "nsWidgetsCID.h"
#include "nsView.h"
#include "nsViewManager.h"
#include "nsString.h"
#include "nsGkAtoms.h"
#include "nsIPluginInstanceOwner.h"
#include "nsNPAPIPluginInstance.h"
#include "nsIDOMElement.h"
#include "nsRenderingContext.h"
#include "npapi.h"
#include "nsIObjectLoadingContent.h"
#include "nsContentUtils.h"
#include "nsDisplayList.h"
#include "nsFocusManager.h"
#include "nsLayoutUtils.h"
#include "nsFrameManager.h"
#include "nsIObserverService.h"
#include "GeckoProfiler.h"
#include <algorithm>
#include "nsIObjectFrame.h"
#include "nsPluginNativeWindow.h"
#include "FrameLayerBuilder.h"
#include "ImageLayers.h"
#include "nsPluginInstanceOwner.h"
#ifdef XP_WIN
#include "gfxWindowsNativeDrawing.h"
#include "gfxWindowsSurface.h"
#endif
#include "Layers.h"
#include "ReadbackLayer.h"
#include "ImageContainer.h"
// accessibility support
#ifdef ACCESSIBILITY
#include "nsAccessibilityService.h"
#endif
#ifdef MOZ_LOGGING
#define FORCE_PR_LOG 1 /* Allow logging in the release build */
#endif /* MOZ_LOGGING */
#include "prlog.h"
#ifdef XP_MACOSX
#include "gfxQuartzNativeDrawing.h"
#include "nsPluginUtilsOSX.h"
#include "mozilla/gfx/QuartzSupport.h"
#endif
#ifdef MOZ_X11
#include "mozilla/X11Util.h"
using mozilla::DefaultXDisplay;
#endif
#ifdef XP_WIN
#include <wtypes.h>
#include <winuser.h>
#endif
#ifdef XP_OS2
#define INCL_PM
#define INCL_GPI
#include <os2.h>
#include "gfxOS2Surface.h"
#endif
#ifdef MOZ_WIDGET_ANDROID
#include "AndroidBridge.h"
#include "GLContext.h"
#endif
#ifdef CreateEvent // Thank you MS.
#undef CreateEvent
#endif
#ifdef PR_LOGGING
static PRLogModuleInfo *
GetObjectFrameLog()
{
static PRLogModuleInfo *sLog;
if (!sLog)
sLog = PR_NewLogModule("nsObjectFrame");
return sLog;
}
#endif /* PR_LOGGING */
#if defined(XP_MACOSX) && !defined(__LP64__)
// The header files QuickdrawAPI.h and QDOffscreen.h are missing on OS X 10.7
// and up (though the QuickDraw APIs defined in them are still present) -- so
// we need to supply the relevant parts of their contents here. It's likely
// that Apple will eventually remove the APIs themselves (probably in OS X
// 10.8), so we need to make them weak imports, and test for their presence
// before using them.
extern "C" {
#if !defined(__QUICKDRAWAPI__)
extern void SetRect(
Rect * r,
short left,
short top,
short right,
short bottom)
__attribute__((weak_import));
#endif /* __QUICKDRAWAPI__ */
#if !defined(__QDOFFSCREEN__)
extern QDErr NewGWorldFromPtr(
GWorldPtr * offscreenGWorld,
UInt32 PixelFormat,
const Rect * boundsRect,
CTabHandle cTable, /* can be nullptr */
GDHandle aGDevice, /* can be nullptr */
GWorldFlags flags,
Ptr newBuffer,
SInt32 rowBytes)
__attribute__((weak_import));
extern void DisposeGWorld(GWorldPtr offscreenGWorld)
__attribute__((weak_import));
#endif /* __QDOFFSCREEN__ */
}
#endif /* #if defined(XP_MACOSX) && !defined(__LP64__) */
using namespace mozilla;
using namespace mozilla::plugins;
using namespace mozilla::layers;
class PluginBackgroundSink : public ReadbackSink {
public:
PluginBackgroundSink(nsObjectFrame* aFrame, uint64_t aStartSequenceNumber)
: mLastSequenceNumber(aStartSequenceNumber), mFrame(aFrame) {}
~PluginBackgroundSink()
{
if (mFrame) {
mFrame->mBackgroundSink = nullptr;
}
}
virtual void SetUnknown(uint64_t aSequenceNumber)
{
if (!AcceptUpdate(aSequenceNumber))
return;
mFrame->mInstanceOwner->SetBackgroundUnknown();
}
virtual already_AddRefed<gfxContext>
BeginUpdate(const nsIntRect& aRect, uint64_t aSequenceNumber)
{
if (!AcceptUpdate(aSequenceNumber))
return nullptr;
return mFrame->mInstanceOwner->BeginUpdateBackground(aRect);
}
virtual void EndUpdate(gfxContext* aContext, const nsIntRect& aRect)
{
return mFrame->mInstanceOwner->EndUpdateBackground(aContext, aRect);
}
void Destroy() { mFrame = nullptr; }
protected:
bool AcceptUpdate(uint64_t aSequenceNumber) {
if (aSequenceNumber > mLastSequenceNumber && mFrame &&
mFrame->mInstanceOwner) {
mLastSequenceNumber = aSequenceNumber;
return true;
}
return false;
}
uint64_t mLastSequenceNumber;
nsObjectFrame* mFrame;
};
nsObjectFrame::nsObjectFrame(nsStyleContext* aContext)
: nsObjectFrameSuper(aContext)
, mReflowCallbackPosted(false)
{
PR_LOG(GetObjectFrameLog(), PR_LOG_DEBUG,
("Created new nsObjectFrame %p\n", this));
}
nsObjectFrame::~nsObjectFrame()
{
PR_LOG(GetObjectFrameLog(), PR_LOG_DEBUG,
("nsObjectFrame %p deleted\n", this));
}
NS_QUERYFRAME_HEAD(nsObjectFrame)
NS_QUERYFRAME_ENTRY(nsObjectFrame)
NS_QUERYFRAME_ENTRY(nsIObjectFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsObjectFrameSuper)
#ifdef ACCESSIBILITY
a11y::AccType
nsObjectFrame::AccessibleType()
{
return a11y::ePluginType;
}
#ifdef XP_WIN
NS_IMETHODIMP nsObjectFrame::GetPluginPort(HWND *aPort)
{
*aPort = (HWND) mInstanceOwner->GetPluginPortFromWidget();
return NS_OK;
}
#endif
#endif
void
nsObjectFrame::Init(nsIContent* aContent,
nsIFrame* aParent,
nsIFrame* aPrevInFlow)
{
PR_LOG(GetObjectFrameLog(), PR_LOG_DEBUG,
("Initializing nsObjectFrame %p for content %p\n", this, aContent));
nsObjectFrameSuper::Init(aContent, aParent, aPrevInFlow);
}
void
nsObjectFrame::DestroyFrom(nsIFrame* aDestructRoot)
{
if (mReflowCallbackPosted) {
PresContext()->PresShell()->CancelReflowCallback(this);
}
// Tell content owner of the instance to disconnect its frame.
nsCOMPtr<nsIObjectLoadingContent> objContent(do_QueryInterface(mContent));
NS_ASSERTION(objContent, "Why not an object loading content?");
// The content might not have a reference to the instance owner any longer in
// the case of re-entry during instantiation or teardown, so make sure we're
// dissociated.
if (mInstanceOwner) {
mInstanceOwner->SetFrame(nullptr);
}
objContent->HasNewFrame(nullptr);
if (mBackgroundSink) {
mBackgroundSink->Destroy();
}
nsObjectFrameSuper::DestroyFrom(aDestructRoot);
}
/* virtual */ void
nsObjectFrame::DidSetStyleContext(nsStyleContext* aOldStyleContext)
{
if (HasView()) {
nsView* view = GetView();
nsViewManager* vm = view->GetViewManager();
if (vm) {
nsViewVisibility visibility =
IsHidden() ? nsViewVisibility_kHide : nsViewVisibility_kShow;
vm->SetViewVisibility(view, visibility);
}
}
nsObjectFrameSuper::DidSetStyleContext(aOldStyleContext);
}
nsIAtom*
nsObjectFrame::GetType() const
{
return nsGkAtoms::objectFrame;
}
#ifdef DEBUG
NS_IMETHODIMP
nsObjectFrame::GetFrameName(nsAString& aResult) const
{
return MakeFrameName(NS_LITERAL_STRING("ObjectFrame"), aResult);
}
#endif
nsresult
nsObjectFrame::PrepForDrawing(nsIWidget *aWidget)
{
mWidget = aWidget;
nsView* view = GetView();
NS_ASSERTION(view, "Object frames must have views");
if (!view) {
return NS_ERROR_FAILURE;
}
nsViewManager* viewMan = view->GetViewManager();
// mark the view as hidden since we don't know the (x,y) until Paint
// XXX is the above comment correct?
viewMan->SetViewVisibility(view, nsViewVisibility_kHide);
//this is ugly. it was ripped off from didreflow(). MMP
// Position and size view relative to its parent, not relative to our
// parent frame (our parent frame may not have a view).
nsView* parentWithView;
nsPoint origin;
nsRect r(0, 0, mRect.width, mRect.height);
GetOffsetFromView(origin, &parentWithView);
viewMan->ResizeView(view, r);
viewMan->MoveViewTo(view, origin.x, origin.y);
nsPresContext* presContext = PresContext();
nsRootPresContext* rpc = presContext->GetRootPresContext();
if (!rpc) {
return NS_ERROR_FAILURE;
}
if (mWidget) {
// Disallow windowed plugins in popups
nsIFrame* rootFrame = rpc->PresShell()->FrameManager()->GetRootFrame();
nsIWidget* parentWidget = rootFrame->GetNearestWidget();
if (!parentWidget || nsLayoutUtils::GetDisplayRootFrame(this) != rootFrame) {
return NS_ERROR_FAILURE;
}
mInnerView = viewMan->CreateView(GetContentRectRelativeToSelf(), view);
if (!mInnerView) {
NS_ERROR("Could not create inner view");
return NS_ERROR_OUT_OF_MEMORY;
}
viewMan->InsertChild(view, mInnerView, nullptr, true);
mWidget->SetParent(parentWidget);
mWidget->Show(true);
mWidget->Enable(true);
// Set the plugin window to have an empty clip region until we know
// what our true position, size and clip region are. These
// will be reset when nsRootPresContext computes our true
// geometry. The plugin window does need to have a good size here, so
// set the size explicitly to a reasonable guess.
nsAutoTArray<nsIWidget::Configuration,1> configurations;
nsIWidget::Configuration* configuration = configurations.AppendElement();
nscoord appUnitsPerDevPixel = presContext->AppUnitsPerDevPixel();
configuration->mChild = mWidget;
configuration->mBounds.width = NSAppUnitsToIntPixels(mRect.width, appUnitsPerDevPixel);
configuration->mBounds.height = NSAppUnitsToIntPixels(mRect.height, appUnitsPerDevPixel);
parentWidget->ConfigureChildren(configurations);
nsRefPtr<nsDeviceContext> dx = viewMan->GetDeviceContext();
mInnerView->AttachWidgetEventHandler(mWidget);
#ifdef XP_MACOSX
// On Mac, we need to invalidate ourselves since even windowed
// plugins are painted through Thebes and we need to ensure
// the Thebes layer containing the plugin is updated.
if (parentWidget == GetNearestWidget()) {
InvalidateFrame();
}
#endif
RegisterPluginForGeometryUpdates();
// Here we set the background color for this widget because some plugins will use
// the child window background color when painting. If it's not set, it may default to gray
// Sometimes, a frame doesn't have a background color or is transparent. In this
// case, walk up the frame tree until we do find a frame with a background color
for (nsIFrame* frame = this; frame; frame = frame->GetParent()) {
nscolor bgcolor =
frame->GetVisitedDependentColor(eCSSProperty_background_color);
if (NS_GET_A(bgcolor) > 0) { // make sure we got an actual color
mWidget->SetBackgroundColor(bgcolor);
break;
}
}
} else {
// Changing to windowless mode changes the NPWindow geometry.
FixupWindow(GetContentRectRelativeToSelf().Size());
#ifndef XP_MACOSX
RegisterPluginForGeometryUpdates();
#endif
}
if (!IsHidden()) {
viewMan->SetViewVisibility(view, nsViewVisibility_kShow);
}
#ifdef ACCESSIBILITY
nsAccessibilityService* accService = nsIPresShell::AccService();
if (accService) {
accService->RecreateAccessible(PresContext()->PresShell(), mContent);
}
#endif
return NS_OK;
}
#define EMBED_DEF_WIDTH 240
#define EMBED_DEF_HEIGHT 200
/* virtual */ nscoord
nsObjectFrame::GetMinWidth(nsRenderingContext *aRenderingContext)
{
nscoord result = 0;
if (!IsHidden(false)) {
nsIAtom *atom = mContent->Tag();
if (atom == nsGkAtoms::applet || atom == nsGkAtoms::embed) {
result = nsPresContext::CSSPixelsToAppUnits(EMBED_DEF_WIDTH);
}
}
DISPLAY_MIN_WIDTH(this, result);
return result;
}
/* virtual */ nscoord
nsObjectFrame::GetPrefWidth(nsRenderingContext *aRenderingContext)
{
return nsObjectFrame::GetMinWidth(aRenderingContext);
}
void
nsObjectFrame::GetDesiredSize(nsPresContext* aPresContext,
const nsHTMLReflowState& aReflowState,
nsHTMLReflowMetrics& aMetrics)
{
// By default, we have no area
aMetrics.width = 0;
aMetrics.height = 0;
if (IsHidden(false)) {
return;
}
aMetrics.width = aReflowState.ComputedWidth();
aMetrics.height = aReflowState.ComputedHeight();
// for EMBED and APPLET, default to 240x200 for compatibility
nsIAtom *atom = mContent->Tag();
if (atom == nsGkAtoms::applet || atom == nsGkAtoms::embed) {
if (aMetrics.width == NS_UNCONSTRAINEDSIZE) {
aMetrics.width = clamped(nsPresContext::CSSPixelsToAppUnits(EMBED_DEF_WIDTH),
aReflowState.mComputedMinWidth,
aReflowState.mComputedMaxWidth);
}
if (aMetrics.height == NS_UNCONSTRAINEDSIZE) {
aMetrics.height = clamped(nsPresContext::CSSPixelsToAppUnits(EMBED_DEF_HEIGHT),
aReflowState.mComputedMinHeight,
aReflowState.mComputedMaxHeight);
}
#if defined(MOZ_WIDGET_GTK)
// We need to make sure that the size of the object frame does not
// exceed the maximum size of X coordinates. See bug #225357 for
// more information. In theory Gtk2 can handle large coordinates,
// but underlying plugins can't.
aMetrics.height = std::min(aPresContext->DevPixelsToAppUnits(INT16_MAX), aMetrics.height);
aMetrics.width = std::min(aPresContext->DevPixelsToAppUnits(INT16_MAX), aMetrics.width);
#endif
}
// At this point, the width has an unconstrained value only if we have
// nothing to go on (no width set, no information from the plugin, nothing).
// Make up a number.
if (aMetrics.width == NS_UNCONSTRAINEDSIZE) {
aMetrics.width =
(aReflowState.mComputedMinWidth != NS_UNCONSTRAINEDSIZE) ?
aReflowState.mComputedMinWidth : 0;
}
// At this point, the height has an unconstrained value only in two cases:
// a) We are in standards mode with percent heights and parent is auto-height
// b) We have no height information at all.
// In either case, we have to make up a number.
if (aMetrics.height == NS_UNCONSTRAINEDSIZE) {
aMetrics.height =
(aReflowState.mComputedMinHeight != NS_UNCONSTRAINEDSIZE) ?
aReflowState.mComputedMinHeight : 0;
}
// XXXbz don't add in the border and padding, because we screw up our
// plugin's size and positioning if we do... Eventually we _do_ want to
// paint borders, though! At that point, we will need to adjust the desired
// size either here or in Reflow.... Further, we will need to fix Paint() to
// call the superclass in all cases.
}
NS_IMETHODIMP
nsObjectFrame::Reflow(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aMetrics,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
{
DO_GLOBAL_REFLOW_COUNT("nsObjectFrame");
DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
// Get our desired size
GetDesiredSize(aPresContext, aReflowState, aMetrics);
aMetrics.SetOverflowAreasToDesiredBounds();
FinishAndStoreOverflow(&aMetrics);
// delay plugin instantiation until all children have
// arrived. Otherwise there may be PARAMs or other stuff that the
// plugin needs to see that haven't arrived yet.
if (!GetContent()->IsDoneAddingChildren()) {
aStatus = NS_FRAME_COMPLETE;
return NS_OK;
}
// if we are printing or print previewing, bail for now
if (aPresContext->Medium() == nsGkAtoms::print) {
aStatus = NS_FRAME_COMPLETE;
return NS_OK;
}
nsRect r(0, 0, aMetrics.width, aMetrics.height);
r.Deflate(aReflowState.mComputedBorderPadding);
if (mInnerView) {
nsViewManager* vm = mInnerView->GetViewManager();
vm->MoveViewTo(mInnerView, r.x, r.y);
vm->ResizeView(mInnerView, nsRect(nsPoint(0, 0), r.Size()), true);
}
FixupWindow(r.Size());
if (!mReflowCallbackPosted) {
mReflowCallbackPosted = true;
aPresContext->PresShell()->PostReflowCallback(this);
}
aStatus = NS_FRAME_COMPLETE;
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
return NS_OK;
}
///////////// nsIReflowCallback ///////////////
bool
nsObjectFrame::ReflowFinished()
{
mReflowCallbackPosted = false;
CallSetWindow();
return true;
}
void
nsObjectFrame::ReflowCallbackCanceled()
{
mReflowCallbackPosted = false;
}
void
nsObjectFrame::FixupWindow(const nsSize& aSize)
{
nsPresContext* presContext = PresContext();
if (!mInstanceOwner)
return;
NPWindow *window;
mInstanceOwner->GetWindow(window);
NS_ENSURE_TRUE_VOID(window);
#ifdef XP_MACOSX
nsWeakFrame weakFrame(this);
mInstanceOwner->FixUpPluginWindow(nsPluginInstanceOwner::ePluginPaintDisable);
if (!weakFrame.IsAlive()) {
return;
}
#endif
bool windowless = (window->type == NPWindowTypeDrawable);
nsIntPoint origin = GetWindowOriginInPixels(windowless);
// window must be in "display pixels"
double scaleFactor = 1.0;
if (NS_FAILED(mInstanceOwner->GetContentsScaleFactor(&scaleFactor))) {
scaleFactor = 1.0;
}
int intScaleFactor = ceil(scaleFactor);
window->x = origin.x / intScaleFactor;
window->y = origin.y / intScaleFactor;
window->width = presContext->AppUnitsToDevPixels(aSize.width) / intScaleFactor;
window->height = presContext->AppUnitsToDevPixels(aSize.height) / intScaleFactor;
// on the Mac we need to set the clipRect to { 0, 0, 0, 0 } for now. This will keep
// us from drawing on screen until the widget is properly positioned, which will not
// happen until we have finished the reflow process.
#ifdef XP_MACOSX
window->clipRect.top = 0;
window->clipRect.left = 0;
window->clipRect.bottom = 0;
window->clipRect.right = 0;
#else
mInstanceOwner->UpdateWindowPositionAndClipRect(false);
#endif
NotifyPluginReflowObservers();
}
nsresult
nsObjectFrame::CallSetWindow(bool aCheckIsHidden)
{
NPWindow *win = nullptr;
nsresult rv = NS_ERROR_FAILURE;
nsRefPtr<nsNPAPIPluginInstance> pi;
if (!mInstanceOwner ||
NS_FAILED(rv = mInstanceOwner->GetInstance(getter_AddRefs(pi))) ||
!pi ||
NS_FAILED(rv = mInstanceOwner->GetWindow(win)) ||
!win)
return rv;
nsPluginNativeWindow *window = (nsPluginNativeWindow *)win;
#ifdef XP_MACOSX
nsWeakFrame weakFrame(this);
mInstanceOwner->FixUpPluginWindow(nsPluginInstanceOwner::ePluginPaintDisable);
if (!weakFrame.IsAlive()) {
return NS_ERROR_NOT_AVAILABLE;
}
#endif
if (aCheckIsHidden && IsHidden())
return NS_ERROR_FAILURE;
// refresh the plugin port as well
window->window = mInstanceOwner->GetPluginPortFromWidget();
// Adjust plugin dimensions according to pixel snap results
// and reduce amount of SetWindow calls
nsPresContext* presContext = PresContext();
nsRootPresContext* rootPC = presContext->GetRootPresContext();
if (!rootPC)
return NS_ERROR_FAILURE;
int32_t appUnitsPerDevPixel = presContext->AppUnitsPerDevPixel();
nsIFrame* rootFrame = rootPC->PresShell()->FrameManager()->GetRootFrame();
nsRect bounds = GetContentRectRelativeToSelf() + GetOffsetToCrossDoc(rootFrame);
nsIntRect intBounds = bounds.ToNearestPixels(appUnitsPerDevPixel);
// window must be in "display pixels"
double scaleFactor = 1.0;
if (NS_FAILED(mInstanceOwner->GetContentsScaleFactor(&scaleFactor))) {
scaleFactor = 1.0;
}
size_t intScaleFactor = ceil(scaleFactor);
window->x = intBounds.x / intScaleFactor;
window->y = intBounds.y / intScaleFactor;
window->width = intBounds.width / intScaleFactor;
window->height = intBounds.height / intScaleFactor;
// Calling SetWindow might destroy this frame. We need to use the instance
// owner to clean up so hold a ref.
nsRefPtr<nsPluginInstanceOwner> instanceOwnerRef(mInstanceOwner);
// This will call pi->SetWindow and take care of window subclassing
// if needed, see bug 132759. Calling SetWindow can destroy this frame
// so check for that before doing anything else with this frame's memory.
if (mInstanceOwner->UseAsyncRendering()) {
rv = pi->AsyncSetWindow(window);
}
else {
rv = window->CallSetWindow(pi);
}
instanceOwnerRef->ReleasePluginPort(window->window);
return rv;
}
void
nsObjectFrame::RegisterPluginForGeometryUpdates()
{
nsRootPresContext* rpc = PresContext()->GetRootPresContext();
NS_ASSERTION(rpc, "We should have a root pres context!");
if (mRootPresContextRegisteredWith == rpc || !rpc) {
// Already registered with current root pres context,
// or null root pres context...
return;
}
if (mRootPresContextRegisteredWith && mRootPresContextRegisteredWith != rpc) {
// Registered to some other root pres context. Unregister, and
// re-register with our current one...
UnregisterPluginForGeometryUpdates();
}
mRootPresContextRegisteredWith = rpc;
mRootPresContextRegisteredWith->RegisterPluginForGeometryUpdates(mContent);
}
void
nsObjectFrame::UnregisterPluginForGeometryUpdates()
{
if (!mRootPresContextRegisteredWith) {
// Not registered...
return;
}
mRootPresContextRegisteredWith->UnregisterPluginForGeometryUpdates(mContent);
mRootPresContextRegisteredWith = nullptr;
}
void
nsObjectFrame::SetInstanceOwner(nsPluginInstanceOwner* aOwner)
{
// The ownership model here is historically fuzzy. This should only be called
// by nsPluginInstanceOwner when it is given a new frame, and
// nsObjectLoadingContent should be arbitrating frame-ownership via its
// HasNewFrame callback.
mInstanceOwner = aOwner;
if (mInstanceOwner) {
return;
}
UnregisterPluginForGeometryUpdates();
if (mWidget && mInnerView) {
mInnerView->DetachWidgetEventHandler(mWidget);
// Make sure the plugin is hidden in case an update of plugin geometry
// hasn't happened since this plugin became hidden.
nsIWidget* parent = mWidget->GetParent();
if (parent) {
nsTArray<nsIWidget::Configuration> configurations;
nsIWidget::Configuration* configuration = configurations.AppendElement();
configuration->mChild = mWidget;
parent->ConfigureChildren(configurations);
mWidget->Show(false);
mWidget->Enable(false);
mWidget->SetParent(nullptr);
}
}
}
bool
nsObjectFrame::IsFocusable(int32_t *aTabIndex, bool aWithMouse)
{
if (aTabIndex)
*aTabIndex = -1;
return nsObjectFrameSuper::IsFocusable(aTabIndex, aWithMouse);
}
bool
nsObjectFrame::IsHidden(bool aCheckVisibilityStyle) const
{
if (aCheckVisibilityStyle) {
if (!StyleVisibility()->IsVisibleOrCollapsed())
return true;
}
// only <embed> tags support the HIDDEN attribute
if (mContent->Tag() == nsGkAtoms::embed) {
// Yes, these are really the kooky ways that you could tell 4.x
// not to hide the <embed> once you'd put the 'hidden' attribute
// on the tag...
// HIDDEN w/ no attributes gets translated as we are hidden for
// compatibility w/ 4.x and IE so we don't create a non-painting
// widget in layout. See bug 188959.
nsAutoString hidden;
if (mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::hidden, hidden) &&
(hidden.IsEmpty() ||
(!hidden.LowerCaseEqualsLiteral("false") &&
!hidden.LowerCaseEqualsLiteral("no") &&
!hidden.LowerCaseEqualsLiteral("off")))) {
return true;
}
}
return false;
}
nsIntPoint nsObjectFrame::GetWindowOriginInPixels(bool aWindowless)
{
nsView * parentWithView;
nsPoint origin(0,0);
GetOffsetFromView(origin, &parentWithView);
// if it's windowless, let's make sure we have our origin set right
// it may need to be corrected, like after scrolling
if (aWindowless && parentWithView) {
nsPoint offsetToWidget;
parentWithView->GetNearestWidget(&offsetToWidget);
origin += offsetToWidget;
}
origin += GetContentRectRelativeToSelf().TopLeft();
return nsIntPoint(PresContext()->AppUnitsToDevPixels(origin.x),
PresContext()->AppUnitsToDevPixels(origin.y));
}
NS_IMETHODIMP
nsObjectFrame::DidReflow(nsPresContext* aPresContext,
const nsHTMLReflowState* aReflowState,
nsDidReflowStatus aStatus)
{
// Do this check before calling the superclass, as that clears
// NS_FRAME_FIRST_REFLOW
if (aStatus == nsDidReflowStatus::FINISHED &&
(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
nsCOMPtr<nsIObjectLoadingContent> objContent(do_QueryInterface(mContent));
NS_ASSERTION(objContent, "Why not an object loading content?");
objContent->HasNewFrame(this);
}
nsresult rv = nsObjectFrameSuper::DidReflow(aPresContext, aReflowState, aStatus);
// The view is created hidden; once we have reflowed it and it has been
// positioned then we show it.
if (aStatus != nsDidReflowStatus::FINISHED)
return rv;
if (HasView()) {
nsView* view = GetView();
nsViewManager* vm = view->GetViewManager();
if (vm)
vm->SetViewVisibility(view, IsHidden() ? nsViewVisibility_kHide : nsViewVisibility_kShow);
}
return rv;
}
/* static */ void
nsObjectFrame::PaintPrintPlugin(nsIFrame* aFrame, nsRenderingContext* aCtx,
const nsRect& aDirtyRect, nsPoint aPt)
{
nsPoint pt = aPt + aFrame->GetContentRectRelativeToSelf().TopLeft();
nsRenderingContext::AutoPushTranslation translate(aCtx, pt);
// FIXME - Bug 385435: Doesn't aDirtyRect need translating too?
static_cast<nsObjectFrame*>(aFrame)->PrintPlugin(*aCtx, aDirtyRect);
}
class nsDisplayPluginReadback : public nsDisplayItem {
public:
nsDisplayPluginReadback(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame)
: nsDisplayItem(aBuilder, aFrame)
{
MOZ_COUNT_CTOR(nsDisplayPluginReadback);
}
#ifdef NS_BUILD_REFCNT_LOGGING
virtual ~nsDisplayPluginReadback() {
MOZ_COUNT_DTOR(nsDisplayPluginReadback);
}
#endif
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap);
virtual bool ComputeVisibility(nsDisplayListBuilder* aBuilder,
nsRegion* aVisibleRegion,
const nsRect& aAllowVisibleRegionExpansion);
NS_DISPLAY_DECL_NAME("PluginReadback", TYPE_PLUGIN_READBACK)
virtual already_AddRefed<Layer> BuildLayer(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerParameters& aContainerParameters)
{
return static_cast<nsObjectFrame*>(mFrame)->BuildLayer(aBuilder, aManager, this, aContainerParameters);
}
virtual LayerState GetLayerState(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerParameters& aParameters)
{
return LAYER_ACTIVE;
}
};
static nsRect
GetDisplayItemBounds(nsDisplayListBuilder* aBuilder, nsDisplayItem* aItem, nsIFrame* aFrame)
{
// XXX For slightly more accurate region computations we should pixel-snap this
return aFrame->GetContentRectRelativeToSelf() + aItem->ToReferenceFrame();
}
nsRect
nsDisplayPluginReadback::GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap)
{
*aSnap = false;
return GetDisplayItemBounds(aBuilder, this, mFrame);
}
bool
nsDisplayPluginReadback::ComputeVisibility(nsDisplayListBuilder* aBuilder,
nsRegion* aVisibleRegion,
const nsRect& aAllowVisibleRegionExpansion)
{
if (!nsDisplayItem::ComputeVisibility(aBuilder, aVisibleRegion,
aAllowVisibleRegionExpansion))
return false;
nsRect expand;
bool snap;
expand.IntersectRect(aAllowVisibleRegionExpansion, GetBounds(aBuilder, &snap));
// *Add* our bounds to the visible region so that stuff underneath us is
// likely to be made visible, so we can use it for a background! This is
// a bit crazy since we normally only subtract from the visible region.
aVisibleRegion->Or(*aVisibleRegion, expand);
return true;
}
#ifdef MOZ_WIDGET_ANDROID
class nsDisplayPluginVideo : public nsDisplayItem {
public:
nsDisplayPluginVideo(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame, nsNPAPIPluginInstance::VideoInfo* aVideoInfo)
: nsDisplayItem(aBuilder, aFrame), mVideoInfo(aVideoInfo)
{
MOZ_COUNT_CTOR(nsDisplayPluginVideo);
}
#ifdef NS_BUILD_REFCNT_LOGGING
virtual ~nsDisplayPluginVideo() {
MOZ_COUNT_DTOR(nsDisplayPluginVideo);
}
#endif
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap);
virtual bool ComputeVisibility(nsDisplayListBuilder* aBuilder,
nsRegion* aVisibleRegion,
const nsRect& aAllowVisibleRegionExpansion);
NS_DISPLAY_DECL_NAME("PluginVideo", TYPE_PLUGIN_VIDEO)
virtual already_AddRefed<Layer> BuildLayer(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerParameters& aContainerParameters)
{
return static_cast<nsObjectFrame*>(mFrame)->BuildLayer(aBuilder, aManager, this, aContainerParameters);
}
virtual LayerState GetLayerState(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerParameters& aParameters)
{
return LAYER_ACTIVE;
}
nsNPAPIPluginInstance::VideoInfo* VideoInfo() { return mVideoInfo; }
private:
nsNPAPIPluginInstance::VideoInfo* mVideoInfo;
};
nsRect
nsDisplayPluginVideo::GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap)
{
*aSnap = false;
return GetDisplayItemBounds(aBuilder, this, mFrame);
}
bool
nsDisplayPluginVideo::ComputeVisibility(nsDisplayListBuilder* aBuilder,
nsRegion* aVisibleRegion,
const nsRect& aAllowVisibleRegionExpansion)
{
return nsDisplayItem::ComputeVisibility(aBuilder, aVisibleRegion,
aAllowVisibleRegionExpansion);
}
#endif
nsRect
nsDisplayPlugin::GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap)
{
*aSnap = true;
return GetDisplayItemBounds(aBuilder, this, mFrame);
}
void
nsDisplayPlugin::Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx)
{
nsObjectFrame* f = static_cast<nsObjectFrame*>(mFrame);
bool snap;
f->PaintPlugin(aBuilder, *aCtx, mVisibleRect, GetBounds(aBuilder, &snap));
}
bool
nsDisplayPlugin::ComputeVisibility(nsDisplayListBuilder* aBuilder,
nsRegion* aVisibleRegion,
const nsRect& aAllowVisibleRegionExpansion)
{
if (aBuilder->IsForPluginGeometry()) {