dom/events/Event.h
author Boris Zbarsky <bzbarsky@mit.edu>
Fri, 20 Apr 2018 00:49:30 -0400
changeset 414736 d03756b18579a4bd43e4123af683d1cbfe9660d7
parent 410057 ee7f2d1b57538153dc89f135fdd0430357ae0358
child 414763 80abe3305b24b7f2c251ac973a287275a488428f
permissions -rwxr-xr-x
Bug 1429903 part 3. Remove C++ uses of nsIDOMEventTarget. r=mccr8 MozReview-Commit-ID: 4NdssvnWn0H

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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_Event_h_
#define mozilla_dom_Event_h_

#include "mozilla/Attributes.h"
#include "mozilla/BasicEvents.h"
#include "nsIDOMEvent.h"
#include "nsISupports.h"
#include "nsCOMPtr.h"
#include "nsPIDOMWindow.h"
#include "nsPoint.h"
#include "nsCycleCollectionParticipant.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/EventBinding.h"
#include "nsIScriptGlobalObject.h"
#include "Units.h"
#include "js/TypeDecls.h"
#include "nsIGlobalObject.h"

class nsIContent;
class nsPresContext;

namespace mozilla {
namespace dom {

class BeforeUnloadEvent;
class CustomEvent;
class DragEvent;
class EventTarget;
class EventMessageAutoOverride;
// ExtendableEvent is a ServiceWorker event that is not
// autogenerated since it has some extra methods.
class ExtendableEvent;
class KeyboardEvent;
class MouseEvent;
class TimeEvent;
class UIEvent;
class WantsPopupControlCheck;
class XULCommandEvent;
#define GENERATED_EVENT(EventClass_) class EventClass_;
#include "mozilla/dom/GeneratedEventList.h"
#undef GENERATED_EVENT

class Event : public nsIDOMEvent,
              public nsWrapperCache
{
public:
  Event(EventTarget* aOwner,
        nsPresContext* aPresContext,
        WidgetEvent* aEvent);
  explicit Event(nsPIDOMWindowInner* aWindow);

protected:
  virtual ~Event();

private:
  void ConstructorInit(EventTarget* aOwner,
                       nsPresContext* aPresContext,
                       WidgetEvent* aEvent);

public:
  static Event* FromSupports(nsISupports* aSupports)
  {
    nsIDOMEvent* event =
      static_cast<nsIDOMEvent*>(aSupports);
#ifdef DEBUG
    {
      nsCOMPtr<nsIDOMEvent> target_qi =
        do_QueryInterface(aSupports);

      // If this assertion fires the QI implementation for the object in
      // question doesn't use the nsIDOMEvent pointer as the
      // nsISupports pointer. That must be fixed, or we'll crash...
      MOZ_ASSERT(target_qi == event, "Uh, fix QI!");
    }
#endif
    return static_cast<Event*>(event);
  }

  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Event)

  nsIGlobalObject* GetParentObject()
  {
    return mOwner;
  }

  JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) final;

  virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto);

#define GENERATED_EVENT(EventClass_) \
  virtual EventClass_* As##EventClass_()  \
  {                                       \
    return nullptr;                       \
  }
