Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/xbl/nsXBLWindowKeyHandler.cpp
Line
Count
Source (jump to first uncovered line)
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 "nsIPresShell.h"
25
#include "mozilla/EventListenerManager.h"
26
#include "mozilla/EventStateManager.h"
27
#include "mozilla/HTMLEditor.h"
28
#include "mozilla/Move.h"
29
#include "mozilla/Preferences.h"
30
#include "mozilla/StaticPtr.h"
31
#include "mozilla/TextEvents.h"
32
#include "mozilla/dom/Element.h"
33
#include "mozilla/dom/Event.h"
34
#include "mozilla/dom/EventBinding.h"
35
#include "mozilla/dom/KeyboardEvent.h"
36
#include "mozilla/layers/KeyboardMap.h"
37
38
using namespace mozilla;
39
using namespace mozilla::dom;
40
using namespace mozilla::layers;
41
42
class nsXBLSpecialDocInfo : public nsIObserver
43
{
44
public:
45
  RefPtr<nsXBLDocumentInfo> mHTMLBindings;
46
47
  static const char sHTMLBindingStr[];
48
  static const char sUserHTMLBindingStr[];
49
50
  bool mInitialized;
51
52
public:
53
  NS_DECL_ISUPPORTS
54
  NS_DECL_NSIOBSERVER
55
56
  void LoadDocInfo();
57
  void GetHandlers(const nsACString& aRef,
58
                   nsXBLPrototypeHandler** handler);
59
60
0
  nsXBLSpecialDocInfo() : mInitialized(false) {}
61
62
protected:
63
0
  virtual ~nsXBLSpecialDocInfo() {}
64
65
};
66
67
const char nsXBLSpecialDocInfo::sHTMLBindingStr[] =
68
  "chrome://global/content/platformHTMLBindings.xml";
69
70
NS_IMPL_ISUPPORTS(nsXBLSpecialDocInfo, nsIObserver)
71
72
NS_IMETHODIMP
73
nsXBLSpecialDocInfo::Observe(nsISupports* aSubject,
74
                             const char* aTopic,
75
                             const char16_t* aData)
76
0
{
77
0
  MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown"), "wrong topic");
78
0
79
0
  // On shutdown, clear our fields to avoid an extra cycle collection.
80
0
  mHTMLBindings = nullptr;
81
0
  mInitialized = false;
82
0
  nsContentUtils::UnregisterShutdownObserver(this);
83
0
84
0
  return NS_OK;
85
0
}
86
87
void nsXBLSpecialDocInfo::LoadDocInfo()
88
0
{
89
0
  if (mInitialized)
90
0
    return;
91
0
  mInitialized = true;
92
0
  nsContentUtils::RegisterShutdownObserver(this);
93
0
94
0
  nsXBLService* xblService = nsXBLService::GetInstance();
95
0
  if (!xblService)
96
0
    return;
97
0
98
0
  // Obtain the platform doc info
99
0
  nsCOMPtr<nsIURI> bindingURI;
100
0
  NS_NewURI(getter_AddRefs(bindingURI), sHTMLBindingStr);
101
0
  if (!bindingURI) {
102
0
    return;
103
0
  }
104
0
  xblService->LoadBindingDocumentInfo(nullptr, nullptr,
105
0
                                      bindingURI,
106
0
                                      nullptr,
107
0
                                      true,
108
0
                                      getter_AddRefs(mHTMLBindings));
109
0
}
110
111
//
112
// GetHandlers
113
//
114
//
115
void
116
nsXBLSpecialDocInfo::GetHandlers(const nsACString& aRef,
117
                                 nsXBLPrototypeHandler** aHandler)
118
0
{
119
0
  if (mHTMLBindings) {
120
0
    nsXBLPrototypeBinding* binding = mHTMLBindings->GetPrototypeBinding(aRef);
121
0
122
0
    NS_ASSERTION(binding, "No binding found for the XBL window key handler.");
123
0
    if (!binding)
124
0
      return;
125
0
126
0
    *aHandler = binding->GetPrototypeHandlers();
127
0
  }
128
0
}
129
130
// Init statics
131
static StaticRefPtr<nsXBLSpecialDocInfo> sXBLSpecialDocInfo;
132
uint32_t nsXBLWindowKeyHandler::sRefCnt = 0;
133
134
/* static */ void
135
nsXBLWindowKeyHandler::EnsureSpecialDocInfo()
136
0
{
137
0
  if (!sXBLSpecialDocInfo) {
138
0
    sXBLSpecialDocInfo = new nsXBLSpecialDocInfo();
139
0
  }
140
0
  sXBLSpecialDocInfo->LoadDocInfo();
141
0
}
142
143
nsXBLWindowKeyHandler::nsXBLWindowKeyHandler(Element* aElement,
144
                                             EventTarget* aTarget)
145
  : mTarget(aTarget),
146
    mHandler(nullptr)
