forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIMEStateManager.cpp
1647 lines (1467 loc) · 56 KB
/
IMEStateManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Logging.h"
#include "mozilla/IMEStateManager.h"
#include "mozilla/Attributes.h"
#include "mozilla/EventListenerManager.h"
#include "mozilla/EventStates.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/SizePrintfMacros.h"
#include "mozilla/TextComposition.h"
#include "mozilla/TextEvents.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/HTMLFormElement.h"
#include "mozilla/dom/TabParent.h"
#include "HTMLInputElement.h"
#include "IMEContentObserver.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIDOMMouseEvent.h"
#include "nsIEditor.h"
#include "nsIForm.h"
#include "nsIFormControl.h"
#include "nsINode.h"
#include "nsIObserverService.h"
#include "nsIPresShell.h"
#include "nsISelection.h"
#include "nsISupports.h"
#include "nsPresContext.h"
namespace mozilla {
using namespace dom;
using namespace widget;
/**
* When a method is called, log its arguments and/or related static variables
* with LogLevel::Info. However, if it puts too many logs like
* OnDestroyPresContext(), should long only when the method actually does
* something. In this case, the log should start with "<method name>".
*
* When a method quits due to unexpected situation, log the reason with
* LogLevel::Error. In this case, the log should start with
* "<method name>(), FAILED". The indent makes the log look easier.
*
* When a method does something only in some situations and it may be important
* for debug, log the information with LogLevel::Debug. In this case, the log
* should start with " <method name>(),".
*/
LazyLogModule sISMLog("IMEStateManager");
static const char*
GetBoolName(bool aBool)
{
return aBool ? "true" : "false";
}
static const char*
GetActionCauseName(InputContextAction::Cause aCause)
{
switch (aCause) {
case InputContextAction::CAUSE_UNKNOWN:
return "CAUSE_UNKNOWN";
case InputContextAction::CAUSE_UNKNOWN_CHROME:
return "CAUSE_UNKNOWN_CHROME";
case InputContextAction::CAUSE_KEY:
return "CAUSE_KEY";
case InputContextAction::CAUSE_MOUSE:
return "CAUSE_MOUSE";
case InputContextAction::CAUSE_TOUCH:
return "CAUSE_TOUCH";
default:
return "illegal value";
}
}
static const char*
GetActionFocusChangeName(InputContextAction::FocusChange aFocusChange)
{
switch (aFocusChange) {
case InputContextAction::FOCUS_NOT_CHANGED:
return "FOCUS_NOT_CHANGED";
case InputContextAction::GOT_FOCUS:
return "GOT_FOCUS";
case InputContextAction::LOST_FOCUS:
return "LOST_FOCUS";
case InputContextAction::MENU_GOT_PSEUDO_FOCUS:
return "MENU_GOT_PSEUDO_FOCUS";
case InputContextAction::MENU_LOST_PSEUDO_FOCUS:
return "MENU_LOST_PSEUDO_FOCUS";
default:
return "illegal value";
}
}
static const char*
GetIMEStateEnabledName(IMEState::Enabled aEnabled)
{
switch (aEnabled) {
case IMEState::DISABLED:
return "DISABLED";
case IMEState::ENABLED:
return "ENABLED";
case IMEState::PASSWORD:
return "PASSWORD";
case IMEState::PLUGIN:
return "PLUGIN";
default:
return "illegal value";
}
}
static const char*
GetIMEStateSetOpenName(IMEState::Open aOpen)
{
switch (aOpen) {
case IMEState::DONT_CHANGE_OPEN_STATE:
return "DONT_CHANGE_OPEN_STATE";
case IMEState::OPEN:
return "OPEN";
case IMEState::CLOSED:
return "CLOSED";
default:
return "illegal value";
}
}
StaticRefPtr<nsIContent> IMEStateManager::sContent;
StaticRefPtr<nsPresContext> IMEStateManager::sPresContext;
nsIWidget* IMEStateManager::sFocusedIMEWidget = nullptr;
nsIWidget* IMEStateManager::sActiveInputContextWidget = nullptr;
StaticRefPtr<TabParent> IMEStateManager::sActiveTabParent;
StaticRefPtr<IMEContentObserver> IMEStateManager::sActiveIMEContentObserver;
TextCompositionArray* IMEStateManager::sTextCompositions = nullptr;
bool IMEStateManager::sInstalledMenuKeyboardListener = false;
bool IMEStateManager::sIsGettingNewIMEState = false;
bool IMEStateManager::sCheckForIMEUnawareWebApps = false;
bool IMEStateManager::sRemoteHasFocus = false;
// static
void
IMEStateManager::Init()
{
Preferences::AddBoolVarCache(
&sCheckForIMEUnawareWebApps,
"intl.ime.hack.on_ime_unaware_apps.fire_key_events_for_composition",
false);
}
// static
void
IMEStateManager::Shutdown()
{
MOZ_LOG(sISMLog, LogLevel::Info,
("Shutdown(), sTextCompositions=0x%p, sTextCompositions->Length()=%" PRIuSIZE,
sTextCompositions, sTextCompositions ? sTextCompositions->Length() : 0));
MOZ_ASSERT(!sTextCompositions || !sTextCompositions->Length());
delete sTextCompositions;
sTextCompositions = nullptr;
}
// static
void
IMEStateManager::OnTabParentDestroying(TabParent* aTabParent)
{
if (sActiveTabParent != aTabParent) {
return;
}
MOZ_LOG(sISMLog, LogLevel::Info,
("OnTabParentDestroying(aTabParent=0x%p), "
"The active TabParent is being destroyed", aTabParent));
// The active remote process might have crashed.
sActiveTabParent = nullptr;
// TODO: Need to cancel composition without TextComposition and make
// disable IME.
}
// static
void
IMEStateManager::WidgetDestroyed(nsIWidget* aWidget)
{
if (sFocusedIMEWidget == aWidget) {
sFocusedIMEWidget = nullptr;
}
if (sActiveInputContextWidget == aWidget) {
sActiveInputContextWidget = nullptr;
}
}
// static
void
IMEStateManager::StopIMEStateManagement()
{
MOZ_LOG(sISMLog, LogLevel::Info,
("StopIMEStateManagement()"));
// NOTE: Don't set input context from here since this has already lost
// the rights to change input context.
if (sTextCompositions && sPresContext) {
NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, sPresContext);
}
sActiveInputContextWidget = nullptr;
sPresContext = nullptr;
sContent = nullptr;
sActiveTabParent = nullptr;
DestroyIMEContentObserver();
}
// static
void
IMEStateManager::MaybeStartOffsetUpdatedInChild(nsIWidget* aWidget,
uint32_t aStartOffset)
{
if (NS_WARN_IF(!sTextCompositions)) {
MOZ_LOG(sISMLog, LogLevel::Warning,
("MaybeStartOffsetUpdatedInChild(aWidget=0x%p, aStartOffset=%u), "
"called when there is no composition", aWidget, aStartOffset));
return;
}
RefPtr<TextComposition> composition = GetTextCompositionFor(aWidget);
if (NS_WARN_IF(!composition)) {
MOZ_LOG(sISMLog, LogLevel::Warning,
("MaybeStartOffsetUpdatedInChild(aWidget=0x%p, aStartOffset=%u), "
"called when there is no composition", aWidget, aStartOffset));
return;
}
if (composition->NativeOffsetOfStartComposition() == aStartOffset) {
return;
}
MOZ_LOG(sISMLog, LogLevel::Info,
("MaybeStartOffsetUpdatedInChild(aWidget=0x%p, aStartOffset=%u), "
"old offset=%u",
aWidget, aStartOffset, composition->NativeOffsetOfStartComposition()));
composition->OnStartOffsetUpdatedInChild(aStartOffset);
}
// static
nsresult
IMEStateManager::OnDestroyPresContext(nsPresContext* aPresContext)
{
NS_ENSURE_ARG_POINTER(aPresContext);
// First, if there is a composition in the aPresContext, clean up it.
if (sTextCompositions) {
TextCompositionArray::index_type i =
sTextCompositions->IndexOf(aPresContext);
if (i != TextCompositionArray::NoIndex) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnDestroyPresContext(), "
"removing TextComposition instance from the array (index=%" PRIuSIZE ")", i));
// there should be only one composition per presContext object.
sTextCompositions->ElementAt(i)->Destroy();
sTextCompositions->RemoveElementAt(i);
if (sTextCompositions->IndexOf(aPresContext) !=
TextCompositionArray::NoIndex) {
MOZ_LOG(sISMLog, LogLevel::Error,
(" OnDestroyPresContext(), FAILED to remove "
"TextComposition instance from the array"));
MOZ_CRASH("Failed to remove TextComposition instance from the array");
}
}
}
if (aPresContext != sPresContext) {
return NS_OK;
}
MOZ_LOG(sISMLog, LogLevel::Info,
("OnDestroyPresContext(aPresContext=0x%p), "
"sPresContext=0x%p, sContent=0x%p, sTextCompositions=0x%p",
aPresContext, sPresContext.get(), sContent.get(), sTextCompositions));
DestroyIMEContentObserver();
nsCOMPtr<nsIWidget> widget = sPresContext->GetRootWidget();
if (widget) {
IMEState newState = GetNewIMEState(sPresContext, nullptr);
InputContextAction action(InputContextAction::CAUSE_UNKNOWN,
InputContextAction::LOST_FOCUS);
SetIMEState(newState, nullptr, widget, action);
}
sContent = nullptr;
sPresContext = nullptr;
sActiveTabParent = nullptr;
return NS_OK;
}
// static
nsresult
IMEStateManager::OnRemoveContent(nsPresContext* aPresContext,
nsIContent* aContent)
{
NS_ENSURE_ARG_POINTER(aPresContext);
// First, if there is a composition in the aContent, clean up it.
if (sTextCompositions) {
RefPtr<TextComposition> compositionInContent =
sTextCompositions->GetCompositionInContent(aPresContext, aContent);
if (compositionInContent) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnRemoveContent(), "
"composition is in the content"));
// Try resetting the native IME state. Be aware, typically, this method
// is called during the content being removed. Then, the native
// composition events which are caused by following APIs are ignored due
// to unsafe to run script (in PresShell::HandleEvent()).
DebugOnly<void*> widget = aPresContext->GetRootWidget();
MOZ_ASSERT(widget, "Why is there no widget?");
nsresult rv =
compositionInContent->NotifyIME(REQUEST_TO_CANCEL_COMPOSITION);
if (NS_FAILED(rv)) {
compositionInContent->NotifyIME(REQUEST_TO_COMMIT_COMPOSITION);
}
}
}
if (!sPresContext || !sContent ||
!nsContentUtils::ContentIsDescendantOf(sContent, aContent)) {
return NS_OK;
}
MOZ_LOG(sISMLog, LogLevel::Info,
("OnRemoveContent(aPresContext=0x%p, aContent=0x%p), "
"sPresContext=0x%p, sContent=0x%p, sTextCompositions=0x%p",
aPresContext, aContent, sPresContext.get(), sContent.get(), sTextCompositions));
DestroyIMEContentObserver();
// Current IME transaction should commit
nsCOMPtr<nsIWidget> widget = sPresContext->GetRootWidget();
if (widget) {
IMEState newState = GetNewIMEState(sPresContext, nullptr);
InputContextAction action(InputContextAction::CAUSE_UNKNOWN,
InputContextAction::LOST_FOCUS);
SetIMEState(newState, nullptr, widget, action);
}
sContent = nullptr;
sPresContext = nullptr;
sActiveTabParent = nullptr;
return NS_OK;
}
// static
nsresult
IMEStateManager::OnChangeFocus(nsPresContext* aPresContext,
nsIContent* aContent,
InputContextAction::Cause aCause)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("OnChangeFocus(aPresContext=0x%p, aContent=0x%p, aCause=%s)",
aPresContext, aContent, GetActionCauseName(aCause)));
InputContextAction action(aCause);
return OnChangeFocusInternal(aPresContext, aContent, action);
}
// static
nsresult
IMEStateManager::OnChangeFocusInternal(nsPresContext* aPresContext,
nsIContent* aContent,
InputContextAction aAction)
{
RefPtr<TabParent> newTabParent = TabParent::GetFrom(aContent);
MOZ_LOG(sISMLog, LogLevel::Info,
("OnChangeFocusInternal(aPresContext=0x%p, "
"aContent=0x%p (TabParent=0x%p), aAction={ mCause=%s, mFocusChange=%s }), "
"sPresContext=0x%p, sContent=0x%p, sActiveTabParent=0x%p, "
"sActiveIMEContentObserver=0x%p, sInstalledMenuKeyboardListener=%s",
aPresContext, aContent, newTabParent.get(),
GetActionCauseName(aAction.mCause),
GetActionFocusChangeName(aAction.mFocusChange),
sPresContext.get(), sContent.get(), sActiveTabParent.get(),
sActiveIMEContentObserver.get(),
GetBoolName(sInstalledMenuKeyboardListener)));
bool focusActuallyChanging =
(sContent != aContent || sPresContext != aPresContext ||
sActiveTabParent != newTabParent);
nsCOMPtr<nsIWidget> oldWidget =
sPresContext ? sPresContext->GetRootWidget() : nullptr;
if (oldWidget && focusActuallyChanging) {
// If we're deactivating, we shouldn't commit composition forcibly because
// the user may want to continue the composition.
if (aPresContext) {
NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, oldWidget);
}
}
if (sActiveIMEContentObserver &&
(aPresContext || !sActiveIMEContentObserver->KeepAliveDuringDeactive()) &&
!sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) {
DestroyIMEContentObserver();
}
if (!aPresContext) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnChangeFocusInternal(), "
"no nsPresContext is being activated"));
return NS_OK;
}
nsIContentParent* currentContentParent =
sActiveTabParent ? sActiveTabParent->Manager() : nullptr;
nsIContentParent* newContentParent =
newTabParent ? newTabParent->Manager() : nullptr;
if (sActiveTabParent && currentContentParent != newContentParent) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnChangeFocusInternal(), notifying previous "
"focused child process of parent process or another child process "
"getting focus"));
Unused << sActiveTabParent->SendStopIMEStateManagement();
}
nsCOMPtr<nsIWidget> widget =
(sPresContext == aPresContext) ? oldWidget.get() :
aPresContext->GetRootWidget();
if (NS_WARN_IF(!widget)) {
MOZ_LOG(sISMLog, LogLevel::Error,
(" OnChangeFocusInternal(), FAILED due to "
"no widget to manage its IME state"));
return NS_OK;
}
// If a child process has focus, we should disable IME state until the child
// process actually gets focus because if user types keys before that they
// are handled by IME.
IMEState newState =
newTabParent ? IMEState(IMEState::DISABLED) :
GetNewIMEState(aPresContext, aContent);
bool setIMEState = true;
if (newTabParent) {
if (aAction.mFocusChange == InputContextAction::MENU_GOT_PSEUDO_FOCUS ||
aAction.mFocusChange == InputContextAction::MENU_LOST_PSEUDO_FOCUS) {
// XXX When menu keyboard listener is being uninstalled, IME state needs
// to be restored by the child process asynchronously. Therefore,
// some key events which are fired immediately after closing menu
// may not be handled by IME.
Unused << newTabParent->
SendMenuKeyboardListenerInstalled(sInstalledMenuKeyboardListener);
setIMEState = sInstalledMenuKeyboardListener;
} else if (focusActuallyChanging) {
InputContext context = widget->GetInputContext();
if (context.mIMEState.mEnabled == IMEState::DISABLED) {
setIMEState = false;
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnChangeFocusInternal(), doesn't set IME "
"state because focused element (or document) is in a child process "
"and the IME state is already disabled"));
} else {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnChangeFocusInternal(), will disable IME "
"until new focused element (or document) in the child process "
"will get focus actually"));
}
} else {
// When focus is NOT changed actually, we shouldn't set IME state since
// that means that the window is being activated and the child process
// may have composition. Then, we shouldn't commit the composition with
// making IME state disabled.
setIMEState = false;
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnChangeFocusInternal(), doesn't set IME "
"state because focused element (or document) is already in the child "
"process"));
}
}
if (setIMEState) {
if (!focusActuallyChanging) {
// actual focus isn't changing, but if IME enabled state is changing,
// we should do it.
InputContext context = widget->GetInputContext();
if (context.mIMEState.mEnabled == newState.mEnabled) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnChangeFocusInternal(), "
"neither focus nor IME state is changing"));
return NS_OK;
}
aAction.mFocusChange = InputContextAction::FOCUS_NOT_CHANGED;
// Even if focus isn't changing actually, we should commit current
// composition here since the IME state is changing.
if (sPresContext && oldWidget && !focusActuallyChanging) {
NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, oldWidget);
}
} else if (aAction.mFocusChange == InputContextAction::FOCUS_NOT_CHANGED) {
// If aContent isn't null or aContent is null but editable, somebody gets
// focus.
bool gotFocus = aContent || (newState.mEnabled == IMEState::ENABLED);
aAction.mFocusChange =
gotFocus ? InputContextAction::GOT_FOCUS :
InputContextAction::LOST_FOCUS;
}
// Update IME state for new focus widget
SetIMEState(newState, aContent, widget, aAction);
}
sActiveTabParent = newTabParent;
sPresContext = aPresContext;
sContent = aContent;
// Don't call CreateIMEContentObserver() here except when a plugin gets
// focus because it will be called from the focus event handler of focused
// editor.
if (newState.mEnabled == IMEState::PLUGIN) {
CreateIMEContentObserver(nullptr);
if (sActiveIMEContentObserver) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnChangeFocusInternal(), an "
"IMEContentObserver instance is created for plugin and trying to "
"flush its pending notifications..."));
sActiveIMEContentObserver->TryToFlushPendingNotifications();
}
}
return NS_OK;
}
// static
void
IMEStateManager::OnInstalledMenuKeyboardListener(bool aInstalling)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("OnInstalledMenuKeyboardListener(aInstalling=%s), "
"sInstalledMenuKeyboardListener=%s",
GetBoolName(aInstalling), GetBoolName(sInstalledMenuKeyboardListener)));
sInstalledMenuKeyboardListener = aInstalling;
InputContextAction action(InputContextAction::CAUSE_UNKNOWN,
aInstalling ? InputContextAction::MENU_GOT_PSEUDO_FOCUS :
InputContextAction::MENU_LOST_PSEUDO_FOCUS);
OnChangeFocusInternal(sPresContext, sContent, action);
}
// static
bool
IMEStateManager::OnMouseButtonEventInEditor(nsPresContext* aPresContext,
nsIContent* aContent,
WidgetMouseEvent* aMouseEvent)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("OnMouseButtonEventInEditor(aPresContext=0x%p, "
"aContent=0x%p, aMouseEvent=0x%p), sPresContext=0x%p, sContent=0x%p",
aPresContext, aContent, aMouseEvent, sPresContext.get(), sContent.get()));
if (NS_WARN_IF(!aMouseEvent)) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnMouseButtonEventInEditor(), aMouseEvent is nullptr"));
return false;
}
if (sPresContext != aPresContext || sContent != aContent) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnMouseButtonEventInEditor(), "
"the mouse event isn't fired on the editor managed by ISM"));
return false;
}
if (!sActiveIMEContentObserver) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnMouseButtonEventInEditor(), "
"there is no active IMEContentObserver"));
return false;
}
if (!sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnMouseButtonEventInEditor(), "
"the active IMEContentObserver isn't managing the editor"));
return false;
}
bool consumed =
sActiveIMEContentObserver->OnMouseButtonEvent(aPresContext, aMouseEvent);
if (MOZ_LOG_TEST(sISMLog, LogLevel::Info)) {
nsAutoString eventType;
MOZ_LOG(sISMLog, LogLevel::Info,
(" OnMouseButtonEventInEditor(), "
"mouse event (mMessage=%s, button=%d) is %s",
ToChar(aMouseEvent->mMessage), aMouseEvent->button,
consumed ? "consumed" : "not consumed"));
}
return consumed;
}
// static
void
IMEStateManager::OnClickInEditor(nsPresContext* aPresContext,
nsIContent* aContent,
const WidgetMouseEvent* aMouseEvent)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("OnClickInEditor(aPresContext=0x%p, aContent=0x%p, aMouseEvent=0x%p), "
"sPresContext=0x%p, sContent=0x%p",
aPresContext, aContent, aMouseEvent, sPresContext.get(), sContent.get()));
if (NS_WARN_IF(!aMouseEvent)) {
return;
}
if (sPresContext != aPresContext || sContent != aContent) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnClickInEditor(), "
"the mouse event isn't fired on the editor managed by ISM"));
return;
}
nsCOMPtr<nsIWidget> widget = aPresContext->GetRootWidget();
NS_ENSURE_TRUE_VOID(widget);
if (!aMouseEvent->IsTrusted()) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnClickInEditor(), "
"the mouse event isn't a trusted event"));
return; // ignore untrusted event.
}
if (aMouseEvent->button) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnClickInEditor(), "
"the mouse event isn't a left mouse button event"));
return; // not a left click event.
}
if (aMouseEvent->mClickCount != 1) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnClickInEditor(), "
"the mouse event isn't a single click event"));
return; // should notify only first click event.
}
InputContextAction::Cause cause =
aMouseEvent->inputSource == nsIDOMMouseEvent::MOZ_SOURCE_TOUCH ?
InputContextAction::CAUSE_TOUCH : InputContextAction::CAUSE_MOUSE;
InputContextAction action(cause, InputContextAction::FOCUS_NOT_CHANGED);
IMEState newState = GetNewIMEState(aPresContext, aContent);
SetIMEState(newState, aContent, widget, action);
}
// static
void
IMEStateManager::OnFocusInEditor(nsPresContext* aPresContext,
nsIContent* aContent,
nsIEditor* aEditor)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("OnFocusInEditor(aPresContext=0x%p, aContent=0x%p, aEditor=0x%p), "
"sPresContext=0x%p, sContent=0x%p, sActiveIMEContentObserver=0x%p",
aPresContext, aContent, aEditor, sPresContext.get(), sContent.get(),
sActiveIMEContentObserver.get()));
if (sPresContext != aPresContext || sContent != aContent) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnFocusInEditor(), "
"an editor not managed by ISM gets focus"));
return;
}
// If the IMEContentObserver instance isn't managing the editor actually,
// we need to recreate the instance.
if (sActiveIMEContentObserver) {
if (sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnFocusInEditor(), "
"the editor is already being managed by sActiveIMEContentObserver"));
return;
}
DestroyIMEContentObserver();
}
CreateIMEContentObserver(aEditor);
// Let's flush the focus notification now.
if (sActiveIMEContentObserver) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" OnFocusInEditor(), new IMEContentObserver is "
"created, trying to flush pending notifications..."));
sActiveIMEContentObserver->TryToFlushPendingNotifications();
}
}
// static
void
IMEStateManager::OnEditorInitialized(nsIEditor* aEditor)
{
if (!sActiveIMEContentObserver ||
sActiveIMEContentObserver->GetEditor() != aEditor) {
return;
}
MOZ_LOG(sISMLog, LogLevel::Info,
("OnEditorInitialized(aEditor=0x%p)",
aEditor));
sActiveIMEContentObserver->UnsuppressNotifyingIME();
}
// static
void
IMEStateManager::OnEditorDestroying(nsIEditor* aEditor)
{
if (!sActiveIMEContentObserver ||
sActiveIMEContentObserver->GetEditor() != aEditor) {
return;
}
MOZ_LOG(sISMLog, LogLevel::Info,
("OnEditorDestroying(aEditor=0x%p)",
aEditor));
// The IMEContentObserver shouldn't notify IME of anything until reframing
// is finished.
sActiveIMEContentObserver->SuppressNotifyingIME();
}
// static
void
IMEStateManager::UpdateIMEState(const IMEState& aNewIMEState,
nsIContent* aContent,
nsIEditor* aEditor)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("UpdateIMEState(aNewIMEState={ mEnabled=%s, "
"mOpen=%s }, aContent=0x%p, aEditor=0x%p), "
"sPresContext=0x%p, sContent=0x%p, sActiveIMEContentObserver=0x%p, "
"sIsGettingNewIMEState=%s",
GetIMEStateEnabledName(aNewIMEState.mEnabled),
GetIMEStateSetOpenName(aNewIMEState.mOpen), aContent, aEditor,
sPresContext.get(), sContent.get(), sActiveIMEContentObserver.get(),
GetBoolName(sIsGettingNewIMEState)));
if (sIsGettingNewIMEState) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" UpdateIMEState(), "
"does nothing because of called while getting new IME state"));
return;
}
if (NS_WARN_IF(!sPresContext)) {
MOZ_LOG(sISMLog, LogLevel::Error,
(" UpdateIMEState(), FAILED due to "
"no managing nsPresContext"));
return;
}
nsCOMPtr<nsIWidget> widget = sPresContext->GetRootWidget();
if (NS_WARN_IF(!widget)) {
MOZ_LOG(sISMLog, LogLevel::Error,
(" UpdateIMEState(), FAILED due to "
"no widget for the managing nsPresContext"));
return;
}
// Even if there is active IMEContentObserver, it may not be observing the
// editor with current editable root content due to reframed. In such case,
// We should try to reinitialize the IMEContentObserver.
if (sActiveIMEContentObserver && IsIMEObserverNeeded(aNewIMEState)) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" UpdateIMEState(), try to reinitialize the "
"active IMEContentObserver"));
if (!sActiveIMEContentObserver->MaybeReinitialize(widget, sPresContext,
aContent, aEditor)) {
MOZ_LOG(sISMLog, LogLevel::Error,
(" UpdateIMEState(), failed to reinitialize the "
"active IMEContentObserver"));
}
}
// If there is no active IMEContentObserver or it isn't observing the
// editor correctly, we should recreate it.
bool createTextStateManager =
(!sActiveIMEContentObserver ||
!sActiveIMEContentObserver->IsManaging(sPresContext, aContent));
bool updateIMEState =
(widget->GetInputContext().mIMEState.mEnabled != aNewIMEState.mEnabled);
if (updateIMEState) {
// commit current composition before modifying IME state.
NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, widget);
}
if (createTextStateManager) {
DestroyIMEContentObserver();
}
if (updateIMEState) {
InputContextAction action(InputContextAction::CAUSE_UNKNOWN,
InputContextAction::FOCUS_NOT_CHANGED);
SetIMEState(aNewIMEState, aContent, widget, action);
}
if (createTextStateManager) {
// XXX In this case, it might not be enough safe to notify IME of anything.
// So, don't try to flush pending notifications of IMEContentObserver
// here.
CreateIMEContentObserver(aEditor);
}
}
// static
IMEState
IMEStateManager::GetNewIMEState(nsPresContext* aPresContext,
nsIContent* aContent)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("GetNewIMEState(aPresContext=0x%p, aContent=0x%p), "
"sInstalledMenuKeyboardListener=%s",
aPresContext, aContent, GetBoolName(sInstalledMenuKeyboardListener)));
// On Printing or Print Preview, we don't need IME.
if (aPresContext->Type() == nsPresContext::eContext_PrintPreview ||
aPresContext->Type() == nsPresContext::eContext_Print) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" GetNewIMEState() returns DISABLED because "
"the nsPresContext is for print or print preview"));
return IMEState(IMEState::DISABLED);
}
if (sInstalledMenuKeyboardListener) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" GetNewIMEState() returns DISABLED because "
"menu keyboard listener was installed"));
return IMEState(IMEState::DISABLED);
}
if (!aContent) {
// Even if there are no focused content, the focused document might be
// editable, such case is design mode.
nsIDocument* doc = aPresContext->Document();
if (doc && doc->HasFlag(NODE_IS_EDITABLE)) {
MOZ_LOG(sISMLog, LogLevel::Debug,
(" GetNewIMEState() returns ENABLED because "
"design mode editor has focus"));
return IMEState(IMEState::ENABLED);
}
MOZ_LOG(sISMLog, LogLevel::Debug,
(" GetNewIMEState() returns DISABLED because "
"no content has focus"));
return IMEState(IMEState::DISABLED);
}
// nsIContent::GetDesiredIMEState() may cause a call of UpdateIMEState()
// from EditorBase::PostCreate() because GetDesiredIMEState() needs to
// retrieve an editor instance for the element if it's editable element.
// For avoiding such nested IME state updates, we should set
// sIsGettingNewIMEState here and UpdateIMEState() should check it.
GettingNewIMEStateBlocker blocker;
IMEState newIMEState = aContent->GetDesiredIMEState();
MOZ_LOG(sISMLog, LogLevel::Debug,
(" GetNewIMEState() returns { mEnabled=%s, "
"mOpen=%s }",
GetIMEStateEnabledName(newIMEState.mEnabled),
GetIMEStateSetOpenName(newIMEState.mOpen)));
return newIMEState;
}
static bool
MayBeIMEUnawareWebApp(nsINode* aNode)
{
bool haveKeyEventsListener = false;
while (aNode) {
EventListenerManager* const mgr = aNode->GetExistingListenerManager();
if (mgr) {
if (mgr->MayHaveInputOrCompositionEventListener()) {
return false;
}
haveKeyEventsListener |= mgr->MayHaveKeyEventListener();
}
aNode = aNode->GetParentNode();
}
return haveKeyEventsListener;
}
// static
void
IMEStateManager::SetInputContextForChildProcess(
TabParent* aTabParent,
const InputContext& aInputContext,
const InputContextAction& aAction)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("SetInputContextForChildProcess(aTabParent=0x%p, "
"aInputContext={ mIMEState={ mEnabled=%s, mOpen=%s }, "
"mHTMLInputType=\"%s\", mHTMLInputInputmode=\"%s\", mActionHint=\"%s\" }, "
"aAction={ mCause=%s, mAction=%s }), sPresContext=0x%p, "
"sActiveTabParent=0x%p",
aTabParent, GetIMEStateEnabledName(aInputContext.mIMEState.mEnabled),
GetIMEStateSetOpenName(aInputContext.mIMEState.mOpen),
NS_ConvertUTF16toUTF8(aInputContext.mHTMLInputType).get(),
NS_ConvertUTF16toUTF8(aInputContext.mHTMLInputInputmode).get(),
NS_ConvertUTF16toUTF8(aInputContext.mActionHint).get(),
GetActionCauseName(aAction.mCause),
GetActionFocusChangeName(aAction.mFocusChange),
sPresContext.get(), sActiveTabParent.get()));
if (aTabParent != sActiveTabParent) {
MOZ_LOG(sISMLog, LogLevel::Error,
(" SetInputContextForChildProcess(), FAILED, "
"because non-focused tab parent tries to set input context"));
return;
}
if (NS_WARN_IF(!sPresContext)) {
MOZ_LOG(sISMLog, LogLevel::Error,
(" SetInputContextForChildProcess(), FAILED, "
"due to no focused presContext"));
return;
}
nsCOMPtr<nsIWidget> widget = sPresContext->GetRootWidget();
if (NS_WARN_IF(!widget)) {
MOZ_LOG(sISMLog, LogLevel::Error,
(" SetInputContextForChildProcess(), FAILED, "
"due to no widget in the focused presContext"));
return;
}
MOZ_ASSERT(aInputContext.mOrigin == InputContext::ORIGIN_CONTENT);
SetInputContext(widget, aInputContext, aAction);
}
// static
void
IMEStateManager::SetIMEState(const IMEState& aState,
nsIContent* aContent,
nsIWidget* aWidget,
InputContextAction aAction)
{
MOZ_LOG(sISMLog, LogLevel::Info,
("SetIMEState(aState={ mEnabled=%s, mOpen=%s }, "
"aContent=0x%p (TabParent=0x%p), aWidget=0x%p, aAction={ mCause=%s, "
"mFocusChange=%s })",
GetIMEStateEnabledName(aState.mEnabled),
GetIMEStateSetOpenName(aState.mOpen), aContent,
TabParent::GetFrom(aContent), aWidget,
GetActionCauseName(aAction.mCause),
GetActionFocusChangeName(aAction.mFocusChange)));
NS_ENSURE_TRUE_VOID(aWidget);
InputContext context;
context.mIMEState = aState;
context.mMayBeIMEUnaware = context.mIMEState.IsEditable() &&
sCheckForIMEUnawareWebApps && MayBeIMEUnawareWebApp(aContent);
if (aContent &&
aContent->IsAnyOfHTMLElements(nsGkAtoms::input, nsGkAtoms::textarea)) {
if (!aContent->IsHTMLElement(nsGkAtoms::textarea)) {
// <input type=number> has an anonymous <input type=text> descendant
// that gets focus whenever anyone tries to focus the number control. We
// need to check if aContent is one of those anonymous text controls and,
// if so, use the number control instead:
nsIContent* content = aContent;
HTMLInputElement* inputElement =
HTMLInputElement::FromContentOrNull(aContent);
if (inputElement) {
HTMLInputElement* ownerNumberControl =
inputElement->GetOwnerNumberControl();
if (ownerNumberControl) {
content = ownerNumberControl; // an <input type=number>
}
}
content->GetAttr(kNameSpaceID_None, nsGkAtoms::type,
context.mHTMLInputType);
} else {
context.mHTMLInputType.Assign(nsGkAtoms::textarea->GetUTF16String());