#include "mozilla/dom/GeneratedEventList.h"
#undef GENERATED_EVENT

  // ExtendableEvent is a ServiceWorker event that is not
  // autogenerated since it has some extra methods.
  virtual ExtendableEvent* AsExtendableEvent()
  {
    return nullptr;
  }

  virtual TimeEvent* AsTimeEvent() { return nullptr; }

  // BeforeUnloadEvent is not autogenerated because it has a setter.
  virtual BeforeUnloadEvent* AsBeforeUnloadEvent()
  {
    return nullptr;
  }

  // KeyboardEvent has all sorts of non-autogeneratable bits so far.
  virtual KeyboardEvent* AsKeyboardEvent()
  {
    return nullptr;
  }

  // DragEvent has a non-autogeneratable initDragEvent.
  virtual DragEvent* AsDragEvent()
  {
    return nullptr;
  }

  // XULCommandEvent has a non-autogeneratable initCommandEvent.
  virtual XULCommandEvent* AsXULCommandEvent()
  {
    return nullptr;
  }

  // MouseEvent has a non-autogeneratable initMouseEvent and other
  // non-autogeneratable methods.
  virtual MouseEvent* AsMouseEvent()
  {
    return nullptr;
  }

  // UIEvent has a non-autogeneratable initUIEvent.
  virtual UIEvent* AsUIEvent()
  {
    return nullptr;
  }

  // CustomEvent has a non-autogeneratable initCustomEvent.
  virtual CustomEvent* AsCustomEvent()
  {
    return nullptr;
  }

  // nsIDOMEvent Interface
  NS_DECL_NSIDOMEVENT

  void InitPresContextData(nsPresContext* aPresContext);

  // Returns true if the event should be trusted.
  bool Init(EventTarget* aGlobal);

  static PopupControlState GetEventPopupControlState(WidgetEvent* aEvent,
                                                     nsIDOMEvent* aDOMEvent = nullptr);

  static void PopupAllowedEventsChanged();

  static void Shutdown();

  static const char* GetEventName(EventMessage aEventType);
  static CSSIntPoint GetClientCoords(nsPresContext* aPresContext,
                                     WidgetEvent* aEvent,
                                     LayoutDeviceIntPoint aPoint,
                                     CSSIntPoint aDefaultPoint);
  static CSSIntPoint GetPageCoords(nsPresContext* aPresContext,
                                   WidgetEvent* aEvent,
                                   LayoutDeviceIntPoint aPoint,
                                   CSSIntPoint aDefaultPoint);
  static CSSIntPoint GetScreenCoords(nsPresContext* aPresContext,
                                     WidgetEvent* aEvent,
                                     LayoutDeviceIntPoint aPoint);
  static CSSIntPoint GetOffsetCoords(nsPresContext* aPresContext,
                                     WidgetEvent* aEvent,
                                     LayoutDeviceIntPoint aPoint,
                                     CSSIntPoint aDefaultPoint);

  static already_AddRefed<Event> Constructor(EventTarget* aEventTarget,
                                             const nsAString& aType,
                                             const EventInit& aParam);

  static already_AddRefed<Event> Constructor(const GlobalObject& aGlobal,
                                             const nsAString& aType,
                                             const EventInit& aParam,
                                             ErrorResult& aRv);

  // Implemented as xpidl method
  // void GetType(nsString& aRetval) {}

  EventTarget* GetTarget() const;
  static bool IsSrcElementEnabled(JSContext* /* unused */, JSObject* /* unused */);
  EventTarget* GetCurrentTarget() const;

  void ComposedPath(nsTArray<RefPtr<EventTarget>>& aPath);

  uint16_t EventPhase() const;

  // xpidl implementation
  // void StopPropagation();

  // xpidl implementation
  // void StopImmediatePropagation();

  bool Bubbles() const
  {
    return mEvent->mFlags.mBubbles;
  }

  bool Cancelable() const
  {
    return mEvent->mFlags.mCancelable;
  }

  bool Composed() const
  {
    return mEvent->mFlags.mComposed;
  }

  bool CancelBubble() const
  {
    return mEvent->PropagationStopped();
  }

  // xpidl implementation
  // void PreventDefault();

  // You MUST NOT call PreventDefault(JSContext*, CallerType) from C++ code.  A
  // call of this method always sets Event.defaultPrevented true for web
  // contents.  If default action handler calls this, web applications see wrong
  // defaultPrevented value.
  virtual void PreventDefault(JSContext* aCx, CallerType aCallerType);

  // You MUST NOT call DefaultPrevented(CallerType) from C++ code.  This may
  // return false even if PreventDefault() has been called.
  // See comments in its implementation for the details.
  bool DefaultPrevented(CallerType aCallerType) const;

  bool DefaultPrevented() const
  {
    return mEvent->DefaultPrevented();
  }

  bool DefaultPreventedByChrome() const
  {
    return mEvent->mFlags.mDefaultPreventedByChrome;
  }

  bool DefaultPreventedByContent() const
  {
    return mEvent->mFlags.mDefaultPreventedByContent;
  }

  bool MultipleActionsPrevented() const
  {
    return mEvent->mFlags.mMultipleActionsPrevented;
  }

  bool IsTrusted() const
  {
    return mEvent->IsTrusted();
  }

  bool IsSynthesized() const
  {
    return mEvent->mFlags.mIsSynthesizedForTests;
  }

  double TimeStamp();

  EventTarget* GetOriginalTarget() const;
  EventTarget* GetExplicitOriginalTarget() const;
  EventTarget* GetComposedTarget() const;

  /**
   * @param aCalledByDefaultHandler     Should be true when this is called by
   *                                    C++ or Chrome.  Otherwise, e.g., called
   *                                    by a call of Event.preventDefault() in
   *                                    content script, false.
   */
  void PreventDefaultInternal(bool aCalledByDefaultHandler,
                              nsIPrincipal* aPrincipal = nullptr);

  bool IsMainThreadEvent()
  {
    return mIsMainThreadEvent;
  }

  void MarkUninitialized()
  {
    mEvent->mMessage = eVoidEvent;
    mEvent->mSpecifiedEventTypeString.Truncate();
    mEvent->mSpecifiedEventType = nullptr;
  }

  /**
   * For WidgetEvent, return it's type in string.
   *
   * @param aEvent is a WidgetEvent to get its type.
   * @param aType is a string where to return the type.
   */
  static void GetWidgetEventType(WidgetEvent* aEvent, nsAString& aType);

