@@ -532,6 +532,7 @@ StaticRefPtr<ITfCompartment> TSFUtils::sCompartmentForOpenClose;
532
532
StaticRefPtr<ITfInputProcessorProfiles> TSFUtils::sInputProcessorProfiles ;
533
533
StaticRefPtr<TSFTextStore> TSFUtils::sActiveTextStore ;
534
534
StaticRefPtr<TSFTextStoreBase> TSFUtils::sCurrentTextStore ;
535
+ StaticRefPtr<TSFEmptyTextStore> TSFUtils::sEmptyTextStore ;
535
536
DWORD TSFUtils::sClientId = 0 ;
536
537
537
538
template void TSFUtils::ClearStoringTextStoresIf (
@@ -583,6 +584,10 @@ void TSFUtils::Shutdown() {
583
584
584
585
sDisplayAttrMgr = nullptr ;
585
586
sCategoryMgr = nullptr ;
587
+ if (RefPtr<TSFEmptyTextStore> emptyTextStore = sEmptyTextStore .forget ()) {
588
+ emptyTextStore->Destroy ();
589
+ MOZ_ASSERT (!sEmptyTextStore );
590
+ }
586
591
sActiveTextStore = nullptr ;
587
592
if (RefPtr<TSFTextStoreBase> textStore = sCurrentTextStore .forget ()) {
588
593
textStore->Destroy ();
@@ -649,34 +654,34 @@ nsresult TSFUtils::OnFocusChange(GotFocus aGotFocus, nsWindow* aFocusedWindow,
649
654
// NOTE: We never associate the document manager of TSFEmptyTextStore with
650
655
// a window handle. Therefore, we don't need to do this if the oldTextStore
651
656
// is not editable.
652
- if (oldTextStore && oldTextStore->IsEditable () &&
653
- oldTextStore->MaybeHasFocus ()) {
654
- const RefPtr<ITfThreadMgr> threadMgr (sThreadMgr );
655
- // If active window is switched, threadMgr has already handled the focus
656
- // change, then, we'll fail AssociateFocus() and the following assertions
657
- // will fail. To avoid the latter, we should check whether the focused
658
- // documentMgr is still what oldTextStore set to.
659
- RefPtr<ITfDocumentMgr> focusedDocumentMgr;
660
- threadMgr->GetFocus (getter_AddRefs (focusedDocumentMgr));
661
- if (focusedDocumentMgr) {
662
- RefPtr<ITfDocumentMgr> prevFocusedDocumentMgr;
663
- DebugOnly<HRESULT> hr = threadMgr->AssociateFocus (
664
- oldTextStore->GetWindow ()->GetWindowHandle (), nullptr ,
665
- getter_AddRefs (prevFocusedDocumentMgr));
666
- NS_WARNING_ASSERTION (SUCCEEDED(hr), "Disassociating focus failed");
667
- NS_ASSERTION (FAILED(hr) ||
668
- prevFocusedDocumentMgr == oldTextStore->GetDocumentMgr(),
669
- nsPrintfCString(" different documentMgr has been associated "
670
- " with the window: expected: %p, but got: %p" ,
671
- oldTextStore->GetDocumentMgr (),
672
- prevFocusedDocumentMgr.get())
673
- .get());
657
+ if (oldTextStore && oldTextStore->IsEditable ()) {
658
+ if (oldTextStore->MaybeHasFocus ()) {
659
+ const RefPtr<ITfThreadMgr> threadMgr (sThreadMgr );
660
+ // If active window is switched, threadMgr has already handled the focus
661
+ // change, then, we'll fail AssociateFocus() and the following assertions
662
+ // will fail. To avoid the latter, we should check whether the focused
663
+ // documentMgr is still what oldTextStore set to.
664
+ RefPtr<ITfDocumentMgr> focusedDocumentMgr;
665
+ threadMgr->GetFocus (getter_AddRefs (focusedDocumentMgr));
666
+ if (focusedDocumentMgr) {
667
+ RefPtr<ITfDocumentMgr> prevFocusedDocumentMgr;
668
+ DebugOnly<HRESULT> hr = threadMgr->AssociateFocus (
669
+ oldTextStore->GetWindow ()->GetWindowHandle (), nullptr ,
670
+ getter_AddRefs (prevFocusedDocumentMgr));
671
+ NS_WARNING_ASSERTION (SUCCEEDED(hr), "Disassociating focus failed");
672
+ NS_ASSERTION (
673
+ FAILED (hr) ||
674
+ prevFocusedDocumentMgr == oldTextStore->GetDocumentMgr(),
675
+ nsPrintfCString(" different documentMgr has been associated "
676
+ " with the window: expected: %p, but got: %p" ,
677
+ oldTextStore->GetDocumentMgr (),
678
+ prevFocusedDocumentMgr.get())
679
+ .get());
680
+ }
674
681
}
675
- }
676
682
677
- // Even if there was a focused TextStore, we won't use it with new focused
678
- // editor. So, release it now.
679
- if (oldTextStore) {
683
+ // Even if there was an editable focused TextStore, we won't use it with new
684
+ // focused editor. So, release it now.
680
685
oldTextStore->Destroy ();
681
686
}
682
687
@@ -701,6 +706,22 @@ nsresult TSFUtils::OnFocusChange(GotFocus aGotFocus, nsWindow* aFocusedWindow,
701
706
if (aFocusedWindow->Destroyed ()) {
702
707
return NS_OK;
703
708
}
709
+ if (!aContext.mIMEState .IsEditable ()) {
710
+ if (RefPtr<TSFEmptyTextStore> emptyTextStore = sEmptyTextStore ) {
711
+ nsresult rv =
712
+ emptyTextStore->SetFocusAndUpdateDocumentURLAndBrowsingMode (
713
+ aFocusedWindow, aContext);
714
+ if (NS_SUCCEEDED(rv)) {
715
+ sCurrentTextStore = emptyTextStore.forget ();
716
+ return NS_OK;
717
+ }
718
+ MOZ_LOG (gIMELog , LogLevel::Error,
719
+ (" TSFUtils::OnFocusChange() FAILED due to the failure of "
720
+ " TSFEmptyTextStore::"
721
+ " SetFocusAndUpdateDocumentURLAndBrowsingMode(), trying to "
722
+ " create new TSFEmptyTextStore..." ));
723
+ }
724
+ }
704
725
Result<RefPtr<TSFEmptyTextStore>, nsresult> ret =
705
726
TSFEmptyTextStore::CreateAndSetFocus (aFocusedWindow, aContext);
706
727
if (NS_WARN_IF(ret.isErr ())) {
@@ -709,7 +730,12 @@ nsresult TSFUtils::OnFocusChange(GotFocus aGotFocus, nsWindow* aFocusedWindow,
709
730
" create and set focus to new TSFEmptyTextStore" ));
710
731
return ret.unwrapErr ();
711
732
}
712
- sCurrentTextStore = ret.unwrap ().forget ();
733
+ if (const RefPtr<TSFEmptyTextStore> oldEmptyTextStore =
734
+ sEmptyTextStore .forget ()) {
735
+ oldEmptyTextStore->Destroy ();
736
+ }
737
+ sEmptyTextStore = ret.unwrap ().forget ();
738
+ sCurrentTextStore = sEmptyTextStore ;
713
739
return NS_OK;
714
740
}
715
741
0 commit comments