Bug 1369696 - part 2: TSFTextStore::sMessagePump should be QIed from TSFTextStore::sThreadMgr at first use r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 13 Jun 2017 19:38:03 +0900
changeset 593816 8c4a46cf4a708253275645787a66f90575690297
parent 593815 613574c225a336eca0572dd24b0a735435e13600
child 593832 d0f70123a94499858bcbc3f6b83dbe1e075fe587
child 593834 f82d4bcdf66d5f4fd1b0129db99cd1b9499e5916
child 593837 6c4d1946b768c4d384fd936bc46638cb29e9c4a8
push id63818
push usermasayuki@d-toybox.com
push dateWed, 14 Jun 2017 06:14:22 +0000
reviewersm_kato
bugs1369696
milestone56.0a1
Bug 1369696 - part 2: TSFTextStore::sMessagePump should be QIed from TSFTextStore::sThreadMgr at first use r?m_kato sMessagePump is necessary only when WinUtils::(Get|Peek)Message() retrieves a message from the queue. Therefore, we can put off to initialize it until then. MozReview-Commit-ID: ByMJk6AIw1r
widget/windows/TSFTextStore.cpp
widget/windows/TSFTextStore.h
--- a/widget/windows/TSFTextStore.cpp
+++ b/widget/windows/TSFTextStore.cpp
@@ -6147,26 +6147,16 @@ TSFTextStore::Initialize()
                                   getter_AddRefs(threadMgr));
   if (FAILED(hr) || !threadMgr) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("  TSFTextStore::Initialize() FAILED to "
        "create the thread manager, hr=0x%08X", hr));
     return;
   }
 
-  RefPtr<ITfMessagePump> messagePump;
-  hr = threadMgr->QueryInterface(IID_ITfMessagePump,
-                                 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;
-  }
-
   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;
@@ -6189,17 +6179,16 @@ TSFTextStore::Initialize()
        "a context for disabled mode, hr=0x%08X", hr));
     return;
   }
 
   MarkContextAsKeyboardDisabled(disabledContext);
   MarkContextAsEmpty(disabledContext);
 
   sThreadMgr = threadMgr;
-  sMessagePump = messagePump;
   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()));
@@ -6209,16 +6198,49 @@ TSFTextStore::Initialize()
 already_AddRefed<ITfThreadMgr>
 TSFTextStore::GetThreadMgr()
 {
   RefPtr<ITfThreadMgr> threadMgr = sThreadMgr;
   return threadMgr.forget();
 }
 
 // static
+already_AddRefed<ITfMessagePump>
+TSFTextStore::GetMessagePump()
+{
+  static bool sInitialized = false;
+  if (!sThreadMgr) {
+    return nullptr;
+  }
+  if (sMessagePump) {
+    RefPtr<ITfMessagePump> messagePump = sMessagePump;
+    return messagePump.forget();
+  }
+  // If it tried to retrieve ITfMessagePump from sThreadMgr but it failed,
+  // we shouldn't retry it at every message due to performance reason.
+  // Although this shouldn't occur actually.
+  if (sInitialized) {
+    return nullptr;
+  }
+  sInitialized = true;
+
+  RefPtr<ITfMessagePump> messagePump;
+  HRESULT hr = sThreadMgr->QueryInterface(IID_ITfMessagePump,
+                                          getter_AddRefs(messagePump));
+  if (NS_WARN_IF(FAILED(hr)) || NS_WARN_IF(!messagePump)) {
+    MOZ_LOG(sTextStoreLog, LogLevel::Error,
+      ("TSFTextStore::GetMessagePump() FAILED to "
+       "QI message pump from the thread manager, hr=0x%08X", hr));
+    return nullptr;
+  }
+  sMessagePump = messagePump;
+  return messagePump.forget();
+}
+
+// static
 already_AddRefed<ITfDisplayAttributeMgr>
 TSFTextStore::GetDisplayAttributeMgr()
 {
   RefPtr<ITfDisplayAttributeMgr> displayAttributeMgr;
   if (sDisplayAttrMgr) {
     displayAttributeMgr = sDisplayAttrMgr;
     return displayAttributeMgr.forget();
   }
--- a/widget/windows/TSFTextStore.h
+++ b/widget/windows/TSFTextStore.h
@@ -205,21 +205,16 @@ public:
         return static_cast<void*>(&sCategoryMgr);
       case NS_NATIVE_TSF_DISPLAY_ATTR_MGR:
         return static_cast<void*>(&sDisplayAttrMgr);
       default:
         return nullptr;
     }
   }
 
-  static ITfMessagePump* GetMessagePump()
-  {
-    return sMessagePump;
-  }
-
   static void* GetThreadManager()
   {
     return static_cast<void*>(sThreadMgr);
   }
 
   static bool ThinksHavingFocus()
   {
     return (sEnabledTextStore && sEnabledTextStore->mContext);
@@ -1002,16 +997,20 @@ protected:
   bool                         mBeingDestroyed;
 
 
   // TSF thread manager object for the current application
   static StaticRefPtr<ITfThreadMgr> sThreadMgr;
   static already_AddRefed<ITfThreadMgr> GetThreadMgr();
   // sMessagePump is QI'ed from sThreadMgr
   static StaticRefPtr<ITfMessagePump> sMessagePump;
+public:
+  // Expose GetMessagePump() for WinUtils.
+  static already_AddRefed<ITfMessagePump> GetMessagePump();
+private:
   // sKeystrokeMgr is QI'ed from sThreadMgr
   static StaticRefPtr<ITfKeystrokeMgr> sKeystrokeMgr;
   // TSF display attribute manager
   static StaticRefPtr<ITfDisplayAttributeMgr> sDisplayAttrMgr;
   static already_AddRefed<ITfDisplayAttributeMgr> GetDisplayAttributeMgr();
   // TSF category manager
   static StaticRefPtr<ITfCategoryMgr> sCategoryMgr;
   static already_AddRefed<ITfCategoryMgr> GetCategoryMgr();