1 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
8 #include "nsChromeTreeOwner.h"
13 #include "nsIDocShellTreeItem.h"
15 // Interfaces needed to include
16 #include "nsIPrompt.h"
17 #include "nsIAuthPrompt.h"
18 #include "nsIWebProgress.h"
19 #include "nsIWidget.h"
20 #include "mozilla/Try.h"
21 #include "mozilla/dom/Element.h"
23 using namespace mozilla
;
25 //*****************************************************************************
26 // nsChromeTreeOwner string literals
27 //*****************************************************************************
29 const nsLiteralString
kPersist(u
"persist");
30 const nsLiteralString
kScreenX(u
"screenX");
31 const nsLiteralString
kScreenY(u
"screenY");
32 const nsLiteralString
kWidth(u
"width");
33 const nsLiteralString
kHeight(u
"height");
34 const nsLiteralString
kSizemode(u
"sizemode");
35 const nsLiteralString
kSpace(u
" ");
37 //*****************************************************************************
38 //*** nsChromeTreeOwner: Object Management
39 //*****************************************************************************
41 nsChromeTreeOwner::nsChromeTreeOwner() : mAppWindow(nullptr) {}
43 nsChromeTreeOwner::~nsChromeTreeOwner() {}
45 //*****************************************************************************
46 // nsChromeTreeOwner::nsISupports
47 //*****************************************************************************
49 NS_IMPL_ADDREF(nsChromeTreeOwner
)
50 NS_IMPL_RELEASE(nsChromeTreeOwner
)
52 NS_INTERFACE_MAP_BEGIN(nsChromeTreeOwner
)
53 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIDocShellTreeOwner
)
54 NS_INTERFACE_MAP_ENTRY(nsIDocShellTreeOwner
)
55 NS_INTERFACE_MAP_ENTRY(nsIBaseWindow
)
56 NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener
)
57 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor
)
58 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference
)
61 //*****************************************************************************
62 // nsChromeTreeOwner::nsIInterfaceRequestor
63 //*****************************************************************************
65 NS_IMETHODIMP
nsChromeTreeOwner::GetInterface(const nsIID
& aIID
, void** aSink
) {
66 NS_ENSURE_ARG_POINTER(aSink
);
68 if (aIID
.Equals(NS_GET_IID(nsIPrompt
))) {
69 NS_ENSURE_STATE(mAppWindow
);
70 return mAppWindow
->GetInterface(aIID
, aSink
);
72 if (aIID
.Equals(NS_GET_IID(nsIAuthPrompt
))) {
73 NS_ENSURE_STATE(mAppWindow
);
74 return mAppWindow
->GetInterface(aIID
, aSink
);
76 if (aIID
.Equals(NS_GET_IID(nsIWebBrowserChrome
))) {
77 NS_ENSURE_STATE(mAppWindow
);
78 return mAppWindow
->GetInterface(aIID
, aSink
);
80 if (aIID
.Equals(NS_GET_IID(nsIAppWindow
))) {
81 NS_ENSURE_STATE(mAppWindow
);
82 return mAppWindow
->QueryInterface(aIID
, aSink
);
85 return QueryInterface(aIID
, aSink
);
88 //*****************************************************************************
89 // nsChromeTreeOwner::nsIDocShellTreeOwner
90 //*****************************************************************************
93 nsChromeTreeOwner::ContentShellAdded(nsIDocShellTreeItem
* aContentShell
,
95 NS_ENSURE_STATE(mAppWindow
);
96 return mAppWindow
->ContentShellAdded(aContentShell
, aPrimary
);
100 nsChromeTreeOwner::ContentShellRemoved(nsIDocShellTreeItem
* aContentShell
) {
101 NS_ENSURE_STATE(mAppWindow
);
102 return mAppWindow
->ContentShellRemoved(aContentShell
);
105 NS_IMETHODIMP
nsChromeTreeOwner::GetPrimaryContentShell(
106 nsIDocShellTreeItem
** aShell
) {
107 NS_ENSURE_STATE(mAppWindow
);
108 return mAppWindow
->GetPrimaryContentShell(aShell
);
112 nsChromeTreeOwner::RemoteTabAdded(nsIRemoteTab
* aTab
, bool aPrimary
) {
113 NS_ENSURE_STATE(mAppWindow
);
114 return mAppWindow
->RemoteTabAdded(aTab
, aPrimary
);
118 nsChromeTreeOwner::RemoteTabRemoved(nsIRemoteTab
* aTab
) {
119 NS_ENSURE_STATE(mAppWindow
);
120 return mAppWindow
->RemoteTabRemoved(aTab
);
124 nsChromeTreeOwner::GetPrimaryRemoteTab(nsIRemoteTab
** aTab
) {
125 NS_ENSURE_STATE(mAppWindow
);
126 return mAppWindow
->GetPrimaryRemoteTab(aTab
);
130 nsChromeTreeOwner::GetPrimaryContentBrowsingContext(
131 mozilla::dom::BrowsingContext
** aBc
) {
132 NS_ENSURE_STATE(mAppWindow
);
133 return mAppWindow
->GetPrimaryContentBrowsingContext(aBc
);
137 nsChromeTreeOwner::GetPrimaryContentSize(int32_t* aWidth
, int32_t* aHeight
) {
138 NS_ENSURE_STATE(mAppWindow
);
139 return mAppWindow
->GetPrimaryContentSize(aWidth
, aHeight
);
143 nsChromeTreeOwner::SetPrimaryContentSize(int32_t aWidth
, int32_t aHeight
) {
144 NS_ENSURE_STATE(mAppWindow
);
145 return mAppWindow
->SetPrimaryContentSize(aWidth
, aHeight
);
149 nsChromeTreeOwner::GetRootShellSize(int32_t* aWidth
, int32_t* aHeight
) {
150 NS_ENSURE_STATE(mAppWindow
);
151 return mAppWindow
->GetRootShellSize(aWidth
, aHeight
);
155 nsChromeTreeOwner::SetRootShellSize(int32_t aWidth
, int32_t aHeight
) {
156 NS_ENSURE_STATE(mAppWindow
);
157 return mAppWindow
->SetRootShellSize(aWidth
, aHeight
);
160 NS_IMETHODIMP
nsChromeTreeOwner::SizeShellTo(nsIDocShellTreeItem
* aShellItem
,
161 int32_t aCX
, int32_t aCY
) {
162 NS_ENSURE_STATE(mAppWindow
);
163 return mAppWindow
->SizeShellTo(aShellItem
, aCX
, aCY
);
167 nsChromeTreeOwner::SetPersistence(bool aPersistPosition
, bool aPersistSize
,
168 bool aPersistSizeMode
) {
169 NS_ENSURE_STATE(mAppWindow
);
170 nsCOMPtr
<dom::Element
> docShellElement
= mAppWindow
->GetWindowDOMElement();
171 if (!docShellElement
) return NS_ERROR_FAILURE
;
173 nsAutoString persistString
;
174 docShellElement
->GetAttribute(kPersist
, persistString
);
176 bool saveString
= false;
179 #define FIND_PERSIST_STRING(aString, aCond) \
180 index = persistString.Find(aString); \
181 if (!aCond && index > kNotFound) { \
182 persistString.Cut(index, aString.Length()); \
184 } else if (aCond && index == kNotFound) { \
185 persistString.Append(kSpace + aString); \
188 FIND_PERSIST_STRING(kScreenX
, aPersistPosition
);
189 FIND_PERSIST_STRING(kScreenY
, aPersistPosition
);
190 FIND_PERSIST_STRING(kWidth
, aPersistSize
);
191 FIND_PERSIST_STRING(kHeight
, aPersistSize
);
192 FIND_PERSIST_STRING(kSizemode
, aPersistSizeMode
);
196 docShellElement
->SetAttribute(kPersist
, persistString
, rv
);
203 nsChromeTreeOwner::GetPersistence(bool* aPersistPosition
, bool* aPersistSize
,
204 bool* aPersistSizeMode
) {
205 NS_ENSURE_STATE(mAppWindow
);
206 nsCOMPtr
<dom::Element
> docShellElement
= mAppWindow
->GetWindowDOMElement();
207 if (!docShellElement
) return NS_ERROR_FAILURE
;
209 nsAutoString persistString
;
210 docShellElement
->GetAttribute(kPersist
, persistString
);
212 // data structure doesn't quite match the question, but it's close enough
213 // for what we want (since this method is never actually called...)
214 if (aPersistPosition
)
215 *aPersistPosition
= persistString
.Find(kScreenX
) > kNotFound
||
216 persistString
.Find(kScreenY
) > kNotFound
;
218 *aPersistSize
= persistString
.Find(kWidth
) > kNotFound
||
219 persistString
.Find(kHeight
) > kNotFound
;
220 if (aPersistSizeMode
)
221 *aPersistSizeMode
= persistString
.Find(kSizemode
) > kNotFound
;
227 nsChromeTreeOwner::GetTabCount(uint32_t* aResult
) {
229 return mAppWindow
->GetTabCount(aResult
);
237 nsChromeTreeOwner::GetHasPrimaryContent(bool* aResult
) {
238 NS_ENSURE_STATE(mAppWindow
);
239 return mAppWindow
->GetHasPrimaryContent(aResult
);
242 //*****************************************************************************
243 // nsChromeTreeOwner::nsIBaseWindow
244 //*****************************************************************************
246 NS_IMETHODIMP
nsChromeTreeOwner::InitWindow(nativeWindow aParentNativeWindow
,
247 nsIWidget
* parentWidget
, int32_t x
,
248 int32_t y
, int32_t cx
, int32_t cy
) {
249 // Ignore widget parents for now. Don't think those are a vaild thing to
251 NS_ENSURE_SUCCESS(SetPositionAndSize(x
, y
, cx
, cy
, 0), NS_ERROR_FAILURE
);
256 NS_IMETHODIMP
nsChromeTreeOwner::Destroy() {
257 NS_ENSURE_STATE(mAppWindow
);
258 return mAppWindow
->Destroy();
261 double nsChromeTreeOwner::GetWidgetCSSToDeviceScale() {
262 return mAppWindow
? mAppWindow
->GetWidgetCSSToDeviceScale() : 1.0;
265 NS_IMETHODIMP
nsChromeTreeOwner::GetDevicePixelsPerDesktopPixel(
267 NS_ENSURE_STATE(mAppWindow
);
268 return mAppWindow
->GetDevicePixelsPerDesktopPixel(aScale
);
271 NS_IMETHODIMP
nsChromeTreeOwner::SetPositionDesktopPix(int32_t x
, int32_t y
) {
272 NS_ENSURE_STATE(mAppWindow
);
273 return mAppWindow
->SetPositionDesktopPix(x
, y
);
276 NS_IMETHODIMP
nsChromeTreeOwner::SetPosition(int32_t x
, int32_t y
) {
277 NS_ENSURE_STATE(mAppWindow
);
278 return mAppWindow
->SetPosition(x
, y
);
281 NS_IMETHODIMP
nsChromeTreeOwner::GetPosition(int32_t* x
, int32_t* y
) {
282 NS_ENSURE_STATE(mAppWindow
);
283 return mAppWindow
->GetPosition(x
, y
);
286 NS_IMETHODIMP
nsChromeTreeOwner::SetSize(int32_t cx
, int32_t cy
,
288 NS_ENSURE_STATE(mAppWindow
);
289 return mAppWindow
->SetSize(cx
, cy
, fRepaint
);
292 NS_IMETHODIMP
nsChromeTreeOwner::GetSize(int32_t* cx
, int32_t* cy
) {
293 NS_ENSURE_STATE(mAppWindow
);
294 return mAppWindow
->GetSize(cx
, cy
);
297 NS_IMETHODIMP
nsChromeTreeOwner::SetPositionAndSize(int32_t x
, int32_t y
,
298 int32_t cx
, int32_t cy
,
300 NS_ENSURE_STATE(mAppWindow
);
301 return mAppWindow
->SetPositionAndSize(x
, y
, cx
, cy
, aFlags
);
304 NS_IMETHODIMP
nsChromeTreeOwner::GetPositionAndSize(int32_t* x
, int32_t* y
,
305 int32_t* cx
, int32_t* cy
) {
306 NS_ENSURE_STATE(mAppWindow
);
307 return mAppWindow
->GetPositionAndSize(x
, y
, cx
, cy
);
311 nsChromeTreeOwner::SetDimensions(DimensionRequest
&& aRequest
) {
312 NS_ENSURE_STATE(mAppWindow
);
313 if (aRequest
.mDimensionKind
== DimensionKind::Outer
) {
314 return mAppWindow
->SetDimensions(std::move(aRequest
));
317 MOZ_TRY(aRequest
.SupplementFrom(this));
318 return aRequest
.ApplyInnerTo(this, /* aAsRootShell */ true);
322 nsChromeTreeOwner::GetDimensions(DimensionKind aDimensionKind
, int32_t* aX
,
323 int32_t* aY
, int32_t* aCX
, int32_t* aCY
) {
324 NS_ENSURE_STATE(mAppWindow
);
325 if (aDimensionKind
== DimensionKind::Outer
) {
326 return mAppWindow
->GetDimensions(aDimensionKind
, aX
, aY
, aCX
, aCY
);
329 return NS_ERROR_NOT_IMPLEMENTED
;
331 return GetRootShellSize(aCX
, aCY
);
334 NS_IMETHODIMP
nsChromeTreeOwner::Repaint(bool aForce
) {
335 NS_ENSURE_STATE(mAppWindow
);
336 return mAppWindow
->Repaint(aForce
);
339 NS_IMETHODIMP
nsChromeTreeOwner::GetParentWidget(nsIWidget
** aParentWidget
) {
340 NS_ENSURE_STATE(mAppWindow
);
341 return mAppWindow
->GetParentWidget(aParentWidget
);
344 NS_IMETHODIMP
nsChromeTreeOwner::SetParentWidget(nsIWidget
* aParentWidget
) {
345 NS_ASSERTION(false, "You can't call this");
346 return NS_ERROR_NOT_IMPLEMENTED
;
349 NS_IMETHODIMP
nsChromeTreeOwner::GetParentNativeWindow(
350 nativeWindow
* aParentNativeWindow
) {
351 NS_ENSURE_STATE(mAppWindow
);
352 return mAppWindow
->GetParentNativeWindow(aParentNativeWindow
);
355 NS_IMETHODIMP
nsChromeTreeOwner::SetParentNativeWindow(
356 nativeWindow aParentNativeWindow
) {
357 NS_ASSERTION(false, "You can't call this");
358 return NS_ERROR_NOT_IMPLEMENTED
;
361 NS_IMETHODIMP
nsChromeTreeOwner::GetNativeHandle(nsAString
& aNativeHandle
) {
362 NS_ENSURE_STATE(mAppWindow
);
363 return mAppWindow
->GetNativeHandle(aNativeHandle
);
366 NS_IMETHODIMP
nsChromeTreeOwner::GetVisibility(bool* aVisibility
) {
367 NS_ENSURE_STATE(mAppWindow
);
368 return mAppWindow
->GetVisibility(aVisibility
);
371 NS_IMETHODIMP
nsChromeTreeOwner::SetVisibility(bool aVisibility
) {
372 NS_ENSURE_STATE(mAppWindow
);
373 return mAppWindow
->SetVisibility(aVisibility
);
376 NS_IMETHODIMP
nsChromeTreeOwner::GetEnabled(bool* aEnabled
) {
377 NS_ENSURE_STATE(mAppWindow
);
378 return mAppWindow
->GetEnabled(aEnabled
);
381 NS_IMETHODIMP
nsChromeTreeOwner::SetEnabled(bool aEnable
) {
382 NS_ENSURE_STATE(mAppWindow
);
383 return mAppWindow
->SetEnabled(aEnable
);
386 NS_IMETHODIMP
nsChromeTreeOwner::GetMainWidget(nsIWidget
** aMainWidget
) {
387 NS_ENSURE_ARG_POINTER(aMainWidget
);
388 NS_ENSURE_STATE(mAppWindow
);
390 *aMainWidget
= mAppWindow
->mWindow
;
391 NS_IF_ADDREF(*aMainWidget
);
396 NS_IMETHODIMP
nsChromeTreeOwner::GetTitle(nsAString
& aTitle
) {
397 NS_ENSURE_STATE(mAppWindow
);
398 return mAppWindow
->GetTitle(aTitle
);
401 NS_IMETHODIMP
nsChromeTreeOwner::SetTitle(const nsAString
& aTitle
) {
402 NS_ENSURE_STATE(mAppWindow
);
403 return mAppWindow
->SetTitle(aTitle
);
406 //*****************************************************************************
407 // nsChromeTreeOwner::nsIWebProgressListener
408 //*****************************************************************************
411 nsChromeTreeOwner::OnProgressChange(nsIWebProgress
* aWebProgress
,
412 nsIRequest
* aRequest
,
413 int32_t aCurSelfProgress
,
414 int32_t aMaxSelfProgress
,
415 int32_t aCurTotalProgress
,
416 int32_t aMaxTotalProgress
) {
421 nsChromeTreeOwner::OnStateChange(nsIWebProgress
* aWebProgress
,
422 nsIRequest
* aRequest
,
423 uint32_t aProgressStateFlags
,
429 nsChromeTreeOwner::OnLocationChange(nsIWebProgress
* aWebProgress
,
430 nsIRequest
* aRequest
, nsIURI
* aLocation
,
432 NS_ENSURE_STATE(mAppWindow
);
434 // If loading a new root .xul document, then redo chrome.
436 nsCOMPtr
<nsIDocShell
> docshell
;
437 mAppWindow
->GetDocShell(getter_AddRefs(docshell
));
439 nsCOMPtr
<nsIWebProgress
> webProgress(do_QueryInterface(docshell
));
440 if (webProgress
!= aWebProgress
) {
445 mAppWindow
->mChromeLoaded
= false;
450 nsChromeTreeOwner::OnStatusChange(nsIWebProgress
* aWebProgress
,
451 nsIRequest
* aRequest
, nsresult aStatus
,
452 const char16_t
* aMessage
) {
457 nsChromeTreeOwner::OnSecurityChange(nsIWebProgress
* aWebProgress
,
458 nsIRequest
* aRequest
, uint32_t aState
) {
463 nsChromeTreeOwner::OnContentBlockingEvent(nsIWebProgress
* aWebProgress
,
464 nsIRequest
* aRequest
,
469 //*****************************************************************************
470 // nsChromeTreeOwner: Helpers
471 //*****************************************************************************
473 //*****************************************************************************
474 // nsChromeTreeOwner: Accessors
475 //*****************************************************************************
477 void nsChromeTreeOwner::AppWindow(mozilla::AppWindow
* aAppWindow
) {
478 mAppWindow
= aAppWindow
;
481 mozilla::AppWindow
* nsChromeTreeOwner::AppWindow() { return mAppWindow
; }