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
|
/* -*- 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 http://mozilla.org/MPL/2.0/. */
/*
* nsStubMutationObserver is an implementation of the nsIMutationObserver
* interface (except for the methods on nsISupports) that is intended to be
* used as a base class within the content/layout library. All methods do
* nothing.
*/
#include "nsStubMutationObserver.h"
#include "mozilla/RefCountType.h"
#include "nsISupports.h"
#include "nsINode.h"
/******************************************************************************
* nsStubMutationObserver
*****************************************************************************/
NS_IMPL_NSIMUTATIONOBSERVER_CORE_STUB(nsStubMutationObserver)
NS_IMPL_NSIMUTATIONOBSERVER_CONTENT(nsStubMutationObserver)
/******************************************************************************
* MutationObserverWrapper
*****************************************************************************/
/**
* @brief Wrapper class for a mutation observer that observes multiple nodes.
*
* This wrapper implements all methods of the nsIMutationObserver interface
* and forwards all calls to its owner, which is an instance of
* nsMultiMutationObserver.
*
* This class holds a reference to the owner and AddRefs/Releases its owner
* as well to ensure lifetime.
*/
class MutationObserverWrapper final : public nsIMutationObserver {
public:
NS_DECL_ISUPPORTS
explicit MutationObserverWrapper(nsMultiMutationObserver* aOwner)
: mOwner(aOwner) {}
void CharacterDataWillChange(nsIContent* aContent,
const CharacterDataChangeInfo& aInfo) override {
MOZ_ASSERT(mOwner);
mOwner->CharacterDataWillChange(aContent, aInfo);
}
void CharacterDataChanged(nsIContent* aContent,
const CharacterDataChangeInfo& aInfo) override {
MOZ_ASSERT(mOwner);
mOwner->CharacterDataChanged(aContent, aInfo);
}
void AttributeWillChange(mozilla::dom::Element* aElement,
int32_t aNameSpaceID, nsAtom* aAttribute,
int32_t aModType) override {
MOZ_ASSERT(mOwner);
mOwner->AttributeWillChange(aElement, aNameSpaceID, aAttribute, aModType);
}
void AttributeChanged(mozilla::dom::Element* aElement, int32_t aNameSpaceID,
nsAtom* aAttribute, int32_t aModType,
const nsAttrValue* aOldValue) override {
MOZ_ASSERT(mOwner);
mOwner->AttributeChanged(aElement, aNameSpaceID, aAttribute, aModType,
aOldValue);
}
void AttributeSetToCurrentValue(mozilla::dom::Element* aElement,
int32_t aNameSpaceID,
nsAtom* aAttribute) override {
MOZ_ASSERT(mOwner);
mOwner->AttributeSetToCurrentValue(aElement, aNameSpaceID, aAttribute);
}
void ContentAppended(nsIContent* aFirstNewContent) override {
MOZ_ASSERT(mOwner);
mOwner->ContentAppended(aFirstNewContent);
}
void ContentInserted(nsIContent* aChild) override {
MOZ_ASSERT(mOwner);
mOwner->ContentInserted(aChild);
}
void ContentRemoved(nsIContent* aChild,
nsIContent* aPreviousSibling) override {
MOZ_ASSERT(mOwner);
mOwner->ContentRemoved(aChild, aPreviousSibling);
}
void NodeWillBeDestroyed(nsINode* aNode) override {
MOZ_ASSERT(mOwner);
AddRefWrapper();
RefPtr<nsMultiMutationObserver> owner = mOwner;
owner->NodeWillBeDestroyed(aNode);
owner->RemoveMutationObserverFromNode(aNode);
mOwner = nullptr;
ReleaseWrapper();
}
void ParentChainChanged(nsIContent* aContent) override {
MOZ_ASSERT(mOwner);
mOwner->ParentChainChanged(aContent);
}
void ARIAAttributeDefaultWillChange(mozilla::dom::Element* aElement,
nsAtom* aAttribute,
int32_t aModType) override {
MOZ_ASSERT(mOwner);
mOwner->ARIAAttributeDefaultWillChange(aElement, aAttribute, aModType);
}
void ARIAAttributeDefaultChanged(mozilla::dom::Element* aElement,
nsAtom* aAttribute,
int32_t aModType) override {
MOZ_ASSERT(mOwner);
mOwner->ARIAAttributeDefaultChanged(aElement, aAttribute, aModType);
}
MozExternalRefCountType AddRefWrapper() {
nsrefcnt count = ++mRefCnt;
NS_LOG_ADDREF(this, count, "MutationObserverWrapper", sizeof(*this));
return count;
}
MozExternalRefCountType ReleaseWrapper() {
--mRefCnt;
NS_LOG_RELEASE(this, mRefCnt, "MutationObserverWrapper");
if (mRefCnt == 0) {
mRefCnt = 1;
delete this;
return MozExternalRefCountType(0);
}
return mRefCnt;
}
private:
~MutationObserverWrapper() = default;
nsMultiMutationObserver* mOwner{nullptr};
};
NS_IMPL_QUERY_INTERFACE(MutationObserverWrapper, nsIMutationObserver);
MozExternalRefCountType MutationObserverWrapper::AddRef() {
MOZ_ASSERT(mOwner);
AddRefWrapper();
mOwner->AddRef();
return mRefCnt;
}
MozExternalRefCountType MutationObserverWrapper::Release() {
MOZ_ASSERT(mOwner);
mOwner->Release();
return ReleaseWrapper();
}
/******************************************************************************
* nsMultiMutationObserver
*****************************************************************************/
void nsMultiMutationObserver::AddMutationObserverToNode(nsINode* aNode) {
if (!aNode) {
return;
}
if (mWrapperForNode.Contains(aNode)) {
return;
}
auto* newWrapper = new MutationObserverWrapper{this};
newWrapper->AddRefWrapper();
mWrapperForNode.InsertOrUpdate(aNode, newWrapper);
aNode->AddMutationObserver(newWrapper);
}
void nsMultiMutationObserver::RemoveMutationObserverFromNode(nsINode* aNode) {
if (!aNode) {
return;
}
if (auto obs = mWrapperForNode.MaybeGet(aNode); obs.isSome()) {
aNode->RemoveMutationObserver(*obs);
mWrapperForNode.Remove(aNode);
(*obs)->ReleaseWrapper();
}
}
bool nsMultiMutationObserver::ContainsNode(const nsINode* aNode) const {
return mWrapperForNode.Contains(const_cast<nsINode*>(aNode));
}
/******************************************************************************
* nsStubMultiMutationObserver
*****************************************************************************/
NS_IMPL_NSIMUTATIONOBSERVER_CORE_STUB(nsStubMultiMutationObserver)
NS_IMPL_NSIMUTATIONOBSERVER_CONTENT(nsStubMultiMutationObserver)
|