Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/xbl/nsXBLEventHandler.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 "nsAtom.h"
9
#include "nsIDOMEventListener.h"
10
#include "nsXBLPrototypeHandler.h"
11
#include "nsContentUtils.h"
12
#include "mozilla/dom/Event.h" // for Event
13
#include "mozilla/dom/EventBinding.h" // for Event
14
#include "mozilla/dom/EventTarget.h"
15
#include "mozilla/dom/KeyboardEvent.h"
16
#include "mozilla/TextEvents.h"
17
18
using namespace mozilla;
19
using namespace mozilla::dom;
20
21
nsXBLEventHandler::nsXBLEventHandler(nsXBLPrototypeHandler* aHandler)
22
  : mProtoHandler(aHandler)
23
0
{
24
0
}
25
26
nsXBLEventHandler::~nsXBLEventHandler()
27
0
{
28
0
}
29
30
NS_IMPL_ISUPPORTS(nsXBLEventHandler, nsIDOMEventListener)
31
32
NS_IMETHODIMP
33
nsXBLEventHandler::HandleEvent(Event* aEvent)
34
0
{
35
0
  if (!mProtoHandler)
36
0
    return NS_ERROR_FAILURE;
37
0
38
0
  uint8_t phase = mProtoHandler->GetPhase();
39
0
  if (phase == NS_PHASE_TARGET) {
40
0
    if (aEvent->EventPhase() != Event_Binding::AT_TARGET) {
41
0
      return NS_OK;
42
0
    }
43
0
  }
44
0
45
0
  if (!EventMatched(aEvent))
46
0
    return NS_OK;
47
0
48
0
  mProtoHandler->ExecuteHandler(aEvent->GetCurrentTarget(), aEvent);
49
0
50
0
  return NS_OK;
51
0
}
52
53
nsXBLMouseEventHandler::nsXBLMouseEventHandler(nsXBLPrototypeHandler* aHandler)
54
  : nsXBLEventHandler(aHandler)
55
0
{
56
0
}
57
58
nsXBLMouseEventHandler::~nsXBLMouseEventHandler()
59
{
60
}
61
62
bool
63
nsXBLMouseEventHandler::EventMatched(Event* aEvent)
64
0
{
65
0
  MouseEvent* mouse = aEvent->AsMouseEvent();
66
0
  return mouse && mProtoHandler->MouseEventMatched(mouse);
67
0
}
68
69
nsXBLKeyEventHandler::nsXBLKeyEventHandler(nsAtom* aEventType, uint8_t aPhase,
70
                                           uint8_t aType)
71
  : mEventType(aEventType),
72
    mPhase(aPhase),
73
    mType(aType),
74
    mIsBoundToChrome(false),
75
    mUsingContentXBLScope(false)
76
0
{
77
0
}
78
79
nsXBLKeyEventHandler::~nsXBLKeyEventHandler()
80
0
{
81
0
}
82
83
NS_IMPL_ISUPPORTS(nsXBLKeyEventHandler, nsIDOMEventListener)
84
85
bool
86
nsXBLKeyEventHandler::ExecuteMatchedHandlers(
87
                        KeyboardEvent* aKeyEvent,
88
                        uint32_t aCharCode,
89
                        const IgnoreModifierState& aIgnoreModifierState)
90
0
{
91
0
  WidgetEvent* event = aKeyEvent->WidgetEventPtr();
92
0
  nsCOMPtr<EventTarget> target = aKeyEvent->GetCurrentTarget();
93
0
94
0
  bool executed = false;
95
0
  for (uint32_t i = 0; i < mProtoHandlers.Length(); ++i) {
96
0
    nsXBLPrototypeHandler* handler = mProtoHandlers[i];
97
0
    bool hasAllowUntrustedAttr = handler->HasAllowUntrustedAttr();
98
0
    if ((event->IsTrusted() ||
99
0
        (hasAllowUntrustedAttr && handler->AllowUntrustedEvents()) ||
100
0
        (!hasAllowUntrustedAttr && !mIsBoundToChrome && !mUsingContentXBLScope)) &&
101
0
        handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreModifierState)) {
102
0
      handler->ExecuteHandler(target, aKeyEvent);
103
0
      executed = true;
104
0
    }
105
0
  }
