1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/BrowserHost.h"
9 #include "mozilla/Unused.h"
10 #include "mozilla/dom/CancelContentJSOptionsBinding.h"
11 #include "mozilla/dom/ContentParent.h"
12 #include "mozilla/dom/WindowGlobalParent.h"
13 #include "mozilla/ProcessPriorityManager.h"
15 #include "nsIObserverService.h"
17 namespace mozilla::dom
{
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BrowserHost
)
20 NS_INTERFACE_MAP_ENTRY(nsIRemoteTab
)
21 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference
)
22 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, RemoteBrowser
)
25 NS_IMPL_CYCLE_COLLECTION_WEAK(BrowserHost
, mRoot
)
27 NS_IMPL_CYCLE_COLLECTING_ADDREF(BrowserHost
)
28 NS_IMPL_CYCLE_COLLECTING_RELEASE(BrowserHost
)
30 BrowserHost::BrowserHost(BrowserParent
* aParent
)
31 : mId(aParent
->GetTabId()),
33 mEffectsInfo
{EffectsInfo::FullyHidden()} {
34 mRoot
->SetBrowserHost(this);
37 BrowserHost
* BrowserHost::GetFrom(nsIRemoteTab
* aRemoteTab
) {
38 return static_cast<BrowserHost
*>(aRemoteTab
);
41 TabId
BrowserHost::GetTabId() const { return mId
; }
43 mozilla::layers::LayersId
BrowserHost::GetLayersId() const {
44 return mRoot
->GetLayersId();
47 BrowsingContext
* BrowserHost::GetBrowsingContext() const {
48 return mRoot
->GetBrowsingContext();
51 nsILoadContext
* BrowserHost::GetLoadContext() const {
52 RefPtr
<nsILoadContext
> loadContext
= mRoot
->GetLoadContext();
56 bool BrowserHost::CanRecv() const { return mRoot
&& mRoot
->CanRecv(); }
58 a11y::DocAccessibleParent
* BrowserHost::GetTopLevelDocAccessible() const {
59 return mRoot
? mRoot
->GetTopLevelDocAccessible() : nullptr;
62 void BrowserHost::LoadURL(nsDocShellLoadState
* aLoadState
) {
63 MOZ_ASSERT(aLoadState
);
64 mRoot
->LoadURL(aLoadState
);
67 void BrowserHost::ResumeLoad(uint64_t aPendingSwitchId
) {
68 mRoot
->ResumeLoad(aPendingSwitchId
);
71 void BrowserHost::DestroyStart() { mRoot
->Destroy(); }
73 void BrowserHost::DestroyComplete() {
77 mRoot
->SetOwnerElement(nullptr);
79 mRoot
->SetBrowserHost(nullptr);
82 nsCOMPtr
<nsIObserverService
> os
= services::GetObserverService();
84 os
->NotifyObservers(NS_ISUPPORTS_CAST(nsIRemoteTab
*, this),
85 "ipc:browser-destroyed", nullptr);
89 bool BrowserHost::Show(const OwnerShowInfo
& aShowInfo
) {
90 return mRoot
->Show(aShowInfo
);
93 void BrowserHost::UpdateDimensions(const nsIntRect
& aRect
,
94 const ScreenIntSize
& aSize
) {
95 mRoot
->UpdateDimensions(aRect
, aSize
);
98 void BrowserHost::UpdateEffects(EffectsInfo aEffects
) {
99 if (!mRoot
|| mEffectsInfo
== aEffects
) {
102 mEffectsInfo
= aEffects
;
103 Unused
<< mRoot
->SendUpdateEffects(mEffectsInfo
);
106 /* attribute boolean renderLayers; */
108 BrowserHost::GetRenderLayers(bool* aRenderLayers
) {
110 *aRenderLayers
= false;
113 *aRenderLayers
= mRoot
->GetRenderLayers();
118 BrowserHost::SetRenderLayers(bool aRenderLayers
) {
123 mRoot
->SetRenderLayers(aRenderLayers
);
127 /* readonly attribute boolean hasLayers; */
129 BrowserHost::GetHasLayers(bool* aHasLayers
) {
130 *aHasLayers
= mRoot
&& mRoot
->GetHasLayers();
134 /* attribute boolean priorityHint; */
136 BrowserHost::SetPriorityHint(bool aPriorityHint
) {
140 mRoot
->SetPriorityHint(aPriorityHint
);
145 BrowserHost::GetPriorityHint(bool* aPriorityHint
) {
146 *aPriorityHint
= mRoot
&& mRoot
->GetPriorityHint();
150 /* void resolutionChanged (); */
152 BrowserHost::NotifyResolutionChanged() {
156 VisitAll([](BrowserParent
* aBrowserParent
) {
157 aBrowserParent
->NotifyResolutionChanged();
162 /* void deprioritize (); */
164 BrowserHost::Deprioritize() {
168 auto* bc
= GetBrowsingContext()->Canonical();
169 ProcessPriorityManager::BrowserPriorityChanged(bc
,
170 /* aPriority = */ false);
174 /* void preserveLayers (in boolean aPreserveLayers); */
176 BrowserHost::PreserveLayers(bool aPreserveLayers
) {
180 VisitAll([&](BrowserParent
* aBrowserParent
) {
181 aBrowserParent
->PreserveLayers(aPreserveLayers
);
186 /* readonly attribute uint64_t tabId; */
188 BrowserHost::GetTabId(uint64_t* aTabId
) {
193 /* readonly attribute uint64_t contentProcessId; */
195 BrowserHost::GetContentProcessId(uint64_t* aContentProcessId
) {
197 *aContentProcessId
= 0;
200 *aContentProcessId
= GetContentParent()->ChildID();
204 /* readonly attribute int32_t osPid; */
206 BrowserHost::GetOsPid(int32_t* aOsPid
) {
211 *aOsPid
= GetContentParent()->Pid();
215 /* readonly attribute BrowsingContext browsingContext; */
217 BrowserHost::GetBrowsingContext(BrowsingContext
** aBc
) {
222 RefPtr
<BrowsingContext
> bc
= mRoot
->GetBrowsingContext();
227 /* readonly attribute boolean hasPresented; */
229 BrowserHost::GetHasPresented(bool* aHasPresented
) {
231 *aHasPresented
= false;
234 *aHasPresented
= mRoot
->GetHasPresented();
238 /* void transmitPermissionsForPrincipal (in nsIPrincipal aPrincipal); */
240 BrowserHost::TransmitPermissionsForPrincipal(nsIPrincipal
* aPrincipal
) {
244 return GetContentParent()->TransmitPermissionsForPrincipal(aPrincipal
);
247 /* void createAboutBlankContentViewer(in nsIPrincipal aPrincipal, in
248 * nsIPrincipal aPartitionedPrincipal); */
250 BrowserHost::CreateAboutBlankContentViewer(
251 nsIPrincipal
* aPrincipal
, nsIPrincipal
* aPartitionedPrincipal
) {
256 // Ensure the content process has permisisons for the new document we're about
258 nsresult rv
= GetContentParent()->TransmitPermissionsForPrincipal(aPrincipal
);
263 Unused
<< mRoot
->SendCreateAboutBlankContentViewer(aPrincipal
,
264 aPartitionedPrincipal
);
269 BrowserHost::MaybeCancelContentJSExecutionFromScript(
270 nsIRemoteTab::NavigationType aNavigationType
,
271 JS::Handle
<JS::Value
> aCancelContentJSOptions
, JSContext
* aCx
) {
272 // If we're in the process of creating a new window (via window.open), then
273 // the load that called this function isn't a "normal" load and should be
274 // ignored for the purposes of cancelling content JS.
275 if (!mRoot
|| mRoot
->CreatingWindow()) {
278 dom::CancelContentJSOptions cancelContentJSOptions
;
279 if (!cancelContentJSOptions
.Init(aCx
, aCancelContentJSOptions
)) {
280 return NS_ERROR_INVALID_ARG
;
282 GetContentParent()->CancelContentJSExecutionIfRunning(mRoot
, aNavigationType
,
283 cancelContentJSOptions
);
287 } // namespace mozilla::dom