Bug 1369697 Create ITfDisplayAttributeMgr and ITfCategoryMgr when TSFTextStore::GetDisplayAttribute() needs to use them at first time r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 08 Jun 2017 20:51:50 +0900
changeset 411288 646b691852bd9bf6d50e2ca2b14daa85a7f8ecdb
parent 411237 7c9d96bbc400aa77f5310486162abf6b61344b74
child 411289 52c02dbc1e23924cc7d999a2ec09ca0345ff5e4f
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1369697
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1369697 Create ITfDisplayAttributeMgr and ITfCategoryMgr when TSFTextStore::GetDisplayAttribute() needs to use them at first time r=m_kato Creating them may be expensive due to allocating them in the heap. So, let's put off to create them when first use. MozReview-Commit-ID: HDgijJo7brU
widget/windows/TSFTextStore.cpp
widget/windows/TSFTextStore.h
--- a/widget/windows/TSFTextStore.cpp
+++ b/widget/windows/TSFTextStore.cpp
@@ -2583,31 +2583,37 @@ TSFTextStore::GetDisplayAttribute(ITfPro
   if (VT_I4 != propValue.vt) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("0x%p   TSFTextStore::GetDisplayAttribute() FAILED due to "
        "ITfProperty::GetValue() returns non-VT_I4 value", this));
     ::VariantClear(&propValue);
     return E_FAIL;
   }
 
-  NS_ENSURE_TRUE(sCategoryMgr, E_FAIL);
+  RefPtr<ITfCategoryMgr> categoryMgr = GetCategoryMgr();
+  if (NS_WARN_IF(!categoryMgr)) {
+    return E_FAIL;
+  }
   GUID guid;
-  hr = sCategoryMgr->GetGUID(DWORD(propValue.lVal), &guid);
+  hr = categoryMgr->GetGUID(DWORD(propValue.lVal), &guid);
   ::VariantClear(&propValue);
   if (FAILED(hr)) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("0x%p   TSFTextStore::GetDisplayAttribute() FAILED due to "
        "ITfCategoryMgr::GetGUID() failed", this));
     return hr;
   }
 
-  NS_ENSURE_TRUE(sDisplayAttrMgr, E_FAIL);
+  RefPtr<ITfDisplayAttributeMgr> displayAttrMgr = GetDisplayAttributeMgr();
+  if (NS_WARN_IF(!displayAttrMgr)) {
+    return E_FAIL;
+  }
   RefPtr<ITfDisplayAttributeInfo> info;
-  hr = sDisplayAttrMgr->GetDisplayAttributeInfo(guid, getter_AddRefs(info),
-                                                nullptr);
+  hr = displayAttrMgr->GetDisplayAttributeInfo(guid, getter_AddRefs(info),
+                                               nullptr);
   if (FAILED(hr) || !info) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("0x%p   TSFTextStore::GetDisplayAttribute() FAILED due to "
        "ITfDisplayAttributeMgr::GetDisplayAttributeInfo() failed", this));
     return hr;
   }
 
   hr = info->GetAttributeInfo(aResult);
@@ -6057,38 +6063,16 @@ TSFTextStore::Initialize()
 
   hr = threadMgr->Activate(&sClientId);
   if (FAILED(hr)) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("  TSFTextStore::Initialize() FAILED to activate, hr=0x%08X", hr));
     return;
   }
 
-  RefPtr<ITfDisplayAttributeMgr> displayAttributeMgr;
-  hr = ::CoCreateInstance(CLSID_TF_DisplayAttributeMgr, nullptr,
-                          CLSCTX_INPROC_SERVER, IID_ITfDisplayAttributeMgr,
-                          getter_AddRefs(displayAttributeMgr));
-  if (FAILED(hr) || !displayAttributeMgr) {
-    MOZ_LOG(sTextStoreLog, LogLevel::Error,
-      ("  TSFTextStore::Initialize() FAILED to create "
-       "a display attribute manager instance, hr=0x%08X", hr));
-    return;
-  }
-
-  RefPtr<ITfCategoryMgr> categoryMgr;
-  hr = ::CoCreateInstance(CLSID_TF_CategoryMgr, nullptr,
-                          CLSCTX_INPROC_SERVER, IID_ITfCategoryMgr,
-                          getter_AddRefs(categoryMgr));
-  if (FAILED(hr) || !categoryMgr) {
-    MOZ_LOG(sTextStoreLog, LogLevel::Error,
-      ("  TSFTextStore::Initialize() FAILED to create "
-       "a category manager instance, hr=0x%08X", hr));
-    return;
-  }
-
   RefPtr<ITfDocumentMgr> disabledDocumentMgr;
   hr = threadMgr->CreateDocumentMgr(getter_AddRefs(disabledDocumentMgr));
   if (FAILED(hr) || !disabledDocumentMgr) {
     MOZ_LOG(sTextStoreLog, LogLevel::Error,
       ("  TSFTextStore::Initialize() FAILED to create "
        "a document manager for disabled mode, hr=0x%08X", hr));
     return;
   }
@@ -6119,18 +6103,16 @@ TSFTextStore::Initialize()
        "instance"));
     return;
   }
 
   sInputProcessorProfiles = inputProcessorProfiles;
   sThreadMgr = threadMgr;
   sMessagePump = messagePump;
   sKeystrokeMgr = keystrokeMgr;