106
#ifdef XP_WIN
107
  // Windows native applications ignore Windows-Logo key state when checking
108
  // shortcut keys even if the key is pressed.  Therefore, if there is no
109
  // shortcut key which exactly matches current modifier state, we should
110
  // retry to look for a shortcut key without the Windows-Logo key press.
111
  if (!executed && !aIgnoreModifierState.mOS) {
112
    WidgetKeyboardEvent* keyEvent = event->AsKeyboardEvent();
113
    if (keyEvent && keyEvent->IsOS()) {
114
      IgnoreModifierState ignoreModifierState(aIgnoreModifierState);
115
      ignoreModifierState.mOS = true;
116
      return ExecuteMatchedHandlers(aKeyEvent, aCharCode, ignoreModifierState);
117
    }
118
  }
119
#endif
120
  return executed;
121
0
}
122
123
NS_IMETHODIMP
124
nsXBLKeyEventHandler::HandleEvent(Event* aEvent)
125
0
{
126
0
  uint32_t count = mProtoHandlers.Length();
127
0
  if (count == 0)
128
0
    return NS_ERROR_FAILURE;
129
0
130
0
  if (mPhase == NS_PHASE_TARGET) {
131
0
    if (aEvent->EventPhase() != Event_Binding::AT_TARGET) {
132
0
      return NS_OK;
133
0
    }
134
0
  }
135
0
136
0
  RefPtr<KeyboardEvent> key = aEvent->AsKeyboardEvent();
137
0
  if (!key) {
138
0
    return NS_OK;
139
0
  }
140
0
141
0
  WidgetKeyboardEvent* nativeKeyboardEvent =
142
0
    aEvent->WidgetEventPtr()->AsKeyboardEvent();
143
0
  MOZ_ASSERT(nativeKeyboardEvent);
144
0
  AutoShortcutKeyCandidateArray shortcutKeys;
145
0
  nativeKeyboardEvent->GetShortcutKeyCandidates(shortcutKeys);
146
0
147
0
  if (shortcutKeys.IsEmpty()) {
148
0
    ExecuteMatchedHandlers(key, 0, IgnoreModifierState());
149
0
    return NS_OK;
150
0
  }
151
0
152
0
  for (uint32_t i = 0; i < shortcutKeys.Length(); ++i) {
153
0
    IgnoreModifierState ignoreModifierState;
154
0
    ignoreModifierState.mShift = shortcutKeys[i].mIgnoreShift;
155
0
    if (ExecuteMatchedHandlers(key, shortcutKeys[i].mCharCode,
156
0
                               ignoreModifierState)) {
157
0
      return NS_OK;
158
0
    }
159
0
  }
160
0
  return NS_OK;
161
0
}
162
163
///////////////////////////////////////////////////////////////////////////////////
164
165
already_AddRefed<nsXBLEventHandler>
166
NS_NewXBLEventHandler(nsXBLPrototypeHandler* aHandler,
167
                      nsAtom* aEventType)
168
0
{
169
0
  RefPtr<nsXBLEventHandler> handler;
170
0
171
0
  switch (nsContentUtils::GetEventClassID(nsDependentAtomString(aEventType))) {
172
0
    case eDragEventClass:
173
0
    case eMouseEventClass:
174
0
    case eMouseScrollEventClass:
175
0
    case eWheelEventClass:
176
0
    case eSimpleGestureEventClass:
177
0
      handler = new nsXBLMouseEventHandler(aHandler);
178
0
      break;
179
0
    default:
180
0
      handler = new nsXBLEventHandler(aHandler);
181
0
      break;
182
0
  }
183
0
184
0
  return handler.forget();
185
0
}