2015-05-03 22:32:37 +03: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 15:12:37 +04: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 12:27:29 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
#ifndef mozilla_DOMEventTargetHelper_h_
|
|
|
|
#define mozilla_DOMEventTargetHelper_h_
|
2009-06-15 12:27:29 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2012-08-31 07:45:16 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2009-06-15 12:27:29 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2015-01-18 06:17:06 +03:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2013-09-19 17:54:42 +04:00
|
|
|
#include "MainThreadUtils.h"
|
2012-06-19 06:30:09 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-03-17 10:56:53 +04:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2012-12-07 05:39:51 +04:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2009-06-15 12:27:29 +04:00
|
|
|
|
2014-06-19 04:57:51 +04:00
|
|
|
struct JSCompartment;
|
2016-04-21 01:04:29 +03:00
|
|
|
class nsIDocument;
|
2014-04-01 10:13:50 +04:00
|
|
|
|
2013-11-27 19:22:16 +04:00
|
|
|
namespace mozilla {
|
2014-04-01 10:13:50 +04:00
|
|
|
|
2013-11-27 19:22:16 +04:00
|
|
|
class ErrorResult;
|
|
|
|
|
2018-04-20 19:55:30 +03:00
|
|
|
namespace dom {
|
|
|
|
class Event;
|
|
|
|
} // namespace dom
|
|
|
|
|
2013-03-09 15:34:29 +04:00
|
|
|
#define NS_DOMEVENTTARGETHELPER_IID \
|
2013-11-27 19:22:16 +04:00
|
|
|
{ 0xa28385c6, 0x9451, 0x4d7e, \
|
|
|
|
{ 0xa3, 0xdd, 0xf4, 0xb6, 0x87, 0x2f, 0xa4, 0x76 } }
|
2013-03-09 15:34:29 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
class DOMEventTargetHelper : public dom::EventTarget
|
2009-06-15 12:27:29 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper()
|
2013-04-25 06:44:24 +04:00
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
2017-04-18 14:51:27 +03:00
|
|
|
, mIsKeptAlive(false)
|
2014-04-01 10:13:50 +04:00
|
|
|
{
|
|
|
|
}
|
2016-01-30 20:05:36 +03:00
|
|
|
explicit DOMEventTargetHelper(nsPIDOMWindowInner* aWindow)
|
2013-04-25 06:44:24 +04:00
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
2017-04-18 14:51:27 +03:00
|
|
|
, mIsKeptAlive(false)
|
2013-04-25 06:44:24 +04:00
|
|
|
{
|
2018-04-16 16:08:51 +03:00
|
|
|
// Be careful not to call the virtual BindToOwner() in a
|
|
|
|
// constructor.
|
|
|
|
nsIGlobalObject* global = aWindow ? aWindow->AsGlobal() : nullptr;
|
|
|
|
BindToOwnerInternal(global);
|
2014-01-07 06:53:23 +04:00
|
|
|
}
|
2015-01-05 18:23:36 +03:00
|
|
|
explicit DOMEventTargetHelper(nsIGlobalObject* aGlobalObject)
|
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
2017-04-18 14:51:27 +03:00
|
|
|
, mIsKeptAlive(false)
|
2015-01-05 18:23:36 +03:00
|
|
|
{
|
2018-04-16 16:08:51 +03:00
|
|
|
// Be careful not to call the virtual BindToOwner() in a
|
|
|
|
// constructor.
|
|
|
|
BindToOwnerInternal(aGlobalObject);
|
2015-01-05 18:23:36 +03:00
|
|
|
}
|
2014-08-05 17:19:51 +04:00
|
|
|
explicit DOMEventTargetHelper(DOMEventTargetHelper* aOther)
|
2014-01-07 06:53:23 +04:00
|
|
|
: mParentObject(nullptr)
|
|
|
|
, mOwnerWindow(nullptr)
|
|
|
|
, mHasOrHasHadOwnerWindow(false)
|
2017-04-18 14:51:27 +03:00
|
|
|
, mIsKeptAlive(false)
|
2014-01-07 06:53:23 +04:00
|
|
|
{
|
2018-04-16 16:08:51 +03: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 06:44:24 +04:00
|
|
|
}
|
|
|
|
|
2009-06-15 12:27:29 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(DOMEventTargetHelper)
|
2009-06-15 12:27:29 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual EventListenerManager* GetExistingListenerManager() const override;
|
|
|
|
virtual EventListenerManager* GetOrCreateListenerManager() override;
|
2013-10-23 03:32:04 +04:00
|
|
|
|
2018-04-05 20:42:42 +03:00
|
|
|
bool ComputeDefaultWantsUntrusted(ErrorResult& aRv) override;
|
2009-06-15 12:27:29 +04:00
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
using EventTarget::DispatchEvent;
|
|
|
|
bool DispatchEvent(dom::Event& aEvent, dom::CallerType aCallerType,
|
|
|
|
ErrorResult& aRv) override;
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;
|
|
|
|
|
2013-03-09 15:34:29 +04:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMEVENTTARGETHELPER_IID)
|
|
|
|
|
2012-02-08 06:53:33 +04:00
|
|
|
void GetParentObject(nsIScriptGlobalObject **aParentObject)
|
|
|
|
{
|
2013-04-04 13:30:36 +04:00
|
|
|
if (mParentObject) {
|
|
|
|
CallQueryInterface(mParentObject, aParentObject);
|
|
|
|
} else {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aParentObject = nullptr;
|
2012-02-08 06:53:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
static DOMEventTargetHelper* FromSupports(nsISupports* aSupports)
|
2012-02-08 06:53:33 +04:00
|
|
|
{
|
2014-04-01 10:13:50 +04:00
|
|
|
dom::EventTarget* target = static_cast<dom::EventTarget*>(aSupports);
|
2012-02-08 06:53:33 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2014-04-01 10:13:50 +04:00
|
|
|
nsCOMPtr<dom::EventTarget> target_qi = do_QueryInterface(aSupports);
|
2012-02-08 06:53:33 +04:00
|
|
|
|
|
|
|
// If this assertion fires the QI implementation for the object in
|
2013-04-06 04:44:26 +04:00
|
|
|
// question doesn't use the EventTarget pointer as the
|
2012-02-08 06:53:33 +04:00
|
|
|
// nsISupports pointer. That must be fixed, or we'll crash...
|
|
|
|
NS_ASSERTION(target_qi == target, "Uh, fix QI!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
return static_cast<DOMEventTargetHelper*>(target);
|
2012-02-08 06:53:33 +04:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:40:32 +03:00
|
|
|
bool HasListenersFor(const nsAString& aType) const
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
|
|
|
return mListenerManager && mListenerManager->HasListenersFor(aType);
|
|
|
|
}
|
|
|
|
|
2018-03-29 05:40:32 +03:00
|
|
|
bool HasListenersFor(nsAtom* aTypeWithOn) const
|
2009-06-15 12:27:29 +04:00
|
|
|
{
|
2012-10-29 08:33:51 +04:00
|
|
|
return mListenerManager && mListenerManager->HasListenersFor(aTypeWithOn);
|
2009-06-15 12:27:29 +04:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
virtual nsPIDOMWindowOuter* GetOwnerGlobalForBindings() override
|
2013-05-31 01:46:39 +04:00
|
|
|
{
|
2016-01-30 20:05:36 +03:00
|
|
|
return nsPIDOMWindowOuter::GetFromCurrentInner(GetOwner());
|
2013-05-31 01:46:39 +04:00
|
|
|
}
|
2009-06-15 12:27:29 +04:00
|
|
|
|
2016-04-21 01:04:29 +03:00
|
|
|
nsresult CheckInnerWindowCorrectness() const
|
2009-06-15 12:27:29 +04:00
|
|
|
{
|
2013-04-04 13:30:36 +04:00
|
|
|
NS_ENSURE_STATE(!mHasOrHasHadOwnerWindow || mOwnerWindow);
|
2014-05-20 23:50:57 +04:00
|
|
|
if (mOwnerWindow && !mOwnerWindow->IsCurrentInnerWindow()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-06-15 12:27:29 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-13 04:56:07 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* GetOwner() const { return mOwnerWindow; }
|
2016-04-21 01:04:29 +03: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.
|
|
|
|
nsIDocument* GetDocumentIfCurrent() const;
|
2018-04-16 16:08:51 +03: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 20:05:36 +03:00
|
|
|
void BindToOwner(nsPIDOMWindowInner* aOwner);
|
2014-04-01 10:13:50 +04:00
|
|
|
void BindToOwner(DOMEventTargetHelper* aOther);
|
2018-04-16 16:08:51 +03:00
|
|
|
|
2017-04-18 14:51:27 +03:00
|
|
|
virtual void DisconnectFromOwner();
|
2017-11-20 21:59:22 +03:00
|
|
|
using EventTarget::GetParentObject;
|
2015-05-12 22:56:39 +03:00
|
|
|
virtual nsIGlobalObject* GetOwnerGlobal() const override
|
|
|
|
{
|
2018-02-21 21:53:22 +03:00
|
|
|
return mParentObject;
|
2015-01-18 06:17:06 +03:00
|
|
|
}
|
2013-04-04 13:30:36 +04:00
|
|
|
bool HasOrHasHadOwner() { return mHasOrHasHadOwnerWindow; }
|
2013-11-27 19:22:16 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
virtual void EventListenerAdded(nsAtom* aType) override;
|
2017-04-18 14:51:27 +03:00
|
|
|
virtual void EventListenerAdded(const nsAString& aType) override;
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
virtual void EventListenerRemoved(nsAtom* aType) override;
|
2017-04-18 14:51:27 +03:00
|
|
|
virtual void EventListenerRemoved(const nsAString& aType) override;
|
|
|
|
|
2014-07-15 01:15:23 +04:00
|
|
|
// Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
|
|
|
|
nsresult DispatchTrustedEvent(const nsAString& aEventName);
|
2009-06-15 12:27:29 +04:00
|
|
|
protected:
|
2014-06-23 23:56:07 +04:00
|
|
|
virtual ~DOMEventTargetHelper();
|
|
|
|
|
2013-09-06 01:08:24 +04:00
|
|
|
nsresult WantsUntrusted(bool* aRetVal);
|
|
|
|
|
2017-04-18 14:51:27 +03:00
|
|
|
void MaybeUpdateKeepAlive();
|
|
|
|
void MaybeDontKeepAlive();
|
|
|
|
|
2015-06-29 19:42:41 +03: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
|
|
|
|
{
|
2017-04-18 14:51:27 +03:00
|
|
|
return mIsKeptAlive;
|
2015-06-29 19:42:41 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<EventListenerManager> mListenerManager;
|
2012-09-28 00:11:31 +04:00
|
|
|
// Make |event| trusted and dispatch |aEvent| to |this|.
|
2018-04-20 19:55:30 +03:00
|
|
|
nsresult DispatchTrustedEvent(dom::Event* aEvent);
|
2013-07-09 21:30:58 +04:00
|
|
|
|
|
|
|
virtual void LastRelease() {}
|
2017-04-18 14:51:27 +03:00
|
|
|
|
|
|
|
void KeepAliveIfHasListenersFor(const nsAString& aType);
|
2017-10-03 01:05:19 +03:00
|
|
|
void KeepAliveIfHasListenersFor(nsAtom* aType);
|
2017-04-18 14:51:27 +03:00
|
|
|
|
|
|
|
void IgnoreKeepAliveIfHasListenersFor(const nsAString& aType);
|
2017-10-03 01:05:19 +03:00
|
|
|
void IgnoreKeepAliveIfHasListenersFor(nsAtom* aType);
|
2017-04-18 14:51:27 +03:00
|
|
|
|
2018-04-16 16:08:51 +03:00
|
|
|
void BindToOwnerInternal(nsIGlobalObject* aOwner);
|
|
|
|
|
2012-03-13 04:56:07 +04:00
|
|
|
private:
|
2018-02-21 21:53:22 +03: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 23:50:57 +04:00
|
|
|
// mParentObject pre QI-ed and cached (inner window)
|
2013-04-04 13:30:36 +04:00
|
|
|
// (it is needed for off main thread access)
|
2015-01-18 06:17:06 +03:00
|
|
|
// It is obtained in BindToOwner and reset in DisconnectFromOwner.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* MOZ_NON_OWNING_REF mOwnerWindow;
|
2013-04-04 13:30:36 +04:00
|
|
|
bool mHasOrHasHadOwnerWindow;
|
2017-04-18 14:51:27 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
nsTArray<nsString> mStrings;
|
2017-10-03 01:05:19 +03:00
|
|
|
nsTArray<RefPtr<nsAtom>> mAtoms;
|
2017-04-18 14:51:27 +03:00
|
|
|
} mKeepingAliveTypes;
|
|
|
|
|
|
|
|
bool mIsKeptAlive;
|
2009-06-15 12:27:29 +04:00
|
|
|
};
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(DOMEventTargetHelper,
|
2013-03-09 15:34:29 +04:00
|
|
|
NS_DOMEVENTTARGETHELPER_IID)
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-11-09 20:00:25 +04:00
|
|
|
// WebIDL event handlers
|
|
|
|
#define IMPL_EVENT_HANDLER(_event) \
|
2012-11-09 20:00:25 +04:00
|
|
|
inline mozilla::dom::EventHandlerNonNull* GetOn##_event() \
|
2012-11-09 20:00:25 +04:00
|
|
|
{ \
|
2013-08-16 14:06:24 +04:00
|
|
|
if (NS_IsMainThread()) { \
|
|
|
|
return GetEventHandler(nsGkAtoms::on##_event, EmptyString()); \
|
|
|
|
} \
|
|
|
|
return GetEventHandler(nullptr, NS_LITERAL_STRING(#_event)); \
|
2012-11-09 20:00:25 +04:00
|
|
|
} \
|
2013-09-17 15:01:28 +04:00
|
|
|
inline void SetOn##_event(mozilla::dom::EventHandlerNonNull* aCallback) \
|
2012-11-09 20:00:25 +04:00
|
|
|
{ \
|
2013-08-16 14:06:24 +04:00
|
|
|
if (NS_IsMainThread()) { \
|
2013-09-17 15:01:28 +04:00
|
|
|
SetEventHandler(nsGkAtoms::on##_event, EmptyString(), aCallback); \
|
2013-08-16 14:06:24 +04:00
|
|
|
} else { \
|
2013-09-17 15:01:28 +04:00
|
|
|
SetEventHandler(nullptr, NS_LITERAL_STRING(#_event), aCallback); \
|
2013-08-16 14:06:24 +04:00
|
|
|
} \
|
2012-11-09 20:00:25 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
#endif // mozilla_DOMEventTargetHelper_h_
|