Bug 1453788 - Allow top level HTML windows to have persistent window state. r?smaug
Move tracking of persistent window state into nsXULWindow. Also, move
special handling of the width/height of the window into nsXULWindow.
MozReview-Commit-ID: LOmHGyYeNSU
--- a/dom/xul/XULDocument.cpp
+++ b/dom/xul/XULDocument.cpp
@@ -1156,51 +1156,16 @@ XULDocument::Persist(const nsAString& aI
}
nameSpaceID = kNameSpaceID_None;
}
aRv = Persist(element, nameSpaceID, tag);
}
-enum class ConversionDirection {
- InnerToOuter,
- OuterToInner,
-};
-
-static void
-ConvertWindowSize(nsIXULWindow* aWin,
- nsAtom* aAttr,
- ConversionDirection aDirection,
- nsAString& aInOutString)
-{
- MOZ_ASSERT(aWin);
- MOZ_ASSERT(aAttr == nsGkAtoms::width || aAttr == nsGkAtoms::height);
-
- nsresult rv;
- int32_t size = aInOutString.ToInteger(&rv);
- if (NS_FAILED(rv)) {
- return;
- }
-
- int32_t sizeDiff = aAttr == nsGkAtoms::width
- ? aWin->GetOuterToInnerWidthDifferenceInCSSPixels()
- : aWin->GetOuterToInnerHeightDifferenceInCSSPixels();
-
- if (!sizeDiff) {
- return;
- }
-
- int32_t multiplier =
- aDirection == ConversionDirection::InnerToOuter ? 1 : - 1;
-
- CopyASCIItoUTF16(nsPrintfCString("%d", size + multiplier * sizeDiff),
- aInOutString);
-}
-
nsresult
XULDocument::Persist(Element* aElement, int32_t aNameSpaceID,
nsAtom* aAttribute)
{
// For non-chrome documents, persistance is simply broken
if (!nsContentUtils::IsSystemPrincipal(NodePrincipal()))
return NS_ERROR_NOT_AVAILABLE;
@@ -1231,26 +1196,19 @@ XULDocument::Persist(Element* aElement,
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (hasAttr && valuestr.IsEmpty()) {
return mLocalStore->RemoveValue(uri, id, attrstr);
}
- // Make sure we store the <window> attributes as outer window size, see
- // bug 1444525 & co.
- if (aElement->IsXULElement(nsGkAtoms::window) &&
- (aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height)) {
- if (nsCOMPtr<nsIXULWindow> win = GetXULWindowIfToplevelChrome()) {
- ConvertWindowSize(win,
- aAttribute,
- ConversionDirection::InnerToOuter,
- valuestr);
- }
+ // Persisting attributes to windows is handled by nsXULWindow.
+ if (aElement->IsXULElement(nsGkAtoms::window)) {
+ return NS_OK;
}
return mLocalStore->SetValue(uri, id, attrstr, valuestr);
}
nsresult
XULDocument::GetViewportSize(int32_t* aWidth,
@@ -1832,31 +1790,19 @@ XULDocument::ApplyPersistentAttributesTo
uint32_t cnt = aElements.Count();
for (int32_t i = int32_t(cnt) - 1; i >= 0; --i) {
RefPtr<Element> element = aElements.SafeObjectAt(i);
if (!element) {
continue;
}
- // Convert attributes from outer size to inner size for top-level
- // windows, see bug 1444525 & co.
- if (element->IsXULElement(nsGkAtoms::window) &&
- (attr == nsGkAtoms::width || attr == nsGkAtoms::height)) {
- if (nsCOMPtr<nsIXULWindow> win = GetXULWindowIfToplevelChrome()) {
- nsAutoString maybeConvertedValue = value;
- ConvertWindowSize(win,
- attr,
- ConversionDirection::OuterToInner,
- maybeConvertedValue);
- Unused <<
- element->SetAttr(kNameSpaceID_None, attr, maybeConvertedValue, true);
-
- continue;
- }
+ // Applying persistent attributes to windows is handled by nsXULWindow.
+ if (element->IsXULElement(nsGkAtoms::window)) {
+ continue;
}
Unused << element->SetAttr(kNameSpaceID_None, attr, value, true);
}
}
return NS_OK;
}
--- a/xpcom/ds/nsGkAtomList.h
+++ b/xpcom/ds/nsGkAtomList.h
@@ -1448,16 +1448,17 @@ GK_ATOM(x_western, "x-western")
GK_ATOM(xml, "xml")
GK_ATOM(xml_stylesheet, "xml-stylesheet")
GK_ATOM(xmlns, "xmlns")
GK_ATOM(xmp, "xmp")
GK_ATOM(xulcontentsgenerated, "xulcontentsgenerated")
GK_ATOM(yes, "yes")
GK_ATOM(z_index, "z-index")
GK_ATOM(zeroDigit, "zero-digit")
+GK_ATOM(zlevel, "zlevel")
GK_ATOM(percentage, "%")
GK_ATOM(A, "A")
GK_ATOM(alignment_baseline, "alignment-baseline")
GK_ATOM(amplitude, "amplitude")
GK_ATOM(animate, "animate")
GK_ATOM(animateColor, "animateColor")
--- a/xpfe/appshell/nsXULWindow.cpp
+++ b/xpfe/appshell/nsXULWindow.cpp
@@ -87,16 +87,17 @@ using dom::AutoNoJSAPI;
nsXULWindow::nsXULWindow(uint32_t aChromeFlags)
: mChromeTreeOwner(nullptr),
mContentTreeOwner(nullptr),
mPrimaryContentTreeOwner(nullptr),
mModalStatus(NS_OK),
mContinueModalLoop(false),
mDebuting(false),
mChromeLoaded(false),
+ mPersistentWindowStateLoaded(false),
mSizingShellFromXUL(false),
mShowAfterLoad(false),
mIntrinsicallySized(false),
mCenterAfterLoad(false),
mIsHiddenWindow(false),
mLockedUntilChromeLoad(false),
mIgnoreXULSize(false),
mIgnoreXULPosition(false),
@@ -1118,16 +1119,17 @@ NS_IMETHODIMP nsXULWindow::ForceRoundedD
void nsXULWindow::OnChromeLoaded()
{
nsresult rv = EnsureContentTreeOwner();
if (NS_SUCCEEDED(rv)) {
mChromeLoaded = true;
ApplyChromeFlags();
+ LoadPersistentWindowState();
SyncAttributesToWidget();
SizeShell();
if (mShowAfterLoad) {
SetVisibility(true);
// At this point the window may have been closed during Show(), so
// nsXULWindow::Destroy may already have been called. Take care!
}
@@ -1593,16 +1595,157 @@ void nsXULWindow::SyncAttributesToWidget
// "macanimationtype" attribute
windowElement->GetAttribute(NS_LITERAL_STRING("macanimationtype"), attr);
if (attr.EqualsLiteral("document")) {
mWindow->SetWindowAnimationType(nsIWidget::eDocumentWindowAnimation);
}
}
+enum class ConversionDirection {
+ InnerToOuter,
+ OuterToInner,
+};
+
+static void
+ConvertWindowSize(nsIXULWindow* aWin,
+ const nsAtom* aAttr,
+ ConversionDirection aDirection,
+ nsAString& aInOutString)
+{
+ MOZ_ASSERT(aWin);
+ MOZ_ASSERT(aAttr == nsGkAtoms::width || aAttr == nsGkAtoms::height);
+
+ nsresult rv;
+ int32_t size = aInOutString.ToInteger(&rv);
+ if (NS_FAILED(rv)) {
+ return;
+ }
+
+ int32_t sizeDiff = aAttr == nsGkAtoms::width
+ ? aWin->GetOuterToInnerWidthDifferenceInCSSPixels()
+ : aWin->GetOuterToInnerHeightDifferenceInCSSPixels();
+
+ if (!sizeDiff) {
+ return;
+ }
+
+ int32_t multiplier =
+ aDirection == ConversionDirection::InnerToOuter ? 1 : - 1;
+
+ CopyASCIItoUTF16(nsPrintfCString("%d", size + multiplier * sizeDiff),
+ aInOutString);
+}
+
+nsresult
+nsXULWindow::GetPersistentValue(const nsAtom* aAttr,
+ nsAString& aValue)
+{
+ nsCOMPtr<dom::Element> docShellElement = GetWindowDOMElement();
+ if (!docShellElement) {
+ return NS_ERROR_FAILURE;
+ }
+
+ nsAutoString windowElementId;
+ docShellElement->GetId(windowElementId);
+ // Elements must have an ID to be persisted.
+ if (windowElementId.IsEmpty()) {
+ return NS_OK;
+ }
+
+ nsCOMPtr<nsIDocument> ownerDoc = docShellElement->OwnerDoc();
+ nsIURI* docURI = ownerDoc->GetDocumentURI();
+ if (!docURI) {
+ return NS_ERROR_FAILURE;
+ }
+ nsAutoCString utf8uri;
+ nsresult rv = docURI->GetSpec(utf8uri);
+ NS_ENSURE_SUCCESS(rv, rv);
+ NS_ConvertUTF8toUTF16 uri(utf8uri);
+
+ if (!mLocalStore) {
+ mLocalStore = do_GetService("@mozilla.org/xul/xulstore;1");
+ if (NS_WARN_IF(!mLocalStore)) {
+ return NS_ERROR_NOT_INITIALIZED;
+ }
+ }
+
+ rv = mLocalStore->GetValue(uri,
+ windowElementId,
+ nsDependentAtomString(aAttr),
+ aValue);
+ if (NS_WARN_IF(NS_FAILED(rv))) {
+ return rv;
+ }
+
+ if (aAttr == nsGkAtoms::width || aAttr == nsGkAtoms::height) {
+ // Convert attributes from outer size to inner size for top-level
+ // windows, see bug 1444525 & co.
+ ConvertWindowSize(this,
+ aAttr,
+ ConversionDirection::OuterToInner,
+ aValue);
+ }
+
+ return NS_OK;
+}
+
+nsresult
+nsXULWindow::SetPersistentValue(const nsAtom* aAttr,
+ const nsAString& aValue)
+{
+ nsCOMPtr<dom::Element> docShellElement = GetWindowDOMElement();
+ if (!docShellElement) {
+ return NS_ERROR_FAILURE;
+ }
+
+ nsAutoString windowElementId;
+ docShellElement->GetId(windowElementId);
+ // Match the behavior of XULDocument and only persist values if the element
+ // has an ID.
+ if (windowElementId.IsEmpty()) {
+ return NS_OK;
+ }
+
+ nsCOMPtr<nsIDocument> ownerDoc = docShellElement->OwnerDoc();
+ nsIURI* docURI = ownerDoc->GetDocumentURI();
+ if (!docURI) {
+ return NS_ERROR_FAILURE;
+ }
+
+ nsAutoCString utf8uri;
+ nsresult rv = docURI->GetSpec(utf8uri);
+ if (NS_WARN_IF(NS_FAILED(rv))) {
+ return rv;
+ }
+ NS_ConvertUTF8toUTF16 uri(utf8uri);
+
+ nsAutoString maybeConvertedValue(aValue);
+ if (aAttr == nsGkAtoms::width || aAttr == nsGkAtoms::height) {
+ // Make sure we store the <window> attributes as outer window size, see
+ // bug 1444525 & co.
+ ConvertWindowSize(this,
+ aAttr,
+ ConversionDirection::InnerToOuter,
+ maybeConvertedValue);
+ }
+
+ if (!mLocalStore) {
+ mLocalStore = do_GetService("@mozilla.org/xul/xulstore;1");
+ if (NS_WARN_IF(!mLocalStore)) {
+ return NS_ERROR_NOT_INITIALIZED;
+ }
+ }
+
+ return mLocalStore->SetValue(uri,
+ windowElementId,
+ nsDependentAtomString(aAttr),
+ maybeConvertedValue);
+}
+
NS_IMETHODIMP nsXULWindow::SavePersistentAttributes()
{
// can happen when the persistence timer fires at an inopportune time
// during window shutdown
if (!mDocShell)
return NS_ERROR_FAILURE;
nsCOMPtr<dom::Element> docShellElement = GetWindowDOMElement();
@@ -1634,99 +1777,83 @@ NS_IMETHODIMP nsXULWindow::SavePersisten
if (parent && gotRestoredBounds) {
int32_t parentX, parentY;
if (NS_SUCCEEDED(parent->GetPosition(&parentX, &parentY))) {
rect.MoveBy(-parentX, -parentY);
}
}
nsAutoString sizeString;
- nsAutoString windowElementId;
-
- // fetch docShellElement's ID and XUL owner document
- RefPtr<dom::XULDocument> ownerXULDoc =
- docShellElement->OwnerDoc()->IsXULDocument()
- ? docShellElement->OwnerDoc()->AsXULDocument() : nullptr;
- if (docShellElement->IsXULElement()) {
- docShellElement->GetId(windowElementId);
- }
-
- bool shouldPersist = !isFullscreen && ownerXULDoc;
+ bool shouldPersist = !isFullscreen;
ErrorResult rv;
// (only for size elements which are persisted)
if ((mPersistentAttributesDirty & PAD_POSITION) && gotRestoredBounds) {
if (persistString.Find("screenX") >= 0) {
sizeString.Truncate();
sizeString.AppendInt(NSToIntRound(rect.X() / posScale.scale));
docShellElement->SetAttribute(SCREENX_ATTRIBUTE, sizeString, rv);
if (shouldPersist) {
- IgnoredErrorResult err;
- ownerXULDoc->Persist(windowElementId, SCREENX_ATTRIBUTE, err);
+ Unused << SetPersistentValue(nsGkAtoms::screenX, sizeString);
}
}
if (persistString.Find("screenY") >= 0) {
sizeString.Truncate();
sizeString.AppendInt(NSToIntRound(rect.Y() / posScale.scale));
docShellElement->SetAttribute(SCREENY_ATTRIBUTE, sizeString, rv);
if (shouldPersist) {
- IgnoredErrorResult err;
- ownerXULDoc->Persist(windowElementId, SCREENY_ATTRIBUTE, err);
+ Unused << SetPersistentValue(nsGkAtoms::screenY, sizeString);
}
}
}
if ((mPersistentAttributesDirty & PAD_SIZE) && gotRestoredBounds) {
LayoutDeviceIntRect innerRect = rect - GetOuterToInnerSizeDifference(mWindow);
if (persistString.Find("width") >= 0) {
sizeString.Truncate();
sizeString.AppendInt(NSToIntRound(innerRect.Width() / sizeScale.scale));
docShellElement->SetAttribute(WIDTH_ATTRIBUTE, sizeString, rv);
if (shouldPersist) {
- IgnoredErrorResult err;
- ownerXULDoc->Persist(windowElementId, WIDTH_ATTRIBUTE, err);
+ Unused << SetPersistentValue(nsGkAtoms::width, sizeString);
}
}
if (persistString.Find("height") >= 0) {
sizeString.Truncate();
sizeString.AppendInt(NSToIntRound(innerRect.Height() / sizeScale.scale));
docShellElement->SetAttribute(HEIGHT_ATTRIBUTE, sizeString, rv);
if (shouldPersist) {
- IgnoredErrorResult err;
- ownerXULDoc->Persist(windowElementId, HEIGHT_ATTRIBUTE, err);
+ Unused << SetPersistentValue(nsGkAtoms::height, sizeString);
}
}
}
if (mPersistentAttributesDirty & PAD_MISC) {
nsSizeMode sizeMode = mWindow->SizeMode();
if (sizeMode != nsSizeMode_Minimized) {
if (sizeMode == nsSizeMode_Maximized)
sizeString.Assign(SIZEMODE_MAXIMIZED);
else if (sizeMode == nsSizeMode_Fullscreen)
sizeString.Assign(SIZEMODE_FULLSCREEN);
else
sizeString.Assign(SIZEMODE_NORMAL);
docShellElement->SetAttribute(MODE_ATTRIBUTE, sizeString, rv);
if (shouldPersist && persistString.Find("sizemode") >= 0) {
- IgnoredErrorResult err;
- ownerXULDoc->Persist(windowElementId, MODE_ATTRIBUTE, err);
+ Unused << SetPersistentValue(nsGkAtoms::sizemode, sizeString);
}
}
if (persistString.Find("zlevel") >= 0) {
uint32_t zLevel;
nsCOMPtr<nsIWindowMediator> mediator(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
if (mediator) {
mediator->GetZLevel(this, &zLevel);
sizeString.Truncate();
sizeString.AppendInt(zLevel);
docShellElement->SetAttribute(ZLEVEL_ATTRIBUTE, sizeString, rv);
if (shouldPersist) {
- IgnoredErrorResult err;
- ownerXULDoc->Persist(windowElementId, ZLEVEL_ATTRIBUTE, err);
+ Unused << SetPersistentValue(nsGkAtoms::zlevel, sizeString);
}
}
}
}
mPersistentAttributesDirty = 0;
return NS_OK;
}
@@ -2275,22 +2402,64 @@ nsXULWindow::ApplyChromeFlags()
IgnoredErrorResult rv;
window->SetAttribute(NS_LITERAL_STRING("chromehidden"), newvalue, rv);
}
NS_IMETHODIMP
nsXULWindow::BeforeStartLayout()
{
ApplyChromeFlags();
+ LoadPersistentWindowState();
SyncAttributesToWidget();
SizeShell();
return NS_OK;
}
void
+nsXULWindow::LoadPersistentWindowState()
+{
+ // Only apply the persisted state once.
+ if (mPersistentWindowStateLoaded) {
+ return;
+ }
+ mPersistentWindowStateLoaded = true;
+
+ nsCOMPtr<dom::Element> docShellElement = GetWindowDOMElement();
+ if (!docShellElement) {
+ return;
+ }
+
+ // Check if the window wants to persist anything.
+ nsAutoString persist;
+ docShellElement->GetAttr(kNameSpaceID_None, nsGkAtoms::persist, persist);
+ if (persist.IsEmpty()) {
+ return;
+ }
+
+ auto loadValue = [&] (const nsAtom* aAttr) {
+ nsDependentAtomString attrString(aAttr);
+ if (persist.Find(attrString) >= 0) {
+ nsAutoString value;
+ nsresult rv = GetPersistentValue(aAttr, value);
+ NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to get persistent state.");
+ if (NS_SUCCEEDED(rv) && !value.IsEmpty()) {
+ IgnoredErrorResult err;
+ docShellElement->SetAttribute(attrString, value, err);
+ }
+ }
+ };
+
+ loadValue(nsGkAtoms::screenX);
+ loadValue(nsGkAtoms::screenY);
+ loadValue(nsGkAtoms::width);
+ loadValue(nsGkAtoms::height);
+ loadValue(nsGkAtoms::sizemode);
+}
+
+void
nsXULWindow::SizeShell()
{
AutoRestore<bool> sizingShellFromXUL(mSizingShellFromXUL);
mSizingShellFromXUL = true;
int32_t specWidth = -1, specHeight = -1;
bool gotSize = false;
bool isContent = false;
--- a/xpfe/appshell/nsXULWindow.h
+++ b/xpfe/appshell/nsXULWindow.h
@@ -29,23 +29,26 @@
#include "nsIInterfaceRequestorUtils.h"
#include "nsIXULWindow.h"
#include "nsIPrompt.h"
#include "nsIAuthPrompt.h"
#include "nsIXULBrowserWindow.h"
#include "nsIWeakReference.h"
#include "nsIWidgetListener.h"
#include "nsITabParent.h"
+#include "nsIXULStore.h"
namespace mozilla {
namespace dom {
class Element;
} // namespace dom
} // namespace mozilla
+class nsAtom;
+
// nsXULWindow
#define NS_XULWINDOW_IMPL_CID \
{ /* 8eaec2f3-ed02-4be2-8e0f-342798477298 */ \
0x8eaec2f3, \
0xed02, \
0x4be2, \
{ 0x8e, 0x0f, 0x34, 0x27, 0x98, 0x47, 0x72, 0x98 } \
@@ -137,31 +140,38 @@ protected:
nsIWidget *aReqBelow, nsIWidget **aActualBelow);
void PlaceWindowLayersBehind(uint32_t aLowLevel, uint32_t aHighLevel,
nsIXULWindow *aBehind);
void SetContentScrollbarVisibility(bool aVisible);
bool GetContentScrollbarVisibility();
void PersistentAttributesDirty(uint32_t aDirtyFlags);
nsresult GetTabCount(uint32_t* aResult);
+ void LoadPersistentWindowState();
+ nsresult GetPersistentValue(const nsAtom* aAttr,
+ nsAString& aValue);
+ nsresult SetPersistentValue(const nsAtom* aAttr,
+ const nsAString& aValue);
+
nsChromeTreeOwner* mChromeTreeOwner;
nsContentTreeOwner* mContentTreeOwner;
nsContentTreeOwner* mPrimaryContentTreeOwner;
nsCOMPtr<nsIWidget> mWindow;
nsCOMPtr<nsIDocShell> mDocShell;
nsCOMPtr<nsPIDOMWindowOuter> mDOMWindow;
nsCOMPtr<nsIWeakReference> mParentWindow;
nsCOMPtr<nsIPrompt> mPrompter;
nsCOMPtr<nsIAuthPrompt> mAuthPrompter;
nsCOMPtr<nsIXULBrowserWindow> mXULBrowserWindow;
nsCOMPtr<nsIDocShellTreeItem> mPrimaryContentShell;
nsresult mModalStatus;
bool mContinueModalLoop;
bool mDebuting; // being made visible right now
bool mChromeLoaded; // True when chrome has loaded
+ bool mPersistentWindowStateLoaded;
bool mSizingShellFromXUL; // true when in SizeShell()
bool mShowAfterLoad;
bool mIntrinsicallySized;
bool mCenterAfterLoad;
bool mIsHiddenWindow;
bool mLockedUntilChromeLoad;
bool mIgnoreXULSize;
bool mIgnoreXULPosition;
@@ -181,12 +191,13 @@ protected:
nsCOMPtr<nsITabParent> mPrimaryTabParent;
private:
// GetPrimaryTabParentSize is called from xpidl methods and we don't have a
// good way to annotate those with MOZ_CAN_RUN_SCRIPT yet. It takes no
// refcounted args other than "this", and the "this" uses seem ok.
MOZ_CAN_RUN_SCRIPT_BOUNDARY nsresult GetPrimaryTabParentSize(int32_t* aWidth, int32_t* aHeight);
nsresult GetPrimaryContentShellSize(int32_t* aWidth, int32_t* aHeight);
nsresult SetPrimaryTabParentSize(int32_t aWidth, int32_t aHeight);
+ nsCOMPtr<nsIXULStore> mLocalStore;
};
NS_DEFINE_STATIC_IID_ACCESSOR(nsXULWindow, NS_XULWINDOW_IMPL_CID)
#endif /* nsXULWindow_h__ */