dom/events/Event.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/events/Event.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,285 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_dom_Event_h_
    1.10 +#define mozilla_dom_Event_h_
    1.11 +
    1.12 +#include "mozilla/Attributes.h"
    1.13 +#include "mozilla/BasicEvents.h"
    1.14 +#include "nsIDOMEvent.h"
    1.15 +#include "nsISupports.h"
    1.16 +#include "nsCOMPtr.h"
    1.17 +#include "nsPIDOMWindow.h"
    1.18 +#include "nsPoint.h"
    1.19 +#include "nsCycleCollectionParticipant.h"
    1.20 +#include "nsAutoPtr.h"
    1.21 +#include "mozilla/dom/EventBinding.h"
    1.22 +#include "nsIScriptGlobalObject.h"
    1.23 +#include "Units.h"
    1.24 +#include "js/TypeDecls.h"
    1.25 +
    1.26 +class nsIContent;
    1.27 +class nsIDOMEventTarget;
    1.28 +class nsPresContext;
    1.29 +
    1.30 +namespace mozilla {
    1.31 +namespace dom {
    1.32 +
    1.33 +class EventTarget;
    1.34 +class ErrorEvent;
    1.35 +
    1.36 +// Dummy class so we can cast through it to get from nsISupports to
    1.37 +// Event subclasses with only two non-ambiguous static casts.
    1.38 +class EventBase : public nsIDOMEvent
    1.39 +{
    1.40 +};
    1.41 +
    1.42 +class Event : public EventBase,
    1.43 +              public nsWrapperCache
    1.44 +{
    1.45 +public:
    1.46 +  Event(EventTarget* aOwner,
    1.47 +        nsPresContext* aPresContext,
    1.48 +        WidgetEvent* aEvent);
    1.49 +  Event(nsPIDOMWindow* aWindow);
    1.50 +  virtual ~Event();
    1.51 +
    1.52 +private:
    1.53 +  void ConstructorInit(EventTarget* aOwner,
    1.54 +                       nsPresContext* aPresContext,
    1.55 +                       WidgetEvent* aEvent);
    1.56 +
    1.57 +public:
    1.58 +  void GetParentObject(nsIScriptGlobalObject** aParentObject)
    1.59 +  {
    1.60 +    if (mOwner) {
    1.61 +      CallQueryInterface(mOwner, aParentObject);
    1.62 +    } else {
    1.63 +      *aParentObject = nullptr;
    1.64 +    }
    1.65 +  }
    1.66 +
    1.67 +  static Event* FromSupports(nsISupports* aSupports)
    1.68 +  {
    1.69 +    nsIDOMEvent* event =
    1.70 +      static_cast<nsIDOMEvent*>(aSupports);
    1.71 +#ifdef DEBUG
    1.72 +    {
    1.73 +      nsCOMPtr<nsIDOMEvent> target_qi =
    1.74 +        do_QueryInterface(aSupports);
    1.75 +
    1.76 +      // If this assertion fires the QI implementation for the object in
    1.77 +      // question doesn't use the nsIDOMEvent pointer as the
    1.78 +      // nsISupports pointer. That must be fixed, or we'll crash...
    1.79 +      MOZ_ASSERT(target_qi == event, "Uh, fix QI!");
    1.80 +    }
    1.81 +#endif
    1.82 +    return static_cast<Event*>(event);
    1.83 +  }
    1.84 +
    1.85 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
    1.86 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Event)
    1.87 +
    1.88 +  nsISupports* GetParentObject()
    1.89 +  {
    1.90 +    return mOwner;
    1.91 +  }
    1.92 +
    1.93 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE
    1.94 +  {
    1.95 +    return EventBinding::Wrap(aCx, this);
    1.96 +  }
    1.97 +
    1.98 +  virtual ErrorEvent* AsErrorEvent()
    1.99 +  {
   1.100 +    return nullptr;
   1.101 +  }
   1.102 +
   1.103 +  // nsIDOMEvent Interface
   1.104 +  NS_DECL_NSIDOMEVENT
   1.105 +
   1.106 +  void InitPresContextData(nsPresContext* aPresContext);
   1.107 +
   1.108 +  // Returns true if the event should be trusted.
   1.109 +  bool Init(EventTarget* aGlobal);
   1.110 +
   1.111 +  static PopupControlState GetEventPopupControlState(WidgetEvent* aEvent);
   1.112 +
   1.113 +  static void PopupAllowedEventsChanged();
   1.114 +
   1.115 +  static void Shutdown();
   1.116 +
   1.117 +  static const char* GetEventName(uint32_t aEventType);
   1.118 +  static CSSIntPoint GetClientCoords(nsPresContext* aPresContext,
   1.119 +                                     WidgetEvent* aEvent,
   1.120 +                                     LayoutDeviceIntPoint aPoint,
   1.121 +                                     CSSIntPoint aDefaultPoint);
   1.122 +  static CSSIntPoint GetPageCoords(nsPresContext* aPresContext,
   1.123 +                                   WidgetEvent* aEvent,
   1.124 +                                   LayoutDeviceIntPoint aPoint,
   1.125 +                                   CSSIntPoint aDefaultPoint);
   1.126 +  static nsIntPoint GetScreenCoords(nsPresContext* aPresContext,
   1.127 +                                    WidgetEvent* aEvent,
   1.128 +                                    LayoutDeviceIntPoint aPoint);
   1.129 +
   1.130 +  static already_AddRefed<Event> Constructor(const GlobalObject& aGlobal,
   1.131 +                                             const nsAString& aType,
   1.132 +                                             const EventInit& aParam,
   1.133 +                                             ErrorResult& aRv);
   1.134 +
   1.135 +  // Implemented as xpidl method
   1.136 +  // void GetType(nsString& aRetval) {}
   1.137 +
   1.138 +  EventTarget* GetTarget() const;
   1.139 +  EventTarget* GetCurrentTarget() const;
   1.140 +
   1.141 +  uint16_t EventPhase() const;
   1.142 +
   1.143 +  // xpidl implementation
   1.144 +  // void StopPropagation();
   1.145 +
   1.146 +  // xpidl implementation
   1.147 +  // void StopImmediatePropagation();
   1.148 +
   1.149 +  bool Bubbles() const
   1.150 +  {
   1.151 +    return mEvent->mFlags.mBubbles;
   1.152 +  }
   1.153 +
   1.154 +  bool Cancelable() const
   1.155 +  {
   1.156 +    return mEvent->mFlags.mCancelable;
   1.157 +  }
   1.158 +
   1.159 +  // xpidl implementation
   1.160 +  // void PreventDefault();
   1.161 +
   1.162 +  // You MUST NOT call PreventDefaultJ(JSContext*) from C++ code.  A call of
   1.163 +  // this method always sets Event.defaultPrevented true for web contents.
   1.164 +  // If default action handler calls this, web applications meet wrong
   1.165 +  // defaultPrevented value.
   1.166 +  void PreventDefault(JSContext* aCx);
   1.167 +
   1.168 +  // You MUST NOT call DefaultPrevented(JSContext*) from C++ code.  This may
   1.169 +  // return false even if PreventDefault() has been called.
   1.170 +  // See comments in its implementation for the detail.
   1.171 +  bool DefaultPrevented(JSContext* aCx) const;
   1.172 +
   1.173 +  bool DefaultPrevented() const
   1.174 +  {
   1.175 +    return mEvent->mFlags.mDefaultPrevented;
   1.176 +  }
   1.177 +
   1.178 +  bool MultipleActionsPrevented() const
   1.179 +  {
   1.180 +    return mEvent->mFlags.mMultipleActionsPrevented;
   1.181 +  }
   1.182 +
   1.183 +  bool IsTrusted() const
   1.184 +  {
   1.185 +    return mEvent->mFlags.mIsTrusted;
   1.186 +  }
   1.187 +
   1.188 +  bool IsSynthesized() const
   1.189 +  {
   1.190 +    return mEvent->mFlags.mIsSynthesizedForTests;
   1.191 +  }
   1.192 +
   1.193 +  uint64_t TimeStamp() const
   1.194 +  {
   1.195 +    return mEvent->time;
   1.196 +  }
   1.197 +
   1.198 +  void InitEvent(const nsAString& aType, bool aBubbles, bool aCancelable,
   1.199 +                 ErrorResult& aRv)
   1.200 +  {
   1.201 +    aRv = InitEvent(aType, aBubbles, aCancelable);
   1.202 +  }
   1.203 +
   1.204 +  EventTarget* GetOriginalTarget() const;
   1.205 +  EventTarget* GetExplicitOriginalTarget() const;
   1.206 +
   1.207 +  bool GetPreventDefault() const;
   1.208 +
   1.209 +  /**
   1.210 +   * @param aCalledByDefaultHandler     Should be true when this is called by
   1.211 +   *                                    C++ or Chrome.  Otherwise, e.g., called
   1.212 +   *                                    by a call of Event.preventDefault() in
   1.213 +   *                                    content script, false.
   1.214 +   */
   1.215 +  void PreventDefaultInternal(bool aCalledByDefaultHandler);
   1.216 +
   1.217 +  bool IsMainThreadEvent()
   1.218 +  {
   1.219 +    return mIsMainThreadEvent;
   1.220 +  }
   1.221 +
   1.222 +protected:
   1.223 +
   1.224 +  // Internal helper functions
   1.225 +  void SetEventType(const nsAString& aEventTypeArg);
   1.226 +  already_AddRefed<nsIContent> GetTargetFromFrame();
   1.227 +
   1.228 +  /**
   1.229 +   * IsChrome() returns true if aCx is chrome context or the event is created
   1.230 +   * in chrome's thread.  Otherwise, false.
   1.231 +   */
   1.232 +  bool IsChrome(JSContext* aCx) const;
   1.233 +
   1.234 +  mozilla::WidgetEvent*       mEvent;
   1.235 +  nsRefPtr<nsPresContext>     mPresContext;
   1.236 +  nsCOMPtr<EventTarget>       mExplicitOriginalTarget;
   1.237 +  nsCOMPtr<nsPIDOMWindow>     mOwner; // nsPIDOMWindow for now.
   1.238 +  bool                        mEventIsInternal;
   1.239 +  bool                        mPrivateDataDuplicated;
   1.240 +  bool                        mIsMainThreadEvent;
   1.241 +};
   1.242 +
   1.243 +} // namespace dom
   1.244 +} // namespace mozilla
   1.245 +
   1.246 +#define NS_FORWARD_TO_EVENT \
   1.247 +  NS_FORWARD_NSIDOMEVENT(Event::)
   1.248 +
   1.249 +#define NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(_to) \
   1.250 +  NS_IMETHOD GetType(nsAString& aType){ return _to GetType(aType); } \
   1.251 +  NS_IMETHOD GetTarget(nsIDOMEventTarget * *aTarget) { return _to GetTarget(aTarget); } \
   1.252 +  NS_IMETHOD GetCurrentTarget(nsIDOMEventTarget * *aCurrentTarget) { return _to GetCurrentTarget(aCurrentTarget); } \
   1.253 +  NS_IMETHOD GetEventPhase(uint16_t *aEventPhase) { return _to GetEventPhase(aEventPhase); } \
   1.254 +  NS_IMETHOD GetBubbles(bool *aBubbles) { return _to GetBubbles(aBubbles); } \
   1.255 +  NS_IMETHOD GetCancelable(bool *aCancelable) { return _to GetCancelable(aCancelable); } \
   1.256 +  NS_IMETHOD GetTimeStamp(DOMTimeStamp *aTimeStamp) { return _to GetTimeStamp(aTimeStamp); } \
   1.257 +  NS_IMETHOD StopPropagation(void) { return _to StopPropagation(); } \
   1.258 +  NS_IMETHOD PreventDefault(void) { return _to PreventDefault(); } \
   1.259 +  NS_IMETHOD InitEvent(const nsAString & eventTypeArg, bool canBubbleArg, bool cancelableArg) { return _to InitEvent(eventTypeArg, canBubbleArg, cancelableArg); } \
   1.260 +  NS_IMETHOD GetDefaultPrevented(bool *aDefaultPrevented) { return _to GetDefaultPrevented(aDefaultPrevented); } \
   1.261 +  NS_IMETHOD StopImmediatePropagation(void) { return _to StopImmediatePropagation(); } \
   1.262 +  NS_IMETHOD GetOriginalTarget(nsIDOMEventTarget** aOriginalTarget) { return _to GetOriginalTarget(aOriginalTarget); } \
   1.263 +  NS_IMETHOD GetExplicitOriginalTarget(nsIDOMEventTarget** aExplicitOriginalTarget) { return _to GetExplicitOriginalTarget(aExplicitOriginalTarget); } \
   1.264 +  NS_IMETHOD GetPreventDefault(bool* aRetval) { return _to GetPreventDefault(aRetval); } \
   1.265 +  NS_IMETHOD GetIsTrusted(bool* aIsTrusted) { return _to GetIsTrusted(aIsTrusted); } \
   1.266 +  NS_IMETHOD SetTarget(nsIDOMEventTarget *aTarget) { return _to SetTarget(aTarget); } \
   1.267 +  NS_IMETHOD_(bool) IsDispatchStopped(void) { return _to IsDispatchStopped(); } \
   1.268 +  NS_IMETHOD_(WidgetEvent*) GetInternalNSEvent(void) { return _to GetInternalNSEvent(); } \
   1.269 +  NS_IMETHOD_(void) SetTrusted(bool aTrusted) { _to SetTrusted(aTrusted); } \
   1.270 +  NS_IMETHOD_(void) SetOwner(EventTarget* aOwner) { _to SetOwner(aOwner); } \
   1.271 +  NS_IMETHOD_(Event*) InternalDOMEvent() { return _to InternalDOMEvent(); }
   1.272 +
   1.273 +#define NS_FORWARD_TO_EVENT_NO_SERIALIZATION_NO_DUPLICATION \
   1.274 +  NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(Event::)
   1.275 +
   1.276 +inline nsISupports*
   1.277 +ToSupports(mozilla::dom::Event* e)
   1.278 +{
   1.279 +  return static_cast<nsIDOMEvent*>(e);
   1.280 +}
   1.281 +
   1.282 +inline nsISupports*
   1.283 +ToCanonicalSupports(mozilla::dom::Event* e)
   1.284 +{
   1.285 +  return static_cast<nsIDOMEvent*>(e);
   1.286 +}
   1.287 +
   1.288 +#endif // mozilla_dom_Event_h_

mercurial