summaryrefslogtreecommitdiffstats
path: root/widget/gtk/nsNativeMenuDocListener.cpp
blob: 46a9c3aa924e879776655bb95812c244178e5834 (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
323
324
325
326
327
328
329
/* 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/. */

#include "mozilla/Assertions.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/dom/Element.h"
#include "nsContentUtils.h"
#include "nsIAtom.h"
#include "nsIContent.h"
#include "nsIDocument.h"

#include "nsMenuContainer.h"

#include "nsNativeMenuDocListener.h"

using namespace mozilla;

uint32_t nsNativeMenuDocListener::sUpdateBlockersCount = 0;

nsNativeMenuDocListenerTArray* gPendingListeners;

/*
 * Small helper which caches a single listener, so that consecutive
 * events which go to the same node avoid multiple hash table lookups
 */
class MOZ_STACK_CLASS DispatchHelper {
public:
    DispatchHelper(nsNativeMenuDocListener* aListener,
                   nsIContent* aContent
                   MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
                   mObserver(nullptr) {
        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
        if (aContent == aListener->mLastSource) {
            mObserver = aListener->mLastTarget;
        } else {
            mObserver = aListener->mContentToObserverTable.Get(aContent);
            if (mObserver) {
                aListener->mLastSource = aContent;
                aListener->mLastTarget = mObserver;
            }
        }
    }

    ~DispatchHelper() { };

    nsNativeMenuChangeObserver* Observer() const {
      return mObserver; 
    }

    bool HasObserver() const {
      return !!mObserver;
    }

private:
    nsNativeMenuChangeObserver* mObserver;
    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};

NS_IMPL_ISUPPORTS(nsNativeMenuDocListener, nsIMutationObserver)

nsNativeMenuDocListener::~nsNativeMenuDocListener() {
    MOZ_ASSERT(mContentToObserverTable.Count() == 0,
               "Some nodes forgot to unregister listeners. This is bad! (and we're lucky we made it this far)");
    MOZ_COUNT_DTOR(nsNativeMenuDocListener);
}

void
nsNativeMenuDocListener::AttributeChanged(nsIDocument* aDocument,
                                          mozilla::dom::Element* aElement,
                                          int32_t aNameSpaceID,
                                          nsIAtom* aAttribute,
                                          int32_t aModType,
                                          const nsAttrValue* aOldValue) {
    if (sUpdateBlockersCount == 0) {
        DoAttributeChanged(aElement, aAttribute);
        return;
    }

    MutationRecord* m =* mPendingMutations.AppendElement(new MutationRecord);
    m->mType = MutationRecord::eAttributeChanged;
    m->mTarget = aElement;
    m->mAttribute = aAttribute;

    ScheduleFlush(this);
}

void
nsNativeMenuDocListener::ContentAppended(nsIDocument* aDocument,
                                         nsIContent* aContainer,
                                         nsIContent* aFirstNewContent,
                                         int32_t aNewIndexInContainer) {
    for (nsIContent* c = aFirstNewContent; c; c = c->GetNextSibling()) {
        ContentInserted(aDocument, aContainer, c, 0);
    }
}

void
nsNativeMenuDocListener::ContentInserted(nsIDocument* aDocument,
                                         nsIContent* aContainer,
                                         nsIContent* aChild,
                                         int32_t aIndexInContainer) {
    nsIContent* prevSibling = nsMenuContainer::GetPreviousSupportedSibling(aChild);

    if (sUpdateBlockersCount == 0) {
        DoContentInserted(aContainer, aChild, prevSibling);
        return;
    }

    MutationRecord* m =* mPendingMutations.AppendElement(new MutationRecord);
    m->mType = MutationRecord::eContentInserted;
    m->mTarget = aContainer;
    m->mChild = aChild;
    m->mPrevSibling = prevSibling;

    ScheduleFlush(this);
}

void
nsNativeMenuDocListener::ContentRemoved(nsIDocument* aDocument,
                                        nsIContent* aContainer,
                                        nsIContent* aChild,
                                        int32_t aIndexInContainer,
                                        nsIContent* aPreviousSibling) {
    if (sUpdateBlockersCount == 0) {
        DoContentRemoved(aContainer, aChild);
        return;
    }

    MutationRecord* m =* mPendingMutations.AppendElement(new MutationRecord);
    m->mType = MutationRecord::eContentRemoved;
    m->mTarget = aContainer;
    m->mChild = aChild;

    ScheduleFlush(this);
}                                                           

void
nsNativeMenuDocListener::NodeWillBeDestroyed(const nsINode* aNode) {
    mDocument = nullptr;
}

void
nsNativeMenuDocListener::DoAttributeChanged(nsIContent* aContent,
                                            nsIAtom* aAttribute) {
    DispatchHelper h(this, aContent);
    if (h.HasObserver()) {
        h.Observer()->OnAttributeChanged(aContent, aAttribute);
    }
}

void
nsNativeMenuDocListener::DoContentInserted(nsIContent* aContainer,
                                           nsIContent* aChild,
                                           nsIContent* aPrevSibling) {
    DispatchHelper h(this, aContainer);
    if (h.HasObserver()) {
        h.Observer()->OnContentInserted(aContainer, aChild, aPrevSibling);
    }
}

void
nsNativeMenuDocListener::DoContentRemoved(nsIContent* aContainer,
                                          nsIContent* aChild) {
    DispatchHelper h(this, aContainer);
    if (h.HasObserver()) {
        h.Observer()->OnContentRemoved(aContainer, aChild);
    }
}

void
nsNativeMenuDocListener::DoBeginUpdates(nsIContent* aTarget) {
    DispatchHelper h(this, aTarget);
    if (h.HasObserver()) {
        h.Observer()->OnBeginUpdates(aTarget);
    }
}

void
nsNativeMenuDocListener::DoEndUpdates(nsIContent* aTarget) {
    DispatchHelper h(this, aTarget);
    if (h.HasObserver()) {
        h.Observer()->OnEndUpdates();
    }
}

void
nsNativeMenuDocListener::FlushPendingMutations() {
    nsIContent* currentTarget = nullptr;
    bool inUpdateSequence = false;

    while (mPendingMutations.Length() > 0) {
        MutationRecord* m = mPendingMutations[0];

        if (m->mTarget != currentTarget) {
            if (inUpdateSequence) {
                DoEndUpdates(currentTarget);
                inUpdateSequence = false;
            }

            currentTarget = m->mTarget;

            if (mPendingMutations.Length() > 1 &&
                mPendingMutations[1]->mTarget == currentTarget) {
                DoBeginUpdates(currentTarget);
                inUpdateSequence = true;
            }
        }

        switch (m->mType) {
            case MutationRecord::eAttributeChanged:
                DoAttributeChanged(m->mTarget, m->mAttribute);
                break;
            case MutationRecord::eContentInserted:
                DoContentInserted(m->mTarget, m->mChild, m->mPrevSibling);
                break;
            case MutationRecord::eContentRemoved:
                DoContentRemoved(m->mTarget, m->mChild);
                break;
            default:
                NS_NOTREACHED("Invalid type");
        }

        mPendingMutations.RemoveElementAt(0);
    }

    if (inUpdateSequence) {
        DoEndUpdates(currentTarget);
    }
}

/* static */ void
nsNativeMenuDocListener::ScheduleFlush(nsNativeMenuDocListener* aListener) {
    MOZ_ASSERT(sUpdateBlockersCount > 0, "Shouldn't be doing this now");

    if (!gPendingListeners) {
        gPendingListeners = new nsNativeMenuDocListenerTArray;
    }

    if (gPendingListeners->IndexOf(aListener) ==
        nsNativeMenuDocListenerTArray::NoIndex) {
        gPendingListeners->AppendElement(aListener);
    }
}

/* static */ void
nsNativeMenuDocListener::CancelFlush(nsNativeMenuDocListener* aListener) {
    if (!gPendingListeners) {
        return;
    }

    gPendingListeners->RemoveElement(aListener);
}

/* static */ void
nsNativeMenuDocListener::RemoveUpdateBlocker() {
    if (sUpdateBlockersCount == 1 && gPendingListeners) {
        while (gPendingListeners->Length() > 0) {
            (*gPendingListeners)[0]->FlushPendingMutations();
            gPendingListeners->RemoveElementAt(0);
        }
    }
 
    MOZ_ASSERT(sUpdateBlockersCount > 0, "Negative update blockers count!");
    sUpdateBlockersCount--;
}

nsNativeMenuDocListener::nsNativeMenuDocListener(nsIContent* aRootNode) :
    mRootNode(aRootNode),
    mDocument(nullptr),
    mLastSource(nullptr),
    mLastTarget(nullptr) {
    MOZ_COUNT_CTOR(nsNativeMenuDocListener);
}

void
nsNativeMenuDocListener::RegisterForContentChanges(nsIContent* aContent,
                                                   nsNativeMenuChangeObserver* aObserver) {
    MOZ_ASSERT(aContent, "Need content parameter");
    MOZ_ASSERT(aObserver, "Need observer parameter");
    if (!aContent || !aObserver) {
        return;
    }

    DebugOnly<nsNativeMenuChangeObserver* > old;
    MOZ_ASSERT(!mContentToObserverTable.Get(aContent, &old) || old == aObserver,
               "Multiple observers for the same content node are not supported");

    mContentToObserverTable.Put(aContent, aObserver);
}

void
nsNativeMenuDocListener::UnregisterForContentChanges(nsIContent* aContent) {
    MOZ_ASSERT(aContent, "Need content parameter");
    if (!aContent) {
        return;
    }

    mContentToObserverTable.Remove(aContent);
    if (aContent == mLastSource) {
        mLastSource = nullptr;
        mLastTarget = nullptr;
    }
}

void
nsNativeMenuDocListener::Start() {
    if (mDocument) {
        return;
    }

    mDocument = mRootNode->OwnerDoc();
    if (!mDocument) {
        return;
    }

    mDocument->AddMutationObserver(this);
}

void
nsNativeMenuDocListener::Stop() {
    if (mDocument) {
        mDocument->RemoveMutationObserver(this);
        mDocument = nullptr;
    }

    CancelFlush(this);
    mPendingMutations.Clear();
}