2009-06-15 01:27:29 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2009-06-15 01:27:29 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
#ifndef mozilla_DOMEventTargetHelper_h_
|
|
|
|
#define mozilla_DOMEventTargetHelper_h_
|
2009-06-15 01:27:29 -07:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2012-08-30 20:45:16 -07:00
|
|
|
#include "nsGkAtoms.h"
|
2009-06-15 01:27:29 -07:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsIScriptContext.h"
|
2013-09-19 06:54:42 -07:00
|
|
|
#include "MainThreadUtils.h"
|
2012-06-18 19:30:09 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2012-12-06 17:39:51 -08:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2009-06-15 01:27:29 -07:00
|
|
|
|
2014-06-18 17:57:51 -07:00
|
|
|
struct JSCompartment;
|
2014-03-31 23:13:50 -07:00
|
|
|
|
2013-11-27 07:22:16 -08:00
|
|
|
namespace mozilla {
|
2014-03-31 23:13:50 -07:00
|
|
|
|
2013-11-27 07:22:16 -08:00
|
|
|
class ErrorResult;
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
#define NS_DOMEVENTTARGETHELPER_IID \
|
2013-11-27 07:22:16 -08:00
|
|
|
{ 0xa28385c6, 0x9451, 0x4d7e, \
|
|
|
|
{ 0xa3, 0xdd, 0xf4, 0xb6, 0x87, 0x2f, 0xa4, 0x76 } }
|
2013-03-09 03:34:29 -08:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
class DOMEventTargetHelper : public dom::EventTarget
|
2009-06-15 01:27:29 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper()
|
2013-04-24 19:44:24 -07:00
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
2014-03-31 23:13:50 -07:00
|
|
|
{
|
|
|
|
}
|
2014-08-05 06:19:51 -07:00
|
|
|
explicit DOMEventTargetHelper(nsPIDOMWindow* aWindow)
|
2013-04-24 19:44:24 -07:00
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
|
|
|
{
|
|
|
|
BindToOwner(aWindow);
|
2014-01-06 18:53:23 -08:00
|
|
|
}
|
2014-08-05 06:19:51 -07:00
|
|
|
explicit DOMEventTargetHelper(DOMEventTargetHelper* aOther)
|
2014-01-06 18:53:23 -08:00
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
|
|
|
{
|
|
|
|
BindToOwner(aOther);
|
2013-04-24 19:44:24 -07:00
|
|
|
}
|
|
|
|
|
2009-06-15 01:27:29 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(DOMEventTargetHelper)
|
2009-06-15 01:27:29 -07:00
|
|
|
|
|
|
|
NS_DECL_NSIDOMEVENTTARGET
|
2013-10-22 16:32:04 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
virtual EventListenerManager* GetExistingListenerManager() const MOZ_OVERRIDE;
|
|
|
|
virtual EventListenerManager* GetOrCreateListenerManager() MOZ_OVERRIDE;
|
2013-10-22 16:32:04 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
using dom::EventTarget::RemoveEventListener;
|
2013-04-16 14:16:08 -07:00
|
|
|
virtual void AddEventListener(const nsAString& aType,
|
2014-03-31 23:13:50 -07:00
|
|
|
dom::EventListener* aListener,
|
2013-04-16 14:16:08 -07:00
|
|
|
bool aCapture,
|
2014-03-31 23:13:50 -07:00
|
|
|
const dom::Nullable<bool>& aWantsUntrusted,
|
|
|
|
ErrorResult& aRv) MOZ_OVERRIDE;
|
2009-06-15 01:27:29 -07:00
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMEVENTTARGETHELPER_IID)
|
|
|
|
|
2012-02-07 18:53:33 -08:00
|
|
|
void GetParentObject(nsIScriptGlobalObject **aParentObject)
|
|
|
|
{
|
2013-04-04 02:30:36 -07:00
|
|
|
if (mParentObject) {
|
|
|
|
CallQueryInterface(mParentObject, aParentObject);
|
|
|
|
} else {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aParentObject = nullptr;
|
2012-02-07 18:53:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
static DOMEventTargetHelper* FromSupports(nsISupports* aSupports)
|
2012-02-07 18:53:33 -08:00
|
|
|
{
|
2014-03-31 23:13:50 -07:00
|
|
|
dom::EventTarget* target = static_cast<dom::EventTarget*>(aSupports);
|
2012-02-07 18:53:33 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2014-03-31 23:13:50 -07:00
|
|
|
nsCOMPtr<dom::EventTarget> target_qi = do_QueryInterface(aSupports);
|
2012-02-07 18:53:33 -08:00
|
|
|
|
|
|
|
// If this assertion fires the QI implementation for the object in
|
2013-04-05 17:44:26 -07:00
|
|
|
// question doesn't use the EventTarget pointer as the
|
2012-02-07 18:53:33 -08:00
|
|
|
// nsISupports pointer. That must be fixed, or we'll crash...
|
|
|
|
NS_ASSERTION(target_qi == target, "Uh, fix QI!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
return static_cast<DOMEventTargetHelper*>(target);
|
2012-02-07 18:53:33 -08:00
|
|
|
}
|
|
|
|
|
2012-10-28 21:33:51 -07:00
|
|
|
bool HasListenersFor(nsIAtom* aTypeWithOn)
|
2009-06-15 01:27:29 -07:00
|
|
|
{
|
2012-10-28 21:33:51 -07:00
|
|
|
return mListenerManager && mListenerManager->HasListenersFor(aTypeWithOn);
|
2009-06-15 01:27:29 -07:00
|
|
|
}
|
|
|
|
|
2012-08-30 20:45:16 -07:00
|
|
|
nsresult SetEventHandler(nsIAtom* aType,
|
|
|
|
JSContext* aCx,
|
|
|
|
const JS::Value& aValue);
|
2014-03-31 23:13:50 -07:00
|
|
|
using dom::EventTarget::SetEventHandler;
|
2012-08-30 20:45:16 -07:00
|
|
|
void GetEventHandler(nsIAtom* aType,
|
|
|
|
JSContext* aCx,
|
|
|
|
JS::Value* aValue);
|
2014-03-31 23:13:50 -07:00
|
|
|
using dom::EventTarget::GetEventHandler;
|
2013-05-30 14:46:39 -07:00
|
|
|
virtual nsIDOMWindow* GetOwnerGlobal() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return nsPIDOMWindow::GetOuterFromCurrentInner(GetOwner());
|
|
|
|
}
|
2009-06-15 01:27:29 -07:00
|
|
|
|
|
|
|
nsresult CheckInnerWindowCorrectness()
|
|
|
|
{
|
2013-04-04 02:30:36 -07:00
|
|
|
NS_ENSURE_STATE(!mHasOrHasHadOwnerWindow || mOwnerWindow);
|
2014-05-20 12:50:57 -07:00
|
|
|
if (mOwnerWindow && !mOwnerWindow->IsCurrentInnerWindow()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-06-15 01:27:29 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-12 17:56:07 -07:00
|
|
|
|
2013-04-04 02:30:36 -07:00
|
|
|
nsPIDOMWindow* GetOwner() const { return mOwnerWindow; }
|
|
|
|
void BindToOwner(nsIGlobalObject* aOwner);
|
2012-03-12 17:56:07 -07:00
|
|
|
void BindToOwner(nsPIDOMWindow* aOwner);
|
2014-03-31 23:13:50 -07:00
|
|
|
void BindToOwner(DOMEventTargetHelper* aOther);
|
2012-03-12 17:56:07 -07:00
|
|
|
virtual void DisconnectFromOwner();
|
2013-04-04 02:30:36 -07:00
|
|
|
nsIGlobalObject* GetParentObject() const { return mParentObject; }
|
|
|
|
bool HasOrHasHadOwner() { return mHasOrHasHadOwnerWindow; }
|
2013-11-27 07:22:16 -08:00
|
|
|
|
|
|
|
virtual void EventListenerAdded(nsIAtom* aType) MOZ_OVERRIDE;
|
|
|
|
virtual void EventListenerRemoved(nsIAtom* aType) MOZ_OVERRIDE;
|
|
|
|
virtual void EventListenerWasAdded(const nsAString& aType,
|
2014-03-31 23:13:50 -07:00
|
|
|
ErrorResult& aRv,
|
2013-11-27 07:22:16 -08:00
|
|
|
JSCompartment* aCompartment = nullptr) {}
|
|
|
|
virtual void EventListenerWasRemoved(const nsAString& aType,
|
2014-03-31 23:13:50 -07:00
|
|
|
ErrorResult& aRv,
|
2013-11-27 07:22:16 -08:00
|
|
|
JSCompartment* aCompartment = nullptr) {}
|
2014-07-14 14:15:23 -07:00
|
|
|
|
|
|
|
// Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
|
|
|
|
nsresult DispatchTrustedEvent(const nsAString& aEventName);
|
2009-06-15 01:27:29 -07:00
|
|
|
protected:
|
2014-06-23 12:56:07 -07:00
|
|
|
virtual ~DOMEventTargetHelper();
|
|
|
|
|
2013-09-05 14:08:24 -07:00
|
|
|
nsresult WantsUntrusted(bool* aRetVal);
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
nsRefPtr<EventListenerManager> mListenerManager;
|
2012-09-27 13:11:31 -07:00
|
|
|
// Make |event| trusted and dispatch |aEvent| to |this|.
|
|
|
|
nsresult DispatchTrustedEvent(nsIDOMEvent* aEvent);
|
2013-07-09 10:30:58 -07:00
|
|
|
|
|
|
|
virtual void LastRelease() {}
|
2012-03-12 17:56:07 -07:00
|
|
|
private:
|
2013-04-04 02:30:36 -07:00
|
|
|
// Inner window or sandbox.
|
|
|
|
nsIGlobalObject* mParentObject;
|
2014-05-20 12:50:57 -07:00
|
|
|
// mParentObject pre QI-ed and cached (inner window)
|
2013-04-04 02:30:36 -07:00
|
|
|
// (it is needed for off main thread access)
|
|
|
|
nsPIDOMWindow* mOwnerWindow;
|
|
|
|
bool mHasOrHasHadOwnerWindow;
|
2009-06-15 01:27:29 -07:00
|
|
|
};
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(DOMEventTargetHelper,
|
2013-03-09 03:34:29 -08:00
|
|
|
NS_DOMEVENTTARGETHELPER_IID)
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
// XPIDL event handlers
|
2012-08-30 20:45:16 -07:00
|
|
|
#define NS_IMPL_EVENT_HANDLER(_class, _event) \
|
2014-01-09 09:39:36 -08:00
|
|
|
NS_IMETHODIMP _class::GetOn##_event(JSContext* aCx, \
|
|
|
|
JS::MutableHandle<JS::Value> aValue) \
|
2012-02-07 18:53:33 -08:00
|
|
|
{ \
|
2014-01-09 09:39:36 -08:00
|
|
|
GetEventHandler(nsGkAtoms::on##_event, aCx, aValue.address()); \
|
2012-08-30 20:45:16 -07:00
|
|
|
return NS_OK; \
|
2012-02-07 18:53:33 -08:00
|
|
|
} \
|
2012-08-30 20:45:16 -07:00
|
|
|
NS_IMETHODIMP _class::SetOn##_event(JSContext* aCx, \
|
2014-01-09 09:39:36 -08:00
|
|
|
JS::Handle<JS::Value> aValue) \
|
2012-02-07 18:53:33 -08:00
|
|
|
{ \
|
2012-08-30 20:45:16 -07:00
|
|
|
return SetEventHandler(nsGkAtoms::on##_event, aCx, aValue); \
|
2012-02-07 18:53:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NS_IMPL_FORWARD_EVENT_HANDLER(_class, _event, _baseclass) \
|
2014-01-09 09:39:36 -08:00
|
|
|
NS_IMETHODIMP _class::GetOn##_event(JSContext* aCx, \
|
|
|
|
JS::MutableHandle<JS::Value> aValue) \
|
2012-02-07 18:53:33 -08:00
|
|
|
{ \
|
2012-08-30 20:45:16 -07:00
|
|
|
return _baseclass::GetOn##_event(aCx, aValue); \
|
2012-02-07 18:53:33 -08:00
|
|
|
} \
|
2012-08-30 20:45:16 -07:00
|
|
|
NS_IMETHODIMP _class::SetOn##_event(JSContext* aCx, \
|
2014-01-09 09:39:36 -08:00
|
|
|
JS::Handle<JS::Value> aValue) \
|
2012-02-07 18:53:33 -08:00
|
|
|
{ \
|
2012-08-30 20:45:16 -07:00
|
|
|
return _baseclass::SetOn##_event(aCx, aValue); \
|
2012-02-07 18:53:33 -08:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
// WebIDL event handlers
|
|
|
|
#define IMPL_EVENT_HANDLER(_event) \
|
2012-11-09 08:00:25 -08:00
|
|
|
inline mozilla::dom::EventHandlerNonNull* GetOn##_event() \
|
2012-11-09 08:00:25 -08:00
|
|
|
{ \
|
2013-08-16 03:06:24 -07:00
|
|
|
if (NS_IsMainThread()) { \
|
|
|
|
return GetEventHandler(nsGkAtoms::on##_event, EmptyString()); \
|
|
|
|
} \
|
|
|
|
return GetEventHandler(nullptr, NS_LITERAL_STRING(#_event)); \
|
2012-11-09 08:00:25 -08:00
|
|
|
} \
|
2013-09-17 04:01:28 -07:00
|
|
|
inline void SetOn##_event(mozilla::dom::EventHandlerNonNull* aCallback) \
|
2012-11-09 08:00:25 -08:00
|
|
|
{ \
|
2013-08-16 03:06:24 -07:00
|
|
|
if (NS_IsMainThread()) { \
|
2013-09-17 04:01:28 -07:00
|
|
|
SetEventHandler(nsGkAtoms::on##_event, EmptyString(), aCallback); \
|
2013-08-16 03:06:24 -07:00
|
|
|
} else { \
|
2013-09-17 04:01:28 -07:00
|
|
|
SetEventHandler(nullptr, NS_LITERAL_STRING(#_event), aCallback); \
|
2013-08-16 03:06:24 -07:00
|
|
|
} \
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
|
|
|
|
2012-06-03 09:33:52 -07:00
|
|
|
/* Use this macro to declare functions that forward the behavior of this
|
|
|
|
* interface to another object.
|
|
|
|
* This macro doesn't forward PreHandleEvent because sometimes subclasses
|
|
|
|
* want to override it.
|
|
|
|
*/
|
|
|
|
#define NS_FORWARD_NSIDOMEVENTTARGET_NOPREHANDLEEVENT(_to) \
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHOD AddEventListener(const nsAString & type, nsIDOMEventListener *listener, bool useCapture, bool wantsUntrusted, uint8_t _argc) { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to AddEventListener(type, listener, useCapture, wantsUntrusted, _argc); \
|
|
|
|
} \
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHOD AddSystemEventListener(const nsAString & type, nsIDOMEventListener *listener, bool aUseCapture, bool aWantsUntrusted, uint8_t _argc) { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to AddSystemEventListener(type, listener, aUseCapture, aWantsUntrusted, _argc); \
|
|
|
|
} \
|
2012-07-06 13:31:49 -07:00
|
|
|
NS_IMETHOD RemoveEventListener(const nsAString & type, nsIDOMEventListener *listener, bool useCapture) { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to RemoveEventListener(type, listener, useCapture); \
|
|
|
|
} \
|
|
|
|
NS_IMETHOD RemoveSystemEventListener(const nsAString & type, nsIDOMEventListener *listener, bool aUseCapture) { \
|
|
|
|
return _to RemoveSystemEventListener(type, listener, aUseCapture); \
|
|
|
|
} \
|
2012-07-06 13:31:49 -07:00
|
|
|
NS_IMETHOD DispatchEvent(nsIDOMEvent *evt, bool *_retval) { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to DispatchEvent(evt, _retval); \
|
|
|
|
} \
|
2013-03-17 00:55:17 -07:00
|
|
|
virtual mozilla::dom::EventTarget* GetTargetForDOMEvent() { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to GetTargetForDOMEvent(); \
|
|
|
|
} \
|
2013-03-17 00:55:17 -07:00
|
|
|
virtual mozilla::dom::EventTarget* GetTargetForEventTargetChain() { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to GetTargetForEventTargetChain(); \
|
|
|
|
} \
|
2014-03-17 21:48:20 -07:00
|
|
|
virtual nsresult WillHandleEvent( \
|
|
|
|
mozilla::EventChainPostVisitor & aVisitor) { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to WillHandleEvent(aVisitor); \
|
|
|
|
} \
|
2014-03-17 21:48:20 -07:00
|
|
|
virtual nsresult PostHandleEvent( \
|
|
|
|
mozilla::EventChainPostVisitor & aVisitor) { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to PostHandleEvent(aVisitor); \
|
|
|
|
} \
|
2013-10-01 20:46:04 -07:00
|
|
|
virtual nsresult DispatchDOMEvent(mozilla::WidgetEvent* aEvent, nsIDOMEvent* aDOMEvent, nsPresContext* aPresContext, nsEventStatus* aEventStatus) { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to DispatchDOMEvent(aEvent, aDOMEvent, aPresContext, aEventStatus); \
|
|
|
|
} \
|
2014-03-16 23:56:53 -07:00
|
|
|
virtual mozilla::EventListenerManager* GetOrCreateListenerManager() { \
|
2013-10-22 16:32:04 -07:00
|
|
|
return _to GetOrCreateListenerManager(); \
|
|
|
|
} \
|
2014-03-16 23:56:53 -07:00
|
|
|
virtual mozilla::EventListenerManager* GetExistingListenerManager() const { \
|
2013-10-22 16:32:04 -07:00
|
|
|
return _to GetExistingListenerManager(); \
|
2012-06-03 09:33:52 -07:00
|
|
|
} \
|
2012-07-06 13:14:07 -07:00
|
|
|
virtual nsIScriptContext * GetContextForEventHandlers(nsresult *aRv) { \
|
2012-06-03 09:33:52 -07:00
|
|
|
return _to GetContextForEventHandlers(aRv); \
|
|
|
|
} \
|
|
|
|
virtual JSContext * GetJSContextForEventHandlers(void) { \
|
|
|
|
return _to GetJSContextForEventHandlers(); \
|
2013-04-16 14:16:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NS_REALLY_FORWARD_NSIDOMEVENTTARGET(_class) \
|
|
|
|
using _class::AddEventListener; \
|
|
|
|
using _class::RemoveEventListener; \
|
2013-10-22 16:32:04 -07:00
|
|
|
NS_FORWARD_NSIDOMEVENTTARGET(_class::) \
|
2014-03-16 23:56:53 -07:00
|
|
|
virtual mozilla::EventListenerManager* \
|
2013-10-22 16:32:04 -07:00
|
|
|
GetOrCreateListenerManager() { \
|
|
|
|
return _class::GetOrCreateListenerManager(); \
|
|
|
|
} \
|
2014-03-16 23:56:53 -07:00
|
|
|
virtual mozilla::EventListenerManager* \
|
2013-10-22 16:32:04 -07:00
|
|
|
GetExistingListenerManager() const { \
|
|
|
|
return _class::GetExistingListenerManager(); \
|
|
|
|
}
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
#endif // mozilla_DOMEventTargetHelper_h_
|