forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BrowserChild.cpp
4105 lines (3506 loc) · 141 KB
/
BrowserChild.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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "base/basictypes.h"
#include "BrowserChild.h"
#ifdef ACCESSIBILITY
# include "mozilla/a11y/DocAccessibleChild.h"
#endif
#include <algorithm>
#include <utility>
#include "BrowserParent.h"
#include "ClientLayerManager.h"
#include "ContentChild.h"
#include "DocumentInlines.h"
#include "EventStateManager.h"
#include "FrameLayerBuilder.h"
#include "Layers.h"
#include "MMPrinter.h"
#include "PermissionMessageUtils.h"
#include "PuppetWidget.h"
#include "StructuredCloneData.h"
#include "UnitTransforms.h"
#include "Units.h"
#include "VRManagerChild.h"
#include "ipc/nsGUIEventIPC.h"
#include "js/JSON.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/BrowserElementParent.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/EventForwards.h"
#include "mozilla/EventListenerManager.h"
#include "mozilla/IMEStateManager.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/Preferences.h"
#include "mozilla/PresShell.h"
#include "mozilla/ProcessHangMonitor.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TextEvents.h"
#include "mozilla/TouchEvents.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/BrowserBridgeChild.h"
#include "mozilla/dom/DataTransfer.h"
#include "mozilla/dom/DocGroup.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/JSWindowActorChild.h"
#include "mozilla/dom/LoadURIOptionsBinding.h"
#include "mozilla/dom/MessageManagerBinding.h"
#include "mozilla/dom/MouseEventBinding.h"
#include "mozilla/dom/Nullable.h"
#include "mozilla/dom/PBrowser.h"
#include "mozilla/dom/PaymentRequestChild.h"
#include "mozilla/dom/SessionStoreListener.h"
#include "mozilla/dom/WindowGlobalChild.h"
#include "mozilla/dom/WindowProxyHolder.h"
#include "mozilla/gfx/CrossProcessPaint.h"
#include "mozilla/gfx/Matrix.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/ipc/BackgroundUtils.h"
#include "mozilla/ipc/URIUtils.h"
#include "mozilla/layers/APZCCallbackHelper.h"
#include "mozilla/layers/APZCTreeManagerChild.h"
#include "mozilla/layers/APZChild.h"
#include "mozilla/layers/APZEventState.h"
#include "mozilla/layers/CompositorBridgeChild.h"
#include "mozilla/layers/ContentProcessController.h"
#include "mozilla/layers/DoubleTapToZoom.h"
#include "mozilla/layers/IAPZCTreeManager.h"
#include "mozilla/layers/ImageBridgeChild.h"
#include "mozilla/layers/InputAPZContext.h"
#include "mozilla/layers/LayerTransactionChild.h"
#include "mozilla/layers/ShadowLayers.h"
#include "mozilla/layers/WebRenderLayerManager.h"
#include "mozilla/plugins/PPluginWidgetChild.h"
#include "mozilla/recordreplay/ParentIPC.h"
#include "nsBrowserStatusFilter.h"
#include "nsColorPickerProxy.h"
#include "nsCommandParams.h"
#include "nsContentPermissionHelper.h"
#include "nsContentUtils.h"
#include "nsDeviceContext.h"
#include "nsDocShell.h"
#include "nsDocShellLoadState.h"
#include "nsEmbedCID.h"
#include "nsExceptionHandler.h"
#include "nsFilePickerProxy.h"
#include "nsFocusManager.h"
#include "nsGlobalWindow.h"
#include "nsIBaseWindow.h"
#include "nsIBrowserDOMWindow.h"
#include "nsIClassifiedChannel.h"
#include "nsIDocShell.h"
#include "nsIFrame.h"
#include "nsILoadContext.h"
#include "nsISHEntry.h"
#include "nsISHistory.h"
#include "nsIScriptError.h"
#include "nsISecureBrowserUI.h"
#include "nsIURI.h"
#include "nsIURIMutator.h"
#include "nsIWeakReferenceUtils.h"
#include "nsIWebBrowser.h"
#include "nsIWebProgress.h"
#include "nsLayoutUtils.h"
#include "nsNetUtil.h"
#include "nsPIDOMWindow.h"
#include "nsPIWindowRoot.h"
#include "nsPointerHashKeys.h"
#include "nsPrintfCString.h"
#include "nsQueryActor.h"
#include "nsQueryObject.h"
#include "nsSandboxFlags.h"
#include "nsString.h"
#include "nsTHashtable.h"
#include "nsThreadManager.h"
#include "nsThreadUtils.h"
#include "nsViewManager.h"
#include "nsViewportInfo.h"
#include "nsWebBrowser.h"
#include "nsWindowWatcher.h"
#ifdef XP_WIN
# include "mozilla/plugins/PluginWidgetChild.h"
#endif
#ifdef NS_PRINTING
# include "nsIPrintSession.h"
# include "nsIPrintSettings.h"
# include "nsIPrintSettingsService.h"
# include "nsIWebBrowserPrint.h"
#endif
#define BROWSER_ELEMENT_CHILD_SCRIPT \
NS_LITERAL_STRING("chrome://global/content/BrowserElementChild.js")
#define TABC_LOG(...)
// #define TABC_LOG(...) printf_stderr("TABC: " __VA_ARGS__)
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::dom::ipc;
using namespace mozilla::ipc;
using namespace mozilla::layers;
using namespace mozilla::layout;
using namespace mozilla::docshell;
using namespace mozilla::widget;
using namespace mozilla::jsipc;
using mozilla::layers::GeckoContentController;
NS_IMPL_ISUPPORTS(ContentListener, nsIDOMEventListener)
static const char BEFORE_FIRST_PAINT[] = "before-first-paint";
nsTHashtable<nsPtrHashKey<BrowserChild>>* BrowserChild::sVisibleTabs;
typedef nsDataHashtable<nsUint64HashKey, BrowserChild*> BrowserChildMap;
static BrowserChildMap* sBrowserChildren;
StaticMutex sBrowserChildrenMutex;
already_AddRefed<Document> BrowserChild::GetTopLevelDocument() const {
nsCOMPtr<Document> doc;
WebNavigation()->GetDocument(getter_AddRefs(doc));
return doc.forget();
}
PresShell* BrowserChild::GetTopLevelPresShell() const {
if (RefPtr<Document> doc = GetTopLevelDocument()) {
return doc->GetPresShell();
}
return nullptr;
}
void BrowserChild::DispatchMessageManagerMessage(const nsAString& aMessageName,
const nsAString& aJSONData) {
AutoSafeJSContext cx;
JS::Rooted<JS::Value> json(cx, JS::NullValue());
dom::ipc::StructuredCloneData data;
if (JS_ParseJSON(cx, static_cast<const char16_t*>(aJSONData.BeginReading()),
aJSONData.Length(), &json)) {
ErrorResult rv;
data.Write(cx, json, rv);
if (NS_WARN_IF(rv.Failed())) {
rv.SuppressException();
return;
}
}
RefPtr<BrowserChildMessageManager> kungFuDeathGrip(
mBrowserChildMessageManager);
RefPtr<nsFrameMessageManager> mm = kungFuDeathGrip->GetMessageManager();
mm->ReceiveMessage(static_cast<EventTarget*>(kungFuDeathGrip), nullptr,
aMessageName, false, &data, nullptr, nullptr, nullptr,
IgnoreErrors());
}
bool BrowserChild::UpdateFrame(const RepaintRequest& aRequest) {
MOZ_ASSERT(aRequest.GetScrollId() != ScrollableLayerGuid::NULL_SCROLL_ID);
if (aRequest.IsRootContent()) {
if (PresShell* presShell = GetTopLevelPresShell()) {
// Guard against stale updates (updates meant for a pres shell which
// has since been torn down and destroyed).
if (aRequest.GetPresShellId() == presShell->GetPresShellId()) {
ProcessUpdateFrame(aRequest);
return true;
}
}
} else {
// aRequest.mIsRoot is false, so we are trying to update a subframe.
// This requires special handling.
APZCCallbackHelper::UpdateSubFrame(aRequest);
return true;
}
return true;
}
void BrowserChild::ProcessUpdateFrame(const RepaintRequest& aRequest) {
if (!mBrowserChildMessageManager) {
return;
}
APZCCallbackHelper::UpdateRootFrame(aRequest);
}
NS_IMETHODIMP
ContentListener::HandleEvent(Event* aEvent) {
RemoteDOMEvent remoteEvent;
remoteEvent.mEvent = aEvent;
NS_ENSURE_STATE(remoteEvent.mEvent);
mBrowserChild->SendEvent(remoteEvent);
return NS_OK;
}
class BrowserChild::DelayedDeleteRunnable final : public Runnable,
public nsIRunnablePriority {
RefPtr<BrowserChild> mBrowserChild;
// In order to ensure that this runnable runs after everything that could
// possibly touch this tab, we send it through the event queue twice. The
// first time it runs at normal priority and the second time it runs at
// input priority. This ensures that it runs after all events that were in
// either queue at the time it was first dispatched. mReadyToDelete starts
// out false (when it runs at normal priority) and is then set to true.
bool mReadyToDelete = false;
public:
explicit DelayedDeleteRunnable(BrowserChild* aBrowserChild)
: Runnable("BrowserChild::DelayedDeleteRunnable"),
mBrowserChild(aBrowserChild) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aBrowserChild);
}
NS_DECL_ISUPPORTS_INHERITED
private:
~DelayedDeleteRunnable() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!mBrowserChild);
}
NS_IMETHOD GetPriority(uint32_t* aPriority) override {
*aPriority = mReadyToDelete ? nsIRunnablePriority::PRIORITY_INPUT_HIGH
: nsIRunnablePriority::PRIORITY_NORMAL;
return NS_OK;
}
NS_IMETHOD
Run() override {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mBrowserChild);
if (!mReadyToDelete) {
// This time run this runnable at input priority.
mReadyToDelete = true;
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToCurrentThread(this));
return NS_OK;
}
// Check in case ActorDestroy was called after RecvDestroy message.
// Middleman processes with their own recording child process avoid
// sending a delete message, so that the parent process does not
// receive two deletes for the same actor.
if (mBrowserChild->IPCOpen() &&
!recordreplay::parent::IsMiddlemanWithRecordingChild()) {
Unused << PBrowserChild::Send__delete__(mBrowserChild);
}
mBrowserChild = nullptr;
return NS_OK;
}
};
NS_IMPL_ISUPPORTS_INHERITED(BrowserChild::DelayedDeleteRunnable, Runnable,
nsIRunnablePriority)
namespace {
std::map<TabId, RefPtr<BrowserChild>>& NestedBrowserChildMap() {
MOZ_ASSERT(NS_IsMainThread());
static std::map<TabId, RefPtr<BrowserChild>> sNestedBrowserChildMap;
return sNestedBrowserChildMap;
}
} // namespace
already_AddRefed<BrowserChild> BrowserChild::FindBrowserChild(
const TabId& aTabId) {
auto iter = NestedBrowserChildMap().find(aTabId);
if (iter == NestedBrowserChildMap().end()) {
return nullptr;
}
RefPtr<BrowserChild> browserChild = iter->second;
return browserChild.forget();
}
/*static*/
already_AddRefed<BrowserChild> BrowserChild::Create(
ContentChild* aManager, const TabId& aTabId, const TabId& aSameTabGroupAs,
const TabContext& aContext, BrowsingContext* aBrowsingContext,
uint32_t aChromeFlags, bool aIsTopLevel) {
RefPtr<BrowserChild> groupChild = FindBrowserChild(aSameTabGroupAs);
dom::TabGroup* group = groupChild ? groupChild->TabGroup() : nullptr;
RefPtr<BrowserChild> iframe =
new BrowserChild(aManager, aTabId, group, aContext, aBrowsingContext,
aChromeFlags, aIsTopLevel);
return iframe.forget();
}
BrowserChild::BrowserChild(ContentChild* aManager, const TabId& aTabId,
dom::TabGroup* aTabGroup, const TabContext& aContext,
BrowsingContext* aBrowsingContext,
uint32_t aChromeFlags, bool aIsTopLevel)
: TabContext(aContext),
mBrowserChildMessageManager(nullptr),
mTabGroup(aTabGroup),
mManager(aManager),
mBrowsingContext(aBrowsingContext),
mChromeFlags(aChromeFlags),
mMaxTouchPoints(0),
mLayersId{0},
mEffectsInfo{EffectsInfo::FullyHidden()},
mDidFakeShow(false),
mNotified(false),
mTriedBrowserInit(false),
mOrientation(hal::eScreenOrientation_PortraitPrimary),
mIgnoreKeyPressEvent(false),
mHasValidInnerSize(false),
mDestroyed(false),
mDynamicToolbarMaxHeight(0),
mUniqueId(aTabId),
mIsTopLevel(aIsTopLevel),
mHasSiblings(false),
mIsTransparent(false),
mIPCOpen(false),
mParentIsActive(false),
mDidSetRealShowInfo(false),
mDidLoadURLInit(false),
mAwaitingLA(false),
mSkipKeyPress(false),
mLayersObserverEpoch{1},
#if defined(XP_WIN) && defined(ACCESSIBILITY)
mNativeWindowHandle(0),
#endif
#if defined(ACCESSIBILITY)
mTopLevelDocAccessibleChild(nullptr),
#endif
mShouldSendWebProgressEventsToParent(false),
mRenderLayers(true),
mPendingDocShellIsActive(false),
mPendingDocShellReceivedMessage(false),
mPendingRenderLayers(false),
mPendingRenderLayersReceivedMessage(false),
mPendingLayersObserverEpoch{0},
mPendingDocShellBlockers(0),
mCancelContentJSEpoch(0),
mWidgetNativeData(0) {
mozilla::HoldJSObjects(this);
nsWeakPtr weakPtrThis(do_GetWeakReference(
static_cast<nsIBrowserChild*>(this))); // for capture by the lambda
mSetAllowedTouchBehaviorCallback =
[weakPtrThis](uint64_t aInputBlockId,
const nsTArray<TouchBehaviorFlags>& aFlags) {
if (nsCOMPtr<nsIBrowserChild> browserChild =
do_QueryReferent(weakPtrThis)) {
static_cast<BrowserChild*>(browserChild.get())
->SetAllowedTouchBehavior(aInputBlockId, aFlags);
}
};
// preloaded BrowserChild should not be added to child map
if (mUniqueId) {
MOZ_ASSERT(NestedBrowserChildMap().find(mUniqueId) ==
NestedBrowserChildMap().end());
NestedBrowserChildMap()[mUniqueId] = this;
}
mCoalesceMouseMoveEvents =
Preferences::GetBool("dom.event.coalesce_mouse_move");
if (mCoalesceMouseMoveEvents) {
mCoalescedMouseEventFlusher = new CoalescedMouseMoveFlusher(this);
}
}
const CompositorOptions& BrowserChild::GetCompositorOptions() const {
// If you're calling this before mCompositorOptions is set, well.. don't.
MOZ_ASSERT(mCompositorOptions);
return mCompositorOptions.ref();
}
bool BrowserChild::AsyncPanZoomEnabled() const {
// This might get called by the TouchEvent::PrefEnabled code before we have
// mCompositorOptions populated (bug 1370089). In that case we just assume
// APZ is enabled because we're in a content process (because BrowserChild)
// and APZ is probably going to be enabled here since e10s is enabled.
return mCompositorOptions ? mCompositorOptions->UseAPZ() : true;
}
NS_IMETHODIMP
BrowserChild::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
if (!strcmp(aTopic, BEFORE_FIRST_PAINT)) {
if (AsyncPanZoomEnabled()) {
nsCOMPtr<Document> subject(do_QueryInterface(aSubject));
nsCOMPtr<Document> doc(GetTopLevelDocument());
if (subject == doc && doc->IsTopLevelContentDocument()) {
RefPtr<PresShell> presShell = doc->GetPresShell();
if (presShell) {
presShell->SetIsFirstPaint(true);
}
APZCCallbackHelper::InitializeRootDisplayport(presShell);
}
}
}
return NS_OK;
}
void BrowserChild::ContentReceivedInputBlock(uint64_t aInputBlockId,
bool aPreventDefault) const {
if (mApzcTreeManager) {
mApzcTreeManager->ContentReceivedInputBlock(aInputBlockId, aPreventDefault);
}
}
void BrowserChild::SetTargetAPZC(
uint64_t aInputBlockId,
const nsTArray<SLGuidAndRenderRoot>& aTargets) const {
if (mApzcTreeManager) {
mApzcTreeManager->SetTargetAPZC(aInputBlockId, aTargets);
}
}
void BrowserChild::SetAllowedTouchBehavior(
uint64_t aInputBlockId,
const nsTArray<TouchBehaviorFlags>& aTargets) const {
if (mApzcTreeManager) {
mApzcTreeManager->SetAllowedTouchBehavior(aInputBlockId, aTargets);
}
}
bool BrowserChild::DoUpdateZoomConstraints(
const uint32_t& aPresShellId, const ViewID& aViewId,
const Maybe<ZoomConstraints>& aConstraints) {
if (!mApzcTreeManager || mDestroyed) {
return false;
}
SLGuidAndRenderRoot guid = SLGuidAndRenderRoot(
mLayersId, aPresShellId, aViewId, gfxUtils::GetContentRenderRoot());
mApzcTreeManager->UpdateZoomConstraints(guid, aConstraints);
return true;
}
nsresult BrowserChild::Init(mozIDOMWindowProxy* aParent,
WindowGlobalChild* aInitialWindowChild) {
MOZ_DIAGNOSTIC_ASSERT(mTabGroup);
MOZ_ASSERT_IF(aInitialWindowChild,
aInitialWindowChild->BrowsingContext() == mBrowsingContext);
nsCOMPtr<nsIWidget> widget = nsIWidget::CreatePuppetWidget(this);
mPuppetWidget = static_cast<PuppetWidget*>(widget.get());
if (!mPuppetWidget) {
NS_ERROR("couldn't create fake widget");
return NS_ERROR_FAILURE;
}
mPuppetWidget->InfallibleCreate(nullptr,
nullptr, // no parents
LayoutDeviceIntRect(0, 0, 0, 0),
nullptr); // HandleWidgetEvent
mWebBrowser = nsWebBrowser::Create(this, mPuppetWidget, OriginAttributesRef(),
mBrowsingContext, aInitialWindowChild);
nsIWebBrowser* webBrowser = mWebBrowser;
mWebNav = do_QueryInterface(webBrowser);
NS_ASSERTION(mWebNav, "nsWebBrowser doesn't implement nsIWebNavigation?");
// Set the tab context attributes then pass to docShell
NotifyTabContextUpdated(false);
// IPC uses a WebBrowser object for which DNS prefetching is turned off
// by default. But here we really want it, so enable it explicitly
mWebBrowser->SetAllowDNSPrefetch(true);
nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
MOZ_ASSERT(docShell);
mStatusFilter = new nsBrowserStatusFilter();
RefPtr<nsIEventTarget> eventTarget =
TabGroup()->EventTargetFor(TaskCategory::Network);
mStatusFilter->SetTarget(eventTarget);
nsresult rv =
mStatusFilter->AddProgressListener(this, nsIWebProgress::NOTIFY_ALL);
NS_ENSURE_SUCCESS(rv, rv);
{
nsCOMPtr<nsIWebProgress> webProgress = do_QueryInterface(docShell);
rv = webProgress->AddProgressListener(mStatusFilter,
nsIWebProgress::NOTIFY_ALL);
NS_ENSURE_SUCCESS(rv, rv);
}
docShell->SetAffectPrivateSessionLifetime(
mChromeFlags & nsIWebBrowserChrome::CHROME_PRIVATE_LIFETIME);
nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(WebNavigation());
MOZ_ASSERT(loadContext);
loadContext->SetPrivateBrowsing(OriginAttributesRef().mPrivateBrowsingId > 0);
loadContext->SetRemoteTabs(mChromeFlags &
nsIWebBrowserChrome::CHROME_REMOTE_WINDOW);
loadContext->SetRemoteSubframes(mChromeFlags &
nsIWebBrowserChrome::CHROME_FISSION_WINDOW);
// Few lines before, baseWindow->Create() will end up creating a new
// window root in nsGlobalWindow::SetDocShell.
// Then this chrome event handler, will be inherited to inner windows.
// We want to also set it to the docshell so that inner windows
// and any code that has access to the docshell
// can all listen to the same chrome event handler.
// XXX: ideally, we would set a chrome event handler earlier,
// and all windows, even the root one, will use the docshell one.
nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
nsCOMPtr<EventTarget> chromeHandler = window->GetChromeEventHandler();
docShell->SetChromeEventHandler(chromeHandler);
if (window->GetCurrentInnerWindow()) {
window->SetKeyboardIndicators(ShowFocusRings());
} else {
// Skip ShouldShowFocusRing check if no inner window is available
window->SetInitialKeyboardIndicators(ShowFocusRings());
}
// Window scrollbar flags only affect top level remote frames, not fission
// frames.
if (mIsTopLevel) {
nsContentUtils::SetScrollbarsVisibility(
window->GetDocShell(),
!!(mChromeFlags & nsIWebBrowserChrome::CHROME_SCROLLBARS));
}
nsWeakPtr weakPtrThis = do_GetWeakReference(
static_cast<nsIBrowserChild*>(this)); // for capture by the lambda
ContentReceivedInputBlockCallback callback(
[weakPtrThis](uint64_t aInputBlockId, bool aPreventDefault) {
if (nsCOMPtr<nsIBrowserChild> browserChild =
do_QueryReferent(weakPtrThis)) {
static_cast<BrowserChild*>(browserChild.get())
->ContentReceivedInputBlock(aInputBlockId, aPreventDefault);
}
});
mAPZEventState = new APZEventState(mPuppetWidget, std::move(callback));
mIPCOpen = true;
// Recording/replaying processes use their own compositor.
if (recordreplay::IsRecordingOrReplaying()) {
mPuppetWidget->CreateCompositor();
}
#if !defined(MOZ_WIDGET_ANDROID) && !defined(MOZ_THUNDERBIRD) && \
!defined(MOZ_SUITE)
mSessionStoreListener = new TabListener(docShell, nullptr);
rv = mSessionStoreListener->Init();
NS_ENSURE_SUCCESS(rv, rv);
#endif
return NS_OK;
}
void BrowserChild::NotifyTabContextUpdated(bool aIsPreallocated) {
nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
MOZ_ASSERT(docShell);
if (!docShell) {
return;
}
UpdateFrameType();
if (aIsPreallocated) {
nsDocShell::Cast(docShell)->SetOriginAttributes(OriginAttributesRef());
}
// Set SANDBOXED_AUXILIARY_NAVIGATION flag if this is a receiver page.
if (!PresentationURL().IsEmpty()) {
mBrowsingContext->SetSandboxFlags(SANDBOXED_AUXILIARY_NAVIGATION);
}
}
void BrowserChild::UpdateFrameType() {
nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
MOZ_ASSERT(docShell);
// TODO: Bug 1252794 - remove frameType from nsIDocShell.idl
docShell->SetFrameType(IsMozBrowserElement()
? nsIDocShell::FRAME_TYPE_BROWSER
: nsIDocShell::FRAME_TYPE_REGULAR);
}
NS_IMPL_CYCLE_COLLECTION_CLASS(BrowserChild)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(BrowserChild)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBrowserChildMessageManager)
tmp->nsMessageManagerScriptExecutor::Unlink();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWebBrowserChrome)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mStatusFilter)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWebNav)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBrowsingContext)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(BrowserChild)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBrowserChildMessageManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWebBrowserChrome)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStatusFilter)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWebNav)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBrowsingContext)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(BrowserChild)
tmp->nsMessageManagerScriptExecutor::Trace(aCallbacks, aClosure);
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BrowserChild)
NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome)
NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow)
NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChromeFocus)
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
NS_INTERFACE_MAP_ENTRY(nsIWindowProvider)
NS_INTERFACE_MAP_ENTRY(nsIBrowserChild)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsITooltipListener)
NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener2)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIBrowserChild)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(BrowserChild)
NS_IMPL_CYCLE_COLLECTING_RELEASE(BrowserChild)
NS_IMETHODIMP
BrowserChild::GetChromeFlags(uint32_t* aChromeFlags) {
*aChromeFlags = mChromeFlags;
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::SetChromeFlags(uint32_t aChromeFlags) {
NS_WARNING("trying to SetChromeFlags from content process?");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
BrowserChild::RemoteSizeShellTo(int32_t aWidth, int32_t aHeight,
int32_t aShellItemWidth,
int32_t aShellItemHeight) {
nsCOMPtr<nsIDocShell> ourDocShell = do_GetInterface(WebNavigation());
nsCOMPtr<nsIBaseWindow> docShellAsWin(do_QueryInterface(ourDocShell));
NS_ENSURE_STATE(docShellAsWin);
int32_t width, height;
docShellAsWin->GetSize(&width, &height);
uint32_t flags = 0;
if (width == aWidth) {
flags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CX;
}
if (height == aHeight) {
flags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CY;
}
bool sent = SendSizeShellTo(flags, aWidth, aHeight, aShellItemWidth,
aShellItemHeight);
return sent ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
BrowserChild::RemoteDropLinks(
const nsTArray<RefPtr<nsIDroppedLinkItem>>& aLinks) {
nsTArray<nsString> linksArray;
nsresult rv = NS_OK;
for (nsIDroppedLinkItem* link : aLinks) {
nsString tmp;
rv = link->GetUrl(tmp);
if (NS_FAILED(rv)) {
return rv;
}
linksArray.AppendElement(tmp);
rv = link->GetName(tmp);
if (NS_FAILED(rv)) {
return rv;
}
linksArray.AppendElement(tmp);
rv = link->GetType(tmp);
if (NS_FAILED(rv)) {
return rv;
}
linksArray.AppendElement(tmp);
}
bool sent = SendDropLinks(linksArray);
return sent ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
BrowserChild::ShowAsModal() {
NS_WARNING("BrowserChild::ShowAsModal not supported in BrowserChild");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
BrowserChild::IsWindowModal(bool* aRetVal) {
*aRetVal = false;
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::SetLinkStatus(const nsAString& aStatusText) {
// We can only send the status after the ipc machinery is set up
if (IPCOpen()) {
SendSetLinkStatus(nsString(aStatusText));
}
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::SetDimensions(uint32_t aFlags, int32_t aX, int32_t aY,
int32_t aCx, int32_t aCy) {
// The parent is in charge of the dimension changes. If JS code wants to
// change the dimensions (moveTo, screenX, etc.) we send a message to the
// parent about the new requested dimension, the parent does the resize/move
// then send a message to the child to update itself. For APIs like screenX
// this function is called with the current value for the non-changed values.
// In a series of calls like window.screenX = 10; window.screenY = 10; for
// the second call, since screenX is not yet updated we might accidentally
// reset back screenX to it's old value. To avoid this if a parameter did not
// change we want the parent to ignore its value.
int32_t x, y, cx, cy;
GetDimensions(aFlags, &x, &y, &cx, &cy);
if (x == aX) {
aFlags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_X;
}
if (y == aY) {
aFlags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_Y;
}
if (cx == aCx) {
aFlags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CX;
}
if (cy == aCy) {
aFlags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CY;
}
double scale = mPuppetWidget ? mPuppetWidget->GetDefaultScale().scale : 1.0;
Unused << SendSetDimensions(aFlags, aX, aY, aCx, aCy, scale);
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::GetDimensions(uint32_t aFlags, int32_t* aX, int32_t* aY,
int32_t* aCx, int32_t* aCy) {
ScreenIntRect rect = GetOuterRect();
if (aX) {
*aX = rect.x;
}
if (aY) {
*aY = rect.y;
}
if (aCx) {
*aCx = rect.width;
}
if (aCy) {
*aCy = rect.height;
}
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::SetFocus() { return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
BrowserChild::GetVisibility(bool* aVisibility) {
*aVisibility = true;
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::SetVisibility(bool aVisibility) {
// should the platform support this? Bug 666365
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::GetTitle(nsAString& aTitle) {
NS_WARNING("BrowserChild::GetTitle not supported in BrowserChild");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
BrowserChild::SetTitle(const nsAString& aTitle) {
// JavaScript sends the "DOMTitleChanged" event to the parent
// via the message manager.
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::GetSiteWindow(void** aSiteWindow) {
NS_WARNING("BrowserChild::GetSiteWindow not supported in BrowserChild");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
BrowserChild::Blur() { return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
BrowserChild::FocusNextElement(bool aForDocumentNavigation) {
SendMoveFocus(true, aForDocumentNavigation);
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::FocusPrevElement(bool aForDocumentNavigation) {
SendMoveFocus(false, aForDocumentNavigation);
return NS_OK;
}
NS_IMETHODIMP
BrowserChild::GetInterface(const nsIID& aIID, void** aSink) {
if (aIID.Equals(NS_GET_IID(nsIWebBrowserChrome3))) {
return GetWebBrowserChrome(reinterpret_cast<nsIWebBrowserChrome3**>(aSink));
}
// XXXbz should we restrict the set of interfaces we hand out here?
// See bug 537429
return QueryInterface(aIID, aSink);
}
NS_IMETHODIMP
BrowserChild::ProvideWindow(mozIDOMWindowProxy* aParent, uint32_t aChromeFlags,
bool aCalledFromJS, bool aPositionSpecified,
bool aSizeSpecified, nsIURI* aURI,
const nsAString& aName, const nsACString& aFeatures,
bool aForceNoOpener, bool aForceNoReferrer,
nsDocShellLoadState* aLoadState, bool* aWindowIsNew,
BrowsingContext** aReturn) {
*aReturn = nullptr;
// If aParent is inside an <iframe mozbrowser> and this isn't a request to
// open a modal-type window, we're going to create a new <iframe mozbrowser>
// and return its window here.
nsCOMPtr<nsIDocShell> docshell = do_GetInterface(aParent);
bool iframeMoz =
(docshell && docshell->GetIsInMozBrowser() &&
!(aChromeFlags & (nsIWebBrowserChrome::CHROME_MODAL |
nsIWebBrowserChrome::CHROME_OPENAS_DIALOG |
nsIWebBrowserChrome::CHROME_OPENAS_CHROME)));
if (!iframeMoz) {
int32_t openLocation = nsWindowWatcher::GetWindowOpenLocation(
nsPIDOMWindowOuter::From(aParent), aChromeFlags, aCalledFromJS,
aPositionSpecified, aSizeSpecified);
// If it turns out we're opening in the current browser, just hand over the
// current browser's docshell.
if (openLocation == nsIBrowserDOMWindow::OPEN_CURRENTWINDOW) {
nsCOMPtr<nsIWebBrowser> browser = do_GetInterface(WebNavigation());
*aWindowIsNew = false;
nsCOMPtr<mozIDOMWindowProxy> win;
MOZ_TRY(browser->GetContentDOMWindow(getter_AddRefs(win)));
RefPtr<BrowsingContext> bc(
nsPIDOMWindowOuter::From(win)->GetBrowsingContext());
bc.forget(aReturn);
return NS_OK;
}
}
// Note that ProvideWindowCommon may return NS_ERROR_ABORT if the
// open window call was canceled. It's important that we pass this error
// code back to our caller.
ContentChild* cc = ContentChild::GetSingleton();
return cc->ProvideWindowCommon(
this, aParent, iframeMoz, aChromeFlags, aCalledFromJS, aPositionSpecified,
aSizeSpecified, aURI, aName, aFeatures, aForceNoOpener, aForceNoReferrer,
aLoadState, aWindowIsNew, aReturn);
}
void BrowserChild::DestroyWindow() {
mBrowsingContext = nullptr;
// TabGroups contain circular references to their event queues that they break
// when the last window leaves. If we never attached a window to our TabGroup,
// though, it will never see a window leave, and will therefore never break
// its circular references. If it hasn't had a window attached by now, it
// never will, so have it destroy itself now if it's empty.
mTabGroup->MaybeDestroy();
if (mStatusFilter) {
if (nsCOMPtr<nsIWebProgress> webProgress =
do_QueryInterface(WebNavigation())) {
webProgress->RemoveProgressListener(mStatusFilter);
}
mStatusFilter->RemoveProgressListener(this);
mStatusFilter = nullptr;
}
if (mCoalescedMouseEventFlusher) {
mCoalescedMouseEventFlusher->RemoveObserver();
mCoalescedMouseEventFlusher = nullptr;
}
if (mSessionStoreListener) {
mSessionStoreListener->RemoveListeners();
mSessionStoreListener = nullptr;
}
// In case we don't have chance to process all entries, clean all data in
// the queue.
while (mToBeDispatchedMouseData.GetSize() > 0) {
UniquePtr<CoalescedMouseData> data(
static_cast<CoalescedMouseData*>(mToBeDispatchedMouseData.PopFront()));
data.reset();
}
nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(WebNavigation());
if (baseWindow) baseWindow->Destroy();
if (mPuppetWidget) {
mPuppetWidget->Destroy();
}
mLayersConnected = Nothing();
if (mLayersId.IsValid()) {
StaticMutexAutoLock lock(sBrowserChildrenMutex);
MOZ_ASSERT(sBrowserChildren);
sBrowserChildren->Remove(uint64_t(mLayersId));
if (!sBrowserChildren->Count()) {
delete sBrowserChildren;
sBrowserChildren = nullptr;
}
mLayersId = layers::LayersId{0};
}
}
void BrowserChild::ActorDestroy(ActorDestroyReason why) {
mIPCOpen = false;