Bug 1688354 [wpt PR 27298] - Treat 'rem' as an absolute unit for font size, a=testonly
[gecko.git] / dom / events / KeyboardEvent.cpp
blobead8c034e9ffb34191f12fc2c2c24ba1a552cf65
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/. */
7 #include "mozilla/dom/KeyboardEvent.h"
9 #include "mozilla/TextEvents.h"
10 #include "mozilla/dom/Document.h"
11 #include "nsContentUtils.h"
12 #include "nsRFPService.h"
13 #include "prtime.h"
15 namespace mozilla::dom {
17 KeyboardEvent::KeyboardEvent(EventTarget* aOwner, nsPresContext* aPresContext,
18 WidgetKeyboardEvent* aEvent)
19 : UIEvent(aOwner, aPresContext,
20 aEvent ? aEvent
21 : new WidgetKeyboardEvent(false, eVoidEvent, nullptr)),
22 mInitializedByJS(false),
23 mInitializedByCtor(false),
24 mInitializedWhichValue(0) {
25 if (aEvent) {
26 mEventIsInternal = false;
27 } else {
28 mEventIsInternal = true;
29 mEvent->mTime = PR_Now();
30 mEvent->AsKeyboardEvent()->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
34 bool KeyboardEvent::AltKey(CallerType aCallerType) {
35 bool altState = mEvent->AsKeyboardEvent()->IsAlt();
37 if (!ShouldResistFingerprinting(aCallerType)) {
38 return altState;
41 // We need to give a spoofed state for Alt key since it could be used as a
42 // modifier key in certain keyboard layout. For example, the '@' key for
43 // German keyboard for MAC is Alt+L.
44 return GetSpoofedModifierStates(Modifier::MODIFIER_ALT, altState);
47 bool KeyboardEvent::CtrlKey(CallerType aCallerType) {
48 // We don't spoof this key when privacy.resistFingerprinting
49 // is enabled, because it is often used for command key
50 // combinations in web apps.
51 return mEvent->AsKeyboardEvent()->IsControl();
54 bool KeyboardEvent::ShiftKey(CallerType aCallerType) {
55 bool shiftState = mEvent->AsKeyboardEvent()->IsShift();
57 if (!ShouldResistFingerprinting(aCallerType)) {
58 return shiftState;
61 return GetSpoofedModifierStates(Modifier::MODIFIER_SHIFT, shiftState);
64 bool KeyboardEvent::MetaKey() {
65 // We don't spoof this key when privacy.resistFingerprinting
66 // is enabled, because it is often used for command key
67 // combinations in web apps.
68 return mEvent->AsKeyboardEvent()->IsMeta();
71 bool KeyboardEvent::Repeat() { return mEvent->AsKeyboardEvent()->mIsRepeat; }
73 bool KeyboardEvent::IsComposing() {
74 return mEvent->AsKeyboardEvent()->mIsComposing;
77 void KeyboardEvent::GetKey(nsAString& aKeyName) const {
78 mEvent->AsKeyboardEvent()->GetDOMKeyName(aKeyName);
81 void KeyboardEvent::GetCode(nsAString& aCodeName, CallerType aCallerType) {
82 if (!ShouldResistFingerprinting(aCallerType)) {
83 mEvent->AsKeyboardEvent()->GetDOMCodeName(aCodeName);
84 return;
87 // When fingerprinting resistance is enabled, we will give a spoofed code
88 // according to the content-language of the document.
89 nsCOMPtr<Document> doc = GetDocument();
91 nsRFPService::GetSpoofedCode(doc, mEvent->AsKeyboardEvent(), aCodeName);
94 void KeyboardEvent::GetInitDict(KeyboardEventInit& aParam) {
95 GetKey(aParam.mKey);
96 GetCode(aParam.mCode);
97 aParam.mLocation = Location();
98 aParam.mRepeat = Repeat();
99 aParam.mIsComposing = IsComposing();
101 // legacy attributes
102 aParam.mKeyCode = KeyCode();
103 aParam.mCharCode = CharCode();
104 aParam.mWhich = Which();
106 // modifiers from EventModifierInit
107 aParam.mCtrlKey = CtrlKey();
108 aParam.mShiftKey = ShiftKey();
109 aParam.mAltKey = AltKey();
110 aParam.mMetaKey = MetaKey();
112 WidgetKeyboardEvent* internalEvent = mEvent->AsKeyboardEvent();
113 aParam.mModifierAltGraph = internalEvent->IsAltGraph();
114 aParam.mModifierCapsLock = internalEvent->IsCapsLocked();
115 aParam.mModifierFn = internalEvent->IsFn();
116 aParam.mModifierFnLock = internalEvent->IsFnLocked();
117 aParam.mModifierNumLock = internalEvent->IsNumLocked();
118 aParam.mModifierOS = internalEvent->IsOS();
119 aParam.mModifierScrollLock = internalEvent->IsScrollLocked();
120 aParam.mModifierSymbol = internalEvent->IsSymbol();
121 aParam.mModifierSymbolLock = internalEvent->IsSymbolLocked();
123 // EventInit
124 aParam.mBubbles = internalEvent->mFlags.mBubbles;
125 aParam.mCancelable = internalEvent->mFlags.mCancelable;
128 bool KeyboardEvent::ShouldUseSameValueForCharCodeAndKeyCode(
129 const WidgetKeyboardEvent& aWidgetKeyboardEvent,
130 CallerType aCallerType) const {
131 // - If this event is initialized by JS, we don't need to return same value
132 // for keyCode and charCode since they can be initialized separately.
133 // - If this is not a keypress event, we shouldn't return same value for
134 // keyCode and charCode.
135 // - If we need to return legacy keyCode and charCode values for the web
136 // app due to in the blacklist.
137 // - If this event is referred by default handler, i.e., the caller is
138 // system or this event is now in the system group, we don't need to use
139 // hack for web-compat.
140 if (mInitializedByJS || aWidgetKeyboardEvent.mMessage != eKeyPress ||
141 aWidgetKeyboardEvent.mUseLegacyKeyCodeAndCharCodeValues ||
142 aCallerType == CallerType::System ||
143 aWidgetKeyboardEvent.mFlags.mInSystemGroup) {
144 return false;
147 MOZ_ASSERT(aCallerType == CallerType::NonSystem);
149 return StaticPrefs::
150 dom_keyboardevent_keypress_set_keycode_and_charcode_to_same_value();
153 uint32_t KeyboardEvent::CharCode(CallerType aCallerType) {
154 WidgetKeyboardEvent* widgetKeyboardEvent = mEvent->AsKeyboardEvent();
155 if (mInitializedByJS) {
156 // If this is initialized by Ctor, we should return the initialized value.
157 if (mInitializedByCtor) {
158 return widgetKeyboardEvent->mCharCode;
160 // Otherwise, i.e., initialized by InitKey*Event(), we should return the
161 // initialized value only when eKeyPress or eAccessKeyNotFound event.
162 // Although this is odd, but our traditional behavior.
163 return widgetKeyboardEvent->mMessage == eKeyPress ||
164 widgetKeyboardEvent->mMessage == eAccessKeyNotFound
165 ? widgetKeyboardEvent->mCharCode
166 : 0;
169 // If the key is a function key, we should return the result of KeyCode()
170 // even from CharCode(). Otherwise, i.e., the key may be a printable
171 // key or actually a printable key, we should return the given charCode
172 // value.
174 if (widgetKeyboardEvent->mKeyNameIndex != KEY_NAME_INDEX_USE_STRING &&
175 ShouldUseSameValueForCharCodeAndKeyCode(*widgetKeyboardEvent,
176 aCallerType)) {
177 return ComputeTraditionalKeyCode(*widgetKeyboardEvent, aCallerType);
180 return widgetKeyboardEvent->mCharCode;
183 uint32_t KeyboardEvent::KeyCode(CallerType aCallerType) {
184 WidgetKeyboardEvent* widgetKeyboardEvent = mEvent->AsKeyboardEvent();
185 if (mInitializedByJS) {
186 // If this is initialized by Ctor, we should return the initialized value.
187 if (mInitializedByCtor) {
188 return widgetKeyboardEvent->mKeyCode;
190 // Otherwise, i.e., initialized by InitKey*Event(), we should return the
191 // initialized value only when the event message is a valid keyboard event
192 // message. Although this is odd, but our traditional behavior.
193 // NOTE: The fix of bug 1222285 changed the behavior temporarily if
194 // spoofing is enabled. However, the behavior does not make sense
195 // since if the event is generated by JS, the behavior shouldn't
196 // be changed by whether spoofing is enabled or not. Therefore,
197 // we take back the original behavior.
198 return widgetKeyboardEvent->HasKeyEventMessage()
199 ? widgetKeyboardEvent->mKeyCode
200 : 0;
203 // If the key is not a function key, i.e., the key may be a printable key
204 // or a function key mapped as a printable key, we should use charCode value
205 // for keyCode value if this is a "keypress" event.
207 if (widgetKeyboardEvent->mKeyNameIndex == KEY_NAME_INDEX_USE_STRING &&
208 ShouldUseSameValueForCharCodeAndKeyCode(*widgetKeyboardEvent,
209 aCallerType)) {
210 return widgetKeyboardEvent->mCharCode;
213 return ComputeTraditionalKeyCode(*widgetKeyboardEvent, aCallerType);
216 uint32_t KeyboardEvent::ComputeTraditionalKeyCode(
217 WidgetKeyboardEvent& aKeyboardEvent, CallerType aCallerType) {
218 if (!ShouldResistFingerprinting(aCallerType)) {
219 return aKeyboardEvent.mKeyCode;
222 // In Netscape style (i.e., traditional behavior of Gecko), the keyCode
223 // should be zero if the char code is given.
224 if ((mEvent->mMessage == eKeyPress ||
225 mEvent->mMessage == eAccessKeyNotFound) &&
226 aKeyboardEvent.mCharCode) {
227 return 0;
230 // When fingerprinting resistance is enabled, we will give a spoofed keyCode
231 // according to the content-language of the document.
232 nsCOMPtr<Document> doc = GetDocument();
233 uint32_t spoofedKeyCode;
235 if (nsRFPService::GetSpoofedKeyCode(doc, &aKeyboardEvent, spoofedKeyCode)) {
236 return spoofedKeyCode;
239 return 0;
242 uint32_t KeyboardEvent::Which(CallerType aCallerType) {
243 // If this event is initialized with ctor, which can have independent value.
244 if (mInitializedByCtor) {
245 return mInitializedWhichValue;
248 switch (mEvent->mMessage) {
249 case eKeyDown:
250 case eKeyUp:
251 return KeyCode(aCallerType);
252 case eKeyPress:
253 // Special case for 4xp bug 62878. Try to make value of which
254 // more closely mirror the values that 4.x gave for RETURN and BACKSPACE
256 uint32_t keyCode = mEvent->AsKeyboardEvent()->mKeyCode;
257 if (keyCode == NS_VK_RETURN || keyCode == NS_VK_BACK) {
258 return keyCode;
260 return CharCode();
262 default:
263 break;
266 return 0;
269 uint32_t KeyboardEvent::Location() {
270 return mEvent->AsKeyboardEvent()->mLocation;
273 // static
274 already_AddRefed<KeyboardEvent> KeyboardEvent::ConstructorJS(
275 const GlobalObject& aGlobal, const nsAString& aType,
276 const KeyboardEventInit& aParam) {
277 nsCOMPtr<EventTarget> target = do_QueryInterface(aGlobal.GetAsSupports());
278 RefPtr<KeyboardEvent> newEvent = new KeyboardEvent(target, nullptr, nullptr);
279 newEvent->InitWithKeyboardEventInit(target, aType, aParam);
281 return newEvent.forget();
284 void KeyboardEvent::InitWithKeyboardEventInit(EventTarget* aOwner,
285 const nsAString& aType,
286 const KeyboardEventInit& aParam) {
287 bool trusted = Init(aOwner);
288 InitKeyEventJS(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView,
289 false, false, false, false, aParam.mKeyCode, aParam.mCharCode);
290 InitModifiers(aParam);
291 SetTrusted(trusted);
292 mDetail = aParam.mDetail;
293 mInitializedByJS = true;
294 mInitializedByCtor = true;
295 mInitializedWhichValue = aParam.mWhich;
297 WidgetKeyboardEvent* internalEvent = mEvent->AsKeyboardEvent();
298 internalEvent->mLocation = aParam.mLocation;
299 internalEvent->mIsRepeat = aParam.mRepeat;
300 internalEvent->mIsComposing = aParam.mIsComposing;
301 internalEvent->mKeyNameIndex =
302 WidgetKeyboardEvent::GetKeyNameIndex(aParam.mKey);
303 if (internalEvent->mKeyNameIndex == KEY_NAME_INDEX_USE_STRING) {
304 internalEvent->mKeyValue = aParam.mKey;
306 internalEvent->mCodeNameIndex =
307 WidgetKeyboardEvent::GetCodeNameIndex(aParam.mCode);
308 if (internalEvent->mCodeNameIndex == CODE_NAME_INDEX_USE_STRING) {
309 internalEvent->mCodeValue = aParam.mCode;
313 void KeyboardEvent::InitKeyEventJS(const nsAString& aType, bool aCanBubble,
314 bool aCancelable, nsGlobalWindowInner* aView,
315 bool aCtrlKey, bool aAltKey, bool aShiftKey,
316 bool aMetaKey, uint32_t aKeyCode,
317 uint32_t aCharCode) {
318 NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
319 mInitializedByJS = true;
320 mInitializedByCtor = false;
322 UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
324 WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
325 keyEvent->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
326 keyEvent->mKeyCode = aKeyCode;
327 keyEvent->mCharCode = aCharCode;
330 void KeyboardEvent::InitKeyboardEventJS(
331 const nsAString& aType, bool aCanBubble, bool aCancelable,
332 nsGlobalWindowInner* aView, const nsAString& aKey, uint32_t aLocation,
333 bool aCtrlKey, bool aAltKey, bool aShiftKey, bool aMetaKey) {
334 NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
335 mInitializedByJS = true;
336 mInitializedByCtor = false;
338 UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
340 WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
341 keyEvent->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
342 keyEvent->mLocation = aLocation;
343 keyEvent->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
344 keyEvent->mKeyValue = aKey;
347 bool KeyboardEvent::ShouldResistFingerprinting(CallerType aCallerType) {
348 // There are five situations we don't need to spoof this keyboard event.
349 // 1. This event is initialized by scripts.
350 // 2. This event is from Numpad.
351 // 3. This event is in the system group.
352 // 4. The caller type is system.
353 // 5. The pref privcy.resistFingerprinting' is false, we fast return here
354 // since we don't need to do any QI of following codes.
355 if (mInitializedByJS || aCallerType == CallerType::System ||
356 mEvent->mFlags.mInSystemGroup ||
357 !nsContentUtils::ShouldResistFingerprinting() ||
358 mEvent->AsKeyboardEvent()->mLocation ==
359 KeyboardEvent_Binding::DOM_KEY_LOCATION_NUMPAD) {
360 return false;
363 nsCOMPtr<Document> doc = GetDocument();
365 return doc && !nsContentUtils::IsChromeDoc(doc);
368 bool KeyboardEvent::GetSpoofedModifierStates(const Modifiers aModifierKey,
369 const bool aRawModifierState) {
370 bool spoofedState;
371 nsCOMPtr<Document> doc = GetDocument();
373 if (nsRFPService::GetSpoofedModifierStates(doc, mEvent->AsKeyboardEvent(),
374 aModifierKey, spoofedState)) {
375 return spoofedState;
378 return aRawModifierState;
381 } // namespace mozilla::dom
383 using namespace mozilla;
384 using namespace mozilla::dom;
386 already_AddRefed<KeyboardEvent> NS_NewDOMKeyboardEvent(
387 EventTarget* aOwner, nsPresContext* aPresContext,
388 WidgetKeyboardEvent* aEvent) {
389 RefPtr<KeyboardEvent> it = new KeyboardEvent(aOwner, aPresContext, aEvent);
390 return it.forget();