147
0
{
148
0
  mWeakPtrForElement = do_GetWeakReference(aElement);
149
0
  ++sRefCnt;
150
0
}
151
152
nsXBLWindowKeyHandler::~nsXBLWindowKeyHandler()
153
0
{
154
0
  // If mWeakPtrForElement is non-null, we created a prototype handler.
155
0
  if (mWeakPtrForElement)
156
0
    delete mHandler;
157
0
158
0
  --sRefCnt;
159
0
  if (!sRefCnt) {
160
0
    sXBLSpecialDocInfo = nullptr;
161
0
  }
162
0
}
163
164
NS_IMPL_ISUPPORTS(nsXBLWindowKeyHandler,
165
                  nsIDOMEventListener)
166
167
static void
168
BuildHandlerChain(nsIContent* aContent, nsXBLPrototypeHandler** aResult)
169
0
{
170
0
  *aResult = nullptr;
171
0
172
0
  // Since we chain each handler onto the next handler,
173
0
  // we'll enumerate them here in reverse so that when we
174
0
  // walk the chain they'll come out in the original order
175
0
  for (nsIContent* key = aContent->GetLastChild();
176
0
       key;
177
0
       key = key->GetPreviousSibling()) {
178
0
    if (!key->NodeInfo()->Equals(nsGkAtoms::key, kNameSpaceID_XUL)) {
179
0
      continue;
180
0
    }
181
0
182
0
    Element* keyElement = key->AsElement();
183
0
    // Check whether the key element has empty value at key/char attribute.
184
0
    // Such element is used by localizers for alternative shortcut key
185
0
    // definition on the locale. See bug 426501.
186
0
    nsAutoString valKey, valCharCode, valKeyCode;
187
0
    // Hopefully at least one of the attributes is set:
188
0
    keyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::key, valKey) ||
189
0
      keyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::charcode, valCharCode) ||
190
0
      keyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::keycode, valKeyCode);
191
0
    // If not, ignore this key element.
192
0
    if (valKey.IsEmpty() && valCharCode.IsEmpty() && valKeyCode.IsEmpty()) {
193
0
      continue;
194
0
    }
195
0
196
0
    // reserved="pref" is the default for <key> elements.
197
0
    XBLReservedKey reserved = XBLReservedKey_Unset;
198
0
    if (keyElement->AttrValueIs(kNameSpaceID_None, nsGkAtoms::reserved,
199
0
                                nsGkAtoms::_true, eCaseMatters)) {
200
0
      reserved = XBLReservedKey_True;
201
0
    } else if (keyElement->AttrValueIs(kNameSpaceID_None,
202
0
                                       nsGkAtoms::reserved,
203
0
                                       nsGkAtoms::_false, eCaseMatters)) {
204
0
      reserved = XBLReservedKey_False;
205
0
    }
206
0
207
0
    nsXBLPrototypeHandler* handler =
208
0
      new nsXBLPrototypeHandler(keyElement, reserved);
209
0
210
0
    handler->SetNextHandler(*aResult);
211
0
    *aResult = handler;
212
0
  }
213
0
}
214
215
//
216
// EnsureHandlers
217
//
218
// Lazily load the XBL handlers. Overridden to handle being attached
219
// to a particular element rather than the document
220
//
221
nsresult
222
nsXBLWindowKeyHandler::EnsureHandlers()
223
0
{
224
0
  nsCOMPtr<Element> el = GetElement();
225
0
  NS_ENSURE_STATE(!mWeakPtrForElement || el);
226
0
  if (el) {
227
0
    // We are actually a XUL <keyset>.
228
0
    if (mHandler)
229
0
      return NS_OK;
230
0
231
0
    BuildHandlerChain(el, &mHandler);
232
0
  } else { // We are an XBL file of handlers.
233
0
    EnsureSpecialDocInfo();
234
0
235
0
    // Now determine which handlers we should be using.
236
0
    if (IsHTMLEditableFieldFocused()) {
237
0
      sXBLSpecialDocInfo->GetHandlers(NS_LITERAL_CSTRING("editor"), &mHandler);
238
0
    }
239
0
    else {
240
0
      sXBLSpecialDocInfo->GetHandlers(NS_LITERAL_CSTRING("browser"), &mHandler);
241
0
    }
242
0
  }
243
0
244
0
  return NS_OK;
245
0
}
246
247
nsresult
248
nsXBLWindowKeyHandler::WalkHandlers(KeyboardEvent* aKeyEvent, nsAtom* aEventType)
249
0
{
250
0
  if (aKeyEvent->DefaultPrevented()) {
251
0
    return NS_OK;
252
0
  }
253
0
254
0
  // Don't process the event if it was not dispatched from a trusted source
255
0
  if (!aKeyEvent->IsTrusted()) {
256
0
    return NS_OK;
257
0
  }
258
0
259
0
  nsresult rv = EnsureHandlers();
260
0
  NS_ENSURE_SUCCESS(rv, rv);
261
0
262
0
  bool isDisabled;
263
0
  nsCOMPtr<Element> el = GetElement(&isDisabled);
264
0
265
0
  // skip keysets that are disabled
266
0
  if (el && isDisabled) {
267
0
    return NS_OK;
268
0
  }
269
0
270
0
  WalkHandlersInternal(aKeyEvent, aEventType, true);
271
0
272
0
  return NS_OK;
273
0
}
274
275
void
276
nsXBLWindowKeyHandler::InstallKeyboardEventListenersTo(
277
                         EventListenerManager* aEventListenerManager)
