Bug 1369696 - part 1: TSFTextStore::sKeystrokeMgr should be QIed from TSFTextStore::sThreadMgr at first use r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 13 Jun 2017 19:04:43 +0900
changeset 593815 613574c225a336eca0572dd24b0a735435e13600
parent 593811 e1392ead5a2213a44b298945fdc5b2c9f82aaeb5
child 593816 8c4a46cf4a708253275645787a66f90575690297
push id63818
push usermasayuki@d-toybox.com
push dateWed, 14 Jun 2017 06:14:22 +0000
reviewersm_kato
bugs1369696
milestone56.0a1
Bug 1369696 - part 1: TSFTextStore::sKeystrokeMgr should be QIed from TSFTextStore::sThreadMgr at first use r?m_kato sKeystrokeMgr is required only when WM_KEYDOWN or WM_KEYUP message are received. So, we can put off to initialize sKeystrokeMgr until then. MozReview-Commit-ID: JsLeM0SYXG6
widget/windows/TSFTextStore.cpp
--- a/widget/windows/TSFTextStore.cpp
+++ b/widget/windows/TSFTextStore.cpp
@@ -6157,26 +6157,16 @@ TSFTextStore::Initialize()
                                  getter_AddRefs(messagePump));
   if (FAILED(hr) || !messagePump) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("  TSFTextStore::Initialize() FAILED to "
        "QI message pump from the thread manager, hr=0x%08X", hr));
     return;
   }
 
-  RefPtr<ITfKeystrokeMgr> keystrokeMgr;
-  hr = threadMgr->QueryInterface(IID_ITfKeystrokeMgr,
-                                 getter_AddRefs(keystrokeMgr));
-  if (FAILED(hr) || !keystrokeMgr) {
-    MOZ_LOG(sTextStoreLog, LogLevel::Error,
-      ("  TSFTextStore::Initialize() FAILED to "
-       "QI keystroke manager from the thread manager, hr=0x%08X", hr));
-    return;
-  }
-
   hr = threadMgr->Activate(&sClientId);
   if (FAILED(hr)) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("  TSFTextStore::Initialize() FAILED to activate, hr=0x%08X", hr));
     return;
   }
 
   RefPtr<ITfDocumentMgr> disabledDocumentMgr;
@@ -6200,17 +6190,16 @@ TSFTextStore::Initialize()
     return;
   }
 
   MarkContextAsKeyboardDisabled(disabledContext);
   MarkContextAsEmpty(disabledContext);
 
   sThreadMgr = threadMgr;
   sMessagePump = messagePump;
-  sKeystrokeMgr = keystrokeMgr;
   sDisabledDocumentMgr = disabledDocumentMgr;
   sDisabledContext = disabledContext;
 
   MOZ_LOG(sTextStoreLog, LogLevel::Info,
     ("  TSFTextStore::Initialize(), sThreadMgr=0x%p, "
      "sClientId=0x%08X, sDisabledDocumentMgr=0x%p, sDisabledContext=%p",
      sThreadMgr.get(), sClientId,
      sDisabledDocumentMgr.get(), sDisabledContext.get()));
@@ -6321,18 +6310,38 @@ TSFTextStore::Terminate()
     sKeystrokeMgr = nullptr;
   }
 }
 
 // static
 bool
 TSFTextStore::ProcessRawKeyMessage(const MSG& aMsg)
 {
+  if (!sThreadMgr) {
+    return false; // not in TSF mode
+  }
+  static bool sInitialized = false;
   if (!sKeystrokeMgr) {
-    return false; // not in TSF mode
+    // If it tried to retrieve ITfKeystrokeMgr from sThreadMgr but it failed,
+    // we shouldn't retry it at every keydown nor keyup due to performance
+    // reason.  Although this shouldn't occur actually.
+    if (sInitialized) {
+      return false;
+    }
+    sInitialized = true;
+    RefPtr<ITfKeystrokeMgr> keystrokeMgr;
+    HRESULT hr = sThreadMgr->QueryInterface(IID_ITfKeystrokeMgr,
+                                            getter_AddRefs(keystrokeMgr));
+    if (NS_WARN_IF(FAILED(hr)) || NS_WARN_IF(!keystrokeMgr)) {
+      MOZ_LOG(sTextStoreLog, LogLevel::Error,
+        ("TSFTextStore::ProcessRawKeyMessage() FAILED to "
+         "QI keystroke manager from the thread manager, hr=0x%08X", hr));
+      return false;
+    }
+    sKeystrokeMgr = keystrokeMgr.forget();
   }
 
   if (aMsg.message == WM_KEYDOWN) {
     BOOL eaten;
     RefPtr<ITfKeystrokeMgr> keystrokeMgr = sKeystrokeMgr;
     HRESULT hr = keystrokeMgr->TestKeyDown(aMsg.wParam, aMsg.lParam, &eaten);
     if (FAILED(hr) || !sKeystrokeMgr || !eaten) {
       return false;