Bug 1769952 - Fix running raptor on a Win10-64 VM r=sparky
[gecko.git] / dom / xul / XULPersist.cpp
blob1f0119cb1c59050ae63a79bac7aef10865b09d54
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 https://mozilla.org/MPL/2.0/. */
7 #include "XULPersist.h"
9 #ifdef MOZ_NEW_XULSTORE
10 # include "mozilla/XULStore.h"
11 #else
12 # include "nsIXULStore.h"
13 # include "nsIStringEnumerator.h"
14 #endif
15 #include "mozilla/BasePrincipal.h"
16 #include "mozilla/dom/Document.h"
17 #include "mozilla/dom/Element.h"
18 #include "nsContentUtils.h"
19 #include "nsIAppWindow.h"
21 namespace mozilla::dom {
23 static bool IsRootElement(Element* aElement) {
24 return aElement->OwnerDoc()->GetRootElement() == aElement;
27 static bool ShouldPersistAttribute(Element* aElement, nsAtom* aAttribute) {
28 if (IsRootElement(aElement)) {
29 // This is not an element of the top document, its owner is
30 // not an AppWindow. Persist it.
31 if (aElement->OwnerDoc()->GetInProcessParentDocument()) {
32 return true;
34 // The following attributes of xul:window should be handled in
35 // AppWindow::SavePersistentAttributes instead of here.
36 if (aAttribute == nsGkAtoms::screenX || aAttribute == nsGkAtoms::screenY ||
37 aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height ||
38 aAttribute == nsGkAtoms::sizemode) {
39 return false;
42 return true;
45 NS_IMPL_ISUPPORTS(XULPersist, nsIDocumentObserver)
47 XULPersist::XULPersist(Document* aDocument)
48 : nsStubDocumentObserver(), mDocument(aDocument) {}
50 XULPersist::~XULPersist() = default;
52 void XULPersist::Init() {
53 ApplyPersistentAttributes();
54 mDocument->AddObserver(this);
57 void XULPersist::DropDocumentReference() {
58 mDocument->RemoveObserver(this);
59 mDocument = nullptr;
62 void XULPersist::AttributeChanged(dom::Element* aElement, int32_t aNameSpaceID,
63 nsAtom* aAttribute, int32_t aModType,
64 const nsAttrValue* aOldValue) {
65 NS_ASSERTION(aElement->OwnerDoc() == mDocument, "unexpected doc");
67 // See if there is anything we need to persist in the localstore.
69 // XXX Namespace handling broken :-(
70 nsAutoString persist;
71 // Persistence of attributes of xul:window is handled in AppWindow.
72 if (aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::persist, persist) &&
73 ShouldPersistAttribute(aElement, aAttribute) && !persist.IsEmpty() &&
74 // XXXldb This should check that it's a token, not just a substring.
75 persist.Find(nsDependentAtomString(aAttribute)) >= 0) {
76 // Might not need this, but be safe for now.
77 nsCOMPtr<nsIDocumentObserver> kungFuDeathGrip(this);
78 nsContentUtils::AddScriptRunner(
79 NewRunnableMethod<Element*, int32_t, nsAtom*>(
80 "dom::XULPersist::Persist", this, &XULPersist::Persist, aElement,
81 kNameSpaceID_None, aAttribute));
85 void XULPersist::Persist(Element* aElement, int32_t aNameSpaceID,
86 nsAtom* aAttribute) {
87 if (!mDocument) {
88 return;
90 // For non-chrome documents, persistance is simply broken
91 if (!mDocument->NodePrincipal()->IsSystemPrincipal()) {
92 return;
95 #ifndef MOZ_NEW_XULSTORE
96 if (!mLocalStore) {
97 mLocalStore = do_GetService("@mozilla.org/xul/xulstore;1");
98 if (NS_WARN_IF(!mLocalStore)) {
99 return;
102 #endif
104 nsAutoString id;
106 aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::id, id);
107 nsAtomString attrstr(aAttribute);
109 nsAutoString valuestr;
110 aElement->GetAttr(kNameSpaceID_None, aAttribute, valuestr);
112 nsAutoCString utf8uri;
113 nsresult rv = mDocument->GetDocumentURI()->GetSpec(utf8uri);
114 if (NS_WARN_IF(NS_FAILED(rv))) {
115 return;
117 NS_ConvertUTF8toUTF16 uri(utf8uri);
119 bool hasAttr;
120 #ifdef MOZ_NEW_XULSTORE
121 rv = XULStore::HasValue(uri, id, attrstr, hasAttr);
122 #else
123 rv = mLocalStore->HasValue(uri, id, attrstr, &hasAttr);
124 #endif
126 if (NS_WARN_IF(NS_FAILED(rv))) {
127 return;
130 if (hasAttr && valuestr.IsEmpty()) {
131 #ifdef MOZ_NEW_XULSTORE
132 rv = XULStore::RemoveValue(uri, id, attrstr);
133 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "value removed");
134 #else
135 mLocalStore->RemoveValue(uri, id, attrstr);
136 #endif
137 return;
140 // Persisting attributes to top level windows is handled by AppWindow.
141 if (IsRootElement(aElement)) {
142 if (nsCOMPtr<nsIAppWindow> win =
143 mDocument->GetAppWindowIfToplevelChrome()) {
144 return;
148 #ifdef MOZ_NEW_XULSTORE
149 rv = XULStore::SetValue(uri, id, attrstr, valuestr);
150 #else
151 mLocalStore->SetValue(uri, id, attrstr, valuestr);
152 #endif
153 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "value set");
156 nsresult XULPersist::ApplyPersistentAttributes() {
157 if (!mDocument) {
158 return NS_ERROR_NOT_AVAILABLE;
160 // For non-chrome documents, persistance is simply broken
161 if (!mDocument->NodePrincipal()->IsSystemPrincipal()) {
162 return NS_ERROR_NOT_AVAILABLE;
165 // Add all of the 'persisted' attributes into the content
166 // model.
167 #ifndef MOZ_NEW_XULSTORE
168 if (!mLocalStore) {
169 mLocalStore = do_GetService("@mozilla.org/xul/xulstore;1");
170 if (NS_WARN_IF(!mLocalStore)) {
171 return NS_ERROR_NOT_INITIALIZED;
174 #endif
176 ApplyPersistentAttributesInternal();
178 return NS_OK;
181 nsresult XULPersist::ApplyPersistentAttributesInternal() {
182 nsCOMArray<Element> elements;
184 nsAutoCString utf8uri;
185 nsresult rv = mDocument->GetDocumentURI()->GetSpec(utf8uri);
186 if (NS_WARN_IF(NS_FAILED(rv))) {
187 return rv;
189 NS_ConvertUTF8toUTF16 uri(utf8uri);
191 // Get a list of element IDs for which persisted values are available
192 #ifdef MOZ_NEW_XULSTORE
193 UniquePtr<XULStoreIterator> ids;
194 rv = XULStore::GetIDs(uri, ids);
195 #else
196 nsCOMPtr<nsIStringEnumerator> ids;
197 rv = mLocalStore->GetIDsEnumerator(uri, getter_AddRefs(ids));
198 #endif
199 if (NS_WARN_IF(NS_FAILED(rv))) {
200 return rv;
203 #ifdef MOZ_NEW_XULSTORE
204 while (ids->HasMore()) {
205 nsAutoString id;
206 rv = ids->GetNext(&id);
207 if (NS_WARN_IF(NS_FAILED(rv))) {
208 return rv;
210 #else
211 while (1) {
212 bool hasmore = false;
213 ids->HasMore(&hasmore);
214 if (!hasmore) {
215 break;
218 nsAutoString id;
219 ids->GetNext(id);
220 #endif
222 // We want to hold strong refs to the elements while applying
223 // persistent attributes, just in case.
224 const nsTArray<Element*>* allElements = mDocument->GetAllElementsForId(id);
225 if (!allElements) {
226 continue;
228 elements.Clear();
229 elements.SetCapacity(allElements->Length());
230 for (Element* element : *allElements) {
231 elements.AppendObject(element);
234 rv = ApplyPersistentAttributesToElements(id, elements);
235 if (NS_WARN_IF(NS_FAILED(rv))) {
236 return rv;
240 return NS_OK;
243 nsresult XULPersist::ApplyPersistentAttributesToElements(
244 const nsAString& aID, nsCOMArray<Element>& aElements) {
245 nsAutoCString utf8uri;
246 nsresult rv = mDocument->GetDocumentURI()->GetSpec(utf8uri);
247 if (NS_WARN_IF(NS_FAILED(rv))) {
248 return rv;
250 NS_ConvertUTF8toUTF16 uri(utf8uri);
252 // Get a list of attributes for which persisted values are available
253 #ifdef MOZ_NEW_XULSTORE
254 UniquePtr<XULStoreIterator> attrs;
255 rv = XULStore::GetAttrs(uri, aID, attrs);
256 #else
257 nsCOMPtr<nsIStringEnumerator> attrs;
258 rv = mLocalStore->GetAttributeEnumerator(uri, aID, getter_AddRefs(attrs));
259 #endif
260 if (NS_WARN_IF(NS_FAILED(rv))) {
261 return rv;
264 #ifdef MOZ_NEW_XULSTORE
265 while (attrs->HasMore()) {
266 nsAutoString attrstr;
267 rv = attrs->GetNext(&attrstr);
268 if (NS_WARN_IF(NS_FAILED(rv))) {
269 return rv;
272 nsAutoString value;
273 rv = XULStore::GetValue(uri, aID, attrstr, value);
274 #else
275 while (1) {
276 bool hasmore = PR_FALSE;
277 attrs->HasMore(&hasmore);
278 if (!hasmore) {
279 break;
282 nsAutoString attrstr;
283 attrs->GetNext(attrstr);
285 nsAutoString value;
286 rv = mLocalStore->GetValue(uri, aID, attrstr, value);
287 #endif
288 if (NS_WARN_IF(NS_FAILED(rv))) {
289 return rv;
292 RefPtr<nsAtom> attr = NS_Atomize(attrstr);
293 if (NS_WARN_IF(!attr)) {
294 return NS_ERROR_OUT_OF_MEMORY;
297 uint32_t cnt = aElements.Length();
298 for (int32_t i = int32_t(cnt) - 1; i >= 0; --i) {
299 Element* element = aElements.SafeElementAt(i);
300 if (!element) {
301 continue;
304 // Applying persistent attributes to top level windows is handled
305 // by AppWindow.
306 if (IsRootElement(element)) {
307 if (nsCOMPtr<nsIAppWindow> win =
308 mDocument->GetAppWindowIfToplevelChrome()) {
309 continue;
313 Unused << element->SetAttr(kNameSpaceID_None, attr, value, true);
317 return NS_OK;
320 } // namespace mozilla::dom