278
0
{
279
0
  // For marking each keyboard event as if it's reserved by chrome,
280
0
  // nsXBLWindowKeyHandlers need to listen each keyboard events before
281
0
  // web contents.
282
0
  aEventListenerManager->AddEventListenerByType(
283
0
                           this, NS_LITERAL_STRING("keydown"),
284
0
                           TrustedEventsAtCapture());
285
0
  aEventListenerManager->AddEventListenerByType(
286
0
                           this, NS_LITERAL_STRING("keyup"),
287
0
                           TrustedEventsAtCapture());
288
0
  aEventListenerManager->AddEventListenerByType(
289
0
                           this, NS_LITERAL_STRING("keypress"),
290
0
                           TrustedEventsAtCapture());
291
0
  aEventListenerManager->AddEventListenerByType(
292
0
                           this, NS_LITERAL_STRING("mozkeydownonplugin"),
293
0
                           TrustedEventsAtCapture());
294
0
  aEventListenerManager->AddEventListenerByType(
295
0
                           this, NS_LITERAL_STRING("mozkeyuponplugin"),
296
0
                           TrustedEventsAtCapture());
297
0
298
0
  // For reducing the IPC cost, preventing to dispatch reserved keyboard
299
0
  // events into the content process.
300
0
  aEventListenerManager->AddEventListenerByType(
301
0
                           this, NS_LITERAL_STRING("keydown"),
302
0
                           TrustedEventsAtSystemGroupCapture());
303
0
  aEventListenerManager->AddEventListenerByType(
304
0
                           this, NS_LITERAL_STRING("keyup"),
305
0
                           TrustedEventsAtSystemGroupCapture());
306
0
  aEventListenerManager->AddEventListenerByType(
307
0
                           this, NS_LITERAL_STRING("keypress"),
308
0
                           TrustedEventsAtSystemGroupCapture());
309
0
  aEventListenerManager->AddEventListenerByType(
310
0
                           this, NS_LITERAL_STRING("mozkeydownonplugin"),
311
0
                           TrustedEventsAtSystemGroupCapture());
312
0
  aEventListenerManager->AddEventListenerByType(
313
0
                           this, NS_LITERAL_STRING("mozkeyuponplugin"),
314
0
                           TrustedEventsAtSystemGroupCapture());
315
0
316
0
  // Handle keyboard events in bubbling phase of the system event group.
317
0
  aEventListenerManager->AddEventListenerByType(
318
0
                           this, NS_LITERAL_STRING("keydown"),
319
0
                           TrustedEventsAtSystemGroupBubble());
320
0
  aEventListenerManager->AddEventListenerByType(
321
0
                           this, NS_LITERAL_STRING("keyup"),
322
0
                           TrustedEventsAtSystemGroupBubble());
323
0
  aEventListenerManager->AddEventListenerByType(
324
0
                           this, NS_LITERAL_STRING("keypress"),
325
0
                           TrustedEventsAtSystemGroupBubble());
326
0
  // mozaccesskeynotfound event is fired when modifiers of keypress event
327
0
  // matches with modifier of content access key but it's not consumed by
328
0
  // remote content.
329
0
  aEventListenerManager->AddEventListenerByType(
330
0
                           this, NS_LITERAL_STRING("mozaccesskeynotfound"),
331
0
                           TrustedEventsAtSystemGroupBubble());
332
0
  aEventListenerManager->AddEventListenerByType(
333
0
                           this, NS_LITERAL_STRING("mozkeydownonplugin"),
334
0
                           TrustedEventsAtSystemGroupBubble());
335
0
  aEventListenerManager->AddEventListenerByType(
336
0
                           this, NS_LITERAL_STRING("mozkeyuponplugin"),
337
0
                           TrustedEventsAtSystemGroupBubble());
338
0
}
339
340
void
341
nsXBLWindowKeyHandler::RemoveKeyboardEventListenersFrom(
342
                         EventListenerManager* aEventListenerManager)
343
0
{
344
0
  aEventListenerManager->RemoveEventListenerByType(
345
0
                           this, NS_LITERAL_STRING("keydown"),
346
0
                           TrustedEventsAtCapture());
347
0
  aEventListenerManager->RemoveEventListenerByType(
348
0
                           this, NS_LITERAL_STRING("keyup"),
349
0
                           TrustedEventsAtCapture());
350
0
  aEventListenerManager->RemoveEventListenerByType(
351
0
                           this, NS_LITERAL_STRING("keypress"),
352
0
                           TrustedEventsAtCapture());
353
0
  aEventListenerManager->RemoveEventListenerByType(
354
0
                           this, NS_LITERAL_STRING("mozkeydownonplugin"),
355
0
                           TrustedEventsAtCapture());
356
0
  aEventListenerManager->RemoveEventListenerByType(
357
0
                           this, NS_LITERAL_STRING("mozkeyuponplugin"),
358
0
                           TrustedEventsAtCapture());
359
0
360
0
  aEventListenerManager->RemoveEventListenerByType(
361
0
                           this, NS_LITERAL_STRING("keydown"),
362
0
                           TrustedEventsAtSystemGroupCapture());
363
0
  aEventListenerManager->RemoveEventListenerByType(
364
0
                           this, NS_LITERAL_STRING("keyup"),
365
0
                           TrustedEventsAtSystemGroupCapture());
366
0
  aEventListenerManager->RemoveEventListenerByType(
367
0
                           this, NS_LITERAL_STRING("keypress"),
368
0
                           TrustedEventsAtSystemGroupCapture());
369
0
  aEventListenerManager->RemoveEventListenerByType(
370
0
                           this, NS_LITERAL_STRING("mozkeydownonplugin"),
371
0
                           TrustedEventsAtSystemGroupCapture());
372
0
  aEventListenerManager->RemoveEventListenerByType(
373
0
                           this, NS_LITERAL_STRING("mozkeyuponplugin"),
374
0
                           TrustedEventsAtSystemGroupCapture());
375
0
376
0
  aEventListenerManager->RemoveEventListenerByType(
377
0
                           this, NS_LITERAL_STRING("keydown"),
378
0
                           TrustedEventsAtSystemGroupBubble());
379
0
  aEventListenerManager->RemoveEventListenerByType(
380
0
                           this, NS_LITERAL_STRING("keyup"),
381
0
                           TrustedEventsAtSystemGroupBubble());
382
0
  aEventListenerManager->RemoveEventListenerByType(
383
0
                           this, NS_LITERAL_STRING("keypress"),
384
0
                           TrustedEventsAtSystemGroupBubble());
385
0
  aEventListenerManager->RemoveEventListenerByType(
386
0
                           this, NS_LITERAL_STRING("mozaccesskeynotfound"),
387
0
                           TrustedEventsAtSystemGroupBubble());
388
0
  aEventListenerManager->RemoveEventListenerByType(
389
0
                           this, NS_LITERAL_STRING("mozkeydownonplugin"),
390
0
                           TrustedEventsAtSystemGroupBubble());
391
0
  aEventListenerManager->RemoveEventListenerByType(
392
0
                           this, NS_LITERAL_STRING("mozkeyuponplugin"),
393
0
                           TrustedEventsAtSystemGroupBubble());
394
0
}
395
396
/* static */ KeyboardMap
397
nsXBLWindowKeyHandler::CollectKeyboardShortcuts()
398
0
{
399
0
  // Load the XBL handlers
400
0
  EnsureSpecialDocInfo();
401
0
402
0
  nsXBLPrototypeHandler* handlers = nullptr;
403
0
  sXBLSpecialDocInfo->GetHandlers(NS_LITERAL_CSTRING("browser"), &handlers);
404
0
405
0
  // Convert the handlers into keyboard shortcuts, using an AutoTArray with
406
0
  // the maximum amount of shortcuts used on any platform to minimize allocations
407
0
  AutoTArray<KeyboardShortcut, 48> shortcuts;
408
0
409
0
  // Append keyboard shortcuts for hardcoded actions like tab
410
0
  KeyboardShortcut::AppendHardcodedShortcuts(shortcuts);
411
0
412
0
  for (nsXBLPrototypeHandler* handler = handlers;
413
0
       handler;
414
0
       handler = handler->GetNextHandler()) {
415
0
    KeyboardShortcut shortcut;
416
0
    if (handler->TryConvertToKeyboardShortcut(&shortcut)) {
417
0
      shortcuts.AppendElement(shortcut);
418
0
    }
419
0
  }
420
0
421
0
  return KeyboardMap(std::move(shortcuts));
422
0
}
423
424
nsAtom*
425
nsXBLWindowKeyHandler::ConvertEventToDOMEventType(
426
                         const WidgetKeyboardEvent& aWidgetKeyboardEvent) const
427
0
{
428
0
  if (aWidgetKeyboardEvent.IsKeyDownOrKeyDownOnPlugin()) {
429
0
    return nsGkAtoms::keydown;
430
0
  }
431
0
  if (aWidgetKeyboardEvent.IsKeyUpOrKeyUpOnPlugin()) {
432
0
    return nsGkAtoms::keyup;
433
0
  }
434
0
  // eAccessKeyNotFound event is always created from eKeyPress event and
435
0
  // the original eKeyPress event has stopped its propagation before dispatched
436
0
  // into the DOM tree in this process and not matched with remote content's
437
0
  // access keys.  So, we should treat it as an eKeyPress event and execute
438
0
  // a command if it's registered as a shortcut key.
439
0
  if (aWidgetKeyboardEvent.mMessage == eKeyPress ||
440
0
      aWidgetKeyboardEvent.mMessage == eAccessKeyNotFound) {
441
0
    return nsGkAtoms::keypress;
442
0
  }
443
0
  MOZ_ASSERT_UNREACHABLE(
444
0
    "All event messages which this instance listens to should be handled");
445
0
  return nullptr;
446
0
}
447
448
NS_IMETHODIMP
449
nsXBLWindowKeyHandler::HandleEvent(Event* aEvent)
450
0
{
451
0
  RefPtr<KeyboardEvent> keyEvent = aEvent->AsKeyboardEvent();
452
0
  NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG);
