forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContentCache.cpp
1647 lines (1508 loc) · 65.2 KB
/
ContentCache.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: sw=2 ts=8 et :
*/
/* 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/ContentCache.h"
#include "mozilla/IMEStateManager.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/Logging.h"
#include "mozilla/Move.h"
#include "mozilla/RefPtr.h"
#include "mozilla/TextComposition.h"
#include "mozilla/TextEvents.h"
#include "mozilla/dom/BrowserParent.h"
#include "nsExceptionHandler.h"
#include "nsIWidget.h"
namespace mozilla {
using namespace dom;
using namespace widget;
static const char* GetBoolName(bool aBool) { return aBool ? "true" : "false"; }
static const char* GetNotificationName(const IMENotification* aNotification) {
if (!aNotification) {
return "Not notification";
}
return ToChar(aNotification->mMessage);
}
class GetRectText : public nsAutoCString {
public:
explicit GetRectText(const LayoutDeviceIntRect& aRect) {
AssignLiteral("{ x=");
AppendInt(aRect.X());
AppendLiteral(", y=");
AppendInt(aRect.Y());
AppendLiteral(", width=");
AppendInt(aRect.Width());
AppendLiteral(", height=");
AppendInt(aRect.Height());
AppendLiteral(" }");
}
virtual ~GetRectText() {}
};
class GetWritingModeName : public nsAutoCString {
public:
explicit GetWritingModeName(const WritingMode& aWritingMode) {
if (!aWritingMode.IsVertical()) {
AssignLiteral("Horizontal");
return;
}
if (aWritingMode.IsVerticalLR()) {
AssignLiteral("Vertical (LTR)");
return;
}
AssignLiteral("Vertical (RTL)");
}
virtual ~GetWritingModeName() {}
};
class GetEscapedUTF8String final : public NS_ConvertUTF16toUTF8 {
public:
explicit GetEscapedUTF8String(const nsAString& aString)
: NS_ConvertUTF16toUTF8(aString) {
Escape();
}
explicit GetEscapedUTF8String(const char16ptr_t aString)
: NS_ConvertUTF16toUTF8(aString) {
Escape();
}
GetEscapedUTF8String(const char16ptr_t aString, uint32_t aLength)
: NS_ConvertUTF16toUTF8(aString, aLength) {
Escape();
}
private:
void Escape() {
ReplaceSubstring("\r", "\\r");
ReplaceSubstring("\n", "\\n");
ReplaceSubstring("\t", "\\t");
}
};
/*****************************************************************************
* mozilla::ContentCache
*****************************************************************************/
LazyLogModule sContentCacheLog("ContentCacheWidgets");
ContentCache::ContentCache() : mCompositionStart(UINT32_MAX) {}
/*****************************************************************************
* mozilla::ContentCacheInChild
*****************************************************************************/
ContentCacheInChild::ContentCacheInChild() : ContentCache() {}
void ContentCacheInChild::Clear() {
MOZ_LOG(sContentCacheLog, LogLevel::Info, ("0x%p Clear()", this));
mCompositionStart = UINT32_MAX;
mText.Truncate();
mSelection.Clear();
mFirstCharRect.SetEmpty();
mCaret.Clear();
mTextRectArray.Clear();
mEditorRect.SetEmpty();
}
bool ContentCacheInChild::CacheAll(nsIWidget* aWidget,
const IMENotification* aNotification) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheAll(aWidget=0x%p, aNotification=%s)", this, aWidget,
GetNotificationName(aNotification)));
if (NS_WARN_IF(!CacheText(aWidget, aNotification)) ||
NS_WARN_IF(!CacheEditorRect(aWidget, aNotification))) {
return false;
}
return true;
}
bool ContentCacheInChild::CacheSelection(nsIWidget* aWidget,
const IMENotification* aNotification) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheSelection(aWidget=0x%p, aNotification=%s)", this, aWidget,
GetNotificationName(aNotification)));
mCaret.Clear();
mSelection.Clear();
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent selection(true, eQuerySelectedText, aWidget);
aWidget->DispatchEvent(&selection, status);
if (NS_WARN_IF(!selection.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p CacheSelection(), FAILED, "
"couldn't retrieve the selected text",
this));
return false;
}
if (selection.mReply.mReversed) {
mSelection.mAnchor =
selection.mReply.mOffset + selection.mReply.mString.Length();
mSelection.mFocus = selection.mReply.mOffset;
} else {
mSelection.mAnchor = selection.mReply.mOffset;
mSelection.mFocus =
selection.mReply.mOffset + selection.mReply.mString.Length();
}
mSelection.mWritingMode = selection.GetWritingMode();
return CacheCaret(aWidget, aNotification) &&
CacheTextRects(aWidget, aNotification);
}
bool ContentCacheInChild::CacheCaret(nsIWidget* aWidget,
const IMENotification* aNotification) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheCaret(aWidget=0x%p, aNotification=%s)", this, aWidget,
GetNotificationName(aNotification)));
mCaret.Clear();
if (NS_WARN_IF(!mSelection.IsValid())) {
return false;
}
// XXX Should be mSelection.mFocus?
mCaret.mOffset = mSelection.StartOffset();
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent caretRect(true, eQueryCaretRect, aWidget);
caretRect.InitForQueryCaretRect(mCaret.mOffset);
aWidget->DispatchEvent(&caretRect, status);
if (NS_WARN_IF(!caretRect.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p CacheCaret(), FAILED, "
"couldn't retrieve the caret rect at offset=%u",
this, mCaret.mOffset));
mCaret.Clear();
return false;
}
mCaret.mRect = caretRect.mReply.mRect;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheCaret(), Succeeded, "
"mSelection={ mAnchor=%u, mFocus=%u, mWritingMode=%s }, "
"mCaret={ mOffset=%u, mRect=%s }",
this, mSelection.mAnchor, mSelection.mFocus,
GetWritingModeName(mSelection.mWritingMode).get(), mCaret.mOffset,
GetRectText(mCaret.mRect).get()));
return true;
}
bool ContentCacheInChild::CacheEditorRect(
nsIWidget* aWidget, const IMENotification* aNotification) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheEditorRect(aWidget=0x%p, aNotification=%s)", this,
aWidget, GetNotificationName(aNotification)));
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent editorRectEvent(true, eQueryEditorRect, aWidget);
aWidget->DispatchEvent(&editorRectEvent, status);
if (NS_WARN_IF(!editorRectEvent.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p CacheEditorRect(), FAILED, "
"couldn't retrieve the editor rect",
this));
return false;
}
mEditorRect = editorRectEvent.mReply.mRect;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheEditorRect(), Succeeded, "
"mEditorRect=%s",
this, GetRectText(mEditorRect).get()));
return true;
}
bool ContentCacheInChild::CacheText(nsIWidget* aWidget,
const IMENotification* aNotification) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheText(aWidget=0x%p, aNotification=%s)", this, aWidget,
GetNotificationName(aNotification)));
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent queryText(true, eQueryTextContent, aWidget);
queryText.InitForQueryTextContent(0, UINT32_MAX);
aWidget->DispatchEvent(&queryText, status);
if (NS_WARN_IF(!queryText.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p CacheText(), FAILED, couldn't retrieve whole text", this));
mText.Truncate();
return false;
}
mText = queryText.mReply.mString;
MOZ_LOG(
sContentCacheLog, LogLevel::Info,
("0x%p CacheText(), Succeeded, mText.Length()=%u", this, mText.Length()));
return CacheSelection(aWidget, aNotification);
}
bool ContentCacheInChild::QueryCharRect(nsIWidget* aWidget, uint32_t aOffset,
LayoutDeviceIntRect& aCharRect) const {
aCharRect.SetEmpty();
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent textRect(true, eQueryTextRect, aWidget);
textRect.InitForQueryTextRect(aOffset, 1);
aWidget->DispatchEvent(&textRect, status);
if (NS_WARN_IF(!textRect.mSucceeded)) {
return false;
}
aCharRect = textRect.mReply.mRect;
// Guarantee the rect is not empty.
if (NS_WARN_IF(!aCharRect.Height())) {
aCharRect.SetHeight(1);
}
if (NS_WARN_IF(!aCharRect.Width())) {
aCharRect.SetWidth(1);
}
return true;
}
bool ContentCacheInChild::QueryCharRectArray(nsIWidget* aWidget,
uint32_t aOffset, uint32_t aLength,
RectArray& aCharRectArray) const {
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent textRects(true, eQueryTextRectArray, aWidget);
textRects.InitForQueryTextRectArray(aOffset, aLength);
aWidget->DispatchEvent(&textRects, status);
if (NS_WARN_IF(!textRects.mSucceeded)) {
aCharRectArray.Clear();
return false;
}
aCharRectArray = std::move(textRects.mReply.mRectArray);
return true;
}
bool ContentCacheInChild::CacheTextRects(nsIWidget* aWidget,
const IMENotification* aNotification) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheTextRects(aWidget=0x%p, aNotification=%s), "
"mCaret={ mOffset=%u, IsValid()=%s }",
this, aWidget, GetNotificationName(aNotification), mCaret.mOffset,
GetBoolName(mCaret.IsValid())));
mCompositionStart = UINT32_MAX;
mTextRectArray.Clear();
mSelection.ClearAnchorCharRects();
mSelection.ClearFocusCharRects();
mSelection.mRect.SetEmpty();
mFirstCharRect.SetEmpty();
if (NS_WARN_IF(!mSelection.IsValid())) {
return false;
}
// Retrieve text rects in composition string if there is.
RefPtr<TextComposition> textComposition =
IMEStateManager::GetTextCompositionFor(aWidget);
if (textComposition) {
// mCompositionStart may be updated by some composition event handlers.
// So, let's update it with the latest information.
mCompositionStart = textComposition->NativeOffsetOfStartComposition();
// Note that TextComposition::String() may not be modified here because
// it's modified after all edit action listeners are performed but this
// is called while some of them are performed.
// FYI: For supporting IME which commits composition and restart new
// composition immediately, we should cache next character of current
// composition too.
uint32_t length = textComposition->LastData().Length() + 1;
mTextRectArray.mStart = mCompositionStart;
if (NS_WARN_IF(!QueryCharRectArray(aWidget, mTextRectArray.mStart, length,
mTextRectArray.mRects))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p CacheTextRects(), FAILED, "
"couldn't retrieve text rect array of the composition string",
this));
}
}
if (mTextRectArray.InRange(mSelection.mAnchor) &&
(!mSelection.mAnchor || mTextRectArray.InRange(mSelection.mAnchor - 1))) {
mSelection.mAnchorCharRects[eNextCharRect] =
mTextRectArray.GetRect(mSelection.mAnchor);
if (mSelection.mAnchor) {
mSelection.mAnchorCharRects[ePrevCharRect] =
mTextRectArray.GetRect(mSelection.mAnchor - 1);
}
} else {
RectArray rects;
uint32_t startOffset = mSelection.mAnchor ? mSelection.mAnchor - 1 : 0;
uint32_t length = mSelection.mAnchor ? 2 : 1;
if (NS_WARN_IF(!QueryCharRectArray(aWidget, startOffset, length, rects))) {
MOZ_LOG(
sContentCacheLog, LogLevel::Error,
("0x%p CacheTextRects(), FAILED, "
"couldn't retrieve text rect array around the selection anchor (%u)",
this, mSelection.mAnchor));
MOZ_ASSERT(mSelection.mAnchorCharRects[ePrevCharRect].IsEmpty());
MOZ_ASSERT(mSelection.mAnchorCharRects[eNextCharRect].IsEmpty());
} else {
if (rects.Length() > 1) {
mSelection.mAnchorCharRects[ePrevCharRect] = rects[0];
mSelection.mAnchorCharRects[eNextCharRect] = rects[1];
} else if (rects.Length()) {
mSelection.mAnchorCharRects[eNextCharRect] = rects[0];
MOZ_ASSERT(mSelection.mAnchorCharRects[ePrevCharRect].IsEmpty());
}
}
}
if (mSelection.Collapsed()) {
mSelection.mFocusCharRects[0] = mSelection.mAnchorCharRects[0];
mSelection.mFocusCharRects[1] = mSelection.mAnchorCharRects[1];
} else if (mTextRectArray.InRange(mSelection.mFocus) &&
(!mSelection.mFocus ||
mTextRectArray.InRange(mSelection.mFocus - 1))) {
mSelection.mFocusCharRects[eNextCharRect] =
mTextRectArray.GetRect(mSelection.mFocus);
if (mSelection.mFocus) {
mSelection.mFocusCharRects[ePrevCharRect] =
mTextRectArray.GetRect(mSelection.mFocus - 1);
}
} else {
RectArray rects;
uint32_t startOffset = mSelection.mFocus ? mSelection.mFocus - 1 : 0;
uint32_t length = mSelection.mFocus ? 2 : 1;
if (NS_WARN_IF(!QueryCharRectArray(aWidget, startOffset, length, rects))) {
MOZ_LOG(
sContentCacheLog, LogLevel::Error,
("0x%p CacheTextRects(), FAILED, "
"couldn't retrieve text rect array around the selection focus (%u)",
this, mSelection.mFocus));
MOZ_ASSERT(mSelection.mFocusCharRects[ePrevCharRect].IsEmpty());
MOZ_ASSERT(mSelection.mFocusCharRects[eNextCharRect].IsEmpty());
} else {
if (rects.Length() > 1) {
mSelection.mFocusCharRects[ePrevCharRect] = rects[0];
mSelection.mFocusCharRects[eNextCharRect] = rects[1];
} else if (rects.Length()) {
mSelection.mFocusCharRects[eNextCharRect] = rects[0];
MOZ_ASSERT(mSelection.mFocusCharRects[ePrevCharRect].IsEmpty());
}
}
}
if (!mSelection.Collapsed()) {
nsEventStatus status = nsEventStatus_eIgnore;
WidgetQueryContentEvent textRect(true, eQueryTextRect, aWidget);
textRect.InitForQueryTextRect(mSelection.StartOffset(),
mSelection.Length());
aWidget->DispatchEvent(&textRect, status);
if (NS_WARN_IF(!textRect.mSucceeded)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p CacheTextRects(), FAILED, "
"couldn't retrieve text rect of whole selected text",
this));
} else {
mSelection.mRect = textRect.mReply.mRect;
}
}
if (!mSelection.mFocus) {
mFirstCharRect = mSelection.mFocusCharRects[eNextCharRect];
} else if (mSelection.mFocus == 1) {
mFirstCharRect = mSelection.mFocusCharRects[ePrevCharRect];
} else if (!mSelection.mAnchor) {
mFirstCharRect = mSelection.mAnchorCharRects[eNextCharRect];
} else if (mSelection.mAnchor == 1) {
mFirstCharRect = mSelection.mFocusCharRects[ePrevCharRect];
} else if (mTextRectArray.InRange(0)) {
mFirstCharRect = mTextRectArray.GetRect(0);
} else {
LayoutDeviceIntRect charRect;
if (NS_WARN_IF(!QueryCharRect(aWidget, 0, charRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p CacheTextRects(), FAILED, "
"couldn't retrieve first char rect",
this));
} else {
mFirstCharRect = charRect;
}
}
MOZ_LOG(
sContentCacheLog, LogLevel::Info,
("0x%p CacheTextRects(), Succeeded, "
"mText.Length()=%x, mTextRectArray={ mStart=%u, mRects.Length()=%zu"
" }, mSelection={ mAnchor=%u, mAnchorCharRects[eNextCharRect]=%s, "
"mAnchorCharRects[ePrevCharRect]=%s, mFocus=%u, "
"mFocusCharRects[eNextCharRect]=%s, mFocusCharRects[ePrevCharRect]=%s, "
"mRect=%s }, mFirstCharRect=%s",
this, mText.Length(), mTextRectArray.mStart,
mTextRectArray.mRects.Length(), mSelection.mAnchor,
GetRectText(mSelection.mAnchorCharRects[eNextCharRect]).get(),
GetRectText(mSelection.mAnchorCharRects[ePrevCharRect]).get(),
mSelection.mFocus,
GetRectText(mSelection.mFocusCharRects[eNextCharRect]).get(),
GetRectText(mSelection.mFocusCharRects[ePrevCharRect]).get(),
GetRectText(mSelection.mRect).get(), GetRectText(mFirstCharRect).get()));
return true;
}
void ContentCacheInChild::SetSelection(nsIWidget* aWidget,
uint32_t aStartOffset, uint32_t aLength,
bool aReversed,
const WritingMode& aWritingMode) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p SetSelection(aStartOffset=%u, "
"aLength=%u, aReversed=%s, aWritingMode=%s), mText.Length()=%u",
this, aStartOffset, aLength, GetBoolName(aReversed),
GetWritingModeName(aWritingMode).get(), mText.Length()));
if (!aReversed) {
mSelection.mAnchor = aStartOffset;
mSelection.mFocus = aStartOffset + aLength;
} else {
mSelection.mAnchor = aStartOffset + aLength;
mSelection.mFocus = aStartOffset;
}
mSelection.mWritingMode = aWritingMode;
if (NS_WARN_IF(!CacheCaret(aWidget))) {
return;
}
Unused << NS_WARN_IF(!CacheTextRects(aWidget));
}
/*****************************************************************************
* mozilla::ContentCacheInParent
*****************************************************************************/
ContentCacheInParent::ContentCacheInParent(BrowserParent& aBrowserParent)
: ContentCache(),
mBrowserParent(aBrowserParent),
mCommitStringByRequest(nullptr),
mPendingEventsNeedingAck(0),
mCompositionStartInChild(UINT32_MAX),
mPendingCommitLength(0),
mPendingCompositionCount(0),
mPendingCommitCount(0),
mWidgetHasComposition(false),
mIsChildIgnoringCompositionEvents(false) {}
void ContentCacheInParent::AssignContent(const ContentCache& aOther,
nsIWidget* aWidget,
const IMENotification* aNotification) {
mText = aOther.mText;
mSelection = aOther.mSelection;
mFirstCharRect = aOther.mFirstCharRect;
mCaret = aOther.mCaret;
mTextRectArray = aOther.mTextRectArray;
mEditorRect = aOther.mEditorRect;
// Only when there is one composition, the TextComposition instance in this
// process is managing the composition in the remote process. Therefore,
// we shouldn't update composition start offset of TextComposition with
// old composition which is still being handled by the child process.
if (mWidgetHasComposition && mPendingCompositionCount == 1) {
IMEStateManager::MaybeStartOffsetUpdatedInChild(aWidget, mCompositionStart);
}
// When this instance allows to query content relative to composition string,
// we should modify mCompositionStart with the latest information in the
// remote process because now we have the information around the composition
// string.
mCompositionStartInChild = aOther.mCompositionStart;
if (mWidgetHasComposition || mPendingCommitCount) {
if (aOther.mCompositionStart != UINT32_MAX) {
if (mCompositionStart != aOther.mCompositionStart) {
mCompositionStart = aOther.mCompositionStart;
mPendingCommitLength = 0;
}
} else if (mCompositionStart != mSelection.StartOffset()) {
mCompositionStart = mSelection.StartOffset();
mPendingCommitLength = 0;
NS_WARNING_ASSERTION(mCompositionStart != UINT32_MAX,
"mCompositionStart shouldn't be invalid offset when "
"the widget has composition");
}
}
MOZ_LOG(
sContentCacheLog, LogLevel::Info,
("0x%p AssignContent(aNotification=%s), "
"Succeeded, mText.Length()=%u, mSelection={ mAnchor=%u, mFocus=%u, "
"mWritingMode=%s, mAnchorCharRects[eNextCharRect]=%s, "
"mAnchorCharRects[ePrevCharRect]=%s, mFocusCharRects[eNextCharRect]=%s, "
"mFocusCharRects[ePrevCharRect]=%s, mRect=%s }, "
"mFirstCharRect=%s, mCaret={ mOffset=%u, mRect=%s }, mTextRectArray={ "
"mStart=%u, mRects.Length()=%zu }, mWidgetHasComposition=%s, "
"mPendingCompositionCount=%u, mCompositionStart=%u, "
"mPendingCommitLength=%u, mEditorRect=%s",
this, GetNotificationName(aNotification), mText.Length(),
mSelection.mAnchor, mSelection.mFocus,
GetWritingModeName(mSelection.mWritingMode).get(),
GetRectText(mSelection.mAnchorCharRects[eNextCharRect]).get(),
GetRectText(mSelection.mAnchorCharRects[ePrevCharRect]).get(),
GetRectText(mSelection.mFocusCharRects[eNextCharRect]).get(),
GetRectText(mSelection.mFocusCharRects[ePrevCharRect]).get(),
GetRectText(mSelection.mRect).get(), GetRectText(mFirstCharRect).get(),
mCaret.mOffset, GetRectText(mCaret.mRect).get(), mTextRectArray.mStart,
mTextRectArray.mRects.Length(), GetBoolName(mWidgetHasComposition),
mPendingCompositionCount, mCompositionStart, mPendingCommitLength,
GetRectText(mEditorRect).get()));
}
bool ContentCacheInParent::HandleQueryContentEvent(
WidgetQueryContentEvent& aEvent, nsIWidget* aWidget) const {
MOZ_ASSERT(aWidget);
aEvent.mSucceeded = false;
aEvent.mReply.mFocusedWidget = aWidget;
// ContentCache doesn't store offset of its start with XP linebreaks.
// So, we don't support to query contents relative to composition start
// offset with XP linebreaks.
if (NS_WARN_IF(!aEvent.mUseNativeLineBreak)) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), FAILED due to query with XP "
"linebreaks",
this));
return false;
}
if (NS_WARN_IF(!aEvent.mInput.IsValidOffset())) {
MOZ_LOG(
sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), FAILED due to invalid offset", this));
return false;
}
if (NS_WARN_IF(!aEvent.mInput.IsValidEventMessage(aEvent.mMessage))) {
MOZ_LOG(
sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), FAILED due to invalid event message",
this));
return false;
}
bool isRelativeToInsertionPoint = aEvent.mInput.mRelativeToInsertionPoint;
if (isRelativeToInsertionPoint) {
if (aWidget->PluginHasFocus()) {
if (NS_WARN_IF(!aEvent.mInput.MakeOffsetAbsolute(0))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), FAILED due to "
"aEvent.mInput.MakeOffsetAbsolute(0) failure, aEvent={ "
"mMessage=%s, "
"mInput={ mOffset=%" PRId64 ", mLength=%" PRIu32 " } }",
this, ToChar(aEvent.mMessage), aEvent.mInput.mOffset,
aEvent.mInput.mLength));
return false;
}
} else if (mWidgetHasComposition || mPendingCommitCount) {
if (NS_WARN_IF(!aEvent.mInput.MakeOffsetAbsolute(mCompositionStart +
mPendingCommitLength))) {
MOZ_LOG(
sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), FAILED due to "
"aEvent.mInput.MakeOffsetAbsolute(mCompositionStart + "
"mPendingCommitLength) failure, "
"mCompositionStart=%" PRIu32 ", mPendingCommitLength=%" PRIu32 ", "
"aEvent={ mMessage=%s, mInput={ mOffset=%" PRId64
", mLength=%" PRIu32 " } }",
this, mCompositionStart, mPendingCommitLength,
ToChar(aEvent.mMessage), aEvent.mInput.mOffset,
aEvent.mInput.mLength));
return false;
}
} else if (NS_WARN_IF(!mSelection.IsValid())) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), FAILED due to mSelection is "
"invalid",
this));
return false;
} else if (NS_WARN_IF(!aEvent.mInput.MakeOffsetAbsolute(
mSelection.StartOffset() + mPendingCommitLength))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), FAILED due to "
"aEvent.mInput.MakeOffsetAbsolute(mSelection.StartOffset() + "
"mPendingCommitLength) failure, "
"mSelection={ StartOffset()=%d, Length()=%d }, "
"mPendingCommitLength=%" PRIu32 ", aEvent={ mMessage=%s, "
"mInput={ mOffset=%" PRId64 ", mLength=%" PRIu32 " } }",
this, mSelection.StartOffset(), mSelection.Length(),
mPendingCommitLength, ToChar(aEvent.mMessage),
aEvent.mInput.mOffset, aEvent.mInput.mLength));
return false;
}
}
switch (aEvent.mMessage) {
case eQuerySelectedText:
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQuerySelectedText }, aWidget=0x%p)",
this, aWidget));
if (aWidget->PluginHasFocus()) {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent(), "
"return emtpy selection becasue plugin has focus",
this));
aEvent.mSucceeded = true;
aEvent.mReply.mOffset = 0;
aEvent.mReply.mReversed = false;
aEvent.mReply.mHasSelection = false;
return true;
}
if (NS_WARN_IF(!IsSelectionValid())) {
// If content cache hasn't been initialized properly, make the query
// failed.
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), "
"FAILED because mSelection is not valid",
this));
return true;
}
aEvent.mReply.mOffset = mSelection.StartOffset();
if (mSelection.Collapsed()) {
aEvent.mReply.mString.Truncate(0);
} else {
if (NS_WARN_IF(mSelection.EndOffset() > mText.Length())) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), "
"FAILED because mSelection.EndOffset()=%u is larger than "
"mText.Length()=%u",
this, mSelection.EndOffset(), mText.Length()));
return false;
}
aEvent.mReply.mString =
Substring(mText, aEvent.mReply.mOffset, mSelection.Length());
}
aEvent.mReply.mReversed = mSelection.Reversed();
aEvent.mReply.mHasSelection = true;
aEvent.mReply.mWritingMode = mSelection.mWritingMode;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mOffset=%u, mString=\"%s\", "
"mReversed=%s, mHasSelection=%s, mWritingMode=%s } }",
this, aEvent.mReply.mOffset,
GetEscapedUTF8String(aEvent.mReply.mString).get(),
GetBoolName(aEvent.mReply.mReversed),
GetBoolName(aEvent.mReply.mHasSelection),
GetWritingModeName(aEvent.mReply.mWritingMode).get()));
break;
case eQueryTextContent: {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQueryTextContent, mInput={ mOffset=%" PRId64
", mLength=%u } }, aWidget=0x%p), mText.Length()=%u",
this, aEvent.mInput.mOffset, aEvent.mInput.mLength, aWidget,
mText.Length()));
uint32_t inputOffset = aEvent.mInput.mOffset;
uint32_t inputEndOffset =
std::min(aEvent.mInput.EndOffset(), mText.Length());
if (NS_WARN_IF(inputEndOffset < inputOffset)) {
MOZ_LOG(
sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), "
"FAILED because inputOffset=%u is larger than inputEndOffset=%u",
this, inputOffset, inputEndOffset));
return false;
}
aEvent.mReply.mOffset = inputOffset;
aEvent.mReply.mString =
Substring(mText, inputOffset, inputEndOffset - inputOffset);
MOZ_LOG(
sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mOffset=%u, mString.Length()=%u } }",
this, aEvent.mReply.mOffset, aEvent.mReply.mString.Length()));
break;
}
case eQueryTextRect:
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQueryTextRect, mInput={ mOffset=%" PRId64
", mLength=%u } }, aWidget=0x%p), mText.Length()=%u",
this, aEvent.mInput.mOffset, aEvent.mInput.mLength, aWidget,
mText.Length()));
if (NS_WARN_IF(!IsSelectionValid())) {
// If content cache hasn't been initialized properly, make the query
// failed.
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), "
"FAILED because mSelection is not valid",
this));
return true;
}
// Note that if the query is relative to insertion point, the query was
// probably requested by native IME. In such case, we should return
// non-empty rect since returning failure causes IME showing its window
// at odd position.
if (aEvent.mInput.mLength) {
if (NS_WARN_IF(!GetUnionTextRects(
aEvent.mInput.mOffset, aEvent.mInput.mLength,
isRelativeToInsertionPoint, aEvent.mReply.mRect))) {
// XXX We don't have cache for this request.
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), "
"FAILED to get union rect",
this));
return false;
}
} else {
// If the length is 0, we should return caret rect instead.
if (NS_WARN_IF(!GetCaretRect(aEvent.mInput.mOffset,
isRelativeToInsertionPoint,
aEvent.mReply.mRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), "
"FAILED to get caret rect",
this));
return false;
}
}
if (aEvent.mInput.mOffset < mText.Length()) {
aEvent.mReply.mString = Substring(
mText, aEvent.mInput.mOffset,
mText.Length() >= aEvent.mInput.EndOffset() ? aEvent.mInput.mLength
: UINT32_MAX);
} else {
aEvent.mReply.mString.Truncate(0);
}
aEvent.mReply.mOffset = aEvent.mInput.mOffset;
// XXX This may be wrong if storing range isn't in the selection range.
aEvent.mReply.mWritingMode = mSelection.mWritingMode;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mOffset=%u, mString=\"%s\", "
"mWritingMode=%s, mRect=%s } }",
this, aEvent.mReply.mOffset,
GetEscapedUTF8String(aEvent.mReply.mString).get(),
GetWritingModeName(aEvent.mReply.mWritingMode).get(),
GetRectText(aEvent.mReply.mRect).get()));
break;
case eQueryCaretRect:
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQueryCaretRect, mInput={ mOffset=%" PRId64
" } }, "
"aWidget=0x%p), mText.Length()=%u",
this, aEvent.mInput.mOffset, aWidget, mText.Length()));
if (NS_WARN_IF(!IsSelectionValid())) {
// If content cache hasn't been initialized properly, make the query
// failed.
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), "
"FAILED because mSelection is not valid",
this));
return true;
}
// Note that if the query is relative to insertion point, the query was
// probably requested by native IME. In such case, we should return
// non-empty rect since returning failure causes IME showing its window
// at odd position.
if (NS_WARN_IF(!GetCaretRect(aEvent.mInput.mOffset,
isRelativeToInsertionPoint,
aEvent.mReply.mRect))) {
MOZ_LOG(sContentCacheLog, LogLevel::Error,
("0x%p HandleQueryContentEvent(), "
"FAILED to get caret rect",
this));
return false;
}
aEvent.mReply.mOffset = aEvent.mInput.mOffset;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mOffset=%u, mRect=%s } }",
this, aEvent.mReply.mOffset,
GetRectText(aEvent.mReply.mRect).get()));
break;
case eQueryEditorRect:
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent("
"aEvent={ mMessage=eQueryEditorRect }, aWidget=0x%p)",
this, aWidget));
aEvent.mReply.mRect = mEditorRect;
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p HandleQueryContentEvent(), "
"Succeeded, aEvent={ mReply={ mRect=%s } }",
this, GetRectText(aEvent.mReply.mRect).get()));
break;
default:
break;
}
aEvent.mSucceeded = true;
return true;
}
bool ContentCacheInParent::GetTextRect(uint32_t aOffset,
bool aRoundToExistingOffset,
LayoutDeviceIntRect& aTextRect) const {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p GetTextRect(aOffset=%u, "
"aRoundToExistingOffset=%s), "
"mTextRectArray={ mStart=%u, mRects.Length()=%zu }, "
"mSelection={ mAnchor=%u, mFocus=%u }",
this, aOffset, GetBoolName(aRoundToExistingOffset),
mTextRectArray.mStart, mTextRectArray.mRects.Length(),
mSelection.mAnchor, mSelection.mFocus));
if (!aOffset) {
NS_WARNING_ASSERTION(!mFirstCharRect.IsEmpty(), "empty rect");
aTextRect = mFirstCharRect;
return !aTextRect.IsEmpty();
}
if (aOffset == mSelection.mAnchor) {
NS_WARNING_ASSERTION(!mSelection.mAnchorCharRects[eNextCharRect].IsEmpty(),
"empty rect");
aTextRect = mSelection.mAnchorCharRects[eNextCharRect];
return !aTextRect.IsEmpty();
}
if (mSelection.mAnchor && aOffset == mSelection.mAnchor - 1) {
NS_WARNING_ASSERTION(!mSelection.mAnchorCharRects[ePrevCharRect].IsEmpty(),
"empty rect");
aTextRect = mSelection.mAnchorCharRects[ePrevCharRect];
return !aTextRect.IsEmpty();
}
if (aOffset == mSelection.mFocus) {
NS_WARNING_ASSERTION(!mSelection.mFocusCharRects[eNextCharRect].IsEmpty(),
"empty rect");
aTextRect = mSelection.mFocusCharRects[eNextCharRect];
return !aTextRect.IsEmpty();
}
if (mSelection.mFocus && aOffset == mSelection.mFocus - 1) {
NS_WARNING_ASSERTION(!mSelection.mFocusCharRects[ePrevCharRect].IsEmpty(),
"empty rect");
aTextRect = mSelection.mFocusCharRects[ePrevCharRect];
return !aTextRect.IsEmpty();
}
uint32_t offset = aOffset;
if (!mTextRectArray.InRange(aOffset)) {
if (!aRoundToExistingOffset) {
aTextRect.SetEmpty();
return false;
}
if (!mTextRectArray.IsValid()) {
// If there are no rects in mTextRectArray, we should refer the start of
// the selection because IME must query a char rect around it if there is
// no composition.
aTextRect = mSelection.StartCharRect();
return !aTextRect.IsEmpty();
}
if (offset < mTextRectArray.StartOffset()) {
offset = mTextRectArray.StartOffset();
} else {
offset = mTextRectArray.EndOffset() - 1;
}
}
aTextRect = mTextRectArray.GetRect(offset);
return !aTextRect.IsEmpty();
}
bool ContentCacheInParent::GetUnionTextRects(
uint32_t aOffset, uint32_t aLength, bool aRoundToExistingOffset,
LayoutDeviceIntRect& aUnionTextRect) const {
MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p GetUnionTextRects(aOffset=%u, "
"aLength=%u, aRoundToExistingOffset=%s), mTextRectArray={ "
"mStart=%u, mRects.Length()=%zu }, "
"mSelection={ mAnchor=%u, mFocus=%u }",
this, aOffset, aLength, GetBoolName(aRoundToExistingOffset),
mTextRectArray.mStart, mTextRectArray.mRects.Length(),
mSelection.mAnchor, mSelection.mFocus));
CheckedInt<uint32_t> endOffset = CheckedInt<uint32_t>(aOffset) + aLength;
if (!endOffset.isValid()) {
return false;
}
if (!mSelection.Collapsed() && aOffset == mSelection.StartOffset() &&
aLength == mSelection.Length()) {
NS_WARNING_ASSERTION(!mSelection.mRect.IsEmpty(), "empty rect");
aUnionTextRect = mSelection.mRect;
return !aUnionTextRect.IsEmpty();
}
if (aLength == 1) {
if (!aOffset) {
NS_WARNING_ASSERTION(!mFirstCharRect.IsEmpty(), "empty rect");
aUnionTextRect = mFirstCharRect;
return !aUnionTextRect.IsEmpty();
}
if (aOffset == mSelection.mAnchor) {
NS_WARNING_ASSERTION(
!mSelection.mAnchorCharRects[eNextCharRect].IsEmpty(), "empty rect");
aUnionTextRect = mSelection.mAnchorCharRects[eNextCharRect];
return !aUnionTextRect.IsEmpty();
}
if (mSelection.mAnchor && aOffset == mSelection.mAnchor - 1) {
NS_WARNING_ASSERTION(
!mSelection.mAnchorCharRects[ePrevCharRect].IsEmpty(), "empty rect");
aUnionTextRect = mSelection.mAnchorCharRects[ePrevCharRect];
return !aUnionTextRect.IsEmpty();
}
if (aOffset == mSelection.mFocus) {
NS_WARNING_ASSERTION(!mSelection.mFocusCharRects[eNextCharRect].IsEmpty(),
"empty rect");
aUnionTextRect = mSelection.mFocusCharRects[eNextCharRect];
return !aUnionTextRect.IsEmpty();
}
if (mSelection.mFocus && aOffset == mSelection.mFocus - 1) {
NS_WARNING_ASSERTION(!mSelection.mFocusCharRects[ePrevCharRect].IsEmpty(),
"empty rect");
aUnionTextRect = mSelection.mFocusCharRects[ePrevCharRect];
return !aUnionTextRect.IsEmpty();
}
}
// Even if some text rects are not cached of the queried range,
// we should return union rect when the first character's rect is cached
// since the first character rect is important and the others are not so
// in most cases.
if (!aOffset && aOffset != mSelection.mAnchor &&
aOffset != mSelection.mFocus && !mTextRectArray.InRange(aOffset)) {
// The first character rect isn't cached.
return false;
}
if ((aRoundToExistingOffset && mTextRectArray.HasRects()) ||
mTextRectArray.IsOverlappingWith(aOffset, aLength)) {
aUnionTextRect = mTextRectArray.GetUnionRectAsFarAsPossible(
aOffset, aLength, aRoundToExistingOffset);
} else {
aUnionTextRect.SetEmpty();
}
if (!aOffset) {
aUnionTextRect = aUnionTextRect.Union(mFirstCharRect);
}
if (aOffset <= mSelection.mAnchor && mSelection.mAnchor < endOffset.value()) {
aUnionTextRect =
aUnionTextRect.Union(mSelection.mAnchorCharRects[eNextCharRect]);
}
if (mSelection.mAnchor && aOffset <= mSelection.mAnchor - 1 &&
mSelection.mAnchor - 1 < endOffset.value()) {
aUnionTextRect =
aUnionTextRect.Union(mSelection.mAnchorCharRects[ePrevCharRect]);
}
if (aOffset <= mSelection.mFocus && mSelection.mFocus < endOffset.value()) {
aUnionTextRect =
aUnionTextRect.Union(mSelection.mFocusCharRects[eNextCharRect]);
}
if (mSelection.mFocus && aOffset <= mSelection.mFocus - 1 &&
mSelection.mFocus - 1 < endOffset.value()) {
aUnionTextRect =
aUnionTextRect.Union(mSelection.mFocusCharRects[ePrevCharRect]);
}