Source code

Revision control

Other Tools

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
#ifndef mozilla_dom_XULBroadcastManager_h
8
#define mozilla_dom_XULBroadcastManager_h
9
10
#include "mozilla/dom/Element.h"
11
#include "nsAtom.h"
12
13
class nsXULElement;
14
15
namespace mozilla {
16
namespace dom {
17
18
class XULBroadcastManager final {
19
public:
20
explicit XULBroadcastManager(Document* aDocument);
21
22
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(XULBroadcastManager)
23
24
/**
25
* Checks whether an element uses any of the special broadcaster attributes
26
* or is an observes element. This mimics the logic in FindBroadcaster, but
27
* is intended to be a lighter weight check and doesn't actually guarantee
28
* that the element will need a listener.
29
*/
30
static bool MayNeedListener(const Element& aElement);
31
32
nsresult AddListener(Element* aElement);
33
nsresult RemoveListener(Element* aElement);
34
void AttributeChanged(Element* aElement, int32_t aNameSpaceID,
35
nsAtom* aAttribute);
36
void MaybeBroadcast();
37
void DropDocumentReference(); // notification that doc is going away
38
protected:
39
enum HookupAction { eHookupAdd = 0, eHookupRemove };
40
41
nsresult UpdateListenerHookup(Element* aElement, HookupAction aAction);
42
43
void RemoveListenerFor(Element& aBroadcaster, Element& aListener,
44
const nsAString& aAttr);
45
void AddListenerFor(Element& aBroadcaster, Element& aListener,
46
const nsAString& aAttr, ErrorResult& aRv);
47
48
nsresult ExecuteOnBroadcastHandlerFor(Element* aBroadcaster,
49
Element* aListener, nsAtom* aAttr);
50
// The out params of FindBroadcaster only have values that make sense when
51
// the method returns NS_FINDBROADCASTER_FOUND. In all other cases, the
52
// values of the out params should not be relied on (though *aListener and
53
// *aBroadcaster do need to be released if non-null, of course).
54
nsresult FindBroadcaster(Element* aElement, Element** aListener,
55
nsString& aBroadcasterID, nsString& aAttribute,
56
Element** aBroadcaster);
57
58
void SynchronizeBroadcastListener(Element* aBroadcaster, Element* aListener,
59
const nsAString& aAttr);
60
61
// This reference is nulled by the Document in it's destructor through
62
// DropDocumentReference().
63
Document* MOZ_NON_OWNING_REF mDocument;
64
65
/**
66
* A map from a broadcaster element to a list of listener elements.
67
*/
68
PLDHashTable* mBroadcasterMap;
69
70
class nsDelayedBroadcastUpdate {
71
public:
72
nsDelayedBroadcastUpdate(Element* aBroadcaster, Element* aListener,
73
const nsAString& aAttr)
74
: mBroadcaster(aBroadcaster),
75
mListener(aListener),
76
mAttr(aAttr),
77
mSetAttr(false),
78
mNeedsAttrChange(false) {}
79
80
nsDelayedBroadcastUpdate(Element* aBroadcaster, Element* aListener,
81
nsAtom* aAttrName, const nsAString& aAttr,
82
bool aSetAttr, bool aNeedsAttrChange)
83
: mBroadcaster(aBroadcaster),
84
mListener(aListener),
85
mAttr(aAttr),
86
mAttrName(aAttrName),
87
mSetAttr(aSetAttr),
88
mNeedsAttrChange(aNeedsAttrChange) {}
89
90
nsDelayedBroadcastUpdate(const nsDelayedBroadcastUpdate& aOther)
91
: mBroadcaster(aOther.mBroadcaster),
92
mListener(aOther.mListener),
93
mAttr(aOther.mAttr),
94
mAttrName(aOther.mAttrName),
95
mSetAttr(aOther.mSetAttr),
96
mNeedsAttrChange(aOther.mNeedsAttrChange) {}
97
98
nsCOMPtr<Element> mBroadcaster;
99
nsCOMPtr<Element> mListener;
100
// Note if mAttrName isn't used, this is the name of the attr, otherwise
101
// this is the value of the attribute.
102
nsString mAttr;
103
RefPtr<nsAtom> mAttrName;
104
bool mSetAttr;
105
bool mNeedsAttrChange;
106
107
class Comparator {
108
public:
109
static bool Equals(const nsDelayedBroadcastUpdate& a,
110
const nsDelayedBroadcastUpdate& b) {
111
return a.mBroadcaster == b.mBroadcaster && a.mListener == b.mListener &&
112
a.mAttrName == b.mAttrName;
113
}
114
};
115
};
116
nsTArray<nsDelayedBroadcastUpdate> mDelayedBroadcasters;
117
nsTArray<nsDelayedBroadcastUpdate> mDelayedAttrChangeBroadcasts;
118
bool mHandlingDelayedAttrChange;
119
bool mHandlingDelayedBroadcasters;
120
121
private:
122
~XULBroadcastManager();
123
};
124
125
} // namespace dom
126
} // namespace mozilla
127
128
#endif // mozilla_dom_XULBroadcastManager_h