Bug 1312302 - Set dummy Selection during initializing TextStore. r=masayuki a=ritu
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Tue, 25 Oct 2016 17:53:38 +0900
changeset 356395 b44e1445660d445ff883584de9de069a765760e8
parent 356394 27607838026a6e1a3185c1e18b5cdaa7405da592
child 356396 2eef3810f7f87cccd3745724cf69b2ea24cde7e0
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmasayuki, ritu
bugs1312302
milestone51.0a2
Bug 1312302 - Set dummy Selection during initializing TextStore. r=masayuki a=ritu When PC supports table mode, TextInputFramme.dll is loaded and it can be used for TIP. When creating new TextStore object, selection might be nothing yet on e10s. At this time, GetSelection will return E_FAIL since selection data isn't received yet. If GetSelection returns error during SetFocus, TextInputFrame.dll will crash. So we set temporary selection to avoid crash. MozReview-Commit-ID: HyVSwvhXGJh
widget/windows/TSFTextStore.cpp
widget/windows/TSFTextStore.h
--- a/widget/windows/TSFTextStore.cpp
+++ b/widget/windows/TSFTextStore.cpp
@@ -4777,17 +4777,27 @@ TSFTextStore::CreateAndSetFocus(nsWindow
   if (aContext.mIMEState.mEnabled == IMEState::PASSWORD) {
     MarkContextAsKeyboardDisabled(textStore->mContext);
     RefPtr<ITfContext> topContext;
     textStore->mDocumentMgr->GetTop(getter_AddRefs(topContext));
     if (topContext && topContext != textStore->mContext) {
       MarkContextAsKeyboardDisabled(topContext);
     }
   }
-  HRESULT hr = sThreadMgr->SetFocus(textStore->mDocumentMgr);
+
+  HRESULT hr;
+  {
+    // Windows 10's softwware keyboard requires that SetSelection must be
+    // always successful into SetFocus.  If returning error, it might crash
+    // into TextInputFramework.dll.
+    AutoSetTemporarySelection setSelection(textStore->SelectionForTSFRef());
+
+    hr = sThreadMgr->SetFocus(textStore->mDocumentMgr);
+  }
+
   if (NS_WARN_IF(FAILED(hr))) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("  TSFTextStore::CreateAndSetFocus() FAILED due to "
        "ITfTheadMgr::SetFocus() failure"));
     return false;
   }
   // Use AssociateFocus() for ensuring that any native focus event
   // never steal focus from our documentMgr.
--- a/widget/windows/TSFTextStore.h
+++ b/widget/windows/TSFTextStore.h
@@ -588,16 +588,40 @@ protected:
    * Get the selection expected by TSF.  If mSelectionForTSF is already valid,
    * this just return the reference to it.  Otherwise, this initializes it
    * with eQuerySelectedText.  Please check if the result is valid before
    * actually using it.
    * Note that this is also called by ContentForTSFRef().
    */
   Selection& SelectionForTSFRef();
 
+  class MOZ_STACK_CLASS AutoSetTemporarySelection final
+  {
+  public:
+    AutoSetTemporarySelection(Selection& aSelection)
+      : mSelection(aSelection)
+    {
+      mDirty = mSelection.IsDirty();
+      if (mDirty) {
+        mSelection.CollapseAt(0);
+      }
+    }
+
+    ~AutoSetTemporarySelection()
+    {
+      if (mDirty) {
+        mSelection.MarkDirty();
+      }
+    }
+
+ private:
+    Selection& mSelection;
+    bool mDirty;
+  };
+
   struct PendingAction final
   {
     enum ActionType : uint8_t
     {
       COMPOSITION_START,
       COMPOSITION_UPDATE,
       COMPOSITION_END,
       SET_SELECTION