|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsCOMPtr.h" |
|
7 #include "nsIAtom.h" |
|
8 #include "nsIDOMEventListener.h" |
|
9 #include "nsIDOMKeyEvent.h" |
|
10 #include "nsIDOMMouseEvent.h" |
|
11 #include "nsXBLPrototypeHandler.h" |
|
12 #include "nsContentUtils.h" |
|
13 #include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent() |
|
14 #include "mozilla/dom/EventTarget.h" |
|
15 |
|
16 using namespace mozilla::dom; |
|
17 |
|
18 nsXBLEventHandler::nsXBLEventHandler(nsXBLPrototypeHandler* aHandler) |
|
19 : mProtoHandler(aHandler) |
|
20 { |
|
21 } |
|
22 |
|
23 nsXBLEventHandler::~nsXBLEventHandler() |
|
24 { |
|
25 } |
|
26 |
|
27 NS_IMPL_ISUPPORTS(nsXBLEventHandler, nsIDOMEventListener) |
|
28 |
|
29 NS_IMETHODIMP |
|
30 nsXBLEventHandler::HandleEvent(nsIDOMEvent* aEvent) |
|
31 { |
|
32 if (!mProtoHandler) |
|
33 return NS_ERROR_FAILURE; |
|
34 |
|
35 uint8_t phase = mProtoHandler->GetPhase(); |
|
36 if (phase == NS_PHASE_TARGET) { |
|
37 uint16_t eventPhase; |
|
38 aEvent->GetEventPhase(&eventPhase); |
|
39 if (eventPhase != nsIDOMEvent::AT_TARGET) |
|
40 return NS_OK; |
|
41 } |
|
42 |
|
43 if (!EventMatched(aEvent)) |
|
44 return NS_OK; |
|
45 |
|
46 mProtoHandler->ExecuteHandler(aEvent->InternalDOMEvent()->GetCurrentTarget(), |
|
47 aEvent); |
|
48 |
|
49 return NS_OK; |
|
50 } |
|
51 |
|
52 nsXBLMouseEventHandler::nsXBLMouseEventHandler(nsXBLPrototypeHandler* aHandler) |
|
53 : nsXBLEventHandler(aHandler) |
|
54 { |
|
55 } |
|
56 |
|
57 nsXBLMouseEventHandler::~nsXBLMouseEventHandler() |
|
58 { |
|
59 } |
|
60 |
|
61 bool |
|
62 nsXBLMouseEventHandler::EventMatched(nsIDOMEvent* aEvent) |
|
63 { |
|
64 nsCOMPtr<nsIDOMMouseEvent> mouse(do_QueryInterface(aEvent)); |
|
65 return mouse && mProtoHandler->MouseEventMatched(mouse); |
|
66 } |
|
67 |
|
68 nsXBLKeyEventHandler::nsXBLKeyEventHandler(nsIAtom* aEventType, uint8_t aPhase, |
|
69 uint8_t aType) |
|
70 : mEventType(aEventType), |
|
71 mPhase(aPhase), |
|
72 mType(aType), |
|
73 mIsBoundToChrome(false), |
|
74 mUsingXBLScope(false) |
|
75 { |
|
76 } |
|
77 |
|
78 nsXBLKeyEventHandler::~nsXBLKeyEventHandler() |
|
79 { |
|
80 } |
|
81 |
|
82 NS_IMPL_ISUPPORTS(nsXBLKeyEventHandler, nsIDOMEventListener) |
|
83 |
|
84 bool |
|
85 nsXBLKeyEventHandler::ExecuteMatchedHandlers(nsIDOMKeyEvent* aKeyEvent, |
|
86 uint32_t aCharCode, |
|
87 bool aIgnoreShiftKey) |
|
88 { |
|
89 bool trustedEvent = false; |
|
90 aKeyEvent->GetIsTrusted(&trustedEvent); |
|
91 |
|
92 nsCOMPtr<EventTarget> target = aKeyEvent->InternalDOMEvent()->GetCurrentTarget(); |
|
93 |
|
94 bool executed = false; |
|
95 for (uint32_t i = 0; i < mProtoHandlers.Length(); ++i) { |
|
96 nsXBLPrototypeHandler* handler = mProtoHandlers[i]; |
|
97 bool hasAllowUntrustedAttr = handler->HasAllowUntrustedAttr(); |
|
98 if ((trustedEvent || |
|
99 (hasAllowUntrustedAttr && handler->AllowUntrustedEvents()) || |
|
100 (!hasAllowUntrustedAttr && !mIsBoundToChrome && !mUsingXBLScope)) && |
|
101 handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreShiftKey)) { |
|
102 handler->ExecuteHandler(target, aKeyEvent); |
|
103 executed = true; |
|
104 } |
|
105 } |
|
106 return executed; |
|
107 } |
|
108 |
|
109 NS_IMETHODIMP |
|
110 nsXBLKeyEventHandler::HandleEvent(nsIDOMEvent* aEvent) |
|
111 { |
|
112 uint32_t count = mProtoHandlers.Length(); |
|
113 if (count == 0) |
|
114 return NS_ERROR_FAILURE; |
|
115 |
|
116 if (mPhase == NS_PHASE_TARGET) { |
|
117 uint16_t eventPhase; |
|
118 aEvent->GetEventPhase(&eventPhase); |
|
119 if (eventPhase != nsIDOMEvent::AT_TARGET) |
|
120 return NS_OK; |
|
121 } |
|
122 |
|
123 nsCOMPtr<nsIDOMKeyEvent> key(do_QueryInterface(aEvent)); |
|
124 if (!key) |
|
125 return NS_OK; |
|
126 |
|
127 nsAutoTArray<nsShortcutCandidate, 10> accessKeys; |
|
128 nsContentUtils::GetAccelKeyCandidates(key, accessKeys); |
|
129 |
|
130 if (accessKeys.IsEmpty()) { |
|
131 ExecuteMatchedHandlers(key, 0, false); |
|
132 return NS_OK; |
|
133 } |
|
134 |
|
135 for (uint32_t i = 0; i < accessKeys.Length(); ++i) { |
|
136 if (ExecuteMatchedHandlers(key, accessKeys[i].mCharCode, |
|
137 accessKeys[i].mIgnoreShift)) |
|
138 return NS_OK; |
|
139 } |
|
140 return NS_OK; |
|
141 } |
|
142 |
|
143 /////////////////////////////////////////////////////////////////////////////////// |
|
144 |
|
145 nsresult |
|
146 NS_NewXBLEventHandler(nsXBLPrototypeHandler* aHandler, |
|
147 nsIAtom* aEventType, |
|
148 nsXBLEventHandler** aResult) |
|
149 { |
|
150 switch (nsContentUtils::GetEventCategory(nsDependentAtomString(aEventType))) { |
|
151 case NS_DRAG_EVENT: |
|
152 case NS_MOUSE_EVENT: |
|
153 case NS_MOUSE_SCROLL_EVENT: |
|
154 case NS_WHEEL_EVENT: |
|
155 case NS_SIMPLE_GESTURE_EVENT: |
|
156 *aResult = new nsXBLMouseEventHandler(aHandler); |
|
157 break; |
|
158 default: |
|
159 *aResult = new nsXBLEventHandler(aHandler); |
|
160 break; |
|
161 } |
|
162 |
|
163 if (!*aResult) |
|
164 return NS_ERROR_OUT_OF_MEMORY; |
|
165 |
|
166 NS_ADDREF(*aResult); |
|
167 |
|
168 return NS_OK; |
|
169 } |
|
170 |
|
171 nsresult |
|
172 NS_NewXBLKeyEventHandler(nsIAtom* aEventType, uint8_t aPhase, uint8_t aType, |
|
173 nsXBLKeyEventHandler** aResult) |
|
174 { |
|
175 *aResult = new nsXBLKeyEventHandler(aEventType, aPhase, aType); |
|
176 |
|
177 if (!*aResult) |
|
178 return NS_ERROR_OUT_OF_MEMORY; |
|
179 |
|
180 NS_ADDREF(*aResult); |
|
181 |
|
182 return NS_OK; |
|
183 } |