2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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 08:27:29 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
#ifndef mozilla_DOMEventTargetHelper_h_
|
|
|
|
#define mozilla_DOMEventTargetHelper_h_
|
2009-06-15 08:27:29 +00:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2012-08-31 03:45:16 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2009-06-15 08:27:29 +00:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "nsIScriptContext.h"
|
2013-09-19 13:54:42 +00:00
|
|
|
#include "MainThreadUtils.h"
|
2012-06-19 02:30:09 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-03-17 06:56:53 +00:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2018-05-04 16:25:05 +00:00
|
|
|
#include "mozilla/LinkedList.h"
|
2012-12-07 01:39:51 +00:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2009-06-15 08:27:29 +00:00
|
|
|
|
2013-11-27 15:22:16 +00:00
|
|
|
namespace mozilla {
|
2014-04-01 06:13:50 +00:00
|
|
|
|
2013-11-27 15:22:16 +00:00
|
|
|
class ErrorResult;
|
|
|
|
|
2018-04-20 16:55:30 +00:00
|
|
|
namespace dom {
|
2019-01-02 13:05:23 +00:00
|
|
|
class Document;
|
2018-04-20 16:55:30 +00:00
|
|
|
class Event;
|
|
|
|
} // namespace dom
|
|
|
|
|
2013-03-09 11:34:29 +00:00
|
|
|
#define NS_DOMEVENTTARGETHELPER_IID \
|
2018-11-30 10:46:48 +00:00
|
|
|
{ \
|
2013-11-27 15:22:16 +00:00
|
|
|
0xa28385c6, 0x9451, 0x4d7e, { \
|
|
|
|
0xa3, 0xdd, 0xf4, 0xb6, 0x87, 0x2f, 0xa4, 0x76 \
|
|
|
|
} \
|
|
|
|
}
|
2013-03-09 11:34:29 +00:00
|
|
|
|
2018-05-04 16:25:05 +00:00
|
|
|
class DOMEventTargetHelper : public dom::EventTarget,
|
|
|
|
public LinkedListElement<DOMEventTargetHelper> {
|
2009-06-15 08:27:29 +00:00
|
|
|
public:
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper()
|
2013-04-25 02:44:24 +00:00
|
|
|
: mParentObject(nullptr),
|
|
|
|
mOwnerWindow(nullptr),
|
|
|
|
mHasOrHasHadOwnerWindow(false),
|
2017-04-18 11:51:27 +00:00
|
|
|
mIsKeptAlive(false) {}
|
2016-01-30 17:05:36 +00:00
|
|
|
explicit DOMEventTargetHelper(nsPIDOMWindowInner* aWindow)
|
2013-04-25 02:44:24 +00:00
|
|
|
: mParentObject(nullptr),
|
|
|
|
mOwnerWindow(nullptr),
|
|
|
|
mHasOrHasHadOwnerWindow(false),
|
2017-04-18 11:51:27 +00:00
|
|
|
mIsKeptAlive(false) {
|
2018-04-16 13:08:51 +00:00
|
|
|
// Be careful not to call the virtual BindToOwner() in a
|
|
|
|
// constructor.
|
|
|
|
nsIGlobalObject* global = aWindow ? aWindow->AsGlobal() : nullptr;
|
|
|
|
BindToOwnerInternal(global);
|
2014-01-07 02:53:23 +00:00
|
|
|
}
|
2015-01-05 15:23:36 +00:00
|
|
|
explicit DOMEventTargetHelper(nsIGlobalObject* aGlobalObject)
|
|
|
|
: mParentObject(nullptr),
|
|
|
|
mOwnerWindow(nullptr),
|
|
|
|
mHasOrHasHadOwnerWindow(false),
|
2017-04-18 11:51:27 +00:00
|
|
|
mIsKeptAlive(false) {
|
2018-04-16 13:08:51 +00:00
|
|
|
// Be careful not to call the virtual BindToOwner() in a
|
|
|
|
// constructor.
|
|
|
|
BindToOwnerInternal(aGlobalObject);
|
2015-01-05 15:23:36 +00:00
|
|
|
}
|
2014-08-05 13:19:51 +00:00
|
|
|
explicit DOMEventTargetHelper(DOMEventTargetHelper* aOther)
|
2014-01-07 02:53:23 +00:00
|
|
|
: mParentObject(nullptr),
|
|
|
|
mOwnerWindow(nullptr),
|
|
|
|
mHasOrHasHadOwnerWindow(false),
|
2017-04-18 11:51:27 +00:00
|
|
|
mIsKeptAlive(false) {
|
2018-04-16 13:08:51 +00:00
|
|
|
// Be careful not to call the virtual BindToOwner() in a
|
|
|
|
// constructor.
|
|
|
|
if (!aOther) {
|
|
|
|
BindToOwnerInternal(static_cast<nsIGlobalObject*>(nullptr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BindToOwnerInternal(aOther->GetParentObject());
|
|
|
|
mHasOrHasHadOwnerWindow = aOther->HasOrHasHadOwner();
|
2013-04-25 02:44:24 +00:00
|
|
|
}
|
|
|
|
|
2009-06-15 08:27:29 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(DOMEventTargetHelper)
|
2009-06-15 08:27:29 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual EventListenerManager* GetExistingListenerManager() const override;
|
|
|
|
virtual EventListenerManager* GetOrCreateListenerManager() override;
|
2013-10-22 23:32:04 +00:00
|
|
|
|
2018-04-05 17:42:42 +00:00
|
|
|
bool ComputeDefaultWantsUntrusted(ErrorResult& aRv) override;
|
2009-06-15 08:27:29 +00:00
|
|
|
|
2018-04-05 17:42:41 +00:00
|
|
|
using EventTarget::DispatchEvent;
|
|
|
|
bool DispatchEvent(dom::Event& aEvent, dom::CallerType aCallerType,
|
|
|
|
ErrorResult& aRv) override;
|
|
|
|
|
2018-04-05 17:42:41 +00:00
|
|
|
void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
|
|
|
|
|
2018-04-05 17:42:41 +00:00
|
|
|
nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;
|
|
|
|
|
2013-03-09 11:34:29 +00:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMEVENTTARGETHELPER_IID)
|
|
|
|
|
2012-02-08 02:53:33 +00:00
|
|
|
void GetParentObject(nsIScriptGlobalObject** aParentObject) {
|
2013-04-04 09:30:36 +00:00
|
|
|
if (mParentObject) {
|
|
|
|
CallQueryInterface(mParentObject, aParentObject);
|
|
|
|
} else {
|
2012-07-30 14:20:58 +00:00
|
|
|
*aParentObject = nullptr;
|
2012-02-08 02:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
static DOMEventTargetHelper* FromSupports(nsISupports* aSupports) {
|
|
|
|
dom::EventTarget* target = static_cast<dom::EventTarget*>(aSupports);
|
2012-02-08 02:53:33 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2014-04-01 06:13:50 +00:00
|
|
|
nsCOMPtr<dom::EventTarget> target_qi = do_QueryInterface(aSupports);
|
2012-02-08 02:53:33 +00:00
|
|
|
|
|
|
|
// If this assertion fires the QI implementation for the object in
|
2013-04-06 00:44:26 +00:00
|
|
|
// question doesn't use the EventTarget pointer as the
|
2012-02-08 02:53:33 +00:00
|
|
|
// nsISupports pointer. That must be fixed, or we'll crash...
|
|
|
|
NS_ASSERTION(target_qi == target, "Uh, fix QI!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
return static_cast<DOMEventTargetHelper*>(target);
|
2012-02-08 02:53:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 02:40:32 +00:00
|
|
|
bool HasListenersFor(const nsAString& aType) const {
|
2015-01-15 16:58:40 +00:00
|
|
|
return mListenerManager && mListenerManager->HasListenersFor(aType);
|
|
|
|
}
|
|
|
|
|
2018-03-29 02:40:32 +00:00
|
|
|
bool HasListenersFor(nsAtom* aTypeWithOn) const {
|
2012-10-29 04:33:51 +00:00
|
|
|
return mListenerManager && mListenerManager->HasListenersFor(aTypeWithOn);
|
2009-06-15 08:27:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 13:26:56 +00:00
|
|
|
virtual nsPIDOMWindowOuter* GetOwnerGlobalForBindingsInternal() override {
|
2016-01-30 17:05:36 +00:00
|
|
|
return nsPIDOMWindowOuter::GetFromCurrentInner(GetOwner());
|
2013-05-30 21:46:39 +00:00
|
|
|
}
|
2009-06-15 08:27:29 +00:00
|
|
|
|
2019-04-01 16:46:46 +00:00
|
|
|
nsresult CheckCurrentGlobalCorrectness() const;
|
2012-03-13 00:56:07 +00:00
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsPIDOMWindowInner* GetOwner() const { return mOwnerWindow; }
|
2016-04-20 22:04:29 +00:00
|
|
|
// Like GetOwner, but only returns non-null if the window being returned is
|
|
|
|
// current (in the "current document" sense of the HTML spec).
|
|
|
|
nsPIDOMWindowInner* GetWindowIfCurrent() const;
|
|
|
|
// Returns the document associated with this event target, if that document is
|
|
|
|
// the current document of its browsing context. Will return null otherwise.
|
2019-01-02 13:05:23 +00:00
|
|
|
mozilla::dom::Document* GetDocumentIfCurrent() const;
|
2018-04-16 13:08:51 +00:00
|
|
|
|
|
|
|
// DETH subclasses may override the BindToOwner(nsIGlobalObject*) method
|
|
|
|
// to take action when dynamically binding to a new global. This is only
|
|
|
|
// called on rebind since virtual methods cannot be called from the
|
|
|
|
// constructor. The other BindToOwner() methods will call into this
|
|
|
|
// method.
|
|
|
|
//
|
|
|
|
// NOTE: Any overrides of BindToOwner() *must* invoke
|
|
|
|
// DOMEventTargetHelper::BindToOwner(aOwner).
|
|
|
|
virtual void BindToOwner(nsIGlobalObject* aOwner);
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
void BindToOwner(nsPIDOMWindowInner* aOwner);
|
2014-04-01 06:13:50 +00:00
|
|
|
void BindToOwner(DOMEventTargetHelper* aOther);
|
2018-04-16 13:08:51 +00:00
|
|
|
|
2017-04-18 11:51:27 +00:00
|
|
|
virtual void DisconnectFromOwner();
|
2017-11-20 18:59:22 +00:00
|
|
|
using EventTarget::GetParentObject;
|
2018-05-04 17:01:32 +00:00
|
|
|
nsIGlobalObject* GetOwnerGlobal() const final { return mParentObject; }
|
2013-04-04 09:30:36 +00:00
|
|
|
bool HasOrHasHadOwner() { return mHasOrHasHadOwnerWindow; }
|
2013-11-27 15:22:16 +00:00
|
|
|
|
2017-10-02 22:05:19 +00:00
|
|
|
virtual void EventListenerAdded(nsAtom* aType) override;
|
2017-04-18 11:51:27 +00:00
|
|
|
|
2017-10-02 22:05:19 +00:00
|
|
|
virtual void EventListenerRemoved(nsAtom* aType) override;
|
2017-04-18 11:51:27 +00:00
|
|
|
|
2014-07-14 21:15:23 +00:00
|
|
|
// Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
|
|
|
|
nsresult DispatchTrustedEvent(const nsAString& aEventName);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2009-06-15 08:27:29 +00:00
|
|
|
protected:
|
2014-06-23 19:56:07 +00:00
|
|
|
virtual ~DOMEventTargetHelper();
|
|
|
|
|
2013-09-05 21:08:24 +00:00
|
|
|
nsresult WantsUntrusted(bool* aRetVal);
|
|
|
|
|
2017-04-18 11:51:27 +00:00
|
|
|
void MaybeUpdateKeepAlive();
|
|
|
|
void MaybeDontKeepAlive();
|
|
|
|
|
2015-06-29 16:42:41 +00:00
|
|
|
// If this method returns true your object is kept alive until it returns
|
|
|
|
// false. You can use this method instead using
|
|
|
|
// NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN macro.
|
|
|
|
virtual bool IsCertainlyAliveForCC() const { return mIsKeptAlive; }
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<EventListenerManager> mListenerManager;
|
2012-09-27 20:11:31 +00:00
|
|
|
// Make |event| trusted and dispatch |aEvent| to |this|.
|
2018-04-20 16:55:30 +00:00
|
|
|
nsresult DispatchTrustedEvent(dom::Event* aEvent);
|
2013-07-09 17:30:58 +00:00
|
|
|
|
|
|
|
virtual void LastRelease() {}
|
2017-04-18 11:51:27 +00:00
|
|
|
|
|
|
|
void KeepAliveIfHasListenersFor(const nsAString& aType);
|
2017-10-02 22:05:19 +00:00
|
|
|
void KeepAliveIfHasListenersFor(nsAtom* aType);
|
2017-04-18 11:51:27 +00:00
|
|
|
|
|
|
|
void IgnoreKeepAliveIfHasListenersFor(const nsAString& aType);
|
2017-10-02 22:05:19 +00:00
|
|
|
void IgnoreKeepAliveIfHasListenersFor(nsAtom* aType);
|
2017-04-18 11:51:27 +00:00
|
|
|
|
2018-04-16 13:08:51 +00:00
|
|
|
void BindToOwnerInternal(nsIGlobalObject* aOwner);
|
|
|
|
|
2012-03-13 00:56:07 +00:00
|
|
|
private:
|
2018-02-21 18:53:22 +00:00
|
|
|
// The parent global object. The global will clear this when
|
|
|
|
// it is destroyed by calling DisconnectFromOwner().
|
|
|
|
nsIGlobalObject* MOZ_NON_OWNING_REF mParentObject;
|
2014-05-20 19:50:57 +00:00
|
|
|
// mParentObject pre QI-ed and cached (inner window)
|
2013-04-04 09:30:36 +00:00
|
|
|
// (it is needed for off main thread access)
|
2015-01-18 03:17:06 +00:00
|
|
|
// It is obtained in BindToOwner and reset in DisconnectFromOwner.
|
2016-01-30 17:05:36 +00:00
|
|
|
nsPIDOMWindowInner* MOZ_NON_OWNING_REF mOwnerWindow;
|
2013-04-04 09:30:36 +00:00
|
|
|
bool mHasOrHasHadOwnerWindow;
|
2017-04-18 11:51:27 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
nsTArray<nsString> mStrings;
|
2017-10-02 22:05:19 +00:00
|
|
|
nsTArray<RefPtr<nsAtom>> mAtoms;
|
2017-04-18 11:51:27 +00:00
|
|
|
} mKeepingAliveTypes;
|
|
|
|
|
|
|
|
bool mIsKeptAlive;
|
2009-06-15 08:27:29 +00:00
|
|
|
};
|
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(DOMEventTargetHelper, NS_DOMEVENTTARGETHELPER_IID)
|
2013-03-09 11:34:29 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-11-09 16:00:25 +00:00
|
|
|
// WebIDL event handlers
|
|
|
|
#define IMPL_EVENT_HANDLER(_event) \
|
2012-11-09 16:00:25 +00:00
|
|
|
inline mozilla::dom::EventHandlerNonNull* GetOn##_event() { \
|
2018-07-24 22:15:19 +00:00
|
|
|
return GetEventHandler(nsGkAtoms::on##_event); \
|
2012-11-09 16:00:25 +00:00
|
|
|
} \
|
2013-09-17 11:01:28 +00:00
|
|
|
inline void SetOn##_event(mozilla::dom::EventHandlerNonNull* aCallback) { \
|
2018-07-24 22:15:19 +00:00
|
|
|
SetEventHandler(nsGkAtoms::on##_event, aCallback); \
|
2012-11-09 16:00:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
#endif // mozilla_DOMEventTargetHelper_h_
|