2012-03-31 08:42:20 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_workers_listenermanager_h__
|
|
|
|
#define mozilla_dom_workers_listenermanager_h__
|
|
|
|
|
|
|
|
#include "mozilla/dom/workers/Workers.h"
|
|
|
|
|
2013-03-28 06:47:25 +04:00
|
|
|
#include "mozilla/LinkedList.h"
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
class EventTarget;
|
|
|
|
|
|
|
|
// XXX Current impl doesn't divide into phases.
|
|
|
|
// XXX Current impl doesn't handle event target chains.
|
|
|
|
class EventListenerManager
|
|
|
|
{
|
2013-03-28 06:47:25 +04:00
|
|
|
public:
|
|
|
|
struct ListenerCollection;
|
|
|
|
|
|
|
|
private:
|
|
|
|
LinkedList<ListenerCollection> mCollections;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
EventListenerManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
~EventListenerManager();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2012-04-27 00:57:33 +04:00
|
|
|
_trace(JSTracer* aTrc) const
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-03-28 06:47:25 +04:00
|
|
|
if (!mCollections.isEmpty()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
TraceInternal(aTrc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-04-27 00:57:33 +04:00
|
|
|
_finalize(JSFreeOp* aFop)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-03-28 06:47:25 +04:00
|
|
|
if (!mCollections.isEmpty()) {
|
2012-03-19 18:34:55 +04:00
|
|
|
FinalizeInternal(aFop);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Phase
|
|
|
|
{
|
|
|
|
All = 0,
|
|
|
|
Capturing,
|
|
|
|
Onfoo,
|
|
|
|
Bubbling
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2013-05-18 05:48:25 +04:00
|
|
|
AddEventListener(JSContext* aCx, const jsid& aType,
|
|
|
|
JS::Handle<JSObject*> aListener,
|
2012-05-06 05:15:11 +04:00
|
|
|
bool aCapturing, bool aWantsUntrusted, ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
Add(aCx, aType, aListener, aCapturing ? Capturing : Bubbling,
|
|
|
|
aWantsUntrusted, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-18 05:48:25 +04:00
|
|
|
RemoveEventListener(JSContext* aCx, const jsid& aType,
|
|
|
|
JS::Handle<JSObject*> aListener, bool aCapturing)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-03-28 06:47:25 +04:00
|
|
|
if (mCollections.isEmpty()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Remove(aCx, aType, aListener, aCapturing ? Capturing : Bubbling, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DispatchEvent(JSContext* aCx, const EventTarget& aTarget, JSObject* aEvent,
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult& aRv) const;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
JSObject*
|
|
|
|
GetEventListener(const jsid& aType) const;
|
|
|
|
|
|
|
|
void
|
2013-05-18 05:48:25 +04:00
|
|
|
SetEventListener(JSContext* aCx, const jsid& aType,
|
|
|
|
JS::Handle<JSObject*> aListener,
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult& aRv)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-05-18 05:48:25 +04:00
|
|
|
JS::Rooted<JSObject*> existing(aCx, GetEventListener(aType));
|
2012-03-31 08:42:20 +04:00
|
|
|
if (existing) {
|
|
|
|
Remove(aCx, aType, existing, Onfoo, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aListener) {
|
|
|
|
Add(aCx, aType, aListener, Onfoo, false, aRv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasListeners() const
|
|
|
|
{
|
2013-03-28 06:47:25 +04:00
|
|
|
return !mCollections.isEmpty();
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasListenersForType(JSContext* aCx, const jsid& aType) const
|
|
|
|
{
|
|
|
|
return HasListeners() && HasListenersForTypeInternal(aCx, aType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void
|
|
|
|
TraceInternal(JSTracer* aTrc) const;
|
|
|
|
|
|
|
|
void
|
2012-03-19 18:34:55 +04:00
|
|
|
FinalizeInternal(JSFreeOp* aFop);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
void
|
2013-05-18 05:48:25 +04:00
|
|
|
Add(JSContext* aCx, const jsid& aType, JS::Handle<JSObject*> aListener,
|
|
|
|
Phase aPhase, bool aWantsUntrusted, ErrorResult& aRv);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
void
|
2013-05-18 05:48:25 +04:00
|
|
|
Remove(JSContext* aCx, const jsid& aType, JS::Handle<JSObject*> aListener,
|
|
|
|
Phase aPhase, bool aClearEmpty);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
HasListenersForTypeInternal(JSContext* aCx, const jsid& aType) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_workers_listenermanager_h__ */
|