-  sDisplayAttrMgr = displayAttributeMgr;
-  sCategoryMgr = categoryMgr;
   sDisabledDocumentMgr = disabledDocumentMgr;
   sDisabledContext = disabledContext;
 
   sCreateNativeCaretForLegacyATOK =
     Preferences::GetBool("intl.tsf.hack.atok.create_native_caret", true);
   sDoNotReturnNoLayoutErrorToATOKOfCompositionString =
     Preferences::GetBool(
       "intl.tsf.hack.atok.do_not_return_no_layout_error_of_composition_string",
@@ -6161,35 +6143,81 @@ TSFTextStore::Initialize()
     Preferences::GetBool(
       "intl.tsf.hack.ms_simplified_chinese.query_insert_result", true);
   sHackQueryInsertForMSTraditionalTIP =
     Preferences::GetBool(
       "intl.tsf.hack.ms_traditional_chinese.query_insert_result", true);
 
   MOZ_LOG(sTextStoreLog, LogLevel::Info,
     ("  TSFTextStore::Initialize(), sThreadMgr=0x%p, "
-     "sClientId=0x%08X, sDisplayAttrMgr=0x%p, "
-     "sCategoryMgr=0x%p, sDisabledDocumentMgr=0x%p, sDisabledContext=%p, "
+     "sClientId=0x%08X, sDisabledDocumentMgr=0x%p, sDisabledContext=%p, "
      "sCreateNativeCaretForLegacyATOK=%s, "
      "sDoNotReturnNoLayoutErrorToATOKOfCompositionString=%s, "
      "sDoNotReturnNoLayoutErrorToFreeChangJie=%s, "
      "sDoNotReturnNoLayoutErrorToEasyChangjei=%s, "
      "sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar=%s, "
      "sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret=%s",
-     sThreadMgr.get(), sClientId, sDisplayAttrMgr.get(),
-     sCategoryMgr.get(), sDisabledDocumentMgr.get(), sDisabledContext.get(),
+     sThreadMgr.get(), sClientId,
+     sDisabledDocumentMgr.get(), sDisabledContext.get(),
      GetBoolName(sCreateNativeCaretForLegacyATOK),
      GetBoolName(sDoNotReturnNoLayoutErrorToATOKOfCompositionString),
      GetBoolName(sDoNotReturnNoLayoutErrorToFreeChangJie),
      GetBoolName(sDoNotReturnNoLayoutErrorToEasyChangjei),
      GetBoolName(sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar),
      GetBoolName(sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret)));
 }
 
 // static
+already_AddRefed<ITfDisplayAttributeMgr>
+TSFTextStore::GetDisplayAttributeMgr()
+{
+  RefPtr<ITfDisplayAttributeMgr> displayAttributeMgr;
+  if (sDisplayAttrMgr) {
+    displayAttributeMgr = sDisplayAttrMgr;
+    return displayAttributeMgr.forget();
+  }
+
+  HRESULT hr =
+    ::CoCreateInstance(CLSID_TF_DisplayAttributeMgr, nullptr,
+                       CLSCTX_INPROC_SERVER, IID_ITfDisplayAttributeMgr,
+                       getter_AddRefs(displayAttributeMgr));
+  if (NS_WARN_IF(FAILED(hr)) || NS_WARN_IF(!displayAttributeMgr)) {
+    MOZ_LOG(sTextStoreLog, LogLevel::Error,
+      ("TSFTextStore::GetDisplayAttributeMgr() FAILED to create "
+       "a display attribute manager instance, hr=0x%08X", hr));
+    return nullptr;
+  }
+  sDisplayAttrMgr = displayAttributeMgr;
+  return displayAttributeMgr.forget();
+}
+
+// static
+already_AddRefed<ITfCategoryMgr>
+TSFTextStore::GetCategoryMgr()
+{
+  RefPtr<ITfCategoryMgr> categoryMgr;
+  if (sCategoryMgr) {
+    categoryMgr = sCategoryMgr;
+    return categoryMgr.forget();
+  }
+  HRESULT hr =
+    ::CoCreateInstance(CLSID_TF_CategoryMgr, nullptr,
+                       CLSCTX_INPROC_SERVER, IID_ITfCategoryMgr,
+                       getter_AddRefs(categoryMgr));
+  if (NS_WARN_IF(FAILED(hr)) || NS_WARN_IF(!categoryMgr)) {
+    MOZ_LOG(sTextStoreLog, LogLevel::Error,
+      ("TSFTextStore::GetCategoryMgr() FAILED to create "
+       "a category manager instance, hr=0x%08X", hr));
+    return nullptr;
+  }
+  sCategoryMgr = categoryMgr;
+  return categoryMgr.forget();
+}
+
+// static
 void
 TSFTextStore::Terminate()
 {
   MOZ_LOG(sTextStoreLog, LogLevel::Info, ("TSFTextStore::Terminate()"));
 
   TSFStaticSink::Shutdown();
 
   sDisplayAttrMgr = nullptr;
--- a/widget/windows/TSFTextStore.h
+++ b/widget/windows/TSFTextStore.h
@@ -1003,18 +1003,20 @@ protected:
   // TSF thread manager object for the current application
   static StaticRefPtr<ITfThreadMgr> sThreadMgr;
   // sMessagePump is QI'ed from sThreadMgr
   static StaticRefPtr<ITfMessagePump> sMessagePump;
   // 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();
 
   // Current text store which is managing a keyboard enabled editor (i.e.,
   // editable editor).  Currently only ONE TSFTextStore instance is ever used,
   // although Create is called when an editor is focused and Destroy called
   // when the focused editor is blurred.
   static StaticRefPtr<TSFTextStore> sEnabledTextStore;
 
   // For IME (keyboard) disabled state: