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
|
|
|
|
|
|
|
#ifndef nsDOMEventTargetHelper_h_
|
|
|
|
#define nsDOMEventTargetHelper_h_
|
|
|
|
|
|
|
|
#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"
|
2011-06-23 19:18:01 -07:00
|
|
|
#include "nsEventListenerManager.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"
|
2012-12-06 17:39:51 -08:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2009-06-15 01:27:29 -07:00
|
|
|
|
2013-11-27 07:22:16 -08:00
|
|
|
class JSCompartment;
|
|
|
|
namespace mozilla {
|
|
|
|
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
|
|
|
|
2012-12-06 17:39:51 -08:00
|
|
|
class nsDOMEventTargetHelper : public mozilla::dom::EventTarget
|
2009-06-15 01:27:29 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-04-24 19:44:24 -07:00
|
|
|
nsDOMEventTargetHelper()
|
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
|
|
|
{}
|
|
|
|
nsDOMEventTargetHelper(nsPIDOMWindow* aWindow)
|
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
|
|
|
{
|
|
|
|
BindToOwner(aWindow);
|
|
|
|
// All objects coming through here are WebIDL objects
|
2014-01-06 18:53:23 -08:00
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
nsDOMEventTargetHelper(nsDOMEventTargetHelper* aOther)
|
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
|
|
|
{
|
|
|
|
BindToOwner(aOther);
|
|
|
|
// All objects coming through here are WebIDL objects
|
2013-04-24 19:44:24 -07:00
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2011-12-08 00:31:14 -08:00
|
|
|
virtual ~nsDOMEventTargetHelper();
|
2009-06-15 01:27:29 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2012-09-24 04:05:30 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsDOMEventTargetHelper)
|
2009-06-15 01:27:29 -07:00
|
|
|
|
|
|
|
NS_DECL_NSIDOMEVENTTARGET
|
2013-10-22 16:32:04 -07:00
|
|
|
|
|
|
|
virtual nsEventListenerManager*
|
|
|
|
GetExistingListenerManager() const MOZ_OVERRIDE;
|
|
|
|
virtual nsEventListenerManager*
|
|
|
|
GetOrCreateListenerManager() MOZ_OVERRIDE;
|
|
|
|
|
2013-04-16 14:16:08 -07:00
|
|
|
using mozilla::dom::EventTarget::RemoveEventListener;
|
|
|
|
virtual void AddEventListener(const nsAString& aType,
|
2013-09-23 17:25:17 -07:00
|
|
|
mozilla::dom::EventListener* aListener,
|
2013-04-16 14:16:08 -07:00
|
|
|
bool aCapture,
|
|
|
|
const mozilla::dom::Nullable<bool>& aWantsUntrusted,
|
|
|
|
mozilla::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsDOMEventTargetHelper* FromSupports(nsISupports* aSupports)
|
|
|
|
{
|
2013-04-05 17:44:26 -07:00
|
|
|
mozilla::dom::EventTarget* target =
|
|
|
|
static_cast<mozilla::dom::EventTarget*>(aSupports);
|
2012-02-07 18:53:33 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2013-04-05 17:44:26 -07:00
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> target_qi =
|
2012-02-07 18:53:33 -08:00
|
|
|
do_QueryInterface(aSupports);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
return static_cast<nsDOMEventTargetHelper*>(target);
|
|
|
|
}
|
|
|
|
|
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);
|
2013-05-09 10:07:16 -07:00
|
|
|
using mozilla::dom::EventTarget::SetEventHandler;
|
2012-08-30 20:45:16 -07:00
|
|
|
void GetEventHandler(nsIAtom* aType,
|
|
|
|
JSContext* aCx,
|
|
|
|
JS::Value* aValue);
|
2013-05-09 10:07:16 -07:00
|
|
|
using mozilla::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);
|
|
|
|
if (mOwnerWindow) {
|
|
|
|
NS_ASSERTION(mOwnerWindow->IsInnerWindow(), "Should have inner window here!\n");
|
|
|
|
nsPIDOMWindow* outer = mOwnerWindow->GetOuterWindow();
|
|
|
|
if (!outer || outer->GetCurrentInnerWindow() != mOwnerWindow) {
|
2009-06-15 01:27:29 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
void BindToOwner(nsDOMEventTargetHelper* aOther);
|
|
|
|
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,
|
|
|
|
mozilla::ErrorResult& aRv,
|
|
|
|
JSCompartment* aCompartment = nullptr) {}
|
|
|
|
virtual void EventListenerWasRemoved(const nsAString& aType,
|
|
|
|
mozilla::ErrorResult& aRv,
|
|
|
|
JSCompartment* aCompartment = nullptr) {}
|
2009-06-15 01:27:29 -07:00
|
|
|
protected:
|
2013-09-05 14:08:24 -07:00
|
|
|
nsresult WantsUntrusted(bool* aRetVal);
|
|
|
|
|
2011-06-23 19:18:01 -07:00
|
|
|
nsRefPtr<nsEventListenerManager> mListenerManager;
|
2012-09-27 13:11:31 -07:00
|
|
|
// Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
|
|
|
|
nsresult DispatchTrustedEvent(const nsAString& aEventName);
|
|
|
|
// 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;
|
|
|
|
// mParentObject pre QI-ed and cached
|
|
|
|
// (it is needed for off main thread access)
|
|
|
|
nsPIDOMWindow* mOwnerWindow;
|
|
|
|
bool mHasOrHasHadOwnerWindow;
|
2009-06-15 01:27:29 -07:00
|
|
|
};
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsDOMEventTargetHelper,
|
|
|
|
NS_DOMEVENTTARGETHELPER_IID)
|
|
|
|
|
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(); \
|
|
|
|
} \
|
|
|
|
virtual nsresult WillHandleEvent(nsEventChainPostVisitor & aVisitor) { \
|
|
|
|
return _to WillHandleEvent(aVisitor); \
|
|
|
|
} \
|
|
|
|
virtual nsresult PostHandleEvent(nsEventChainPostVisitor & aVisitor) { \
|
|
|
|
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); \
|
|
|
|
} \
|
2013-10-22 16:32:04 -07:00
|
|
|
virtual nsEventListenerManager * GetOrCreateListenerManager() { \
|
|
|
|
return _to GetOrCreateListenerManager(); \
|
|
|
|
} \
|
|
|
|
virtual nsEventListenerManager * GetExistingListenerManager() const { \
|
|
|
|
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::) \
|
|
|
|
virtual nsEventListenerManager* \
|
|
|
|
GetOrCreateListenerManager() { \
|
|
|
|
return _class::GetOrCreateListenerManager(); \
|
|
|
|
} \
|
|
|
|
virtual nsEventListenerManager* \
|
|
|
|
GetExistingListenerManager() const { \
|
|
|
|
return _class::GetExistingListenerManager(); \
|
|
|
|
}
|
2012-06-03 09:33:52 -07:00
|
|
|
|
2009-06-15 01:27:29 -07:00
|
|
|
#endif // nsDOMEventTargetHelper_h_
|