453
0
454
0
  if (aEvent->EventPhase() == Event_Binding::CAPTURING_PHASE) {
455
0
    if (aEvent->WidgetEventPtr()->mFlags.mInSystemGroup) {
456
0
      HandleEventOnCaptureInSystemEventGroup(keyEvent);
457
0
    } else {
458
0
      HandleEventOnCaptureInDefaultEventGroup(keyEvent);
459
0
    }
460
0
    return NS_OK;
461
0
  }
462
0
463
0
  WidgetKeyboardEvent* widgetKeyboardEvent =
464
0
    aEvent->WidgetEventPtr()->AsKeyboardEvent();
465
0
  if (widgetKeyboardEvent->IsKeyEventOnPlugin()) {
466
0
    // key events on plugin shouldn't execute shortcut key handlers which are
467
0
    // not reserved.
468
0
    if (!widgetKeyboardEvent->IsReservedByChrome()) {
469
0
      return NS_OK;
470
0
    }
471
0
472
0
    // If the event is untrusted event or was already consumed, do nothing.
473
0
    if (!widgetKeyboardEvent->IsTrusted() ||
474
0
        widgetKeyboardEvent->DefaultPrevented()) {
475
0
      return NS_OK;
476
0
    }
477
0
478
0
    // XXX Don't check isReserved here because even if the handler in this
479
0
    //     instance isn't reserved but another instance reserves the key
480
0
    //     combination, it will be executed when the event is normal keyboard
481
0
    //     events...
482
0
    bool isReserved = false;
483
0
    if (!HasHandlerForEvent(keyEvent, &isReserved)) {
484
0
      return NS_OK;
485
0
    }
486
0
  }
487
0
488
0
  // If this event was handled by APZ then don't do the default action, and
489
0
  // preventDefault to prevent any other listeners from handling the event.
490
0
  if (widgetKeyboardEvent->mFlags.mHandledByAPZ) {
491
0
    aEvent->PreventDefault();
492
0
    return NS_OK;
493
0
  }
494
0
495
0
  RefPtr<nsAtom> eventTypeAtom =
496
0
    ConvertEventToDOMEventType(*widgetKeyboardEvent);
497
0
  return WalkHandlers(keyEvent, eventTypeAtom);
498
0
}
499
500
void
501
nsXBLWindowKeyHandler::HandleEventOnCaptureInDefaultEventGroup(
502
                         KeyboardEvent* aEvent)
503
0
{
504
0
  WidgetKeyboardEvent* widgetKeyboardEvent =
505
0
    aEvent->WidgetEventPtr()->AsKeyboardEvent();
506
0
507
0
  if (widgetKeyboardEvent->IsReservedByChrome()) {
508
0
    return;
509
0
  }
510
0
511
0
  bool isReserved = false;
512
0
  if (HasHandlerForEvent(aEvent, &isReserved) && isReserved) {
513
0
    widgetKeyboardEvent->MarkAsReservedByChrome();
514
0
  }
515
0
}
516
517
void
518
nsXBLWindowKeyHandler::HandleEventOnCaptureInSystemEventGroup(
519
                         KeyboardEvent* aEvent)