protected:

  // Internal helper functions
  void SetEventType(const nsAString& aEventTypeArg);
  already_AddRefed<nsIContent> GetTargetFromFrame();

  friend class EventMessageAutoOverride;
  friend class WantsPopupControlCheck;
  void SetWantsPopupControlCheck(bool aCheck)
  {
    mWantsPopupControlCheck = aCheck;
  }

  bool GetWantsPopupControlCheck()
  {
    return IsTrusted() && mWantsPopupControlCheck;
  }

  void SetComposed(bool aComposed)
  {
    mEvent->SetComposed(aComposed);
  }

  already_AddRefed<EventTarget>
  EnsureWebAccessibleRelatedTarget(EventTarget* aRelatedTarget);

  mozilla::WidgetEvent*       mEvent;
  RefPtr<nsPresContext>     mPresContext;
  nsCOMPtr<EventTarget>       mExplicitOriginalTarget;
  nsCOMPtr<nsIGlobalObject>   mOwner;
  bool                        mEventIsInternal;
  bool                        mPrivateDataDuplicated;
  bool                        mIsMainThreadEvent;
  // True when popup control check should rely on event.type, not
  // WidgetEvent.mMessage.
  bool                        mWantsPopupControlCheck;
};

/**
 * RAII helper-class to override an event's message (i.e. its DOM-exposed
 * type), for as long as the object is alive.  Restores the original
 * EventMessage when destructed.
 *
 * Notable requirements:
 *  - The original & overriding messages must be known (not eUnidentifiedEvent).
 *  - The original & overriding messages must be different.
 *  - The passed-in nsIDOMEvent must outlive this RAII helper.
 */
class MOZ_RAII EventMessageAutoOverride
{
public:
  explicit EventMessageAutoOverride(nsIDOMEvent* aEvent,
                                    EventMessage aOverridingMessage)
    : mEvent(aEvent->InternalDOMEvent()),
      mOrigMessage(mEvent->mEvent->mMessage)
  {
    MOZ_ASSERT(aOverridingMessage != mOrigMessage,
               "Don't use this class if you're not actually overriding");
    MOZ_ASSERT(aOverridingMessage != eUnidentifiedEvent,
               "Only use this class with a valid overriding EventMessage");
    MOZ_ASSERT(mOrigMessage != eUnidentifiedEvent &&
               mEvent->mEvent->mSpecifiedEventTypeString.IsEmpty(),
               "Only use this class on events whose overridden type is "
               "known (so we can restore it properly)");

    mEvent->mEvent->mMessage = aOverridingMessage;
  }

  ~EventMessageAutoOverride()
  {
    mEvent->mEvent->mMessage = mOrigMessage;
  }

protected:
  // Non-owning ref, which should be safe since we're a stack-allocated object
  // with limited lifetime. Whoever creates us should keep mEvent alive.
  Event* const MOZ_NON_OWNING_REF mEvent;
  const EventMessage mOrigMessage;
};

class MOZ_STACK_CLASS WantsPopupControlCheck
{
public:
  explicit WantsPopupControlCheck(nsIDOMEvent* aEvent) :
    mEvent(aEvent->InternalDOMEvent())
  {
    mOriginalWantsPopupControlCheck = mEvent->GetWantsPopupControlCheck();
    mEvent->SetWantsPopupControlCheck(mEvent->IsTrusted());
  }

  ~WantsPopupControlCheck()
  {
    mEvent->SetWantsPopupControlCheck(mOriginalWantsPopupControlCheck);
  }

private:
  Event* mEvent;
  bool mOriginalWantsPopupControlCheck;
};

} // namespace dom
} // namespace mozilla

inline nsISupports*
ToSupports(mozilla::dom::Event* e)
{
  return static_cast<nsIDOMEvent*>(e);
}

inline nsISupports*
ToCanonicalSupports(mozilla::dom::Event* e)
{
  return static_cast<nsIDOMEvent*>(e);
}

already_AddRefed<mozilla::dom::Event>
NS_NewDOMEvent(mozilla::dom::EventTarget* aOwner,
               nsPresContext* aPresContext,
               mozilla::WidgetEvent* aEvent);

#endif // mozilla_dom_Event_h_