Backed out changeset 62f7af8fe549 (bug 1843981) for causing valgrind bustage. CLOSED...
[gecko.git] / dom / ipc / BrowserHost.cpp
blob0b24db74d3594bcf15d578492c7a96178af29ada
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)
23 NS_INTERFACE_MAP_END
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()),
32 mRoot(aParent),
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();
53 return loadContext;
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() {
74 if (!mRoot) {
75 return;
77 mRoot->SetOwnerElement(nullptr);
78 mRoot->Destroy();
79 mRoot->SetBrowserHost(nullptr);
80 mRoot = nullptr;
82 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
83 if (os) {
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) {
100 return;
102 mEffectsInfo = aEffects;
103 Unused << mRoot->SendUpdateEffects(mEffectsInfo);
106 /* attribute boolean renderLayers; */
107 NS_IMETHODIMP
108 BrowserHost::GetRenderLayers(bool* aRenderLayers) {
109 if (!mRoot) {
110 *aRenderLayers = false;
111 return NS_OK;
113 *aRenderLayers = mRoot->GetRenderLayers();
114 return NS_OK;
117 NS_IMETHODIMP
118 BrowserHost::SetRenderLayers(bool aRenderLayers) {
119 if (!mRoot) {
120 return NS_OK;
123 mRoot->SetRenderLayers(aRenderLayers);
124 return NS_OK;
127 /* readonly attribute boolean hasLayers; */
128 NS_IMETHODIMP
129 BrowserHost::GetHasLayers(bool* aHasLayers) {
130 *aHasLayers = mRoot && mRoot->GetHasLayers();
131 return NS_OK;
134 /* attribute boolean priorityHint; */
135 NS_IMETHODIMP
136 BrowserHost::SetPriorityHint(bool aPriorityHint) {
137 if (!mRoot) {
138 return NS_OK;
140 mRoot->SetPriorityHint(aPriorityHint);
141 return NS_OK;
144 NS_IMETHODIMP
145 BrowserHost::GetPriorityHint(bool* aPriorityHint) {
146 *aPriorityHint = mRoot && mRoot->GetPriorityHint();
147 return NS_OK;
150 /* void resolutionChanged (); */
151 NS_IMETHODIMP
152 BrowserHost::NotifyResolutionChanged() {
153 if (!mRoot) {
154 return NS_OK;
156 VisitAll([](BrowserParent* aBrowserParent) {
157 aBrowserParent->NotifyResolutionChanged();
159 return NS_OK;
162 /* void deprioritize (); */
163 NS_IMETHODIMP
164 BrowserHost::Deprioritize() {
165 if (!mRoot) {
166 return NS_OK;
168 auto* bc = GetBrowsingContext()->Canonical();
169 ProcessPriorityManager::BrowserPriorityChanged(bc,
170 /* aPriority = */ false);
171 return NS_OK;
174 /* void preserveLayers (in boolean aPreserveLayers); */
175 NS_IMETHODIMP
176 BrowserHost::PreserveLayers(bool aPreserveLayers) {
177 if (!mRoot) {
178 return NS_OK;
180 VisitAll([&](BrowserParent* aBrowserParent) {
181 aBrowserParent->PreserveLayers(aPreserveLayers);
183 return NS_OK;
186 /* readonly attribute uint64_t tabId; */
187 NS_IMETHODIMP
188 BrowserHost::GetTabId(uint64_t* aTabId) {
189 *aTabId = mId;
190 return NS_OK;
193 /* readonly attribute uint64_t contentProcessId; */
194 NS_IMETHODIMP
195 BrowserHost::GetContentProcessId(uint64_t* aContentProcessId) {
196 if (!mRoot) {
197 *aContentProcessId = 0;
198 return NS_OK;
200 *aContentProcessId = GetContentParent()->ChildID();
201 return NS_OK;
204 /* readonly attribute int32_t osPid; */
205 NS_IMETHODIMP
206 BrowserHost::GetOsPid(int32_t* aOsPid) {
207 if (!mRoot) {
208 *aOsPid = 0;
209 return NS_OK;
211 *aOsPid = GetContentParent()->Pid();
212 return NS_OK;
215 /* readonly attribute BrowsingContext browsingContext; */
216 NS_IMETHODIMP
217 BrowserHost::GetBrowsingContext(BrowsingContext** aBc) {
218 if (!mRoot) {
219 *aBc = nullptr;
220 return NS_OK;
222 RefPtr<BrowsingContext> bc = mRoot->GetBrowsingContext();
223 bc.forget(aBc);
224 return NS_OK;
227 /* readonly attribute boolean hasPresented; */
228 NS_IMETHODIMP
229 BrowserHost::GetHasPresented(bool* aHasPresented) {
230 if (!mRoot) {
231 *aHasPresented = false;
232 return NS_OK;
234 *aHasPresented = mRoot->GetHasPresented();
235 return NS_OK;
238 /* void transmitPermissionsForPrincipal (in nsIPrincipal aPrincipal); */
239 NS_IMETHODIMP
240 BrowserHost::TransmitPermissionsForPrincipal(nsIPrincipal* aPrincipal) {
241 if (!mRoot) {
242 return NS_OK;
244 return GetContentParent()->TransmitPermissionsForPrincipal(aPrincipal);
247 /* void createAboutBlankContentViewer(in nsIPrincipal aPrincipal, in
248 * nsIPrincipal aPartitionedPrincipal); */
249 NS_IMETHODIMP
250 BrowserHost::CreateAboutBlankContentViewer(
251 nsIPrincipal* aPrincipal, nsIPrincipal* aPartitionedPrincipal) {
252 if (!mRoot) {
253 return NS_OK;
256 // Ensure the content process has permisisons for the new document we're about
257 // to create in it.
258 nsresult rv = GetContentParent()->TransmitPermissionsForPrincipal(aPrincipal);
259 if (NS_FAILED(rv)) {
260 return rv;
263 Unused << mRoot->SendCreateAboutBlankContentViewer(aPrincipal,
264 aPartitionedPrincipal);
265 return NS_OK;
268 NS_IMETHODIMP
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()) {
276 return NS_OK;
278 dom::CancelContentJSOptions cancelContentJSOptions;
279 if (!cancelContentJSOptions.Init(aCx, aCancelContentJSOptions)) {
280 return NS_ERROR_INVALID_ARG;
282 GetContentParent()->CancelContentJSExecutionIfRunning(mRoot, aNavigationType,
283 cancelContentJSOptions);
284 return NS_OK;
287 } // namespace mozilla::dom