520
0
{
521
0
  WidgetKeyboardEvent* widgetEvent =
522
0
    aEvent->WidgetEventPtr()->AsKeyboardEvent();
523
0
524
0
  // If the event won't be sent to remote process, this listener needs to do
525
0
  // nothing.  Note that even if mOnlySystemGroupDispatchInContent is true,
526
0
  // we need to send the event to remote process and check reply event
527
0
  // before matching it with registered shortcut keys because event listeners
528
0
  // in the system event group may want to handle the event before registered
529
0
  // shortcut key handlers.
530
0
  if (!widgetEvent->WillBeSentToRemoteProcess()) {
531
0
    return;
532
0
  }
533
0
534
0
  if (!HasHandlerForEvent(aEvent)) {
535
0
    return;
536
0
  }
537
0
538
0
  // If this event wasn't marked as IsCrossProcessForwardingStopped,
539
0
  // yet, it means it wasn't processed by content. We'll not call any
540
0
  // of the handlers at this moment, and will wait the reply event.
541
0
  // So, stop immediate propagation in this event first, then, mark it as
542
0
  // waiting reply from remote process.  Finally, when this process receives
543
0
  // a reply from the remote process, it should be dispatched into this
544
0
  // DOM tree again.
545
0
  widgetEvent->StopImmediatePropagation();
546
0
  widgetEvent->MarkAsWaitingReplyFromRemoteProcess();
547
0
}
548
549
bool
550
nsXBLWindowKeyHandler::IsHTMLEditableFieldFocused()
551
0
{
552
0
  nsFocusManager* fm = nsFocusManager::GetFocusManager();
553
0
  if (!fm)
554
0
    return false;
555
0
556
0
  nsCOMPtr<mozIDOMWindowProxy> focusedWindow;
557
0
  fm->GetFocusedWindow(getter_AddRefs(focusedWindow));
558
0
  if (!focusedWindow)
559
0
    return false;
560
0
561
0
  auto* piwin = nsPIDOMWindowOuter::From(focusedWindow);
562
0
  nsIDocShell *docShell = piwin->GetDocShell();
563
0
  if (!docShell) {
564
0
    return false;
565
0
  }
566
0
567
0
  RefPtr<HTMLEditor> htmlEditor = docShell->GetHTMLEditor();
568
0
  if (!htmlEditor) {
569
0
    return false;
570
0
  }
571
0
572
0
  nsCOMPtr<nsIDocument> doc = htmlEditor->GetDocument();
573
0
  if (doc->HasFlag(NODE_IS_EDITABLE)) {
574
0
    // Don't need to perform any checks in designMode documents.
575
0
    return true;
576
0
  }
577
0
578
0
  nsINode* focusedNode = fm->GetFocusedElement();
579
0
  if (focusedNode && focusedNode->IsElement()) {
580
0
    // If there is a focused element, make sure it's in the active editing host.
581
0
    // Note that GetActiveEditingHost finds the current editing host based on
582
0
    // the document's selection.  Even though the document selection is usually
583
0
    // collapsed to where the focus is, but the page may modify the selection
584
0
    // without our knowledge, in which case this check will do something useful.
585
0
    nsCOMPtr<Element> activeEditingHost = htmlEditor->GetActiveEditingHost();
586
0
    if (!activeEditingHost) {
587
0
      return false;
588
0
    }
589
0
    return nsContentUtils::ContentIsDescendantOf(focusedNode, activeEditingHost);
590
0
  }
591
0
592
0
  return false;
593
0
}
594
595
//
596
// WalkHandlersInternal and WalkHandlersAndExecute
597
//
598
// Given a particular DOM event and a pointer to the first handler in the list,
599
// scan through the list to find something to handle the event. If aExecute = true,
600
// the handler will be executed; otherwise just return an answer telling if a handler
601
// for that event was found.
602
//
603
bool
604
nsXBLWindowKeyHandler::WalkHandlersInternal(KeyboardEvent* aKeyEvent,
605
                                            nsAtom* aEventType,
606
                                            bool aExecute,
607
                                            bool* aOutReservedForChrome)
608
0
{
609
0
  WidgetKeyboardEvent* nativeKeyboardEvent =
610
0
    aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
611
0
  MOZ_ASSERT(nativeKeyboardEvent);
612
0
613
0
  AutoShortcutKeyCandidateArray shortcutKeys;
614
0
  nativeKeyboardEvent->GetShortcutKeyCandidates(shortcutKeys);
615
0
616
0
  if (shortcutKeys.IsEmpty()) {
617
0
    return WalkHandlersAndExecute(aKeyEvent, aEventType,
618
0
                                  0, IgnoreModifierState(),
619
0
                                  aExecute, aOutReservedForChrome);
620
0
  }
621
0
622
0
  for (uint32_t i = 0; i < shortcutKeys.Length(); ++i) {
623
0
    ShortcutKeyCandidate& key = shortcutKeys[i];
624
0
    IgnoreModifierState ignoreModifierState;
625
0
    ignoreModifierState.mShift = key.mIgnoreShift;
626
0
    if (WalkHandlersAndExecute(aKeyEvent, aEventType,
627
0
                               key.mCharCode, ignoreModifierState,
628
0
                               aExecute, aOutReservedForChrome)) {
629
0
      return true;
630
0
    }
631
0
  }
632
0
  return false;
633
0
}
634
635
bool
636
nsXBLWindowKeyHandler::WalkHandlersAndExecute(
637
                         KeyboardEvent* aKeyEvent,
638
                         nsAtom* aEventType,
639
                         uint32_t aCharCode,
640
                         const IgnoreModifierState& aIgnoreModifierState,
641
                         bool aExecute,
642
                         bool* aOutReservedForChrome)
