forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppWindow.cpp
3498 lines (2947 loc) · 114 KB
/
AppWindow.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 sw=2 sts=2 ci et: */
/* 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/. */
#include "ErrorList.h"
#include "mozilla/MathAlgorithms.h"
// Local includes
#include "AppWindow.h"
#include <algorithm>
// Helper classes
#include "nsPrintfCString.h"
#include "nsString.h"
#include "nsWidgetsCID.h"
#include "nsThreadUtils.h"
#include "nsNetCID.h"
#include "nsQueryObject.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/Sprintf.h"
#include "mozilla/Try.h"
// Interfaces needed to be included
#include "nsGlobalWindowOuter.h"
#include "nsIAppShell.h"
#include "nsIAppShellService.h"
#include "nsIDocumentViewer.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "nsPIDOMWindow.h"
#include "nsScreen.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIIOService.h"
#include "nsIObserverService.h"
#include "nsIOpenWindowInfo.h"
#include "nsIWindowMediator.h"
#include "nsIScreenManager.h"
#include "nsIScreen.h"
#include "nsIWindowWatcher.h"
#include "nsIURI.h"
#include "nsAppShellCID.h"
#include "nsReadableUtils.h"
#include "nsStyleConsts.h"
#include "nsPresContext.h"
#include "nsContentUtils.h"
#include "nsXULTooltipListener.h"
#include "nsXULPopupManager.h"
#include "nsFocusManager.h"
#include "nsContentList.h"
#include "nsIDOMWindowUtils.h"
#include "nsServiceManagerUtils.h"
#include "prenv.h"
#include "mozilla/AppShutdown.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/Preferences.h"
#include "mozilla/PresShell.h"
#include "mozilla/Services.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/dom/BarProps.h"
#include "mozilla/dom/DOMRect.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/BrowserHost.h"
#include "mozilla/dom/BrowserParent.h"
#include "mozilla/dom/LoadURIOptionsBinding.h"
#include "mozilla/intl/LocaleService.h"
#include "mozilla/EventDispatcher.h"
#ifdef XP_WIN
# include "mozilla/PreXULSkeletonUI.h"
# include "nsIWindowsUIUtils.h"
#endif
#include "mozilla/dom/DocumentL10n.h"
#ifdef XP_MACOSX
# include "mozilla/widget/NativeMenuSupport.h"
# define USE_NATIVE_MENUS
#endif
#define SIZEMODE_NORMAL u"normal"_ns
#define SIZEMODE_MAXIMIZED u"maximized"_ns
#define SIZEMODE_MINIMIZED u"minimized"_ns
#define SIZEMODE_FULLSCREEN u"fullscreen"_ns
#define SIZE_PERSISTENCE_TIMEOUT 500 // msec
//*****************************************************************************
//*** AppWindow: Object Management
//*****************************************************************************
namespace mozilla {
using dom::AutoNoJSAPI;
using dom::BrowserHost;
using dom::BrowsingContext;
using dom::Document;
using dom::DocumentL10n;
using dom::Element;
using dom::EventTarget;
using dom::LoadURIOptions;
using dom::Promise;
AppWindow::AppWindow(uint32_t aChromeFlags)
: mChromeTreeOwner(nullptr),
mContentTreeOwner(nullptr),
mPrimaryContentTreeOwner(nullptr),
mModalStatus(NS_OK),
mFullscreenChangeState(FullscreenChangeState::NotChanging),
mContinueModalLoop(false),
mDebuting(false),
mChromeLoaded(false),
mSizingShellFromXUL(false),
mShowAfterLoad(false),
mIntrinsicallySized(false),
mCenterAfterLoad(false),
mIsHiddenWindow(false),
mLockedUntilChromeLoad(false),
mIgnoreXULSize(false),
mIgnoreXULPosition(false),
mChromeFlagsFrozen(false),
mIgnoreXULSizeMode(false),
mDestroying(false),
mRegistered(false),
mDominantClientSize(false),
mChromeFlags(aChromeFlags),
mWidgetListenerDelegate(this) {}
AppWindow::~AppWindow() {
if (mSPTimer) {
mSPTimer->Cancel();
mSPTimer = nullptr;
}
Destroy();
}
//*****************************************************************************
// AppWindow::nsISupports
//*****************************************************************************
NS_IMPL_ADDREF(AppWindow)
NS_IMPL_RELEASE(AppWindow)
NS_INTERFACE_MAP_BEGIN(AppWindow)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAppWindow)
NS_INTERFACE_MAP_ENTRY(nsIAppWindow)
NS_INTERFACE_MAP_ENTRY(nsIBaseWindow)
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
NS_INTERFACE_MAP_ENTRY_CONCRETE(AppWindow)
NS_INTERFACE_MAP_END
nsresult AppWindow::Initialize(nsIAppWindow* aParent, nsIAppWindow* aOpener,
int32_t aInitialWidth, int32_t aInitialHeight,
bool aIsHiddenWindow,
widget::InitData& widgetInitData) {
nsresult rv;
nsCOMPtr<nsIWidget> parentWidget;
mIsHiddenWindow = aIsHiddenWindow;
DesktopIntPoint initialPos;
nsCOMPtr<nsIBaseWindow> base(do_QueryInterface(aOpener));
if (base) {
LayoutDeviceIntRect rect = base->GetPositionAndSize();
mOpenerScreenRect =
DesktopIntRect::Round(rect / base->DevicePixelsPerDesktopPixel());
if (!mOpenerScreenRect.IsEmpty()) {
initialPos = mOpenerScreenRect.TopLeft();
ConstrainToOpenerScreen(&initialPos.x.value, &initialPos.y.value);
}
}
// XXX: need to get the default window size from prefs...
// Doesn't come from prefs... will come from CSS/XUL/RDF
DesktopIntRect deskRect(initialPos,
DesktopIntSize(aInitialWidth, aInitialHeight));
// Create top level window
if (gfxPlatform::IsHeadless()) {
mWindow = nsIWidget::CreateHeadlessWidget();
} else {
mWindow = nsIWidget::CreateTopLevelWindow();
}
if (!mWindow) {
return NS_ERROR_FAILURE;
}
/* This next bit is troublesome. We carry two different versions of a pointer
to our parent window. One is the parent window's widget, which is passed
to our own widget. The other is a weak reference we keep here to our
parent AppWindow. The former is useful to the widget, and we can't
trust its treatment of the parent reference because they're platform-
specific. The latter is useful to this class.
A better implementation would be one in which the parent keeps strong
references to its children and closes them before it allows itself
to be closed. This would mimic the behaviour of OSes that support
top-level child windows in OSes that do not. Later.
*/
nsCOMPtr<nsIBaseWindow> parentAsWin(do_QueryInterface(aParent));
if (parentAsWin) {
parentAsWin->GetMainWidget(getter_AddRefs(parentWidget));
mParentWindow = do_GetWeakReference(aParent);
}
mWindow->SetWidgetListener(&mWidgetListenerDelegate);
rv = mWindow->Create((nsIWidget*)parentWidget, // Parent nsIWidget
nullptr, // Native parent widget
deskRect, // Widget dimensions
&widgetInitData); // Widget initialization data
NS_ENSURE_SUCCESS(rv, rv);
LayoutDeviceIntRect r = mWindow->GetClientBounds();
// Match the default background color of content. Important on windows
// since we no longer use content child widgets.
mWindow->SetBackgroundColor(NS_RGB(255, 255, 255));
// All Chrome BCs exist within the same BrowsingContextGroup, so we don't need
// to pass in the opener window here. The opener is set later, if needed, by
// nsWindowWatcher.
RefPtr<BrowsingContext> browsingContext =
BrowsingContext::CreateIndependent(BrowsingContext::Type::Chrome);
// Create web shell
mDocShell = nsDocShell::Create(browsingContext);
NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
// Make sure to set the item type on the docshell _before_ calling
// InitWindow() so it knows what type it is.
NS_ENSURE_SUCCESS(EnsureChromeTreeOwner(), NS_ERROR_FAILURE);
mDocShell->SetTreeOwner(mChromeTreeOwner);
r.MoveTo(0, 0);
NS_ENSURE_SUCCESS(mDocShell->InitWindow(nullptr, mWindow, r.X(), r.Y(),
r.Width(), r.Height()),
NS_ERROR_FAILURE);
// Attach a WebProgress listener.during initialization...
mDocShell->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_NETWORK);
mWindow->MaybeDispatchInitialFocusEvent();
return rv;
}
//*****************************************************************************
// AppWindow::nsIIntefaceRequestor
//*****************************************************************************
NS_IMETHODIMP AppWindow::GetInterface(const nsIID& aIID, void** aSink) {
nsresult rv;
NS_ENSURE_ARG_POINTER(aSink);
if (aIID.Equals(NS_GET_IID(nsIPrompt))) {
rv = EnsurePrompter();
if (NS_FAILED(rv)) return rv;
return mPrompter->QueryInterface(aIID, aSink);
}
if (aIID.Equals(NS_GET_IID(nsIAuthPrompt))) {
rv = EnsureAuthPrompter();
if (NS_FAILED(rv)) return rv;
return mAuthPrompter->QueryInterface(aIID, aSink);
}
if (aIID.Equals(NS_GET_IID(mozIDOMWindowProxy))) {
return GetWindowDOMWindow(reinterpret_cast<mozIDOMWindowProxy**>(aSink));
}
if (aIID.Equals(NS_GET_IID(nsIDOMWindow))) {
nsCOMPtr<mozIDOMWindowProxy> window = nullptr;
rv = GetWindowDOMWindow(getter_AddRefs(window));
nsCOMPtr<nsIDOMWindow> domWindow = do_QueryInterface(window);
domWindow.forget(aSink);
return rv;
}
if (aIID.Equals(NS_GET_IID(nsIWebBrowserChrome)) &&
NS_SUCCEEDED(EnsureContentTreeOwner()) &&
NS_SUCCEEDED(mContentTreeOwner->QueryInterface(aIID, aSink))) {
return NS_OK;
}
return QueryInterface(aIID, aSink);
}
//*****************************************************************************
// AppWindow::nsIAppWindow
//*****************************************************************************
NS_IMETHODIMP AppWindow::GetDocShell(nsIDocShell** aDocShell) {
NS_ENSURE_ARG_POINTER(aDocShell);
*aDocShell = mDocShell;
NS_IF_ADDREF(*aDocShell);
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetZLevel(uint32_t* outLevel) {
nsCOMPtr<nsIWindowMediator> mediator(
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
if (mediator)
mediator->GetZLevel(this, outLevel);
else
*outLevel = normalZ;
return NS_OK;
}
NS_IMETHODIMP AppWindow::SetZLevel(uint32_t aLevel) {
nsCOMPtr<nsIWindowMediator> mediator(
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
if (!mediator) return NS_ERROR_FAILURE;
uint32_t zLevel;
mediator->GetZLevel(this, &zLevel);
if (zLevel == aLevel) return NS_OK;
/* refuse to raise a maximized window above the normal browser level,
for fear it could hide newly opened browser windows */
if (aLevel > nsIAppWindow::normalZ && mWindow) {
nsSizeMode sizeMode = mWindow->SizeMode();
if (sizeMode == nsSizeMode_Maximized || sizeMode == nsSizeMode_Fullscreen) {
return NS_ERROR_FAILURE;
}
}
// do it
mediator->SetZLevel(this, aLevel);
PersistentAttributesDirty(PersistentAttribute::Misc, Sync);
nsCOMPtr<nsIDocumentViewer> viewer;
mDocShell->GetDocViewer(getter_AddRefs(viewer));
if (viewer) {
RefPtr<dom::Document> doc = viewer->GetDocument();
if (doc) {
ErrorResult rv;
RefPtr<dom::Event> event =
doc->CreateEvent(u"Events"_ns, dom::CallerType::System, rv);
if (event) {
event->InitEvent(u"windowZLevel"_ns, true, false);
event->SetTrusted(true);
doc->DispatchEvent(*event);
}
}
}
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetChromeFlags(uint32_t* aChromeFlags) {
NS_ENSURE_ARG_POINTER(aChromeFlags);
*aChromeFlags = mChromeFlags;
return NS_OK;
}
NS_IMETHODIMP AppWindow::SetChromeFlags(uint32_t aChromeFlags) {
NS_ASSERTION(!mChromeFlagsFrozen,
"SetChromeFlags() after AssumeChromeFlagsAreFrozen()!");
mChromeFlags = aChromeFlags;
if (mChromeLoaded) {
ApplyChromeFlags();
}
return NS_OK;
}
NS_IMETHODIMP AppWindow::AssumeChromeFlagsAreFrozen() {
mChromeFlagsFrozen = true;
return NS_OK;
}
NS_IMETHODIMP AppWindow::SetIntrinsicallySized(bool aIntrinsicallySized) {
mIntrinsicallySized = aIntrinsicallySized;
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetIntrinsicallySized(bool* aIntrinsicallySized) {
NS_ENSURE_ARG_POINTER(aIntrinsicallySized);
*aIntrinsicallySized = mIntrinsicallySized;
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetPrimaryContentShell(
nsIDocShellTreeItem** aDocShellTreeItem) {
NS_ENSURE_ARG_POINTER(aDocShellTreeItem);
NS_IF_ADDREF(*aDocShellTreeItem = mPrimaryContentShell);
return NS_OK;
}
NS_IMETHODIMP
AppWindow::RemoteTabAdded(nsIRemoteTab* aTab, bool aPrimary) {
if (aPrimary) {
mPrimaryBrowserParent = aTab;
mPrimaryContentShell = nullptr;
} else if (mPrimaryBrowserParent == aTab) {
mPrimaryBrowserParent = nullptr;
}
return NS_OK;
}
NS_IMETHODIMP
AppWindow::RemoteTabRemoved(nsIRemoteTab* aTab) {
if (aTab == mPrimaryBrowserParent) {
mPrimaryBrowserParent = nullptr;
}
return NS_OK;
}
NS_IMETHODIMP
AppWindow::GetPrimaryRemoteTab(nsIRemoteTab** aTab) {
nsCOMPtr<nsIRemoteTab> tab = mPrimaryBrowserParent;
tab.forget(aTab);
return NS_OK;
}
NS_IMETHODIMP
AppWindow::GetPrimaryContentBrowsingContext(
mozilla::dom::BrowsingContext** aBc) {
if (mPrimaryBrowserParent) {
return mPrimaryBrowserParent->GetBrowsingContext(aBc);
}
if (mPrimaryContentShell) {
return mPrimaryContentShell->GetBrowsingContextXPCOM(aBc);
}
*aBc = nullptr;
return NS_OK;
}
static LayoutDeviceIntSize GetOuterToInnerSizeDifference(nsIWidget* aWindow) {
if (!aWindow) {
return LayoutDeviceIntSize();
}
return aWindow->ClientToWindowSizeDifference();
}
static CSSIntSize GetOuterToInnerSizeDifferenceInCSSPixels(
nsIWidget* aWindow, CSSToLayoutDeviceScale aScale) {
LayoutDeviceIntSize devPixelSize = GetOuterToInnerSizeDifference(aWindow);
return RoundedToInt(devPixelSize / aScale);
}
NS_IMETHODIMP
AppWindow::GetOuterToInnerHeightDifferenceInCSSPixels(uint32_t* aResult) {
*aResult = GetOuterToInnerSizeDifferenceInCSSPixels(
mWindow, UnscaledDevicePixelsPerCSSPixel())
.height;
return NS_OK;
}
NS_IMETHODIMP
AppWindow::GetOuterToInnerWidthDifferenceInCSSPixels(uint32_t* aResult) {
*aResult = GetOuterToInnerSizeDifferenceInCSSPixels(
mWindow, UnscaledDevicePixelsPerCSSPixel())
.width;
return NS_OK;
}
nsTArray<RefPtr<mozilla::LiveResizeListener>>
AppWindow::GetLiveResizeListeners() {
nsTArray<RefPtr<mozilla::LiveResizeListener>> listeners;
if (mPrimaryBrowserParent) {
BrowserHost* host = BrowserHost::GetFrom(mPrimaryBrowserParent.get());
RefPtr<mozilla::LiveResizeListener> actor = host->GetActor();
if (actor) {
listeners.AppendElement(actor);
}
}
return listeners;
}
NS_IMETHODIMP AppWindow::ShowModal() {
AUTO_PROFILER_LABEL("AppWindow::ShowModal", OTHER);
if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
MOZ_ASSERT_UNREACHABLE(
"Trying to show modal window after shutdown started.");
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
}
// Store locally so it doesn't die on us
nsCOMPtr<nsIWidget> window = mWindow;
nsCOMPtr<nsIAppWindow> tempRef = this;
#ifdef USE_NATIVE_MENUS
if (!gfxPlatform::IsHeadless()) {
// macOS only: For modals created early in startup.
// (e.g. ProfileManager/ProfileDowngrade) this creates a fallback menu for
// the menu bar which only contains a "Quit" menu item.
// This allows the user to quit the application in a regular way with cmd+Q.
widget::NativeMenuSupport::CreateNativeMenuBar(mWindow, nullptr);
}
#endif
window->SetModal(true);
mContinueModalLoop = true;
EnableParent(false);
{
AutoNoJSAPI nojsapi;
SpinEventLoopUntil("AppWindow::ShowModal"_ns, [&]() {
if (MOZ_UNLIKELY(
AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed))) {
// TODO: Bug 1699041 would apply also here: Should we return an error
// if we are bailing out from a pre-existing modal dialog for shutdown?
ExitModalLoop(NS_OK);
}
return !mContinueModalLoop;
});
}
mContinueModalLoop = false;
window->SetModal(false);
/* Note there's no EnableParent(true) here to match the false one
above. That's done in ExitModalLoop. It's important that the parent
be re-enabled before this window is made invisible; to do otherwise
causes bizarre z-ordering problems. At this point, the window is
already invisible.
No known current implementation of Enable would have a problem with
re-enabling the parent twice, so we could do it again here without
breaking any current implementation. But that's unnecessary if the
modal loop is always exited using ExitModalLoop (the other way would be
to change the protected member variable directly.)
*/
return mModalStatus;
}
//*****************************************************************************
// AppWindow::nsIBaseWindow
//*****************************************************************************
NS_IMETHODIMP AppWindow::InitWindow(nativeWindow aParentNativeWindow,
nsIWidget* parentWidget, int32_t x,
int32_t y, int32_t cx, int32_t cy) {
// XXX First Check In
NS_ASSERTION(false, "Not Yet Implemented");
return NS_OK;
}
NS_IMETHODIMP AppWindow::Destroy() {
nsCOMPtr<nsIAppWindow> kungFuDeathGrip(this);
if (mDocShell) {
mDocShell->RemoveProgressListener(this);
}
if (mSPTimer) {
mSPTimer->Cancel();
SavePersistentAttributes();
mSPTimer = nullptr;
}
if (!mWindow) return NS_OK;
// Ensure we don't reenter this code
if (mDestroying) return NS_OK;
mozilla::AutoRestore<bool> guard(mDestroying);
mDestroying = true;
nsCOMPtr<nsIAppShellService> appShell(
do_GetService(NS_APPSHELLSERVICE_CONTRACTID));
NS_ASSERTION(appShell, "Couldn't get appShell... xpcom shutdown?");
if (appShell) {
appShell->UnregisterTopLevelWindow(static_cast<nsIAppWindow*>(this));
}
// Remove modality (if any) and hide while destroying. More than
// a convenience, the hide prevents user interaction with the partially
// destroyed window. This is especially necessary when the eldest window
// in a stack of modal windows is destroyed first. It happens.
ExitModalLoop(NS_OK);
// XXX: Skip unmapping the window on Linux due to GLX hangs on the compositor
// thread with NVIDIA driver 310.32. We don't need to worry about user
// interactions with destroyed windows on X11 either.
#ifndef MOZ_WIDGET_GTK
if (mWindow) mWindow->Show(false);
#endif
#if defined(XP_WIN)
// We need to explicitly set the focus on Windows, but
// only if the parent is visible.
nsCOMPtr<nsIBaseWindow> parent(do_QueryReferent(mParentWindow));
if (parent) {
nsCOMPtr<nsIWidget> parentWidget;
parent->GetMainWidget(getter_AddRefs(parentWidget));
if (parentWidget && parentWidget->IsVisible()) {
bool isParentHiddenWindow = false;
if (appShell) {
bool hasHiddenWindow = false;
appShell->GetHasHiddenWindow(&hasHiddenWindow);
if (hasHiddenWindow) {
nsCOMPtr<nsIBaseWindow> baseHiddenWindow;
nsCOMPtr<nsIAppWindow> hiddenWindow;
appShell->GetHiddenWindow(getter_AddRefs(hiddenWindow));
if (hiddenWindow) {
baseHiddenWindow = do_GetInterface(hiddenWindow);
isParentHiddenWindow = (baseHiddenWindow == parent);
}
}
}
// somebody screwed up somewhere. hiddenwindow shouldn't be anybody's
// parent. still, when it happens, skip activating it.
if (!isParentHiddenWindow) {
parentWidget->PlaceBehind(eZPlacementTop, 0, true);
}
}
}
#endif
RemoveTooltipSupport();
mDOMWindow = nullptr;
if (mDocShell) {
RefPtr<BrowsingContext> bc(mDocShell->GetBrowsingContext());
mDocShell->Destroy();
bc->Detach();
mDocShell = nullptr; // this can cause reentrancy of this function
}
mPrimaryContentShell = nullptr;
if (mContentTreeOwner) {
mContentTreeOwner->AppWindow(nullptr);
NS_RELEASE(mContentTreeOwner);
}
if (mPrimaryContentTreeOwner) {
mPrimaryContentTreeOwner->AppWindow(nullptr);
NS_RELEASE(mPrimaryContentTreeOwner);
}
if (mChromeTreeOwner) {
mChromeTreeOwner->AppWindow(nullptr);
NS_RELEASE(mChromeTreeOwner);
}
if (mWindow) {
mWindow->SetWidgetListener(nullptr); // nsWebShellWindow hackery
mWindow->Destroy();
mWindow = nullptr;
}
if (!mIsHiddenWindow && mRegistered) {
/* Inform appstartup we've destroyed this window and it could
quit now if it wanted. This must happen at least after mDocShell
is destroyed, because onunload handlers fire then, and those being
script, anything could happen. A new window could open, even.
See bug 130719. */
nsCOMPtr<nsIObserverService> obssvc = services::GetObserverService();
NS_ASSERTION(obssvc, "Couldn't get observer service?");
if (obssvc)
obssvc->NotifyObservers(nullptr, "xul-window-destroyed", nullptr);
}
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetDevicePixelsPerDesktopPixel(double* aScale) {
*aScale = mWindow ? mWindow->GetDesktopToDeviceScale().scale : 1.0;
return NS_OK;
}
double AppWindow::GetWidgetCSSToDeviceScale() {
return mWindow ? mWindow->GetDefaultScale().scale : 1.0;
}
NS_IMETHODIMP AppWindow::SetPositionDesktopPix(int32_t aX, int32_t aY) {
return MoveResize(Some(DesktopIntPoint(aX, aY)), Nothing(), false);
}
// The parameters here are device pixels; do the best we can to convert to
// desktop px, using the window's current scale factor (if available).
NS_IMETHODIMP AppWindow::SetPosition(int32_t aX, int32_t aY) {
// Don't reset the window's size mode here - platforms that don't want to move
// maximized windows should reset it in their respective Move implementation.
return MoveResize(Some(LayoutDeviceIntPoint(aX, aY)), Nothing(), false);
}
NS_IMETHODIMP AppWindow::GetPosition(int32_t* aX, int32_t* aY) {
return GetPositionAndSize(aX, aY, nullptr, nullptr);
}
NS_IMETHODIMP AppWindow::SetSize(int32_t aCX, int32_t aCY, bool aRepaint) {
/* any attempt to set the window's size or position overrides the window's
zoom state. this is important when these two states are competing while
the window is being opened. but it should probably just always be so. */
return MoveResize(Nothing(), Some(LayoutDeviceIntSize(aCX, aCY)), aRepaint);
}
NS_IMETHODIMP AppWindow::GetSize(int32_t* aCX, int32_t* aCY) {
return GetPositionAndSize(nullptr, nullptr, aCX, aCY);
}
NS_IMETHODIMP AppWindow::SetPositionAndSize(int32_t aX, int32_t aY, int32_t aCX,
int32_t aCY, uint32_t aFlags) {
/* any attempt to set the window's size or position overrides the window's
zoom state. this is important when these two states are competing while
the window is being opened. but it should probably just always be so. */
return MoveResize(Some(LayoutDeviceIntPoint(aX, aY)),
Some(LayoutDeviceIntSize(aCX, aCY)),
!!(aFlags & nsIBaseWindow::eRepaint));
}
NS_IMETHODIMP AppWindow::GetPositionAndSize(int32_t* x, int32_t* y, int32_t* cx,
int32_t* cy) {
if (!mWindow) return NS_ERROR_FAILURE;
LayoutDeviceIntRect rect = mWindow->GetScreenBounds();
if (x) *x = rect.X();
if (y) *y = rect.Y();
if (cx) *cx = rect.Width();
if (cy) *cy = rect.Height();
return NS_OK;
}
NS_IMETHODIMP
AppWindow::SetDimensions(DimensionRequest&& aRequest) {
if (aRequest.mDimensionKind == DimensionKind::Inner) {
// For the chrome the inner size is the root shell size, and for the
// content it's the primary content size. We lack an indicator here that
// would allow us to distinguish between the two.
return NS_ERROR_NOT_IMPLEMENTED;
}
MOZ_TRY(aRequest.SupplementFrom(this));
return aRequest.ApplyOuterTo(this);
}
NS_IMETHODIMP
AppWindow::GetDimensions(DimensionKind aDimensionKind, int32_t* aX, int32_t* aY,
int32_t* aCX, int32_t* aCY) {
if (aDimensionKind == DimensionKind::Inner) {
// For the chrome the inner size is the root shell size, and for the
// content it's the primary content size. We lack an indicator here that
// would allow us to distinguish between the two.
return NS_ERROR_NOT_IMPLEMENTED;
}
return GetPositionAndSize(aX, aY, aCX, aCY);
}
nsresult AppWindow::MoveResize(const Maybe<LayoutDeviceIntPoint>& aPosition,
const Maybe<LayoutDeviceIntSize>& aSize,
bool aRepaint) {
DesktopToLayoutDeviceScale scale = mWindow->GetDesktopToDeviceScale();
return MoveResize(aPosition ? Some(*aPosition / scale) : Nothing(),
aSize ? Some(*aSize / scale) : Nothing(), aRepaint);
}
nsresult AppWindow::MoveResize(const Maybe<DesktopPoint>& aPosition,
const Maybe<DesktopSize>& aSize, bool aRepaint) {
NS_ENSURE_STATE(mWindow);
PersistentAttributes dirtyAttributes;
if (!aPosition && !aSize) {
MOZ_ASSERT_UNREACHABLE("Doing nothing?");
return NS_ERROR_UNEXPECTED;
}
if (aSize) {
mWindow->SetSizeMode(nsSizeMode_Normal);
mIntrinsicallySized = false;
mDominantClientSize = false;
}
if (aPosition && aSize) {
mWindow->Resize(aPosition->x, aPosition->y, aSize->width, aSize->height,
aRepaint);
dirtyAttributes = {PersistentAttribute::Size,
PersistentAttribute::Position};
} else if (aSize) {
mWindow->Resize(aSize->width, aSize->height, aRepaint);
dirtyAttributes = {PersistentAttribute::Size};
} else if (aPosition) {
mWindow->Move(aPosition->x, aPosition->y);
dirtyAttributes = {PersistentAttribute::Position};
}
if (mSizingShellFromXUL) {
// If we're invoked for sizing from XUL, we want to neither ignore anything
// nor persist anything, since it's already the value in XUL.
return NS_OK;
}
if (!mChromeLoaded) {
// If we're called before the chrome is loaded someone obviously wants this
// window at this size & in the normal size mode (since it is the only mode
// in which setting dimensions makes sense). We don't persist this one-time
// position/size.
if (aPosition) {
mIgnoreXULPosition = true;
}
if (aSize) {
mIgnoreXULSize = true;
mIgnoreXULSizeMode = true;
}
return NS_OK;
}
PersistentAttributesDirty(dirtyAttributes, Sync);
return NS_OK;
}
NS_IMETHODIMP AppWindow::Center(nsIAppWindow* aRelative, bool aScreen,
bool aAlert) {
DesktopIntRect rect;
bool screenCoordinates = false, windowCoordinates = false;
nsresult result;
if (!mChromeLoaded) {
// note we lose the parameters. at time of writing, this isn't a problem.
mCenterAfterLoad = true;
return NS_OK;
}
if (!aScreen && !aRelative) return NS_ERROR_INVALID_ARG;
nsCOMPtr<nsIScreenManager> screenmgr =
do_GetService("@mozilla.org/gfx/screenmanager;1", &result);
if (NS_FAILED(result)) {
return result;
}
nsCOMPtr<nsIScreen> screen;
if (aRelative) {
nsCOMPtr<nsIBaseWindow> base(do_QueryInterface(aRelative));
if (base) {
rect = RoundedToInt(base->GetPositionAndSize() /
base->DevicePixelsPerDesktopPixel());
// if centering on screen, convert that to the corresponding screen
if (aScreen) {
screen = screenmgr->ScreenForRect(rect);
} else {
windowCoordinates = true;
}
}
}
if (!aRelative) {
if (!mOpenerScreenRect.IsEmpty()) {
screen = screenmgr->ScreenForRect(mOpenerScreenRect);
} else {
screenmgr->GetPrimaryScreen(getter_AddRefs(screen));
}
}
if (aScreen && screen) {
rect = screen->GetAvailRectDisplayPix();
screenCoordinates = true;
}
if (!screenCoordinates && !windowCoordinates) {
return NS_ERROR_FAILURE;
}
NS_ASSERTION(mWindow, "what, no window?");
const LayoutDeviceIntSize ourDevSize = GetSize();
const DesktopIntSize ourSize =
RoundedToInt(ourDevSize / DevicePixelsPerDesktopPixel());
auto newPos =
rect.TopLeft() +
DesktopIntPoint((rect.width - ourSize.width) / 2,
(rect.height - ourSize.height) / (aAlert ? 3 : 2));
if (windowCoordinates) {
mWindow->ConstrainPosition(newPos);
}
SetPositionDesktopPix(newPos.x, newPos.y);
// If moving the window caused it to change size, re-do the centering.
if (GetSize() != ourDevSize) {
return Center(aRelative, aScreen, aAlert);
}
return NS_OK;
}
NS_IMETHODIMP AppWindow::Repaint(bool aForce) {
// XXX First Check In
NS_ASSERTION(false, "Not Yet Implemented");
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetParentWidget(nsIWidget** aParentWidget) {
NS_ENSURE_ARG_POINTER(aParentWidget);
NS_ENSURE_STATE(mWindow);
NS_IF_ADDREF(*aParentWidget = mWindow->GetParent());
return NS_OK;
}
NS_IMETHODIMP AppWindow::SetParentWidget(nsIWidget* aParentWidget) {
// XXX First Check In
NS_ASSERTION(false, "Not Yet Implemented");
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetParentNativeWindow(
nativeWindow* aParentNativeWindow) {
NS_ENSURE_ARG_POINTER(aParentNativeWindow);
nsCOMPtr<nsIWidget> parentWidget;
NS_ENSURE_SUCCESS(GetParentWidget(getter_AddRefs(parentWidget)),
NS_ERROR_FAILURE);
if (parentWidget) {
*aParentNativeWindow = parentWidget->GetNativeData(NS_NATIVE_WIDGET);
}
return NS_OK;
}
NS_IMETHODIMP AppWindow::SetParentNativeWindow(
nativeWindow aParentNativeWindow) {
// XXX First Check In
NS_ASSERTION(false, "Not Yet Implemented");
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetNativeHandle(nsAString& aNativeHandle) {
nsCOMPtr<nsIWidget> mainWidget;
NS_ENSURE_SUCCESS(GetMainWidget(getter_AddRefs(mainWidget)),
NS_ERROR_FAILURE);
if (mainWidget) {
nativeWindow nativeWindowPtr = mainWidget->GetNativeData(NS_NATIVE_WINDOW);
/* the nativeWindow pointer is converted to and exposed as a string. This
is a more reliable way not to lose information (as opposed to JS
|Number| for instance) */
aNativeHandle =
NS_ConvertASCIItoUTF16(nsPrintfCString("0x%p", nativeWindowPtr));
}
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetVisibility(bool* aVisibility) {
NS_ENSURE_ARG_POINTER(aVisibility);
// Always claim to be visible for now. See bug
// https://bugzilla.mozilla.org/show_bug.cgi?id=306245.
*aVisibility = true;
return NS_OK;
}
NS_IMETHODIMP AppWindow::SetVisibility(bool aVisibility) {
if (!mChromeLoaded) {
mShowAfterLoad = aVisibility;
return NS_OK;
}
if (mDebuting) {
return NS_OK;
}
NS_ENSURE_STATE(mDocShell);
mDebuting = true; // (Show / Focus is recursive)
// XXXTAB Do we really need to show docshell and the window? Isn't
// the window good enough?
mDocShell->SetVisibility(aVisibility);
// Store locally so it doesn't die on us. 'Show' can result in the window
// being closed with AppWindow::Destroy being called. That would set
// mWindow to null and posibly destroy the nsIWidget while its Show method
// is on the stack. We need to keep it alive until Show finishes.
nsCOMPtr<nsIWidget> window = mWindow;
window->Show(aVisibility);
nsCOMPtr<nsIWindowMediator> windowMediator(
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
if (windowMediator)
windowMediator->UpdateWindowTimeStamp(static_cast<nsIAppWindow*>(this));
// notify observers so that we can hide the splash screen if possible
nsCOMPtr<nsIObserverService> obssvc = services::GetObserverService();
NS_ASSERTION(obssvc, "Couldn't get observer service.");
if (obssvc) {
obssvc->NotifyObservers(static_cast<nsIAppWindow*>(this),
"xul-window-visible", nullptr);
}
mDebuting = false;
return NS_OK;
}
NS_IMETHODIMP AppWindow::GetEnabled(bool* aEnabled) {
NS_ENSURE_ARG_POINTER(aEnabled);