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/. */
6 #include "TextEventDispatcher.h"
9 #include "PuppetWidget.h"
10 #include "TextEvents.h"
12 #include "mozilla/Preferences.h"
13 #include "mozilla/StaticPrefs_dom.h"
15 #include "nsIWidget.h"
16 #include "nsPIDOMWindow.h"
22 /******************************************************************************
24 *****************************************************************************/
25 TextEventDispatcher::TextEventDispatcher(nsIWidget
* aWidget
)
28 mInputTransactionType(eNoInputTransaction
),
30 mIsHandlingComposition(false),
32 MOZ_RELEASE_ASSERT(mWidget
, "aWidget must not be nullptr");
34 ClearNotificationRequests();
37 nsresult
TextEventDispatcher::BeginInputTransaction(
38 TextEventDispatcherListener
* aListener
) {
39 return BeginInputTransactionInternal(aListener
,
40 eSameProcessSyncInputTransaction
);
43 nsresult
TextEventDispatcher::BeginTestInputTransaction(
44 TextEventDispatcherListener
* aListener
, bool aIsAPZAware
) {
45 return BeginInputTransactionInternal(
46 aListener
, aIsAPZAware
? eAsyncTestInputTransaction
47 : eSameProcessSyncTestInputTransaction
);
50 nsresult
TextEventDispatcher::BeginNativeInputTransaction() {
51 if (NS_WARN_IF(!mWidget
)) {
52 return NS_ERROR_FAILURE
;
54 RefPtr
<TextEventDispatcherListener
> listener
=
55 mWidget
->GetNativeTextEventDispatcherListener();
56 if (NS_WARN_IF(!listener
)) {
57 return NS_ERROR_FAILURE
;
59 return BeginInputTransactionInternal(listener
, eNativeInputTransaction
);
62 nsresult
TextEventDispatcher::BeginInputTransactionInternal(
63 TextEventDispatcherListener
* aListener
, InputTransactionType aType
) {
64 if (NS_WARN_IF(!aListener
)) {
65 return NS_ERROR_INVALID_ARG
;
67 nsCOMPtr
<TextEventDispatcherListener
> listener
= do_QueryReferent(mListener
);
69 if (listener
== aListener
&& mInputTransactionType
== aType
) {
70 UpdateNotificationRequests();
73 // If this has composition or is dispatching an event, any other listener
74 // can steal ownership. Especially, if the latter case is allowed,
75 // nobody cannot begin input transaction with this if a modal dialog is
76 // opened during dispatching an event.
77 if (IsComposing() || IsDispatchingEvent()) {
78 return NS_ERROR_ALREADY_INITIALIZED
;
81 mListener
= do_GetWeakReference(aListener
);
82 mInputTransactionType
= aType
;
83 if (listener
&& listener
!= aListener
) {
84 listener
->OnRemovedFrom(this);
86 UpdateNotificationRequests();
90 nsresult
TextEventDispatcher::BeginInputTransactionFor(
91 const WidgetGUIEvent
* aEvent
, PuppetWidget
* aPuppetWidget
) {
92 MOZ_ASSERT(XRE_IsContentProcess());
93 MOZ_ASSERT(!IsDispatchingEvent());
95 switch (aEvent
->mMessage
) {
99 MOZ_ASSERT(aEvent
->mClass
== eKeyboardEventClass
);
101 case eCompositionStart
:
102 case eCompositionChange
:
103 case eCompositionCommit
:
104 case eCompositionCommitAsIs
:
105 MOZ_ASSERT(aEvent
->mClass
== eCompositionEventClass
);
108 return NS_ERROR_INVALID_ARG
;
111 if (aEvent
->mFlags
.mIsSynthesizedForTests
) {
112 // If the event is for an automated test and this instance dispatched
113 // an event to the parent process, we can assume that this is already
114 // initialized properly.
115 if (mInputTransactionType
== eAsyncTestInputTransaction
) {
118 // Even if the event coming from the parent process is synthesized for
119 // tests, this process should treat it as "sync" test here because
120 // it won't be go back to the parent process.
121 nsresult rv
= BeginInputTransactionInternal(
122 static_cast<TextEventDispatcherListener
*>(aPuppetWidget
),
123 eSameProcessSyncTestInputTransaction
);
124 if (NS_WARN_IF(NS_FAILED(rv
))) {
128 nsresult rv
= BeginNativeInputTransaction();
129 if (NS_WARN_IF(NS_FAILED(rv
))) {
134 // Emulate modifying members which indicate the state of composition.
135 // If we need to manage more states and/or more complexly, we should create
136 // internal methods which are called by both here and each event dispatcher
137 // method of this class.
138 switch (aEvent
->mMessage
) {
143 case eCompositionStart
:
144 MOZ_ASSERT(!mIsComposing
);
145 mIsComposing
= mIsHandlingComposition
= true;
147 case eCompositionChange
:
148 MOZ_ASSERT(mIsComposing
);
149 MOZ_ASSERT(mIsHandlingComposition
);
150 mIsComposing
= mIsHandlingComposition
= true;
152 case eCompositionCommit
:
153 case eCompositionCommitAsIs
:
154 MOZ_ASSERT(mIsComposing
);
155 MOZ_ASSERT(mIsHandlingComposition
);
156 mIsComposing
= false;
157 mIsHandlingComposition
= true;
160 MOZ_ASSERT_UNREACHABLE("You forgot to handle the event");
161 return NS_ERROR_UNEXPECTED
;
164 void TextEventDispatcher::EndInputTransaction(
165 TextEventDispatcherListener
* aListener
) {
166 if (NS_WARN_IF(IsComposing()) || NS_WARN_IF(IsDispatchingEvent())) {
170 mInputTransactionType
= eNoInputTransaction
;
172 nsCOMPtr
<TextEventDispatcherListener
> listener
= do_QueryReferent(mListener
);
173 if (NS_WARN_IF(!listener
)) {
177 if (NS_WARN_IF(listener
!= aListener
)) {
182 listener
->OnRemovedFrom(this);
183 UpdateNotificationRequests();
186 void TextEventDispatcher::OnDestroyWidget() {
189 ClearNotificationRequests();
190 mPendingComposition
.Clear();
191 nsCOMPtr
<TextEventDispatcherListener
> listener
= do_QueryReferent(mListener
);
193 mWritingMode
.reset();
194 mInputTransactionType
= eNoInputTransaction
;
196 listener
->OnRemovedFrom(this);
200 nsresult
TextEventDispatcher::GetState() const {
201 nsCOMPtr
<TextEventDispatcherListener
> listener
= do_QueryReferent(mListener
);
203 return NS_ERROR_NOT_INITIALIZED
;
205 if (!mWidget
|| mWidget
->Destroyed()) {
206 return NS_ERROR_NOT_AVAILABLE
;
211 void TextEventDispatcher::InitEvent(WidgetGUIEvent
& aEvent
) const {
212 aEvent
.mTime
= PR_IntervalNow();
213 aEvent
.mRefPoint
= LayoutDeviceIntPoint(0, 0);
214 aEvent
.mFlags
.mIsSynthesizedForTests
= IsForTests();
215 if (aEvent
.mClass
!= eCompositionEventClass
) {
218 void* pseudoIMEContext
= GetPseudoIMEContext();
219 if (pseudoIMEContext
) {
220 aEvent
.AsCompositionEvent()->mNativeIMEContext
.InitWithRawNativeIMEContext(
225 MOZ_ASSERT(!XRE_IsContentProcess(),
226 "Why did the content process start native event transaction?");
227 MOZ_ASSERT(aEvent
.AsCompositionEvent()->mNativeIMEContext
.IsValid(),
228 "Native IME context shouldn't be invalid");
230 #endif // #ifdef DEBUG
233 Maybe
<WritingMode
> TextEventDispatcher::MaybeQueryWritingModeAtSelection()
235 if (mHasFocus
|| mWritingMode
.isSome()) {
239 if (NS_WARN_IF(!mWidget
)) {
243 WidgetQueryContentEvent
querySelectedTextEvent(true, eQuerySelectedText
,
245 nsEventStatus status
= nsEventStatus_eIgnore
;
246 const_cast<TextEventDispatcher
*>(this)->DispatchEvent(
247 mWidget
, querySelectedTextEvent
, status
);
248 if (!querySelectedTextEvent
.FoundSelection()) {
252 return Some(querySelectedTextEvent
.mReply
->mWritingMode
);
255 nsresult
TextEventDispatcher::DispatchEvent(nsIWidget
* aWidget
,
256 WidgetGUIEvent
& aEvent
,
257 nsEventStatus
& aStatus
) {
258 MOZ_ASSERT(!aEvent
.AsInputEvent(), "Use DispatchInputEvent()");
260 RefPtr
<TextEventDispatcher
> kungFuDeathGrip(this);
261 nsCOMPtr
<nsIWidget
> widget(aWidget
);
263 nsresult rv
= widget
->DispatchEvent(&aEvent
, aStatus
);
268 nsresult
TextEventDispatcher::DispatchInputEvent(nsIWidget
* aWidget
,
269 WidgetInputEvent
& aEvent
,
270 nsEventStatus
& aStatus
) {
271 RefPtr
<TextEventDispatcher
> kungFuDeathGrip(this);
272 nsCOMPtr
<nsIWidget
> widget(aWidget
);
275 // If the event is dispatched via nsIWidget::DispatchInputEvent(), it
276 // sends the event to the parent process first since APZ needs to handle it
277 // first. However, some callers (e.g., keyboard apps on B2G and tests
278 // expecting synchronous dispatch) don't want this to do that.
280 if (ShouldSendInputEventToAPZ()) {
281 aStatus
= widget
->DispatchInputEvent(&aEvent
).mContentStatus
;
283 rv
= widget
->DispatchEvent(&aEvent
, aStatus
);
290 nsresult
TextEventDispatcher::StartComposition(
291 nsEventStatus
& aStatus
, const WidgetEventTime
* aEventTime
) {
292 aStatus
= nsEventStatus_eIgnore
;
294 nsresult rv
= GetState();
295 if (NS_WARN_IF(NS_FAILED(rv
))) {
299 if (NS_WARN_IF(mIsComposing
)) {
300 return NS_ERROR_FAILURE
;
303 // When you change some members from here, you may need same change in
304 // BeginInputTransactionFor().
305 mIsComposing
= mIsHandlingComposition
= true;
306 WidgetCompositionEvent
compositionStartEvent(true, eCompositionStart
,
308 InitEvent(compositionStartEvent
);
310 compositionStartEvent
.AssignEventTime(*aEventTime
);
312 rv
= DispatchEvent(mWidget
, compositionStartEvent
, aStatus
);
313 if (NS_WARN_IF(NS_FAILED(rv
))) {
320 nsresult
TextEventDispatcher::StartCompositionAutomaticallyIfNecessary(
321 nsEventStatus
& aStatus
, const WidgetEventTime
* aEventTime
) {
326 nsresult rv
= StartComposition(aStatus
, aEventTime
);
327 if (NS_WARN_IF(NS_FAILED(rv
))) {
331 // If started composition has already been committed, we shouldn't dispatch
332 // the compositionchange event.
333 if (!IsComposing()) {
334 aStatus
= nsEventStatus_eConsumeNoDefault
;
338 // Note that the widget might be destroyed during a call of
339 // StartComposition(). In such case, we shouldn't keep dispatching next
343 MOZ_ASSERT(rv
!= NS_ERROR_NOT_INITIALIZED
,
344 "aDispatcher must still be initialized in this case");
345 aStatus
= nsEventStatus_eConsumeNoDefault
;
346 return NS_OK
; // Don't throw exception in this case
349 aStatus
= nsEventStatus_eIgnore
;
353 nsresult
TextEventDispatcher::CommitComposition(
354 nsEventStatus
& aStatus
, const nsAString
* aCommitString
,
355 const WidgetEventTime
* aEventTime
) {
356 aStatus
= nsEventStatus_eIgnore
;
358 nsresult rv
= GetState();
359 if (NS_WARN_IF(NS_FAILED(rv
))) {
363 // When there is no composition, caller shouldn't try to commit composition
364 // with non-existing composition string nor commit composition with empty
366 if (NS_WARN_IF(!IsComposing() &&
367 (!aCommitString
|| aCommitString
->IsEmpty()))) {
368 return NS_ERROR_FAILURE
;
371 nsCOMPtr
<nsIWidget
> widget(mWidget
);
372 rv
= StartCompositionAutomaticallyIfNecessary(aStatus
, aEventTime
);
373 if (NS_WARN_IF(NS_FAILED(rv
))) {
376 if (aStatus
== nsEventStatus_eConsumeNoDefault
) {
380 // When you change some members from here, you may need same change in
381 // BeginInputTransactionFor().
383 // End current composition and make this free for other IMEs.
384 mIsComposing
= false;
386 EventMessage message
=
387 aCommitString
? eCompositionCommit
: eCompositionCommitAsIs
;
388 WidgetCompositionEvent
compositionCommitEvent(true, message
, widget
);
389 InitEvent(compositionCommitEvent
);
391 compositionCommitEvent
.AssignEventTime(*aEventTime
);
393 if (message
== eCompositionCommit
) {
394 compositionCommitEvent
.mData
= *aCommitString
;
395 // If aCommitString comes from TextInputProcessor, it may be void, but
396 // editor requires non-void string even when it's empty.
397 compositionCommitEvent
.mData
.SetIsVoid(false);
398 // Don't send CRLF nor CR, replace it with LF here.
399 compositionCommitEvent
.mData
.ReplaceSubstring(u
"\r\n"_ns
, u
"\n"_ns
);
400 compositionCommitEvent
.mData
.ReplaceSubstring(u
"\r"_ns
, u
"\n"_ns
);
402 rv
= DispatchEvent(widget
, compositionCommitEvent
, aStatus
);
403 if (NS_WARN_IF(NS_FAILED(rv
))) {
410 nsresult
TextEventDispatcher::NotifyIME(
411 const IMENotification
& aIMENotification
) {
412 nsresult rv
= NS_ERROR_NOT_IMPLEMENTED
;
414 switch (aIMENotification
.mMessage
) {
415 case NOTIFY_IME_OF_FOCUS
: {
416 mWritingMode
= MaybeQueryWritingModeAtSelection();
419 case NOTIFY_IME_OF_BLUR
:
421 mWritingMode
.reset();
422 ClearNotificationRequests();
424 case NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED
:
425 // If content handles composition events when native IME doesn't have
426 // composition, that means that we completely finished handling
427 // composition(s). Note that when focused content is in a remote
428 // process, this is sent when all dispatched composition events
429 // have been handled in the remote process.
430 if (!IsComposing()) {
431 mIsHandlingComposition
= false;
434 case NOTIFY_IME_OF_SELECTION_CHANGE
:
435 if (mHasFocus
&& aIMENotification
.mSelectionChangeData
.HasRange()) {
437 Some(aIMENotification
.mSelectionChangeData
.GetWritingMode());
444 // First, send the notification to current input transaction's listener.
445 nsCOMPtr
<TextEventDispatcherListener
> listener
= do_QueryReferent(mListener
);
447 rv
= listener
->NotifyIME(this, aIMENotification
);
454 // If current input transaction isn't for native event handler, we should
455 // send the notification to the native text event dispatcher listener
456 // since native event handler may need to do something from
457 // TextEventDispatcherListener::NotifyIME() even before there is no
458 // input transaction yet. For example, native IME handler may need to
459 // create new context at receiving NOTIFY_IME_OF_FOCUS. In this case,
460 // mListener may not be initialized since input transaction should be
461 // initialized immediately before dispatching every WidgetKeyboardEvent
462 // and WidgetCompositionEvent (dispatching events always occurs after
464 nsCOMPtr
<TextEventDispatcherListener
> nativeListener
=
465 mWidget
->GetNativeTextEventDispatcherListener();
466 if (listener
!= nativeListener
&& nativeListener
) {
467 switch (aIMENotification
.mMessage
) {
468 case REQUEST_TO_COMMIT_COMPOSITION
:
469 case REQUEST_TO_CANCEL_COMPOSITION
:
470 // It's not necessary to notify native IME of requests.
473 // Even if current input transaction's listener returns NS_OK or
474 // something, we need to notify native IME of notifications because
475 // when user typing after TIP does something, the changed information
476 // is necessary for them.
477 nsresult rv2
= nativeListener
->NotifyIME(this, aIMENotification
);
478 // But return the result from current listener except when the
479 // notification isn't handled.
480 if (rv
== NS_ERROR_NOT_IMPLEMENTED
) {
488 if (aIMENotification
.mMessage
== NOTIFY_IME_OF_FOCUS
) {
490 UpdateNotificationRequests();
496 void TextEventDispatcher::ClearNotificationRequests() {
497 mIMENotificationRequests
= IMENotificationRequests();
500 void TextEventDispatcher::UpdateNotificationRequests() {
501 ClearNotificationRequests();
503 // If it doesn't has focus, no notifications are available.
504 if (!mHasFocus
|| !mWidget
) {
508 // If there is a listener, its requests are necessary.
509 nsCOMPtr
<TextEventDispatcherListener
> listener
= do_QueryReferent(mListener
);
511 mIMENotificationRequests
= listener
->GetIMENotificationRequests();
514 // Even if this is in non-native input transaction, native IME needs
515 // requests. So, add native IME requests too.
516 if (!IsInNativeInputTransaction()) {
517 nsCOMPtr
<TextEventDispatcherListener
> nativeListener
=
518 mWidget
->GetNativeTextEventDispatcherListener();
519 if (nativeListener
) {
520 mIMENotificationRequests
|= nativeListener
->GetIMENotificationRequests();
525 bool TextEventDispatcher::DispatchKeyboardEvent(
526 EventMessage aMessage
, const WidgetKeyboardEvent
& aKeyboardEvent
,
527 nsEventStatus
& aStatus
, void* aData
) {
528 return DispatchKeyboardEventInternal(aMessage
, aKeyboardEvent
, aStatus
,
532 bool TextEventDispatcher::DispatchKeyboardEventInternal(
533 EventMessage aMessage
, const WidgetKeyboardEvent
& aKeyboardEvent
,
534 nsEventStatus
& aStatus
, void* aData
, uint32_t aIndexOfKeypress
,
535 bool aNeedsCallback
) {
536 // Note that this method is also used for dispatching key events on a plugin
537 // because key events on a plugin should be dispatched same as normal key
538 // events. Then, only some handlers which need to intercept key events
539 // before the focused plugin (e.g., reserved shortcut key handlers) can
540 // consume the events.
542 aMessage
== eKeyDown
|| aMessage
== eKeyUp
|| aMessage
== eKeyPress
,
543 "Invalid aMessage value");
544 nsresult rv
= GetState();
545 if (NS_WARN_IF(NS_FAILED(rv
))) {
549 // If the key shouldn't cause keypress events, don't this patch them.
550 if (aMessage
== eKeyPress
&& !aKeyboardEvent
.ShouldCauseKeypressEvents()) {
554 // Basically, key events shouldn't be dispatched during composition.
555 // Note that plugin process has different IME context. Therefore, we don't
556 // need to check our composition state when the key event is fired on a
559 // However, if we need to behave like other browsers, we need the keydown
560 // and keyup events. Note that this behavior is also allowed by D3E spec.
561 // FYI: keypress events must not be fired during composition.
562 if (!StaticPrefs::dom_keyboardevent_dispatch_during_composition() ||
563 aMessage
== eKeyPress
) {
566 // XXX If there was mOnlyContentDispatch for this case, it might be useful
567 // because our chrome doesn't assume that key events are fired during
571 WidgetKeyboardEvent
keyEvent(true, aMessage
, mWidget
);
573 keyEvent
.AssignKeyEventData(aKeyboardEvent
, false);
574 // Command arrays are not duplicated by AssignKeyEventData() due to
575 // both performance and footprint reasons. So, when TextInputProcessor
576 // emulates real text input or synthesizing keyboard events for tests,
577 // the arrays may be initialized all commands already. If so, we need to
578 // duplicate the arrays here, but we should do this only when we're
579 // dispatching eKeyPress events because BrowserParent::SendRealKeyEvent()
580 // does this only for eKeyPress event. Note that this is not required if
581 // we're in the main process because in the parent process, the edit commands
582 // will be initialized by `ExecuteEditCommands()` (when the event is handled
583 // by editor event listener) or `InitAllEditCommands()` (when the event is
584 // set to a content process). We should test whether these pathes work or
586 if (XRE_IsContentProcess() && keyEvent
.mIsSynthesizedByTIP
) {
587 if (aMessage
== eKeyPress
) {
588 keyEvent
.AssignCommands(aKeyboardEvent
);
590 // Prevent retriving native edit commands if we're in a content process
591 // because only `eKeyPress` events coming from the main process have
592 // edit commands (See `BrowserParent::SendRealKeyEvent`). And also
593 // retriving edit commands from a content process requires synchonous
594 // IPC and that makes running tests slower. Therefore, we should mark
595 // the `eKeyPress` event does not need to retrieve edit commands anymore.
596 keyEvent
.PreventNativeKeyBindings();
600 if (aStatus
== nsEventStatus_eConsumeNoDefault
) {
601 // If the key event should be dispatched as consumed event, marking it here.
602 // This is useful to prevent double action. This is intended to the system
603 // has already consumed the event but we need to dispatch the event for
604 // compatibility with older version and other browsers. So, we should not
605 // stop cross process forwarding of them.
606 keyEvent
.PreventDefaultBeforeDispatch(CrossProcessForwarding::eAllow
);
609 // Corrects each member for the specific key event type.
610 if (keyEvent
.mKeyNameIndex
!= KEY_NAME_INDEX_USE_STRING
) {
611 MOZ_ASSERT(!aIndexOfKeypress
,
612 "aIndexOfKeypress must be 0 for non-printable key");
613 // If the keyboard event isn't caused by printable key, its charCode should
615 keyEvent
.SetCharCode(0);
617 MOZ_DIAGNOSTIC_ASSERT_IF(aMessage
== eKeyDown
|| aMessage
== eKeyUp
,
619 MOZ_DIAGNOSTIC_ASSERT_IF(
620 aMessage
== eKeyPress
,
621 aIndexOfKeypress
< std::max
<size_t>(keyEvent
.mKeyValue
.Length(), 1));
623 keyEvent
.mKeyValue
.IsEmpty() ? 0 : keyEvent
.mKeyValue
[aIndexOfKeypress
];
624 keyEvent
.SetCharCode(static_cast<uint32_t>(ch
));
625 if (aMessage
== eKeyPress
) {
626 // keyCode of eKeyPress events of printable keys should be always 0.
627 keyEvent
.mKeyCode
= 0;
628 // eKeyPress events are dispatched for every character.
629 // So, each key value of eKeyPress events should be a character.
631 keyEvent
.mKeyValue
.Assign(ch
);
633 keyEvent
.mKeyValue
.Truncate();
637 if (aMessage
== eKeyUp
) {
638 // mIsRepeat of keyup event must be false.
639 keyEvent
.mIsRepeat
= false;
641 // mIsComposing should be initialized later.
642 keyEvent
.mIsComposing
= false;
643 if (mInputTransactionType
== eNativeInputTransaction
) {
644 // Copy mNativeKeyEvent here because for safety for other users of
645 // AssignKeyEventData(), it doesn't copy this.
646 keyEvent
.mNativeKeyEvent
= aKeyboardEvent
.mNativeKeyEvent
;
648 // If it's not a keyboard event for native key event, we should ensure that
649 // mNativeKeyEvent is null.
650 keyEvent
.mNativeKeyEvent
= nullptr;
652 // TODO: Manage mUniqueId here.
654 // Request the alternative char codes for the key event.
655 // eKeyDown also needs alternative char codes because nsXBLWindowKeyHandler
656 // needs to check if a following keypress event is reserved by chrome for
657 // stopping propagation of its preceding keydown event.
658 keyEvent
.mAlternativeCharCodes
.Clear();
659 if ((aMessage
== eKeyDown
|| aMessage
== eKeyPress
) &&
660 (aNeedsCallback
|| keyEvent
.IsControl() || keyEvent
.IsAlt() ||
661 keyEvent
.IsMeta() || keyEvent
.IsOS())) {
662 nsCOMPtr
<TextEventDispatcherListener
> listener
=
663 do_QueryReferent(mListener
);
665 DebugOnly
<WidgetKeyboardEvent
> original(keyEvent
);
666 listener
->WillDispatchKeyboardEvent(this, keyEvent
, aIndexOfKeypress
,
668 MOZ_ASSERT(keyEvent
.mMessage
==
669 static_cast<WidgetKeyboardEvent
&>(original
).mMessage
);
670 MOZ_ASSERT(keyEvent
.mKeyCode
==
671 static_cast<WidgetKeyboardEvent
&>(original
).mKeyCode
);
672 MOZ_ASSERT(keyEvent
.mLocation
==
673 static_cast<WidgetKeyboardEvent
&>(original
).mLocation
);
674 MOZ_ASSERT(keyEvent
.mIsRepeat
==
675 static_cast<WidgetKeyboardEvent
&>(original
).mIsRepeat
);
676 MOZ_ASSERT(keyEvent
.mIsComposing
==
677 static_cast<WidgetKeyboardEvent
&>(original
).mIsComposing
);
678 MOZ_ASSERT(keyEvent
.mKeyNameIndex
==
679 static_cast<WidgetKeyboardEvent
&>(original
).mKeyNameIndex
);
680 MOZ_ASSERT(keyEvent
.mCodeNameIndex
==
681 static_cast<WidgetKeyboardEvent
&>(original
).mCodeNameIndex
);
682 MOZ_ASSERT(keyEvent
.mKeyValue
==
683 static_cast<WidgetKeyboardEvent
&>(original
).mKeyValue
);
684 MOZ_ASSERT(keyEvent
.mCodeValue
==
685 static_cast<WidgetKeyboardEvent
&>(original
).mCodeValue
);
690 dom_keyboardevent_keypress_dispatch_non_printable_keys_only_system_group_in_content() &&
691 keyEvent
.mMessage
== eKeyPress
&&
692 !keyEvent
.ShouldKeyPressEventBeFiredOnContent()) {
693 // Note that even if we set it to true, this may be overwritten by
694 // PresShell::DispatchEventToDOM().
695 keyEvent
.mFlags
.mOnlySystemGroupDispatchInContent
= true;
698 // If an editable element has focus and we're in the parent process, we should
699 // retrieve native key bindings right now because even if it matches with a
700 // reserved shortcut key, it should be handled by the editor.
701 if (XRE_IsParentProcess() && mHasFocus
&&
702 (aMessage
== eKeyDown
|| aMessage
== eKeyPress
)) {
703 keyEvent
.InitAllEditCommands(mWritingMode
);
706 DispatchInputEvent(mWidget
, keyEvent
, aStatus
);
710 bool TextEventDispatcher::MaybeDispatchKeypressEvents(
711 const WidgetKeyboardEvent
& aKeyboardEvent
, nsEventStatus
& aStatus
,
712 void* aData
, bool aNeedsCallback
) {
713 // If the key event was consumed, keypress event shouldn't be fired.
714 if (aStatus
== nsEventStatus_eConsumeNoDefault
) {
718 // If the key shouldn't cause keypress events, don't fire them.
719 if (!aKeyboardEvent
.ShouldCauseKeypressEvents()) {
723 // If the key isn't a printable key or just inputting one character or
724 // no character, we should dispatch only one keypress. Otherwise, i.e.,
725 // if the key is a printable key and inputs multiple characters, keypress
726 // event should be dispatched the count of inputting characters times.
727 size_t keypressCount
=
728 aKeyboardEvent
.mKeyNameIndex
!= KEY_NAME_INDEX_USE_STRING
730 : std::max(static_cast<nsAString::size_type
>(1),
731 aKeyboardEvent
.mKeyValue
.Length());
732 bool isDispatched
= false;
733 bool consumed
= false;
734 for (size_t i
= 0; i
< keypressCount
; i
++) {
735 aStatus
= nsEventStatus_eIgnore
;
736 if (!DispatchKeyboardEventInternal(eKeyPress
, aKeyboardEvent
, aStatus
,
737 aData
, i
, aNeedsCallback
)) {
738 // The widget must have been gone.
743 consumed
= (aStatus
== nsEventStatus_eConsumeNoDefault
);
747 // If one of the keypress event was consumed, return ConsumeNoDefault.
749 aStatus
= nsEventStatus_eConsumeNoDefault
;
755 /******************************************************************************
756 * TextEventDispatcher::PendingComposition
757 *****************************************************************************/
759 TextEventDispatcher::PendingComposition::PendingComposition() { Clear(); }
761 void TextEventDispatcher::PendingComposition::Clear() {
764 mCaret
.mRangeType
= TextRangeType::eUninitialized
;
765 mReplacedNativeLineBreakers
= false;
768 void TextEventDispatcher::PendingComposition::EnsureClauseArray() {
772 mClauses
= new TextRangeArray();
775 nsresult
TextEventDispatcher::PendingComposition::SetString(
776 const nsAString
& aString
) {
777 MOZ_ASSERT(!mReplacedNativeLineBreakers
);
782 nsresult
TextEventDispatcher::PendingComposition::AppendClause(
783 uint32_t aLength
, TextRangeType aTextRangeType
) {
784 MOZ_ASSERT(!mReplacedNativeLineBreakers
);
786 if (NS_WARN_IF(!aLength
)) {
787 return NS_ERROR_INVALID_ARG
;
790 switch (aTextRangeType
) {
791 case TextRangeType::eRawClause
:
792 case TextRangeType::eSelectedRawClause
:
793 case TextRangeType::eConvertedClause
:
794 case TextRangeType::eSelectedClause
: {
797 textRange
.mStartOffset
=
798 mClauses
->IsEmpty() ? 0 : mClauses
->LastElement().mEndOffset
;
799 textRange
.mEndOffset
= textRange
.mStartOffset
+ aLength
;
800 textRange
.mRangeType
= aTextRangeType
;
801 mClauses
->AppendElement(textRange
);
805 return NS_ERROR_INVALID_ARG
;
809 nsresult
TextEventDispatcher::PendingComposition::SetCaret(uint32_t aOffset
,
811 MOZ_ASSERT(!mReplacedNativeLineBreakers
);
813 mCaret
.mStartOffset
= aOffset
;
814 mCaret
.mEndOffset
= mCaret
.mStartOffset
+ aLength
;
815 mCaret
.mRangeType
= TextRangeType::eCaret
;
819 nsresult
TextEventDispatcher::PendingComposition::Set(
820 const nsAString
& aString
, const TextRangeArray
* aRanges
) {
823 nsresult rv
= SetString(aString
);
824 if (NS_WARN_IF(NS_FAILED(rv
))) {
828 if (!aRanges
|| aRanges
->IsEmpty()) {
829 // Create dummy range if mString isn't empty.
830 if (!mString
.IsEmpty()) {
831 rv
= AppendClause(mString
.Length(), TextRangeType::eRawClause
);
832 if (NS_WARN_IF(NS_FAILED(rv
))) {
835 ReplaceNativeLineBreakers();
840 // Adjust offsets in the ranges for XP linefeed character (only \n).
841 for (uint32_t i
= 0; i
< aRanges
->Length(); ++i
) {
842 TextRange range
= aRanges
->ElementAt(i
);
843 if (range
.mRangeType
== TextRangeType::eCaret
) {
847 mClauses
->AppendElement(range
);
850 ReplaceNativeLineBreakers();
854 void TextEventDispatcher::PendingComposition::ReplaceNativeLineBreakers() {
855 mReplacedNativeLineBreakers
= true;
857 // If the composition string is empty, we don't need to do anything.
858 if (mString
.IsEmpty()) {
862 nsAutoString
nativeString(mString
);
863 // Don't expose CRLF nor CR to web contents, instead, use LF.
864 mString
.ReplaceSubstring(u
"\r\n"_ns
, u
"\n"_ns
);
865 mString
.ReplaceSubstring(u
"\r"_ns
, u
"\n"_ns
);
867 // If the length isn't changed, we don't need to adjust any offset and length
868 // of mClauses nor mCaret.
869 if (nativeString
.Length() == mString
.Length()) {
874 for (TextRange
& clause
: *mClauses
) {
875 AdjustRange(clause
, nativeString
);
878 if (mCaret
.mRangeType
== TextRangeType::eCaret
) {
879 AdjustRange(mCaret
, nativeString
);
884 void TextEventDispatcher::PendingComposition::AdjustRange(
885 TextRange
& aRange
, const nsAString
& aNativeString
) {
886 TextRange nativeRange
= aRange
;
887 // XXX Following code wastes runtime cost because this causes computing
888 // mStartOffset for each clause from the start of composition string.
889 // If we'd make TextRange have only its length, we don't need to do
890 // this. However, this must not be so serious problem because
891 // composition string is usually short and separated as a few clauses.
892 if (nativeRange
.mStartOffset
> 0) {
893 nsAutoString
preText(Substring(aNativeString
, 0, nativeRange
.mStartOffset
));
894 preText
.ReplaceSubstring(u
"\r\n"_ns
, u
"\n"_ns
);
895 aRange
.mStartOffset
= preText
.Length();
897 if (nativeRange
.Length() == 0) {
898 aRange
.mEndOffset
= aRange
.mStartOffset
;
900 nsAutoString
clause(Substring(aNativeString
, nativeRange
.mStartOffset
,
901 nativeRange
.Length()));
902 clause
.ReplaceSubstring(u
"\r\n"_ns
, u
"\n"_ns
);
903 aRange
.mEndOffset
= aRange
.mStartOffset
+ clause
.Length();
907 nsresult
TextEventDispatcher::PendingComposition::Flush(
908 TextEventDispatcher
* aDispatcher
, nsEventStatus
& aStatus
,
909 const WidgetEventTime
* aEventTime
) {
910 aStatus
= nsEventStatus_eIgnore
;
912 nsresult rv
= aDispatcher
->GetState();
913 if (NS_WARN_IF(NS_FAILED(rv
))) {
917 if (mClauses
&& !mClauses
->IsEmpty() &&
918 mClauses
->LastElement().mEndOffset
!= mString
.Length()) {
920 "Sum of length of the all clauses must be same as the string "
923 return NS_ERROR_ILLEGAL_VALUE
;
925 if (mCaret
.mRangeType
== TextRangeType::eCaret
) {
926 if (mCaret
.mEndOffset
> mString
.Length()) {
927 NS_WARNING("Caret position is out of the composition string");
929 return NS_ERROR_ILLEGAL_VALUE
;
932 mClauses
->AppendElement(mCaret
);
935 // If the composition string is set without Set(), we need to replace native
936 // line breakers in the composition string with XP line breaker.
937 if (!mReplacedNativeLineBreakers
) {
938 ReplaceNativeLineBreakers();
941 RefPtr
<TextEventDispatcher
> kungFuDeathGrip(aDispatcher
);
942 nsCOMPtr
<nsIWidget
> widget(aDispatcher
->mWidget
);
943 WidgetCompositionEvent
compChangeEvent(true, eCompositionChange
, widget
);
944 aDispatcher
->InitEvent(compChangeEvent
);
946 compChangeEvent
.AssignEventTime(*aEventTime
);
948 compChangeEvent
.mData
= mString
;
949 // If mString comes from TextInputProcessor, it may be void, but editor
950 // requires non-void string even when it's empty.
951 compChangeEvent
.mData
.SetIsVoid(false);
953 MOZ_ASSERT(!mClauses
->IsEmpty(),
954 "mClauses must be non-empty array when it's not nullptr");
955 compChangeEvent
.mRanges
= mClauses
;
958 // While this method dispatches a composition event, some other event handler
959 // cause more clauses to be added. So, we should clear pending composition
960 // before dispatching the event.
963 rv
= aDispatcher
->StartCompositionAutomaticallyIfNecessary(aStatus
,
965 if (NS_WARN_IF(NS_FAILED(rv
))) {
968 if (aStatus
== nsEventStatus_eConsumeNoDefault
) {
971 rv
= aDispatcher
->DispatchEvent(widget
, compChangeEvent
, aStatus
);
972 if (NS_WARN_IF(NS_FAILED(rv
))) {
979 } // namespace widget
980 } // namespace mozilla