643
0
{
644
0
  if (aOutReservedForChrome) {
645
0
    *aOutReservedForChrome = false;
646
0
  }
647
0
648
0
  WidgetKeyboardEvent* widgetKeyboardEvent =
649
0
    aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
650
0
  if (NS_WARN_IF(!widgetKeyboardEvent)) {
651
0
    return false;
652
0
  }
653
0
654
0
  // Try all of the handlers until we find one that matches the event.
655
0
  for (nsXBLPrototypeHandler* handler = mHandler;
656
0
       handler;
657
0
       handler = handler->GetNextHandler()) {
658
0
    bool stopped = aKeyEvent->IsDispatchStopped();
659
0
    if (stopped) {
660
0
      // The event is finished, don't execute any more handlers
661
0
      return false;
662
0
    }
663
0
664
0
    if (aExecute) {
665
0
      // If the event is eKeyDownOnPlugin, it should execute either keydown
666
0
      // handler or keypress handler because eKeyDownOnPlugin events are
667
0
      // never followed by keypress events.
668
0
      if (widgetKeyboardEvent->mMessage == eKeyDownOnPlugin) {
669
0
        if (!handler->EventTypeEquals(nsGkAtoms::keydown) &&
670
0
            !handler->EventTypeEquals(nsGkAtoms::keypress)) {
671
0
          continue;
672
0
        }
673
0
      // The other event types should exactly be matched with the handler's
674
0
      // event type.
675
0
      } else if (!handler->EventTypeEquals(aEventType)) {
676
0
        continue;
677
0
      }
678
0
    } else {
679
0
      if (handler->EventTypeEquals(nsGkAtoms::keypress)) {
680
0
        // If the handler is a keypress event handler, we also need to check
681
0
        // if coming keydown event is a preceding event of reserved key
682
0
        // combination because if default action of a keydown event is
683
0
        // prevented, following keypress event won't be fired.  However, if
684
0
        // following keypress event is reserved, we shouldn't allow web
685
0
        // contents to prevent the default of the preceding keydown event.
686
0
        if (aEventType != nsGkAtoms::keydown &&
687
0
            aEventType != nsGkAtoms::keypress) {
688
0
          continue;
689
0
        }
690
0
      } else if (!handler->EventTypeEquals(aEventType)) {
691
0
        // Otherwise, aEventType should exactly be matched.
692
0
        continue;
693
0
      }
694
0
    }
695
0
696
0
    // Check if the keyboard event *may* execute the handler.
697
0
    if (!handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreModifierState)) {
698
0
      continue;  // try the next one
699
0
    }
700
0
701
0
    // Before executing this handler, check that it's not disabled,
702
0
    // and that it has something to do (oncommand of the <key> or its
703
0
    // <command> is non-empty).
704
0
    nsCOMPtr<Element> commandElement;
705
0
    if (!GetElementForHandler(handler, getter_AddRefs(commandElement))) {
706
0
      continue;
707
0
    }
708
0
709
0
    if (commandElement) {
710
0
      if (aExecute && !IsExecutableElement(commandElement)) {
711
0
        continue;
712
0
      }
713
0
    }
714
0
715
0
    if (!aExecute) {
716
0
      if (handler->EventTypeEquals(aEventType)) {
717
0
        if (aOutReservedForChrome) {
718
0
          *aOutReservedForChrome = IsReservedKey(widgetKeyboardEvent, handler);
719
0
        }
720
0
721
0
        return true;
722
0
      }
723
0
724
0
      // If the command is reserved and the event is keydown, check also if
725
0
      // the handler is for keypress because if following keypress event is
726
0
      // reserved, we shouldn't dispatch the event into web contents.
727
0
      if (aEventType == nsGkAtoms::keydown &&
728
0
          handler->EventTypeEquals(nsGkAtoms::keypress)) {
729
0
        if (IsReservedKey(widgetKeyboardEvent, handler)) {
730
0
          if (aOutReservedForChrome) {
731
0
            *aOutReservedForChrome = true;
732
0
          }
733
0
734
0
          return true;
735
0
        }
736
0
      }
737
0
      // Otherwise, we've not found a handler for the event yet.
738
0
      continue;
739
0
    }
740
0
741
0
    // This should only be assigned when aExecute is false.
742
0
    MOZ_ASSERT(!aOutReservedForChrome);
743
0
744
0
    // If it's not reserved and the event is a key event on a plugin,
745
0
    // the handler shouldn't be executed.
746
0
    if (widgetKeyboardEvent->IsKeyEventOnPlugin() &&
747
0
        !IsReservedKey(widgetKeyboardEvent, handler)) {
748
0
      return false;
749
0
    }
750
0
751
0
    nsCOMPtr<EventTarget> target;
752
0
    nsCOMPtr<Element> chromeHandlerElement = GetElement();
753
0
    if (chromeHandlerElement) {
754
0
      // XXX commandElement may be nullptr...
755
0
      target = commandElement;
756
0
    } else {
757
0
      target = mTarget;
758
0
    }
759
0
760
0
    // XXX Do we execute only one handler even if the handler neither stops
761
0
    //     propagation nor prevents default of the event?
762
0
    nsresult rv = handler->ExecuteHandler(target, aKeyEvent);
763
0
    if (NS_SUCCEEDED(rv)) {
764
0
      return true;
765
0
    }
766
0
  }
767
0
768
#ifdef XP_WIN
769
  // Windows native applications ignore Windows-Logo key state when checking
770
  // shortcut keys even if the key is pressed.  Therefore, if there is no
771
  // shortcut key which exactly matches current modifier state, we should
772
  // retry to look for a shortcut key without the Windows-Logo key press.
773
  if (!aIgnoreModifierState.mOS && widgetKeyboardEvent->IsOS()) {
774
    IgnoreModifierState ignoreModifierState(aIgnoreModifierState);
775
    ignoreModifierState.mOS = true;
776
    return WalkHandlersAndExecute(aKeyEvent, aEventType,
777
                                  aCharCode, ignoreModifierState, aExecute);
778
  }
779
#endif
780
781
0
  return false;
782
0
}
783
784
bool
785
nsXBLWindowKeyHandler::IsReservedKey(WidgetKeyboardEvent* aKeyEvent,
786
                                     nsXBLPrototypeHandler* aHandler)
