tor-browser

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

KeyboardEvent.h (5447B)


      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_dom_KeyboardEvent_h_
      8 #define mozilla_dom_KeyboardEvent_h_
      9 
     10 #include "mozilla/EventForwards.h"
     11 #include "mozilla/dom/KeyboardEventBinding.h"
     12 #include "mozilla/dom/UIEvent.h"
     13 
     14 namespace mozilla::dom {
     15 
     16 class KeyboardEvent : public UIEvent {
     17 public:
     18  KeyboardEvent(EventTarget* aOwner, nsPresContext* aPresContext,
     19                WidgetKeyboardEvent* aEvent);
     20 
     21  NS_INLINE_DECL_REFCOUNTING_INHERITED(KeyboardEvent, UIEvent)
     22 
     23  virtual KeyboardEvent* AsKeyboardEvent() override { return this; }
     24 
     25  static already_AddRefed<KeyboardEvent> ConstructorJS(
     26      const GlobalObject& aGlobal, const nsAString& aType,
     27      const KeyboardEventInit& aParam);
     28 
     29  virtual JSObject* WrapObjectInternal(
     30      JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override {
     31    return KeyboardEvent_Binding::Wrap(aCx, this, aGivenProto);
     32  }
     33 
     34  bool AltKey(CallerType aCallerType = CallerType::System);
     35  bool CtrlKey(CallerType aCallerType = CallerType::System);
     36  bool ShiftKey(CallerType aCallerType = CallerType::System);
     37  bool MetaKey();
     38 
     39  // Returns true if the modifier state of the event matches the modifier state
     40  // of access key.
     41  bool IsMenuAccessKeyPressed() const;
     42  Modifiers GetModifiersForMenuAccessKey() const;
     43 
     44  bool GetModifierState(const nsAString& aKey,
     45                        CallerType aCallerType = CallerType::System) {
     46    bool modifierState = GetModifierStateInternal(aKey);
     47 
     48    if (!ShouldResistFingerprinting(aCallerType)) {
     49      return modifierState;
     50    }
     51 
     52    Modifiers modifier = WidgetInputEvent::GetModifier(aKey);
     53    return GetSpoofedModifierStates(modifier, modifierState);
     54  }
     55 
     56  bool Repeat();
     57  bool IsComposing();
     58  void GetKey(nsAString& aKey) const;
     59  uint32_t CharCode(CallerType aCallerType = CallerType::System);
     60  uint32_t KeyCode(CallerType aCallerType = CallerType::System);
     61  virtual uint32_t Which(CallerType aCallerType = CallerType::System) override;
     62  uint32_t Location();
     63 
     64  void GetCode(nsAString& aCode, CallerType aCallerType = CallerType::System);
     65  void GetInitDict(KeyboardEventInit& aParam);
     66 
     67  void InitKeyEventJS(const nsAString& aType, bool aCanBubble, bool aCancelable,
     68                      nsGlobalWindowInner* aView, bool aCtrlKey, bool aAltKey,
     69                      bool aShiftKey, bool aMetaKey, uint32_t aKeyCode,
     70                      uint32_t aCharCode);
     71  static bool IsInitKeyEventAvailable(JSContext*, JSObject*);
     72 
     73  void InitKeyboardEventJS(const nsAString& aType, bool aCanBubble,
     74                           bool aCancelable, nsGlobalWindowInner* aView,
     75                           const nsAString& aKey, uint32_t aLocation,
     76                           bool aCtrlKey, bool aAltKey, bool aShiftKey,
     77                           bool aMetaKey);
     78 
     79 protected:
     80  ~KeyboardEvent() = default;
     81 
     82  void InitWithKeyboardEventInit(EventTarget* aOwner, const nsAString& aType,
     83                                 const KeyboardEventInit& aParam);
     84 
     85 private:
     86  // True, if the instance is initialized by JS.
     87  bool mInitializedByJS;
     88  // True, if the instance is initialized by Ctor.
     89  bool mInitializedByCtor;
     90 
     91  // If the instance is created with Constructor(), which may have independent
     92  // value.  mInitializedWhichValue stores it.  I.e., this is invalid when
     93  // mInitializedByCtor is false.
     94  uint32_t mInitializedWhichValue;
     95 
     96  // This method returns the boolean to indicate whether spoofing keyboard
     97  // event for fingerprinting resistance. It will return true when pref
     98  // 'privacy.resistFingerprinting' is true and the event target is content.
     99  // Otherwise, it will return false.
    100  bool ShouldResistFingerprinting(CallerType aCallerType);
    101 
    102  // This method returns the spoofed modifier state of the given modifier key
    103  // for fingerprinting resistance.
    104  bool GetSpoofedModifierStates(const Modifiers aModifierKey,
    105                                const bool aRawModifierState);
    106 
    107  /**
    108   * ComputeTraditionalKeyCode() computes traditional keyCode value.  I.e.,
    109   * returns 0 if this event should return non-zero from CharCode().
    110   * In spite of the name containing "traditional", this computes spoof
    111   * keyCode value if user wants it.
    112   *
    113   * @param aKeyboardEvent  Should be |*mEvent->AsKeyboardEvent()|.
    114   * @param aCallerType     Set caller type of KeyCode() or CharCode().
    115   * @return                If traditional charCode value is 0, returns
    116   *                        the raw keyCode value or spoof keyCode value.
    117   *                        Otherwise, 0.
    118   */
    119  uint32_t ComputeTraditionalKeyCode(WidgetKeyboardEvent& aKeyboardEvent,
    120                                     CallerType aCallerType);
    121  /**
    122   * ShouldUseSameValueForCharCodeAndKeyCode() returns true if KeyCode() and
    123   * CharCode() should return same value.
    124   */
    125  bool ShouldUseSameValueForCharCodeAndKeyCode(
    126      const WidgetKeyboardEvent& aKeyboardEvent, CallerType aCallerType) const;
    127 };
    128 
    129 }  // namespace mozilla::dom
    130 
    131 already_AddRefed<mozilla::dom::KeyboardEvent> NS_NewDOMKeyboardEvent(
    132    mozilla::dom::EventTarget* aOwner, nsPresContext* aPresContext,
    133    mozilla::WidgetKeyboardEvent* aEvent);
    134 
    135 #endif  // mozilla_dom_KeyboardEvent_h_