Source code

Revision control

Other Tools

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
#include "nsCOMPtr.h"
8
#include "nsXBLPrototypeHandler.h"
9
#include "nsXBLWindowKeyHandler.h"
10
#include "nsIContent.h"
11
#include "nsAtom.h"
12
#include "nsXBLService.h"
13
#include "nsIServiceManager.h"
14
#include "nsGkAtoms.h"
15
#include "nsXBLDocumentInfo.h"
16
#include "nsFocusManager.h"
17
#include "nsIURI.h"
18
#include "nsNetUtil.h"
19
#include "nsContentUtils.h"
20
#include "nsXBLPrototypeBinding.h"
21
#include "nsPIDOMWindow.h"
22
#include "nsIDocShell.h"
23
#include "nsISelectionController.h"
24
#include "mozilla/EventListenerManager.h"
25
#include "mozilla/EventStateManager.h"
26
#include "mozilla/HTMLEditor.h"
27
#include "mozilla/Move.h"
28
#include "mozilla/Preferences.h"
29
#include "mozilla/StaticPtr.h"
30
#include "mozilla/TextEvents.h"
31
#include "mozilla/dom/Element.h"
32
#include "mozilla/dom/Event.h"
33
#include "mozilla/dom/EventBinding.h"
34
#include "mozilla/dom/KeyboardEvent.h"
35
#include "mozilla/layers/KeyboardMap.h"
36
#include "mozilla/ShortcutKeys.h"
37
38
using namespace mozilla;
39
using namespace mozilla::dom;
40
using namespace mozilla::layers;
41
42
nsXBLWindowKeyHandler::nsXBLWindowKeyHandler(Element* aElement,
43
EventTarget* aTarget)
44
: mTarget(aTarget), mHandler(nullptr) {
45
mWeakPtrForElement = do_GetWeakReference(aElement);
46
}
47
48
nsXBLWindowKeyHandler::~nsXBLWindowKeyHandler() {
49
// If mWeakPtrForElement is non-null, we created a prototype handler.
50
if (mWeakPtrForElement) delete mHandler;
51
}
52
53
NS_IMPL_ISUPPORTS(nsXBLWindowKeyHandler, nsIDOMEventListener)
54
55
static void BuildHandlerChain(nsIContent* aContent,
56
nsXBLPrototypeHandler** aResult) {
57
*aResult = nullptr;
58
59
// Since we chain each handler onto the next handler,
60
// we'll enumerate them here in reverse so that when we
61
// walk the chain they'll come out in the original order
62
for (nsIContent* key = aContent->GetLastChild(); key;
63
key = key->GetPreviousSibling()) {
64
if (!key->NodeInfo()->Equals(nsGkAtoms::key, kNameSpaceID_XUL)) {
65
continue;
66
}
67
68
Element* keyElement = key->AsElement();
69
// Check whether the key element has empty value at key/char attribute.
70
// Such element is used by localizers for alternative shortcut key
71
// definition on the locale. See bug 426501.
72
nsAutoString valKey, valCharCode, valKeyCode;
73
// Hopefully at least one of the attributes is set:
74
keyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::key, valKey) ||
75
keyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::charcode,
76
valCharCode) ||
77
keyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::keycode, valKeyCode);
78
// If not, ignore this key element.
79
if (valKey.IsEmpty() && valCharCode.IsEmpty() && valKeyCode.IsEmpty()) {
80
continue;
81
}
82
83
// reserved="pref" is the default for <key> elements.
84
XBLReservedKey reserved = XBLReservedKey_Unset;
85
if (keyElement->AttrValueIs(kNameSpaceID_None, nsGkAtoms::reserved,
86
nsGkAtoms::_true, eCaseMatters)) {
87
reserved = XBLReservedKey_True;
88
} else if (keyElement->AttrValueIs(kNameSpaceID_None, nsGkAtoms::reserved,
89
nsGkAtoms::_false, eCaseMatters)) {
90
reserved = XBLReservedKey_False;
91
}
92
93
nsXBLPrototypeHandler* handler =
94
new nsXBLPrototypeHandler(keyElement, reserved);
95
96
handler->SetNextHandler(*aResult);
97
*aResult = handler;
98
}
99
}
100
101
//
102
// EnsureHandlers
103
//
104
// Lazily load the XBL handlers. Overridden to handle being attached
105
// to a particular element rather than the document
106
//
107
nsresult nsXBLWindowKeyHandler::EnsureHandlers() {
108
nsCOMPtr<Element> el = GetElement();
109
NS_ENSURE_STATE(!mWeakPtrForElement || el);
110
if (el) {
111
// We are actually a XUL <keyset>.
112
if (mHandler) return NS_OK;
113
114
BuildHandlerChain(el, &mHandler);
115
} else { // We are an XBL file of handlers.
116
// Now determine which handlers we should be using.
117
if (IsHTMLEditableFieldFocused()) {
118
mHandler = ShortcutKeys::GetHandlers(HandlerType::eEditor);
119
} else {
120
mHandler = ShortcutKeys::GetHandlers(HandlerType::eBrowser);
121
}
122
}
123
124
return NS_OK;
125
}
126
127
nsresult nsXBLWindowKeyHandler::WalkHandlers(KeyboardEvent* aKeyEvent) {
128
if (aKeyEvent->DefaultPrevented()) {
129
return NS_OK;
130
}
131
132
// Don't process the event if it was not dispatched from a trusted source
133
if (!aKeyEvent->IsTrusted()) {
134
return NS_OK;
135
}
136
137
nsresult rv = EnsureHandlers();
138
NS_ENSURE_SUCCESS(rv, rv);
139
140
bool isDisabled;
141
nsCOMPtr<Element> el = GetElement(&isDisabled);
142
143
// skip keysets that are disabled
144
if (el && isDisabled) {
145
return NS_OK;
146
}
147
148
WalkHandlersInternal(aKeyEvent, true);
149
150
return NS_OK;
151
}
152
153
void nsXBLWindowKeyHandler::InstallKeyboardEventListenersTo(
154
EventListenerManager* aEventListenerManager) {
155
// For marking each keyboard event as if it's reserved by chrome,
156
// nsXBLWindowKeyHandlers need to listen each keyboard events before
157
// web contents.
158
aEventListenerManager->AddEventListenerByType(
159
this, NS_LITERAL_STRING("keydown"), TrustedEventsAtCapture());
160
aEventListenerManager->AddEventListenerByType(
161
this, NS_LITERAL_STRING("keyup"), TrustedEventsAtCapture());
162
aEventListenerManager->AddEventListenerByType(
163
this, NS_LITERAL_STRING("keypress"), TrustedEventsAtCapture());
164
aEventListenerManager->AddEventListenerByType(
165
this, NS_LITERAL_STRING("mozkeydownonplugin"), TrustedEventsAtCapture());
166
aEventListenerManager->AddEventListenerByType(
167
this, NS_LITERAL_STRING("mozkeyuponplugin"), TrustedEventsAtCapture());
168
169
// For reducing the IPC cost, preventing to dispatch reserved keyboard
170
// events into the content process.
171
aEventListenerManager->AddEventListenerByType(
172
this, NS_LITERAL_STRING("keydown"), TrustedEventsAtSystemGroupCapture());
173
aEventListenerManager->AddEventListenerByType(
174
this, NS_LITERAL_STRING("keyup"), TrustedEventsAtSystemGroupCapture());
175
aEventListenerManager->AddEventListenerByType(
176
this, NS_LITERAL_STRING("keypress"), TrustedEventsAtSystemGroupCapture());
177
aEventListenerManager->AddEventListenerByType(
178
this, NS_LITERAL_STRING("mozkeydownonplugin"),
179
TrustedEventsAtSystemGroupCapture());
180
aEventListenerManager->AddEventListenerByType(
181
this, NS_LITERAL_STRING("mozkeyuponplugin"),
182
TrustedEventsAtSystemGroupCapture());
183
184
// Handle keyboard events in bubbling phase of the system event group.
185
aEventListenerManager->AddEventListenerByType(
186
this, NS_LITERAL_STRING("keydown"), TrustedEventsAtSystemGroupBubble());
187
aEventListenerManager->AddEventListenerByType(
188
this, NS_LITERAL_STRING("keyup"), TrustedEventsAtSystemGroupBubble());
189
aEventListenerManager->AddEventListenerByType(
190
this, NS_LITERAL_STRING("keypress"), TrustedEventsAtSystemGroupBubble());
191
// mozaccesskeynotfound event is fired when modifiers of keypress event
192
// matches with modifier of content access key but it's not consumed by
193
// remote content.
194
aEventListenerManager->AddEventListenerByType(
195
this, NS_LITERAL_STRING("mozaccesskeynotfound"),
196
TrustedEventsAtSystemGroupBubble());
197
aEventListenerManager->AddEventListenerByType(
198
this, NS_LITERAL_STRING("mozkeydownonplugin"),
199
TrustedEventsAtSystemGroupBubble());
200
aEventListenerManager->AddEventListenerByType(
201
this, NS_LITERAL_STRING("mozkeyuponplugin"),
202
TrustedEventsAtSystemGroupBubble());
203
}
204
205
void nsXBLWindowKeyHandler::RemoveKeyboardEventListenersFrom(
206
EventListenerManager* aEventListenerManager) {
207
aEventListenerManager->RemoveEventListenerByType(
208
this, NS_LITERAL_STRING("keydown"), TrustedEventsAtCapture());
209
aEventListenerManager->RemoveEventListenerByType(
210
this, NS_LITERAL_STRING("keyup"), TrustedEventsAtCapture());
211
aEventListenerManager->RemoveEventListenerByType(
212
this, NS_LITERAL_STRING("keypress"), TrustedEventsAtCapture());
213
aEventListenerManager->RemoveEventListenerByType(
214
this, NS_LITERAL_STRING("mozkeydownonplugin"), TrustedEventsAtCapture());
215
aEventListenerManager->RemoveEventListenerByType(
216
this, NS_LITERAL_STRING("mozkeyuponplugin"), TrustedEventsAtCapture());
217
218
aEventListenerManager->RemoveEventListenerByType(
219
this, NS_LITERAL_STRING("keydown"), TrustedEventsAtSystemGroupCapture());
220
aEventListenerManager->RemoveEventListenerByType(
221
this, NS_LITERAL_STRING("keyup"), TrustedEventsAtSystemGroupCapture());
222
aEventListenerManager->RemoveEventListenerByType(
223
this, NS_LITERAL_STRING("keypress"), TrustedEventsAtSystemGroupCapture());
224
aEventListenerManager->RemoveEventListenerByType(
225
this, NS_LITERAL_STRING("mozkeydownonplugin"),
226
TrustedEventsAtSystemGroupCapture());
227
aEventListenerManager->RemoveEventListenerByType(
228
this, NS_LITERAL_STRING("mozkeyuponplugin"),
229
TrustedEventsAtSystemGroupCapture());
230
231
aEventListenerManager->RemoveEventListenerByType(
232
this, NS_LITERAL_STRING("keydown"), TrustedEventsAtSystemGroupBubble());
233
aEventListenerManager->RemoveEventListenerByType(
234
this, NS_LITERAL_STRING("keyup"), TrustedEventsAtSystemGroupBubble());
235
aEventListenerManager->RemoveEventListenerByType(
236
this, NS_LITERAL_STRING("keypress"), TrustedEventsAtSystemGroupBubble());
237
aEventListenerManager->RemoveEventListenerByType(
238
this, NS_LITERAL_STRING("mozaccesskeynotfound"),
239
TrustedEventsAtSystemGroupBubble());
240
aEventListenerManager->RemoveEventListenerByType(
241
this, NS_LITERAL_STRING("mozkeydownonplugin"),
242
TrustedEventsAtSystemGroupBubble());
243
aEventListenerManager->RemoveEventListenerByType(
244
this, NS_LITERAL_STRING("mozkeyuponplugin"),
245
TrustedEventsAtSystemGroupBubble());
246
}
247
248
/* static */
249
KeyboardMap nsXBLWindowKeyHandler::CollectKeyboardShortcuts() {
250
nsXBLPrototypeHandler* handlers =
251
ShortcutKeys::GetHandlers(HandlerType::eBrowser);
252
253
// Convert the handlers into keyboard shortcuts, using an AutoTArray with
254
// the maximum amount of shortcuts used on any platform to minimize
255
// allocations
256
AutoTArray<KeyboardShortcut, 48> shortcuts;
257
258
// Append keyboard shortcuts for hardcoded actions like tab
259
KeyboardShortcut::AppendHardcodedShortcuts(shortcuts);
260
261
for (nsXBLPrototypeHandler* handler = handlers; handler;
262
handler = handler->GetNextHandler()) {
263
KeyboardShortcut shortcut;
264
if (handler->TryConvertToKeyboardShortcut(&shortcut)) {
265
shortcuts.AppendElement(shortcut);
266
}
267
}
268
269
return KeyboardMap(std::move(shortcuts));
270
}
271
272
NS_IMETHODIMP
273
nsXBLWindowKeyHandler::HandleEvent(Event* aEvent) {
274
RefPtr<KeyboardEvent> keyEvent = aEvent->AsKeyboardEvent();
275
NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG);
276
277
if (aEvent->EventPhase() == Event_Binding::CAPTURING_PHASE) {
278
if (aEvent->WidgetEventPtr()->mFlags.mInSystemGroup) {
279
HandleEventOnCaptureInSystemEventGroup(keyEvent);
280
} else {
281
HandleEventOnCaptureInDefaultEventGroup(keyEvent);
282
}
283
return NS_OK;
284
}
285
286
WidgetKeyboardEvent* widgetKeyboardEvent =
287
aEvent->WidgetEventPtr()->AsKeyboardEvent();
288
if (widgetKeyboardEvent->IsKeyEventOnPlugin()) {
289
// key events on plugin shouldn't execute shortcut key handlers which are
290
// not reserved.
291
if (!widgetKeyboardEvent->IsReservedByChrome()) {
292
return NS_OK;
293
}
294
295
// If the event is untrusted event or was already consumed, do nothing.
296
if (!widgetKeyboardEvent->IsTrusted() ||
297
widgetKeyboardEvent->DefaultPrevented()) {
298
return NS_OK;
299
}
300
301
// XXX Don't check isReserved here because even if the handler in this
302
// instance isn't reserved but another instance reserves the key
303
// combination, it will be executed when the event is normal keyboard
304
// events...
305
bool isReserved = false;
306
if (!HasHandlerForEvent(keyEvent, &isReserved)) {
307
return NS_OK;
308
}
309
}
310
311
// If this event was handled by APZ then don't do the default action, and
312
// preventDefault to prevent any other listeners from handling the event.
313
if (widgetKeyboardEvent->mFlags.mHandledByAPZ) {
314
aEvent->PreventDefault();
315
return NS_OK;
316
}
317
318
return WalkHandlers(keyEvent);
319
}
320
321
void nsXBLWindowKeyHandler::HandleEventOnCaptureInDefaultEventGroup(
322
KeyboardEvent* aEvent) {
323
WidgetKeyboardEvent* widgetKeyboardEvent =
324
aEvent->WidgetEventPtr()->AsKeyboardEvent();
325
326
if (widgetKeyboardEvent->IsReservedByChrome()) {
327
return;
328
}
329
330
bool isReserved = false;
331
if (HasHandlerForEvent(aEvent, &isReserved) && isReserved) {
332
widgetKeyboardEvent->MarkAsReservedByChrome();
333
}
334
}
335
336
void nsXBLWindowKeyHandler::HandleEventOnCaptureInSystemEventGroup(
337
KeyboardEvent* aEvent) {
338
WidgetKeyboardEvent* widgetEvent =
339
aEvent->WidgetEventPtr()->AsKeyboardEvent();
340
341
// If the event won't be sent to remote process, this listener needs to do
342
// nothing. Note that even if mOnlySystemGroupDispatchInContent is true,
343
// we need to send the event to remote process and check reply event
344
// before matching it with registered shortcut keys because event listeners
345
// in the system event group may want to handle the event before registered
346
// shortcut key handlers.
347
if (!widgetEvent->WillBeSentToRemoteProcess()) {
348
return;
349
}
350
351
if (!HasHandlerForEvent(aEvent)) {
352
return;
353
}
354
355
// If this event wasn't marked as IsCrossProcessForwardingStopped,
356
// yet, it means it wasn't processed by content. We'll not call any
357
// of the handlers at this moment, and will wait the reply event.
358
// So, stop immediate propagation in this event first, then, mark it as
359
// waiting reply from remote process. Finally, when this process receives
360
// a reply from the remote process, it should be dispatched into this
361
// DOM tree again.
362
widgetEvent->StopImmediatePropagation();
363
widgetEvent->MarkAsWaitingReplyFromRemoteProcess();
364
}
365
366
bool nsXBLWindowKeyHandler::IsHTMLEditableFieldFocused() {
367
nsFocusManager* fm = nsFocusManager::GetFocusManager();
368
if (!fm) return false;
369
370
nsCOMPtr<mozIDOMWindowProxy> focusedWindow;
371
fm->GetFocusedWindow(getter_AddRefs(focusedWindow));
372
if (!focusedWindow) return false;
373
374
auto* piwin = nsPIDOMWindowOuter::From(focusedWindow);
375
nsIDocShell* docShell = piwin->GetDocShell();
376
if (!docShell) {
377
return false;
378
}
379
380
RefPtr<HTMLEditor> htmlEditor = docShell->GetHTMLEditor();
381
if (!htmlEditor) {
382
return false;
383
}
384
385
nsCOMPtr<Document> doc = htmlEditor->GetDocument();
386
if (doc->HasFlag(NODE_IS_EDITABLE)) {
387
// Don't need to perform any checks in designMode documents.
388
return true;
389
}
390
391
nsINode* focusedNode = fm->GetFocusedElement();
392
if (focusedNode && focusedNode->IsElement()) {
393
// If there is a focused element, make sure it's in the active editing host.
394
// Note that GetActiveEditingHost finds the current editing host based on
395
// the document's selection. Even though the document selection is usually
396
// collapsed to where the focus is, but the page may modify the selection
397
// without our knowledge, in which case this check will do something useful.
398
nsCOMPtr<Element> activeEditingHost = htmlEditor->GetActiveEditingHost();
399
if (!activeEditingHost) {
400
return false;
401
}
402
return focusedNode->IsInclusiveDescendantOf(activeEditingHost);
403
}
404
405
return false;
406
}
407
408
//
409
// WalkHandlersInternal and WalkHandlersAndExecute
410
//
411
// Given a particular DOM event and a pointer to the first handler in the list,
412
// scan through the list to find something to handle the event. If aExecute =
413
// true, the handler will be executed; otherwise just return an answer telling
414
// if a handler for that event was found.
415
//
416
bool nsXBLWindowKeyHandler::WalkHandlersInternal(KeyboardEvent* aKeyEvent,
417
bool aExecute,
418
bool* aOutReservedForChrome) {
419
WidgetKeyboardEvent* nativeKeyboardEvent =
420
aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
421
MOZ_ASSERT(nativeKeyboardEvent);
422
423
AutoShortcutKeyCandidateArray shortcutKeys;
424
nativeKeyboardEvent->GetShortcutKeyCandidates(shortcutKeys);
425
426
if (shortcutKeys.IsEmpty()) {
427
return WalkHandlersAndExecute(aKeyEvent, 0, IgnoreModifierState(), aExecute,
428
aOutReservedForChrome);
429
}
430
431
for (uint32_t i = 0; i < shortcutKeys.Length(); ++i) {
432
ShortcutKeyCandidate& key = shortcutKeys[i];
433
IgnoreModifierState ignoreModifierState;
434
ignoreModifierState.mShift = key.mIgnoreShift;
435
if (WalkHandlersAndExecute(aKeyEvent, key.mCharCode, ignoreModifierState,
436
aExecute, aOutReservedForChrome)) {
437
return true;
438
}
439
}
440
return false;
441
}
442
443
bool nsXBLWindowKeyHandler::WalkHandlersAndExecute(
444
KeyboardEvent* aKeyEvent, uint32_t aCharCode,
445
const IgnoreModifierState& aIgnoreModifierState, bool aExecute,
446
bool* aOutReservedForChrome) {
447
if (aOutReservedForChrome) {
448
*aOutReservedForChrome = false;
449
}
450
451
WidgetKeyboardEvent* widgetKeyboardEvent =
452
aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
453
if (NS_WARN_IF(!widgetKeyboardEvent)) {
454
return false;
455
}
456
457
nsAtom* eventType =
458
ShortcutKeys::ConvertEventToDOMEventType(widgetKeyboardEvent);
459
460
// Try all of the handlers until we find one that matches the event.
461
for (nsXBLPrototypeHandler* handler = mHandler; handler;
462
handler = handler->GetNextHandler()) {
463
bool stopped = aKeyEvent->IsDispatchStopped();
464
if (stopped) {
465
// The event is finished, don't execute any more handlers
466
return false;
467
}
468
469
if (aExecute) {
470
// If the event is eKeyDownOnPlugin, it should execute either keydown
471
// handler or keypress handler because eKeyDownOnPlugin events are
472
// never followed by keypress events.
473
if (widgetKeyboardEvent->mMessage == eKeyDownOnPlugin) {
474
if (!handler->EventTypeEquals(nsGkAtoms::keydown) &&
475
!handler->EventTypeEquals(nsGkAtoms::keypress)) {
476
continue;
477
}
478
// The other event types should exactly be matched with the handler's
479
// event type.
480
} else if (!handler->EventTypeEquals(eventType)) {
481
continue;
482
}
483
} else {
484
if (handler->EventTypeEquals(nsGkAtoms::keypress)) {
485
// If the handler is a keypress event handler, we also need to check
486
// if coming keydown event is a preceding event of reserved key
487
// combination because if default action of a keydown event is
488
// prevented, following keypress event won't be fired. However, if
489
// following keypress event is reserved, we shouldn't allow web
490
// contents to prevent the default of the preceding keydown event.
491
if (eventType != nsGkAtoms::keydown &&
492
eventType != nsGkAtoms::keypress) {
493
continue;
494
}
495
} else if (!handler->EventTypeEquals(eventType)) {
496
// Otherwise, eventType should exactly be matched.
497
continue;
498
}
499
}
500
501
// Check if the keyboard event *may* execute the handler.
502
if (!handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreModifierState)) {
503
continue; // try the next one
504
}
505
506
// Before executing this handler, check that it's not disabled,
507
// and that it has something to do (oncommand of the <key> or its
508
// <command> is non-empty).
509
nsCOMPtr<Element> commandElement;
510
if (!GetElementForHandler(handler, getter_AddRefs(commandElement))) {
511
continue;
512
}
513
514
if (commandElement) {
515
if (aExecute && !IsExecutableElement(commandElement)) {
516
continue;
517
}
518
}
519
520
if (!aExecute) {
521
if (handler->EventTypeEquals(eventType)) {
522
if (aOutReservedForChrome) {
523
*aOutReservedForChrome = IsReservedKey(widgetKeyboardEvent, handler);
524
}
525
526
return true;
527
}
528
529
// If the command is reserved and the event is keydown, check also if
530
// the handler is for keypress because if following keypress event is
531
// reserved, we shouldn't dispatch the event into web contents.
532
if (eventType == nsGkAtoms::keydown &&
533
handler->EventTypeEquals(nsGkAtoms::keypress)) {
534
if (IsReservedKey(widgetKeyboardEvent, handler)) {
535
if (aOutReservedForChrome) {
536
*aOutReservedForChrome = true;
537
}
538
539
return true;
540
}
541
}
542
// Otherwise, we've not found a handler for the event yet.
543
continue;
544
}
545
546
// This should only be assigned when aExecute is false.
547
MOZ_ASSERT(!aOutReservedForChrome);
548
549
// If it's not reserved and the event is a key event on a plugin,
550
// the handler shouldn't be executed.
551
if (widgetKeyboardEvent->IsKeyEventOnPlugin() &&
552
!IsReservedKey(widgetKeyboardEvent, handler)) {
553
return false;
554
}
555
556
nsCOMPtr<EventTarget> target;
557
nsCOMPtr<Element> chromeHandlerElement = GetElement();
558
if (chromeHandlerElement) {
559
// XXX commandElement may be nullptr...
560
target = commandElement;
561
} else {
562
target = mTarget;
563
}
564
565
// XXX Do we execute only one handler even if the handler neither stops
566
// propagation nor prevents default of the event?
567
nsresult rv = handler->ExecuteHandler(target, aKeyEvent);
568
if (NS_SUCCEEDED(rv)) {
569
return true;
570
}
571
}
572
573
#ifdef XP_WIN
574
// Windows native applications ignore Windows-Logo key state when checking
575
// shortcut keys even if the key is pressed. Therefore, if there is no
576
// shortcut key which exactly matches current modifier state, we should
577
// retry to look for a shortcut key without the Windows-Logo key press.
578
if (!aIgnoreModifierState.mOS && widgetKeyboardEvent->IsOS()) {
579
IgnoreModifierState ignoreModifierState(aIgnoreModifierState);
580
ignoreModifierState.mOS = true;
581
return WalkHandlersAndExecute(aKeyEvent, aCharCode, ignoreModifierState,
582
aExecute);
583
}
584
#endif
585
586
return false;
587
}
588
589
bool nsXBLWindowKeyHandler::IsReservedKey(WidgetKeyboardEvent* aKeyEvent,
590
nsXBLPrototypeHandler* aHandler) {
591
XBLReservedKey reserved = aHandler->GetIsReserved();
592
// reserved="true" means that the key is always reserved. reserved="false"
593
// means that the key is never reserved. Otherwise, we check site-specific
594
// permissions.
595
if (reserved == XBLReservedKey_False) {
596
return false;
597
}
598
599
if (reserved == XBLReservedKey_True) {
600
return true;
601
}
602
603
return nsContentUtils::ShouldBlockReservedKeys(aKeyEvent);
604
}
605
606
bool nsXBLWindowKeyHandler::HasHandlerForEvent(KeyboardEvent* aEvent,
607
bool* aOutReservedForChrome) {
608
WidgetKeyboardEvent* widgetKeyboardEvent =
609
aEvent->WidgetEventPtr()->AsKeyboardEvent();
610
if (NS_WARN_IF(!widgetKeyboardEvent) || !widgetKeyboardEvent->IsTrusted()) {
611
return false;
612
}
613
614
nsresult rv = EnsureHandlers();
615
NS_ENSURE_SUCCESS(rv, false);
616
617
bool isDisabled;
618
nsCOMPtr<Element> el = GetElement(&isDisabled);
619
if (el && isDisabled) {
620
return false;
621
}
622
623
return WalkHandlersInternal(aEvent, false, aOutReservedForChrome);
624
}
625
626
already_AddRefed<Element> nsXBLWindowKeyHandler::GetElement(bool* aIsDisabled) {
627
nsCOMPtr<Element> element = do_QueryReferent(mWeakPtrForElement);
628
if (element && aIsDisabled) {
629
*aIsDisabled = element->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
630
nsGkAtoms::_true, eCaseMatters);
631
}
632
return element.forget();
633
}
634
635
bool nsXBLWindowKeyHandler::GetElementForHandler(
636
nsXBLPrototypeHandler* aHandler, Element** aElementForHandler) {
637
MOZ_ASSERT(aElementForHandler);
638
*aElementForHandler = nullptr;
639
640
RefPtr<Element> keyElement = aHandler->GetHandlerElement();
641
if (!keyElement) {
642
return true; // XXX Even though no key element?
643
}
644
645
nsCOMPtr<Element> chromeHandlerElement = GetElement();
646
if (!chromeHandlerElement) {
647
NS_WARNING_ASSERTION(keyElement->IsInUncomposedDoc(), "uncomposed");
648
keyElement.swap(*aElementForHandler);
649
return true;
650
}
651
652
// We are in a XUL doc. Obtain our command attribute.
653
nsAutoString command;
654
keyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::command, command);
655
if (command.IsEmpty()) {
656
// There is no command element associated with the key element.
657
NS_WARNING_ASSERTION(keyElement->IsInUncomposedDoc(), "uncomposed");
658
keyElement.swap(*aElementForHandler);
659
return true;
660
}
661
662
// XXX Shouldn't we check this earlier?
663
Document* doc = keyElement->GetUncomposedDoc();
664
if (NS_WARN_IF(!doc)) {
665
return false;
666
}
667
668
nsCOMPtr<Element> commandElement = doc->GetElementById(command);
669
if (!commandElement) {
670
NS_ERROR(
671
"A XUL <key> is observing a command that doesn't exist. "
672
"Unable to execute key binding!");
673
return false;
674
}
675
676
commandElement.swap(*aElementForHandler);
677
return true;
678
}
679
680
bool nsXBLWindowKeyHandler::IsExecutableElement(Element* aElement) const {
681
if (!aElement) {
682
return false;
683
}
684
685
nsAutoString value;
686
aElement->GetAttribute(NS_LITERAL_STRING("disabled"), value);
687
if (value.EqualsLiteral("true")) {
688
return false;
689
}
690
691
aElement->GetAttribute(NS_LITERAL_STRING("oncommand"), value);
692
if (value.IsEmpty()) {
693
return false;
694
}
695
696
return true;
697
}
698
699
///////////////////////////////////////////////////////////////////////////////////
700
701
already_AddRefed<nsXBLWindowKeyHandler> NS_NewXBLWindowKeyHandler(
702
Element* aElement, EventTarget* aTarget) {
703
RefPtr<nsXBLWindowKeyHandler> result =
704
new nsXBLWindowKeyHandler(aElement, aTarget);
705
return result.forget();
706
}