787
0
{
788
0
  XBLReservedKey reserved = aHandler->GetIsReserved();
789
0
  // reserved="true" means that the key is always reserved. reserved="false"
790
0
  // means that the key is never reserved. Otherwise, we check site-specific
791
0
  // permissions.
792
0
  if (reserved == XBLReservedKey_False) {
793
0
    return false;
794
0
  }
795
0
796
0
  if (reserved == XBLReservedKey_True) {
797
0
    return true;
798
0
  }
799
0
800
0
  return nsContentUtils::ShouldBlockReservedKeys(aKeyEvent);
801
0
}
802
803
bool
804
nsXBLWindowKeyHandler::HasHandlerForEvent(KeyboardEvent* aEvent,
805
                                          bool* aOutReservedForChrome)
806
0
{
807
0
  WidgetKeyboardEvent* widgetKeyboardEvent =
808
0
    aEvent->WidgetEventPtr()->AsKeyboardEvent();
809
0
  if (NS_WARN_IF(!widgetKeyboardEvent) || !widgetKeyboardEvent->IsTrusted()) {
810
0
    return false;
811
0
  }
812
0
813
0
  nsresult rv = EnsureHandlers();
814
0
  NS_ENSURE_SUCCESS(rv, false);
815
0
816
0
  bool isDisabled;
817
0
  nsCOMPtr<Element> el = GetElement(&isDisabled);
818
0
  if (el && isDisabled) {
819
0
    return false;
820
0
  }
821
0
822
0
  RefPtr<nsAtom> eventTypeAtom =
823
0
    ConvertEventToDOMEventType(*widgetKeyboardEvent);
824
0
  return WalkHandlersInternal(aEvent, eventTypeAtom, false,
825
0
                              aOutReservedForChrome);
826
0
}
827
828
already_AddRefed<Element>
829
nsXBLWindowKeyHandler::GetElement(bool* aIsDisabled)
830
0
{
831
0
  nsCOMPtr<Element> element = do_QueryReferent(mWeakPtrForElement);
832
0
  if (element && aIsDisabled) {
833
0
    *aIsDisabled = element->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
834
0
                                        nsGkAtoms::_true, eCaseMatters);
835
0
  }
836
0
  return element.forget();
837
0
}
838
839
bool
840
nsXBLWindowKeyHandler::GetElementForHandler(nsXBLPrototypeHandler* aHandler,
841
                                            Element** aElementForHandler)
842
0
{
843
0
  MOZ_ASSERT(aElementForHandler);
844
0
  *aElementForHandler = nullptr;
845
0
846
0
  RefPtr<Element> keyElement = aHandler->GetHandlerElement();
847
0
  if (!keyElement) {
848
0
    return true; // XXX Even though no key element?
849
0
  }
850
0
851
0
  nsCOMPtr<Element> chromeHandlerElement = GetElement();
852
0
  if (!chromeHandlerElement) {
853
0
    NS_WARNING_ASSERTION(keyElement->IsInUncomposedDoc(), "uncomposed");
854
0
    keyElement.swap(*aElementForHandler);
855
0
    return true;
856
0
  }
857
0
858
0
  // We are in a XUL doc.  Obtain our command attribute.
859
0
  nsAutoString command;
860
0
  keyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::command, command);
861
0
  if (command.IsEmpty()) {
862
0
    // There is no command element associated with the key element.
863
0
    NS_WARNING_ASSERTION(keyElement->IsInUncomposedDoc(), "uncomposed");
864
0
    keyElement.swap(*aElementForHandler);
865
0
    return true;
866
0
  }
867
0
868
0
  // XXX Shouldn't we check this earlier?
869
0
  nsIDocument* doc = keyElement->GetUncomposedDoc();
870
0
  if (NS_WARN_IF(!doc)) {
871
0
    return false;
872
0
  }
873
0
874
0
  nsCOMPtr<Element> commandElement = doc->GetElementById(command);
875
0
  if (!commandElement) {
876
0
    NS_ERROR("A XUL <key> is observing a command that doesn't exist. "
877
0
             "Unable to execute key binding!");
878
0
    return false;
879
0
  }
880
0
881
0
  commandElement.swap(*aElementForHandler);
882
0
  return true;
883
0
}
884
885
bool
886
nsXBLWindowKeyHandler::IsExecutableElement(Element* aElement) const
887
0
{
888
0
  if (!aElement) {
889
0
    return false;
890
0
  }
891
0
892
0
  nsAutoString value;
893
0
  aElement->GetAttribute(NS_LITERAL_STRING("disabled"), value);
894
0
  if (value.EqualsLiteral("true")) {
895
0
    return false;
896
0
  }
897
0
898
0
  aElement->GetAttribute(NS_LITERAL_STRING("oncommand"), value);
899
0
  if (value.IsEmpty()) {
900
0
    return false;
901
0
  }
902
0
903
0
  return true;
904
0
}
905
906
///////////////////////////////////////////////////////////////////////////////////
907
908
already_AddRefed<nsXBLWindowKeyHandler>
909
NS_NewXBLWindowKeyHandler(Element* aElement, EventTarget* aTarget)
910
0
{
911
0
  RefPtr<nsXBLWindowKeyHandler> result =
912
0
    new nsXBLWindowKeyHandler(aElement, aTarget);
913
0
  return result.forget();
914
0
}