Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/IDTracker.cpp
Line
Count
Source (jump to first uncovered line)
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 http://mozilla.org/MPL/2.0/. */
6
7
#include "IDTracker.h"
8
9
#include "mozilla/Encoding.h"
10
#include "nsContentUtils.h"
11
#include "nsIURI.h"
12
#include "nsBindingManager.h"
13
#include "nsEscape.h"
14
#include "nsXBLPrototypeBinding.h"
15
#include "nsCycleCollectionParticipant.h"
16
17
namespace mozilla {
18
namespace dom {
19
20
static DocumentOrShadowRoot*
21
DocOrShadowFromContent(nsIContent& aContent)
22
0
{
23
0
  ShadowRoot* shadow = aContent.GetContainingShadow();
24
0
25
0
  // We never look in <svg:use> shadow trees, for backwards compat.
26
0
  while (shadow && shadow->Host()->IsSVGElement(nsGkAtoms::use)) {
27
0
    shadow = shadow->Host()->GetContainingShadow();
28
0
  }
29
0
30
0
  if (shadow) {
31
0
    return shadow;
32
0
  }
33
0
34
0
  return aContent.OwnerDoc();
35
0
}
36
37
void
38
IDTracker::Reset(nsIContent* aFromContent, nsIURI* aURI,nsIURI* aReferrer,
39
                 uint32_t aReferrerPolicy,
40
                 bool aWatch, bool aReferenceImage)
41
0
{
42
0
  MOZ_ASSERT(aFromContent, "Reset() expects non-null content pointer");
43
0
44
0
  Unlink();
45
0
46
0
  if (!aURI)
47
0
    return;
48
0
49
0
  nsAutoCString refPart;
50
0
  aURI->GetRef(refPart);
51
0
  // Unescape %-escapes in the reference. The result will be in the
52
0
  // document charset, hopefully...
53
0
  NS_UnescapeURL(refPart);
54
0
55
0
  // Get the thing to observe changes to.
56
0
  nsIDocument* doc = aFromContent->OwnerDoc();
57
0
  DocumentOrShadowRoot* docOrShadow = DocOrShadowFromContent(*aFromContent);
58
0
  auto encoding = doc->GetDocumentCharacterSet();
59
0
60
0
  nsAutoString ref;
61
0
  nsresult rv = encoding->DecodeWithoutBOMHandling(refPart, ref);
62
0
  if (NS_FAILED(rv) || ref.IsEmpty()) {
63
0
    return;
64
0
  }
65
0
  rv = NS_OK;
66
0
67
0
  nsIContent* bindingParent = aFromContent->GetBindingParent();
68
0
  if (bindingParent && !aFromContent->IsInShadowTree()) {
69
0
    nsXBLBinding* binding = bindingParent->GetXBLBinding();
70
0
    if (!binding) {
71
0
      // This happens, for example, if aFromContent is part of the content
72
0
      // inserted by a call to nsIDocument::InsertAnonymousContent, which we
73
0
      // also want to handle.  (It also happens for <use>'s anonymous
74
0
      // content etc.)
75
0
      Element* anonRoot =
76
0
        doc->GetAnonRootIfInAnonymousContentContainer(aFromContent);
77
0
      if (anonRoot) {
78
0
        mElement = nsContentUtils::MatchElementId(anonRoot, ref);
79
0
        // We don't have watching working yet for anonymous content, so bail out here.
80
0
        return;
81
0
      }
82
0
    } else {
83
0
      bool isEqualExceptRef;
84
0
      rv = aURI->EqualsExceptRef(binding->PrototypeBinding()->DocURI(),
85
0
                                 &isEqualExceptRef);
86
0
      if (NS_SUCCEEDED(rv) && isEqualExceptRef) {
87
0
        // XXX sXBL/XBL2 issue
88
0
        // Our content is an anonymous XBL element from a binding inside the
89
0
        // same document that the referenced URI points to. In order to avoid
90
0
        // the risk of ID collisions we restrict ourselves to anonymous
91
0
        // elements from this binding; specifically, URIs that are relative to
92
0
        // the binding document should resolve to the copy of the target
93
0
        // element that has been inserted into the bound document.
94
0
        // If the URI points to a different document we don't need this
95
0
        // restriction.
96
0
        nsINodeList* anonymousChildren =
97
0
          doc->BindingManager()->GetAnonymousNodesFor(bindingParent);
98
0
99
0
        if (anonymousChildren) {
100
0
          uint32_t length = anonymousChildren->Length();
101
0
          for (uint32_t i = 0; i < length && !mElement; ++i) {
102
0
            mElement =
103
0
              nsContentUtils::MatchElementId(anonymousChildren->Item(i), ref);
104
0
          }
105
0
        }
106
0
107
0
        // We don't have watching working yet for XBL, so bail out here.
108
0
        return;
109
0
      }
110
0
    }
111
0
  }
112
0
113
0
  bool isEqualExceptRef;
114
0
  rv = aURI->EqualsExceptRef(doc->GetDocumentURI(), &isEqualExceptRef);
115
0
  if (NS_FAILED(rv) || !isEqualExceptRef) {
116
0
    RefPtr<nsIDocument::ExternalResourceLoad> load;
117
0
    doc = doc->RequestExternalResource(aURI, aReferrer, aReferrerPolicy,
118
0
                                       aFromContent, getter_AddRefs(load));
119
0
    docOrShadow = doc;
120
0
    if (!doc) {
121
0
      if (!load || !aWatch) {
122
0
        // Nothing will ever happen here
123
0
        return;
124
0
      }
125
0
126
0
      DocumentLoadNotification* observer =
127
0
        new DocumentLoadNotification(this, ref);
128
0
      mPendingNotification = observer;
129
0
      load->AddObserver(observer);
130
0
      // Keep going so we set up our watching stuff a bit
131
0
    }
132
0
  }
133
0
134
0
  if (aWatch) {
135
0
    mWatchID = NS_Atomize(ref);
136
0
  }
137
0
138
0
  mReferencingImage = aReferenceImage;
139
0
  HaveNewDocumentOrShadowRoot(docOrShadow, aWatch, ref);
140
0
}
141
142
void
143
IDTracker::ResetWithID(Element& aFrom, nsAtom* aID, bool aWatch)
144
0
{
145
0
  MOZ_ASSERT(aID);
146
0
147
0
  if (aWatch) {
148
0
    mWatchID = aID;
149
0
  }
150
0
151
0
  mReferencingImage = false;
152
0
153
0
  DocumentOrShadowRoot* docOrShadow = DocOrShadowFromContent(aFrom);
154
0
  HaveNewDocumentOrShadowRoot(docOrShadow, aWatch, nsDependentAtomString(aID));
155
0
}
156
157
void
158
IDTracker::HaveNewDocumentOrShadowRoot(
159
  DocumentOrShadowRoot* aDocOrShadow,
160
  bool aWatch,
161
  const nsString& aRef)
162
0
{
163
0
  if (aWatch) {
164
0
    mWatchDocumentOrShadowRoot = nullptr;
165
0
    if (aDocOrShadow) {
166
0
      mWatchDocumentOrShadowRoot = &aDocOrShadow->AsNode();
167
0
      mElement = aDocOrShadow->AddIDTargetObserver(mWatchID, Observe, this, mReferencingImage);
168
0
    }
169
0
    return;
170
0
  }
171
0
172
0
  if (!aDocOrShadow) {
173
0
    return;
174
0
  }
175
0
176
0
  Element* e = mReferencingImage ? aDocOrShadow->LookupImageElement(aRef)
177
0
                                 : aDocOrShadow->GetElementById(aRef);
178
0
  if (e) {
179
0
    mElement = e;
180
0
  }
181
0
}
182
183
void
184
IDTracker::Traverse(nsCycleCollectionTraversalCallback* aCB)
185
0
{
186
0
  NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCB, "mWatchDocumentOrShadowRoot");
187
0
  aCB->NoteXPCOMChild(mWatchDocumentOrShadowRoot);
188
0
  NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCB, "mElement");
189
0
  aCB->NoteXPCOMChild(mElement);
190
0
}
191
192
void
193
IDTracker::Unlink()
194
0
{
195
0
  if (mWatchID) {
196
0
    if (DocumentOrShadowRoot* docOrShadow = GetWatchDocOrShadowRoot()) {
197
0
      docOrShadow->RemoveIDTargetObserver(
198
0
        mWatchID, Observe, this, mReferencingImage);
199
0
    }
200
0
  }
201
0
  if (mPendingNotification) {
202
0
    mPendingNotification->Clear();
203
0
    mPendingNotification = nullptr;
204
0
  }
205
0
  mWatchDocumentOrShadowRoot = nullptr;
206
0
  mWatchID = nullptr;
207
0
  mElement = nullptr;
208
0
  mReferencingImage = false;
209
0
}
210
211
bool
212
IDTracker::Observe(Element* aOldElement, Element* aNewElement, void* aData)
213
0
{
214
0
  IDTracker* p = static_cast<IDTracker*>(aData);
215
0
  if (p->mPendingNotification) {
216
0
    p->mPendingNotification->SetTo(aNewElement);
217
0
  } else {
218
0
    NS_ASSERTION(aOldElement == p->mElement, "Failed to track content!");
219
0
    ChangeNotification* watcher =
220
0
      new ChangeNotification(p, aOldElement, aNewElement);
221
0
    p->mPendingNotification = watcher;
222
0
    nsContentUtils::AddScriptRunner(watcher);
223
0
  }
224
0
  bool keepTracking = p->IsPersistent();
225
0
  if (!keepTracking) {
226
0
    p->mWatchDocumentOrShadowRoot = nullptr;
227
0
    p->mWatchID = nullptr;
228
0
  }
229
0
  return keepTracking;
230
0
}
231
232
NS_IMPL_ISUPPORTS_INHERITED0(IDTracker::ChangeNotification, mozilla::Runnable)
233
NS_IMPL_ISUPPORTS(IDTracker::DocumentLoadNotification, nsIObserver)
234
235
NS_IMETHODIMP
236
IDTracker::DocumentLoadNotification::Observe(nsISupports* aSubject,
237
                                             const char* aTopic,
238
                                             const char16_t* aData)
239
0
{
240
0
  NS_ASSERTION(PL_strcmp(aTopic, "external-resource-document-created") == 0,
241
0
               "Unexpected topic");
242
0
  if (mTarget) {
243
0
    nsCOMPtr<nsIDocument> doc = do_QueryInterface(aSubject);
244
0
    mTarget->mPendingNotification = nullptr;
245
0
    NS_ASSERTION(!mTarget->mElement, "Why do we have content here?");
246
0
    // If we got here, that means we had Reset() called with aWatch ==
247
0
    // true.  So keep watching if IsPersistent().
248
0
    mTarget->HaveNewDocumentOrShadowRoot(doc, mTarget->IsPersistent(), mRef);
249
0
    mTarget->ElementChanged(nullptr, mTarget->mElement);
250
0
  }
251
0
  return NS_OK;
252
0
}
253
254
} // namespace dom
255
} // namespace mozilla