2018-11-07 00:45:20 +03:00
|
|
|
/* -*- 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 https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "XULPersist.h"
|
2019-05-03 02:01:57 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
|
|
|
# include "mozilla/XULStore.h"
|
|
|
|
#else
|
|
|
|
# include "nsIXULStore.h"
|
2019-08-12 20:57:04 +03:00
|
|
|
# include "nsIStringEnumerator.h"
|
2019-05-03 02:01:57 +03:00
|
|
|
#endif
|
2019-12-05 07:44:32 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2019-11-05 20:56:28 +03:00
|
|
|
#include "nsIAppWindow.h"
|
2019-08-09 22:57:50 +03:00
|
|
|
|
2018-11-07 00:45:20 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-10-25 23:14:25 +03:00
|
|
|
static bool IsRootElement(Element* aElement) {
|
|
|
|
return aElement->OwnerDoc()->GetRootElement() == aElement;
|
|
|
|
}
|
|
|
|
|
2018-11-07 00:45:20 +03:00
|
|
|
static bool ShouldPersistAttribute(Element* aElement, nsAtom* aAttribute) {
|
2019-10-25 23:14:25 +03:00
|
|
|
if (IsRootElement(aElement)) {
|
2018-11-07 00:45:20 +03:00
|
|
|
// This is not an element of the top document, its owner is
|
2019-11-05 20:56:28 +03:00
|
|
|
// not an AppWindow. Persist it.
|
2019-07-26 19:48:31 +03:00
|
|
|
if (aElement->OwnerDoc()->GetInProcessParentDocument()) {
|
2018-11-07 00:45:20 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// The following attributes of xul:window should be handled in
|
2019-11-05 20:56:28 +03:00
|
|
|
// AppWindow::SavePersistentAttributes instead of here.
|
2018-11-07 00:45:20 +03:00
|
|
|
if (aAttribute == nsGkAtoms::screenX || aAttribute == nsGkAtoms::screenY ||
|
|
|
|
aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height ||
|
|
|
|
aAttribute == nsGkAtoms::sizemode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(XULPersist, nsIDocumentObserver)
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
XULPersist::XULPersist(Document* aDocument)
|
2018-11-07 00:45:20 +03:00
|
|
|
: nsStubDocumentObserver(), mDocument(aDocument) {}
|
|
|
|
|
2020-02-20 18:54:52 +03:00
|
|
|
XULPersist::~XULPersist() = default;
|
2018-11-07 00:45:20 +03:00
|
|
|
|
|
|
|
void XULPersist::Init() {
|
|
|
|
ApplyPersistentAttributes();
|
|
|
|
mDocument->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XULPersist::DropDocumentReference() {
|
|
|
|
mDocument->RemoveObserver(this);
|
|
|
|
mDocument = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XULPersist::AttributeChanged(dom::Element* aElement, int32_t aNameSpaceID,
|
|
|
|
nsAtom* aAttribute, int32_t aModType,
|
|
|
|
const nsAttrValue* aOldValue) {
|
|
|
|
NS_ASSERTION(aElement->OwnerDoc() == mDocument, "unexpected doc");
|
|
|
|
|
|
|
|
// See if there is anything we need to persist in the localstore.
|
|
|
|
//
|
|
|
|
// XXX Namespace handling broken :-(
|
|
|
|
nsAutoString persist;
|
2019-11-05 20:56:28 +03:00
|
|
|
// Persistence of attributes of xul:window is handled in AppWindow.
|
2019-06-13 22:06:12 +03:00
|
|
|
if (aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::persist, persist) &&
|
|
|
|
ShouldPersistAttribute(aElement, aAttribute) && !persist.IsEmpty() &&
|
2018-11-07 00:45:20 +03:00
|
|
|
// XXXldb This should check that it's a token, not just a substring.
|
|
|
|
persist.Find(nsDependentAtomString(aAttribute)) >= 0) {
|
2019-06-13 22:06:12 +03:00
|
|
|
// Might not need this, but be safe for now.
|
|
|
|
nsCOMPtr<nsIDocumentObserver> kungFuDeathGrip(this);
|
2018-11-07 00:45:20 +03:00
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
NewRunnableMethod<Element*, int32_t, nsAtom*>(
|
|
|
|
"dom::XULPersist::Persist", this, &XULPersist::Persist, aElement,
|
|
|
|
kNameSpaceID_None, aAttribute));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XULPersist::Persist(Element* aElement, int32_t aNameSpaceID,
|
|
|
|
nsAtom* aAttribute) {
|
|
|
|
if (!mDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// For non-chrome documents, persistance is simply broken
|
2019-12-05 07:44:32 +03:00
|
|
|
if (!mDocument->NodePrincipal()->IsSystemPrincipal()) {
|
2018-11-07 00:45:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifndef MOZ_NEW_XULSTORE
|
|
|
|
if (!mLocalStore) {
|
|
|
|
mLocalStore = do_GetService("@mozilla.org/xul/xulstore;1");
|
|
|
|
if (NS_WARN_IF(!mLocalStore)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-07 00:45:20 +03:00
|
|
|
nsAutoString id;
|
|
|
|
|
|
|
|
aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::id, id);
|
|
|
|
nsAtomString attrstr(aAttribute);
|
|
|
|
|
|
|
|
nsAutoString valuestr;
|
|
|
|
aElement->GetAttr(kNameSpaceID_None, aAttribute, valuestr);
|
|
|
|
|
|
|
|
nsAutoCString utf8uri;
|
|
|
|
nsresult rv = mDocument->GetDocumentURI()->GetSpec(utf8uri);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF8toUTF16 uri(utf8uri);
|
|
|
|
|
|
|
|
bool hasAttr;
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
2019-04-22 05:59:51 +03:00
|
|
|
rv = XULStore::HasValue(uri, id, attrstr, hasAttr);
|
2019-05-03 02:01:57 +03:00
|
|
|
#else
|
|
|
|
rv = mLocalStore->HasValue(uri, id, attrstr, &hasAttr);
|
|
|
|
#endif
|
|
|
|
|
2018-11-07 00:45:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasAttr && valuestr.IsEmpty()) {
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
2019-04-22 05:59:51 +03:00
|
|
|
rv = XULStore::RemoveValue(uri, id, attrstr);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "value removed");
|
2019-05-03 02:01:57 +03:00
|
|
|
#else
|
|
|
|
mLocalStore->RemoveValue(uri, id, attrstr);
|
|
|
|
#endif
|
2018-11-07 00:45:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 20:56:28 +03:00
|
|
|
// Persisting attributes to top level windows is handled by AppWindow.
|
2019-10-25 23:14:25 +03:00
|
|
|
if (IsRootElement(aElement)) {
|
2019-11-05 20:56:28 +03:00
|
|
|
if (nsCOMPtr<nsIAppWindow> win =
|
|
|
|
mDocument->GetAppWindowIfToplevelChrome()) {
|
2018-11-07 00:45:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
2019-04-22 05:59:51 +03:00
|
|
|
rv = XULStore::SetValue(uri, id, attrstr, valuestr);
|
2019-05-03 02:01:57 +03:00
|
|
|
#else
|
|
|
|
mLocalStore->SetValue(uri, id, attrstr, valuestr);
|
|
|
|
#endif
|
2019-04-22 05:59:51 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "value set");
|
2018-11-07 00:45:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult XULPersist::ApplyPersistentAttributes() {
|
|
|
|
if (!mDocument) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
// For non-chrome documents, persistance is simply broken
|
2019-12-05 07:44:32 +03:00
|
|
|
if (!mDocument->NodePrincipal()->IsSystemPrincipal()) {
|
2018-11-07 00:45:20 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all of the 'persisted' attributes into the content
|
|
|
|
// model.
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifndef MOZ_NEW_XULSTORE
|
|
|
|
if (!mLocalStore) {
|
|
|
|
mLocalStore = do_GetService("@mozilla.org/xul/xulstore;1");
|
|
|
|
if (NS_WARN_IF(!mLocalStore)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-07 00:45:20 +03:00
|
|
|
ApplyPersistentAttributesInternal();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult XULPersist::ApplyPersistentAttributesInternal() {
|
|
|
|
nsCOMArray<Element> elements;
|
|
|
|
|
|
|
|
nsAutoCString utf8uri;
|
|
|
|
nsresult rv = mDocument->GetDocumentURI()->GetSpec(utf8uri);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF8toUTF16 uri(utf8uri);
|
|
|
|
|
|
|
|
// Get a list of element IDs for which persisted values are available
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
2019-04-22 05:59:51 +03:00
|
|
|
UniquePtr<XULStoreIterator> ids;
|
|
|
|
rv = XULStore::GetIDs(uri, ids);
|
2019-05-03 02:01:57 +03:00
|
|
|
#else
|
|
|
|
nsCOMPtr<nsIStringEnumerator> ids;
|
|
|
|
rv = mLocalStore->GetIDsEnumerator(uri, getter_AddRefs(ids));
|
|
|
|
#endif
|
2018-11-07 00:45:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
2019-04-22 05:59:51 +03:00
|
|
|
while (ids->HasMore()) {
|
2019-04-20 00:05:55 +03:00
|
|
|
nsAutoString id;
|
2019-04-22 05:59:51 +03:00
|
|
|
rv = ids->GetNext(&id);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2019-05-03 02:01:57 +03:00
|
|
|
#else
|
|
|
|
while (1) {
|
|
|
|
bool hasmore = false;
|
|
|
|
ids->HasMore(&hasmore);
|
|
|
|
if (!hasmore) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString id;
|
|
|
|
ids->GetNext(id);
|
|
|
|
#endif
|
2019-04-20 00:05:55 +03:00
|
|
|
|
2018-11-07 00:45:20 +03:00
|
|
|
// We want to hold strong refs to the elements while applying
|
|
|
|
// persistent attributes, just in case.
|
|
|
|
const nsTArray<Element*>* allElements = mDocument->GetAllElementsForId(id);
|
|
|
|
if (!allElements) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
elements.Clear();
|
|
|
|
elements.SetCapacity(allElements->Length());
|
|
|
|
for (Element* element : *allElements) {
|
|
|
|
elements.AppendObject(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ApplyPersistentAttributesToElements(id, elements);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult XULPersist::ApplyPersistentAttributesToElements(
|
|
|
|
const nsAString& aID, nsCOMArray<Element>& aElements) {
|
|
|
|
nsAutoCString utf8uri;
|
|
|
|
nsresult rv = mDocument->GetDocumentURI()->GetSpec(utf8uri);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF8toUTF16 uri(utf8uri);
|
|
|
|
|
|
|
|
// Get a list of attributes for which persisted values are available
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
2019-04-22 05:59:51 +03:00
|
|
|
UniquePtr<XULStoreIterator> attrs;
|
|
|
|
rv = XULStore::GetAttrs(uri, aID, attrs);
|
2019-05-03 02:01:57 +03:00
|
|
|
#else
|
|
|
|
nsCOMPtr<nsIStringEnumerator> attrs;
|
|
|
|
rv = mLocalStore->GetAttributeEnumerator(uri, aID, getter_AddRefs(attrs));
|
|
|
|
#endif
|
2018-11-07 00:45:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:57 +03:00
|
|
|
#ifdef MOZ_NEW_XULSTORE
|
2019-04-22 05:59:51 +03:00
|
|
|
while (attrs->HasMore()) {
|
2019-04-20 00:05:55 +03:00
|
|
|
nsAutoString attrstr;
|
2019-04-22 05:59:51 +03:00
|
|
|
rv = attrs->GetNext(&attrstr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2019-04-20 00:05:55 +03:00
|
|
|
|
2018-11-07 00:45:20 +03:00
|
|
|
nsAutoString value;
|
2019-04-22 05:59:51 +03:00
|
|
|
rv = XULStore::GetValue(uri, aID, attrstr, value);
|
2019-05-03 02:01:57 +03:00
|
|
|
#else
|
|
|
|
while (1) {
|
|
|
|
bool hasmore = PR_FALSE;
|
|
|
|
attrs->HasMore(&hasmore);
|
|
|
|
if (!hasmore) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString attrstr;
|
|
|
|
attrs->GetNext(attrstr);
|
|
|
|
|
|
|
|
nsAutoString value;
|
|
|
|
rv = mLocalStore->GetValue(uri, aID, attrstr, value);
|
|
|
|
#endif
|
2018-11-07 00:45:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsAtom> attr = NS_Atomize(attrstr);
|
|
|
|
if (NS_WARN_IF(!attr)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t cnt = aElements.Length();
|
|
|
|
for (int32_t i = int32_t(cnt) - 1; i >= 0; --i) {
|
|
|
|
Element* element = aElements.SafeElementAt(i);
|
|
|
|
if (!element) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Applying persistent attributes to top level windows is handled
|
2019-11-05 20:56:28 +03:00
|
|
|
// by AppWindow.
|
2019-10-25 23:14:25 +03:00
|
|
|
if (IsRootElement(element)) {
|
2019-11-05 20:56:28 +03:00
|
|
|
if (nsCOMPtr<nsIAppWindow> win =
|
|
|
|
mDocument->GetAppWindowIfToplevelChrome()) {
|
2018-11-07 00:45:20 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << element->SetAttr(kNameSpaceID_None, attr, value, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|