tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

KeyEventHandler.h (5859B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_KeyEventHandler_h_
      8 #define mozilla_KeyEventHandler_h_
      9 
     10 #include "js/TypeDecls.h"
     11 #include "mozilla/EventForwards.h"
     12 #include "mozilla/MemoryReporting.h"
     13 #include "mozilla/ShortcutKeys.h"
     14 #include "nsAtom.h"
     15 #include "nsCOMPtr.h"
     16 #include "nsCycleCollectionParticipant.h"
     17 #include "nsIController.h"
     18 #include "nsIWeakReference.h"
     19 #include "nsString.h"
     20 
     21 namespace mozilla {
     22 
     23 namespace layers {
     24 class KeyboardShortcut;
     25 }  // namespace layers
     26 
     27 struct IgnoreModifierState;
     28 
     29 namespace dom {
     30 class Event;
     31 class UIEvent;
     32 class Element;
     33 class EventTarget;
     34 class KeyboardEvent;
     35 class Element;
     36 }  // namespace dom
     37 
     38 // Values of the reserved attribute. When unset, the default value depends on
     39 // the permissions.default.shortcuts preference.
     40 enum ReservedKey : uint8_t {
     41  ReservedKey_False = 0,
     42  ReservedKey_True = 1,
     43  ReservedKey_Unset = 2,
     44 };
     45 
     46 class KeyEventHandler final {
     47 public:
     48  // This constructor is used only by XUL key handlers (e.g., <key>)
     49  explicit KeyEventHandler(dom::Element* aHandlerElement,
     50                           ReservedKey aReserved);
     51 
     52  // This constructor is used for keyboard handlers for browser, editor, input
     53  // and textarea elements.
     54  explicit KeyEventHandler(ShortcutKeyData* aKeyData);
     55 
     56  ~KeyEventHandler();
     57 
     58  /**
     59   * Try and convert this XBL handler into an APZ KeyboardShortcut for handling
     60   * key events on the compositor thread. This only works for XBL handlers that
     61   * represent scroll commands.
     62   *
     63   * @param aOut the converted KeyboardShortcut, must be non null
     64   * @return whether the handler was converted into a KeyboardShortcut
     65   */
     66  bool TryConvertToKeyboardShortcut(layers::KeyboardShortcut* aOut) const;
     67 
     68  bool EventTypeEquals(nsAtom* aEventType) const {
     69    return mEventName == aEventType;
     70  }
     71 
     72  // if aCharCode is not zero, it is used instead of the charCode of
     73  // aKeyEventHandler.
     74  bool KeyEventMatched(dom::KeyboardEvent* aDomKeyboardEvent,
     75                       uint32_t aCharCode,
     76                       const IgnoreModifierState& aIgnoreModifierState);
     77 
     78  /**
     79   * Check whether the handler element is disabled.  Note that this requires
     80   * a QI to getting GetHandlerELement().  Therefore, this should not be used
     81   * first in multiple checks.
     82   */
     83  bool KeyElementIsDisabled() const;
     84 
     85  already_AddRefed<dom::Element> GetHandlerElement() const;
     86 
     87  ReservedKey GetIsReserved() { return mReserved; }
     88 
     89  KeyEventHandler* GetNextHandler() { return mNextHandler; }
     90  void SetNextHandler(KeyEventHandler* aHandler) { mNextHandler = aHandler; }
     91 
     92  MOZ_CAN_RUN_SCRIPT
     93  nsresult ExecuteHandler(dom::EventTarget* aTarget, dom::Event* aEvent);
     94 
     95  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
     96 
     97  void GetCommand(nsAString& aCommand) const;
     98 
     99 public:
    100  static uint32_t gRefCnt;
    101 
    102 protected:
    103  void Init() { ++gRefCnt; }
    104 
    105  already_AddRefed<nsIController> GetController(dom::EventTarget* aTarget);
    106 
    107  inline int32_t GetMatchingKeyCode(const nsAString& aKeyName);
    108  void ConstructPrototype(dom::Element* aKeyElement,
    109                          const char16_t* aEvent = nullptr,
    110                          const char16_t* aCommand = nullptr,
    111                          const char16_t* aKeyCode = nullptr,
    112                          const char16_t* aCharCode = nullptr,
    113                          const char16_t* aModifiers = nullptr);
    114  void BuildModifiers(nsAString& aModifiers);
    115 
    116  void ReportKeyConflict(const char16_t* aKey, const char16_t* aModifiers,
    117                         dom::Element* aKeyElement, const char* aMessageName);
    118  void GetEventType(nsAString& aEvent);
    119  bool ModifiersMatchMask(dom::UIEvent* aEvent,
    120                          const IgnoreModifierState& aIgnoreModifierState);
    121  MOZ_CAN_RUN_SCRIPT
    122  nsresult DispatchXBLCommand(dom::EventTarget* aTarget, dom::Event* aEvent);
    123  MOZ_CAN_RUN_SCRIPT
    124  nsresult DispatchXULKeyCommand(dom::Event* aEvent);
    125 
    126  Modifiers GetModifiers() const;
    127  Modifiers GetModifiersMask() const;
    128 
    129  static int32_t KeyToMask(uint32_t aKey);
    130  static int32_t AccelKeyMask();
    131 
    132  static const int32_t cShift;
    133  static const int32_t cAlt;
    134  static const int32_t cControl;
    135  static const int32_t cMeta;
    136 
    137  static const int32_t cShiftMask;
    138  static const int32_t cAltMask;
    139  static const int32_t cControlMask;
    140  static const int32_t cMetaMask;
    141 
    142  static const int32_t cAllModifiers;
    143 
    144 protected:
    145  union {
    146    nsIWeakReference*
    147        mHandlerElement;  // For XUL <key> element handlers. [STRONG]
    148    char16_t* mCommand;   // For built-in shortcuts the command to execute.
    149  };
    150 
    151  // The following four values make up 32 bits.
    152  bool mIsXULKey;  // This handler is either for a XUL <key> element or it is
    153                   // a command dispatcher.
    154  uint8_t mMisc;   // Miscellaneous extra information.  For key events,
    155                   // stores whether or not we're a key code or char code.
    156                   // For mouse events, stores the clickCount.
    157 
    158  ReservedKey mReserved;  // <key> is reserved for chrome. Not used by handlers.
    159 
    160  int32_t mKeyMask;  // Which modifier keys this event handler expects to have
    161                     // down in order to be matched.
    162 
    163  // The primary filter information for mouse/key events.
    164  int32_t mDetail;  // For key events, contains a charcode or keycode. For
    165                    // mouse events, stores the button info.
    166 
    167  // Prototype handlers are chained. We own the next handler in the chain.
    168  KeyEventHandler* mNextHandler;
    169  RefPtr<nsAtom> mEventName;  // The type of the event, e.g., "keypress"
    170 };
    171 
    172 }  // namespace mozilla
    173 
    174 #endif