summaryrefslogtreecommitdiffstats
path: root/dom/xul/XULPersist.cpp
blob: e119b79d7d285d5c00918e95a8071aa0e9d4b142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* -*- 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"

#ifdef MOZ_NEW_XULSTORE
#  include "mozilla/XULStore.h"
#else
#  include "nsIXULStore.h"
#  include "nsIStringEnumerator.h"
#endif
#include "mozilla/BasePrincipal.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Element.h"
#include "nsContentUtils.h"
#include "nsIAppWindow.h"

namespace mozilla {
namespace dom {

static bool IsRootElement(Element* aElement) {
  return aElement->OwnerDoc()->GetRootElement() == aElement;
}

static bool ShouldPersistAttribute(Element* aElement, nsAtom* aAttribute) {
  if (IsRootElement(aElement)) {
    // This is not an element of the top document, its owner is
    // not an AppWindow. Persist it.
    if (aElement->OwnerDoc()->GetInProcessParentDocument()) {
      return true;
    }
    // The following attributes of xul:window should be handled in
    // AppWindow::SavePersistentAttributes instead of here.
    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)

XULPersist::XULPersist(Document* aDocument)
    : nsStubDocumentObserver(), mDocument(aDocument) {}

XULPersist::~XULPersist() = default;

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;
  // Persistence of attributes of xul:window is handled in AppWindow.
  if (aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::persist, persist) &&
      ShouldPersistAttribute(aElement, aAttribute) && !persist.IsEmpty() &&
      // XXXldb This should check that it's a token, not just a substring.
      persist.Find(nsDependentAtomString(aAttribute)) >= 0) {
    // Might not need this, but be safe for now.
    nsCOMPtr<nsIDocumentObserver> kungFuDeathGrip(this);
    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
  if (!mDocument->NodePrincipal()->IsSystemPrincipal()) {
    return;
  }

#ifndef MOZ_NEW_XULSTORE
  if (!mLocalStore) {
    mLocalStore = do_GetService("@mozilla.org/xul/xulstore;1");
    if (NS_WARN_IF(!mLocalStore)) {
      return;
    }
  }
#endif

  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;
#ifdef MOZ_NEW_XULSTORE
  rv = XULStore::HasValue(uri, id, attrstr, hasAttr);
#else
  rv = mLocalStore->HasValue(uri, id, attrstr, &hasAttr);
#endif

  if (NS_WARN_IF(NS_FAILED(rv))) {
    return;
  }

  if (hasAttr && valuestr.IsEmpty()) {
#ifdef MOZ_NEW_XULSTORE
    rv = XULStore::RemoveValue(uri, id, attrstr);
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "value removed");
#else
    mLocalStore->RemoveValue(uri, id, attrstr);
#endif
    return;
  }

  // Persisting attributes to top level windows is handled by AppWindow.
  if (IsRootElement(aElement)) {
    if (nsCOMPtr<nsIAppWindow> win =
            mDocument->GetAppWindowIfToplevelChrome()) {
      return;
    }
  }

#ifdef MOZ_NEW_XULSTORE
  rv = XULStore::SetValue(uri, id, attrstr, valuestr);
#else
  mLocalStore->SetValue(uri, id, attrstr, valuestr);
#endif
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "value set");
}

nsresult XULPersist::ApplyPersistentAttributes() {
  if (!mDocument) {
    return NS_ERROR_NOT_AVAILABLE;
  }
  // For non-chrome documents, persistance is simply broken
  if (!mDocument->NodePrincipal()->IsSystemPrincipal()) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  // Add all of the 'persisted' attributes into the content
  // model.
#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

  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
#ifdef MOZ_NEW_XULSTORE
  UniquePtr<XULStoreIterator> ids;
  rv = XULStore::GetIDs(uri, ids);
#else
  nsCOMPtr<nsIStringEnumerator> ids;
  rv = mLocalStore->GetIDsEnumerator(uri, getter_AddRefs(ids));
#endif
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

#ifdef MOZ_NEW_XULSTORE
  while (ids->HasMore()) {
    nsAutoString id;
    rv = ids->GetNext(&id);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }
#else
  while (1) {
    bool hasmore = false;
    ids->HasMore(&hasmore);
    if (!hasmore) {
      break;
    }

    nsAutoString id;
    ids->GetNext(id);
#endif

    // 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
#ifdef MOZ_NEW_XULSTORE
  UniquePtr<XULStoreIterator> attrs;
  rv = XULStore::GetAttrs(uri, aID, attrs);
#else
  nsCOMPtr<nsIStringEnumerator> attrs;
  rv = mLocalStore->GetAttributeEnumerator(uri, aID, getter_AddRefs(attrs));
#endif
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

#ifdef MOZ_NEW_XULSTORE
  while (attrs->HasMore()) {
    nsAutoString attrstr;
    rv = attrs->GetNext(&attrstr);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }

    nsAutoString value;
    rv = XULStore::GetValue(uri, aID, attrstr, value);
#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
    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
      // by AppWindow.
      if (IsRootElement(element)) {
        if (nsCOMPtr<nsIAppWindow> win =
                mDocument->GetAppWindowIfToplevelChrome()) {
          continue;
        }
      }

      Unused << element->SetAttr(kNameSpaceID_None, attr, value, true);
    }
  }

  return NS_OK;
}

}  // namespace dom
}  // namespace mozilla