Bug 1369694 Create TSFPrefs class and make it cache each pref at first use r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Sat, 10 Jun 2017 04:19:27 +0900
changeset 592454 063c5850f99c582d0319e7a99db99001dd4ae5c0
parent 592453 8257f5c3934005ee4699c653e2c4887e0dc34446
child 632831 d744caa08286f4da819f261a0c1661663ea2cc4d
push id63401
push usermasayuki@d-toybox.com
push dateMon, 12 Jun 2017 09:07:08 +0000
reviewersm_kato
bugs1369694
milestone55.0a1
Bug 1369694 Create TSFPrefs class and make it cache each pref at first use r?m_kato Getting all prefs for TSFTextStore during initializing may make damage to start up performance. So, each one should be retrieved when the one is actually necessary. This patch creates TSFPrefs (I like better to name it TSFPreferences, but such long name isn't better when calling long name methods.) and implemented by simple macro. MozReview-Commit-ID: A01LEAW4E7i
widget/windows/TSFTextStore.cpp
widget/windows/TSFTextStore.h
--- a/widget/windows/TSFTextStore.cpp
+++ b/widget/windows/TSFTextStore.cpp
@@ -1543,41 +1543,79 @@ TSFStaticSink::IsTIPCategoryKeyboard(REF
         profile.catid == GUID_TFCAT_TIP_KEYBOARD) {
       return true;
     }
   }
   return false;
 }
 
 /******************************************************************/
+/* TSFPreference                                                  */
+/******************************************************************/
+
+class TSFPrefs final
+{
+public:
+#define DECL_AND_IMPL_BOOL_PREF(aPref, aName, aDefaultValue)                   \
+  static bool aName ()                                                         \
+  {                                                                            \
+    static bool s ## aName ## Value =                                          \
+      Preferences::GetBool(aPref, aDefaultValue);                              \
+    return s ## aName ## Value;                                                \
+  }
+
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.atok.create_native_caret",
+    NeedToCreateNativeCaretForLegacyATOK, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.atok.do_not_return_no_layout_error_of_composition_string",
+    DoNotReturnNoLayoutErrorToATOKOfCompositionString, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.ms_simplified_chinese.do_not_return_no_layout_error",
+    DoNotReturnNoLayoutErrorToMSSimplifiedTIP, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.ms_traditional_chinese.do_not_return_no_layout_error",
+    DoNotReturnNoLayoutErrorToMSTraditionalTIP, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.free_chang_jie.do_not_return_no_layout_error",
+    DoNotReturnNoLayoutErrorToFreeChangJie, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.easy_changjei.do_not_return_no_layout_error",
+    DoNotReturnNoLayoutErrorToEasyChangjei, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.ms_japanese_ime.do_not_return_no_layout_error_at_first_char",
+    DoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.ms_japanese_ime.do_not_return_no_layout_error_at_caret",
+    DoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.ms_simplified_chinese.query_insert_result",
+    NeedToHackQueryInsertForMSSimplifiedTIP, true)
+  DECL_AND_IMPL_BOOL_PREF(
+    "intl.tsf.hack.ms_traditional_chinese.query_insert_result",
+    NeedToHackQueryInsertForMSTraditionalTIP, true)
+
+#undef DECL_AND_IMPL_BOOL_PREF
+};
+
+/******************************************************************/
 /* TSFTextStore                                                   */
 /******************************************************************/
 
 StaticRefPtr<ITfThreadMgr> TSFTextStore::sThreadMgr;
 StaticRefPtr<ITfMessagePump> TSFTextStore::sMessagePump;
 StaticRefPtr<ITfKeystrokeMgr> TSFTextStore::sKeystrokeMgr;
 StaticRefPtr<ITfDisplayAttributeMgr> TSFTextStore::sDisplayAttrMgr;
 StaticRefPtr<ITfCategoryMgr> TSFTextStore::sCategoryMgr;
 StaticRefPtr<ITfDocumentMgr> TSFTextStore::sDisabledDocumentMgr;
 StaticRefPtr<ITfContext> TSFTextStore::sDisabledContext;
 StaticRefPtr<ITfInputProcessorProfiles> TSFTextStore::sInputProcessorProfiles;
 StaticRefPtr<TSFTextStore> TSFTextStore::sEnabledTextStore;
 DWORD TSFTextStore::sClientId  = 0;
 
-bool TSFTextStore::sCreateNativeCaretForLegacyATOK = false;
-bool TSFTextStore::sDoNotReturnNoLayoutErrorToATOKOfCompositionString = false;
-bool TSFTextStore::sDoNotReturnNoLayoutErrorToMSSimplifiedTIP = false;
-bool TSFTextStore::sDoNotReturnNoLayoutErrorToMSTraditionalTIP = false;
-bool TSFTextStore::sDoNotReturnNoLayoutErrorToFreeChangJie = false;
-bool TSFTextStore::sDoNotReturnNoLayoutErrorToEasyChangjei = false;
-bool TSFTextStore::sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar = false;
-bool TSFTextStore::sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret = false;
-bool TSFTextStore::sHackQueryInsertForMSSimplifiedTIP = false;
-bool TSFTextStore::sHackQueryInsertForMSTraditionalTIP = false;
-
 #define TEXTSTORE_DEFAULT_VIEW (1)
 
 TSFTextStore::TSFTextStore()
   : mEditCookie(0)
   , mSinkMask(0)
   , mLock(0)
   , mLockQueued(0)
   , mHandlingKeyMessage(0)
@@ -2352,20 +2390,20 @@ TSFTextStore::QueryInsert(LONG acpTestSt
       ("0x%p   TSFTextStore::QueryInsert() FAILED due to "
        "wrong argument", this));
     return E_INVALIDARG;
   }
 
   // XXX need to adjust to cluster boundary
   // Assume we are given good offsets for now
   if (IsWin8OrLater() && !mComposition.IsComposing() &&
-      ((sHackQueryInsertForMSTraditionalTIP &&
+      ((TSFPrefs::NeedToHackQueryInsertForMSTraditionalTIP() &&
          (TSFStaticSink::IsMSChangJieActive() ||
           TSFStaticSink::IsMSQuickQuickActive())) ||
-       (sHackQueryInsertForMSSimplifiedTIP &&
+       (TSFPrefs::NeedToHackQueryInsertForMSSimplifiedTIP() &&
          (TSFStaticSink::IsMSPinyinActive() ||
           TSFStaticSink::IsMSWubiActive())))) {
     MOZ_LOG(sTextStoreLog, LogLevel::Warning,
       ("0x%p   TSFTextStore::QueryInsert() WARNING using different "
        "result for the TIP", this));
     // Chinese TIPs of Microsoft assume that QueryInsert() returns selected
     // range which should be removed.
     *pacpResultStart = acpTestStart;
@@ -4051,36 +4089,36 @@ TSFTextStore::GetTextExt(TsViewCookie vc
     const bool kIsMSOfficeJapaneseIME2010 =
       TSFStaticSink::IsMSOfficeJapaneseIME2010Active();
     // MS IME for Japanese doesn't support asynchronous handling at deciding
     // its suggest list window position.  The feature was implemented
     // starting from Windows 8.  And also we may meet same trouble in e10s
     // mode on Win7.  So, we should never return TS_E_NOLAYOUT to MS IME for
     // Japanese.
     if (kIsMSOfficeJapaneseIME2010 ||
-        ((sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar ||
-          sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret) &&
+        ((TSFPrefs::DoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar() ||
+          TSFPrefs::DoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret()) &&
          TSFStaticSink::IsMSJapaneseIMEActive())) {
       // Basically, MS-IME tries to retrieve whole composition string rect
       // at deciding suggest window immediately after unlocking the document.
       // However, in e10s mode, the content hasn't updated yet in most cases.
       // Therefore, if the first character at the retrieving range rect is
       // available, we should use it as the result.
       if ((kIsMSOfficeJapaneseIME2010 ||
-           sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar) &&
+           TSFPrefs::DoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar()) &&
           acpStart < acpEnd) {
         acpEnd = acpStart;
         dontReturnNoLayoutError = true;
       }
       // Although, the condition is not clear, MS-IME sometimes retrieves the
       // caret rect immediately after modifying the composition string but
       // before unlocking the document.  In such case, we should return the
       // nearest character rect.
       else if ((kIsMSOfficeJapaneseIME2010 ||
-                sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret) &&
+                TSFPrefs::DoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret()) &&
                acpStart == acpEnd &&
                selectionForTSF.IsCollapsed() &&
                selectionForTSF.EndOffset() == acpEnd) {
         if (mContentForTSF.MinOffsetOfLayoutChanged() > LONG_MAX) {
           MOZ_LOG(sTextStoreLog, LogLevel::Error,
             ("0x%p   TSFTextStore::GetTextExt(), FAILED due to the text "
              "is too big for TSF (cannot treat modified offset as LONG), "
              "mContentForTSF.MinOffsetOfLayoutChanged()=%u",
@@ -4096,45 +4134,46 @@ TSFTextStore::GetTextExt(TsViewCookie vc
     // ATOK fails to handle TS_E_NOLAYOUT only when it decides the position of
     // suggest window.  In such case, ATOK tries to query rect of whole
     // composition string.
     // XXX For testing with legacy ATOK, we should hack it even if current ATOK
     //     refers native caret rect on windows whose window class is one of
     //     Mozilla window classes and we stop creating native caret for ATOK
     //     because creating native caret causes ATOK refers caret position
     //     when GetTextExt() returns TS_E_NOLAYOUT.
-    else if (sDoNotReturnNoLayoutErrorToATOKOfCompositionString &&
+    else if (TSFPrefs::DoNotReturnNoLayoutErrorToATOKOfCompositionString() &&
              TSFStaticSink::IsATOKActive() &&
              (!TSFStaticSink::IsATOKReferringNativeCaretActive() ||
-              !sCreateNativeCaretForLegacyATOK) &&
+              !TSFPrefs::NeedToCreateNativeCaretForLegacyATOK()) &&
              mComposition.mStart == acpStart &&
              mComposition.EndOffset() == acpEnd) {
       dontReturnNoLayoutError = true;
     }
     // Free ChangJie 2010 and Easy Changjei 1.0.12.0 doesn't handle
     // ITfContextView::GetTextExt() properly.  Prehaps, it's due to the bug of
     // TSF.  We need to check if this is necessary on Windows 10 before
     // disabling this on Windows 10.
-    else if ((sDoNotReturnNoLayoutErrorToFreeChangJie &&
+    else if ((TSFPrefs::DoNotReturnNoLayoutErrorToFreeChangJie() &&
               TSFStaticSink::IsFreeChangJieActive()) ||
-             (sDoNotReturnNoLayoutErrorToEasyChangjei &&
+             (TSFPrefs::DoNotReturnNoLayoutErrorToEasyChangjei() &&
               TSFStaticSink::IsEasyChangjeiActive())) {
       acpEnd = mComposition.mStart;
       acpStart = std::min(acpStart, acpEnd);
       dontReturnNoLayoutError = true;
     }
     // Some Chinese TIPs of Microsoft doesn't show candidate window in e10s
     // mode on Win8 or later.
-    else if (IsWin8OrLater() &&
-             ((sDoNotReturnNoLayoutErrorToMSTraditionalTIP &&
-               (TSFStaticSink::IsMSChangJieActive() ||
-                TSFStaticSink::IsMSQuickQuickActive())) ||
-              (sDoNotReturnNoLayoutErrorToMSSimplifiedTIP &&
-                (TSFStaticSink::IsMSPinyinActive() ||
-                 TSFStaticSink::IsMSWubiActive())))) {
+    else if (
+      IsWin8OrLater() &&
+      ((TSFPrefs::DoNotReturnNoLayoutErrorToMSTraditionalTIP() &&
+        (TSFStaticSink::IsMSChangJieActive() ||
+         TSFStaticSink::IsMSQuickQuickActive())) ||
+       (TSFPrefs::DoNotReturnNoLayoutErrorToMSSimplifiedTIP() &&
+         (TSFStaticSink::IsMSPinyinActive() ||
+          TSFStaticSink::IsMSWubiActive())))) {
       acpEnd = mComposition.mStart;
       acpStart = std::min(acpStart, acpEnd);
       dontReturnNoLayoutError = true;
     }
 
     // If we hack the queried range for active TIP, that means we should not
     // return TS_E_NOLAYOUT even if hacked offset is still modified.  So, as
     // far as possible, we should adjust the offset.
@@ -4273,17 +4312,17 @@ TSFTextStore::GetTextExt(TsViewCookie vc
 
   // not equal if text rect was clipped
   *pfClipped = !::EqualRect(prc, &textRect);
 
   // ATOK 2011 - 2016 refers native caret position and size on windows whose
   // class name is one of Mozilla's windows for deciding candidate window
   // position.  Therefore, we need to create native caret only when ATOK 2011 -
   // 2016 is active.
-  if (sCreateNativeCaretForLegacyATOK &&
+  if (TSFPrefs::NeedToCreateNativeCaretForLegacyATOK() &&
       TSFStaticSink::IsATOKReferringNativeCaretActive() &&
       mComposition.IsComposing() &&
       mComposition.mStart <= acpStart && mComposition.EndOffset() >= acpStart &&
       mComposition.mStart <= acpEnd && mComposition.EndOffset() >= acpEnd) {
     CreateNativeCaret();
   }
 
   MOZ_LOG(sTextStoreLog, LogLevel::Info,
@@ -6139,68 +6178,21 @@ TSFTextStore::Initialize()
   MarkContextAsEmpty(disabledContext);
 
   sThreadMgr = threadMgr;
   sMessagePump = messagePump;
   sKeystrokeMgr = keystrokeMgr;
   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",
-      true);
-  sDoNotReturnNoLayoutErrorToMSSimplifiedTIP =
-    Preferences::GetBool(
-      "intl.tsf.hack.ms_simplified_chinese.do_not_return_no_layout_error",
-      true);
-  sDoNotReturnNoLayoutErrorToMSTraditionalTIP =
-    Preferences::GetBool(
-      "intl.tsf.hack.ms_traditional_chinese.do_not_return_no_layout_error",
-      true);
-  sDoNotReturnNoLayoutErrorToFreeChangJie =
-    Preferences::GetBool(
-      "intl.tsf.hack.free_chang_jie.do_not_return_no_layout_error", true);
-  sDoNotReturnNoLayoutErrorToEasyChangjei =
-    Preferences::GetBool(
-      "intl.tsf.hack.easy_changjei.do_not_return_no_layout_error", true);
-  sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar =
-    Preferences::GetBool(
-      "intl.tsf.hack.ms_japanese_ime."
-      "do_not_return_no_layout_error_at_first_char", true);
-  sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret =
-    Preferences::GetBool(
-      "intl.tsf.hack.ms_japanese_ime.do_not_return_no_layout_error_at_caret",
-      true);
-  sHackQueryInsertForMSSimplifiedTIP =
-    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, sDisabledDocumentMgr=0x%p, sDisabledContext=%p, "
-     "sCreateNativeCaretForLegacyATOK=%s, "
-     "sDoNotReturnNoLayoutErrorToATOKOfCompositionString=%s, "
-     "sDoNotReturnNoLayoutErrorToFreeChangJie=%s, "
-     "sDoNotReturnNoLayoutErrorToEasyChangjei=%s, "
-     "sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar=%s, "
-     "sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret=%s",
+     "sClientId=0x%08X, sDisabledDocumentMgr=0x%p, sDisabledContext=%p",
      sThreadMgr.get(), sClientId,
-     sDisabledDocumentMgr.get(), sDisabledContext.get(),
-     GetBoolName(sCreateNativeCaretForLegacyATOK),
-     GetBoolName(sDoNotReturnNoLayoutErrorToATOKOfCompositionString),
-     GetBoolName(sDoNotReturnNoLayoutErrorToFreeChangJie),
-     GetBoolName(sDoNotReturnNoLayoutErrorToEasyChangjei),
-     GetBoolName(sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar),
-     GetBoolName(sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret)));
+     sDisabledDocumentMgr.get(), sDisabledContext.get()));
 }
 
 // static
 already_AddRefed<ITfThreadMgr>
 TSFTextStore::GetThreadMgr()
 {
   RefPtr<ITfThreadMgr> threadMgr = sThreadMgr;
   return threadMgr.forget();
--- a/widget/windows/TSFTextStore.h
+++ b/widget/windows/TSFTextStore.h
@@ -1027,26 +1027,14 @@ protected:
   static StaticRefPtr<ITfContext> sDisabledContext;
 
   static StaticRefPtr<ITfInputProcessorProfiles> sInputProcessorProfiles;
   static already_AddRefed<ITfInputProcessorProfiles>
            GetInputProcessorProfiles();
 
   // TSF client ID for the current application
   static DWORD sClientId;
-
-  // Enables/Disables hack for specific TIP.
-  static bool sCreateNativeCaretForLegacyATOK;
-  static bool sDoNotReturnNoLayoutErrorToATOKOfCompositionString;
-  static bool sDoNotReturnNoLayoutErrorToMSSimplifiedTIP;
-  static bool sDoNotReturnNoLayoutErrorToMSTraditionalTIP;
-  static bool sDoNotReturnNoLayoutErrorToFreeChangJie;
-  static bool sDoNotReturnNoLayoutErrorToEasyChangjei;
-  static bool sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtFirstChar;
-  static bool sDoNotReturnNoLayoutErrorToMSJapaneseIMEAtCaret;
-  static bool sHackQueryInsertForMSSimplifiedTIP;
-  static bool sHackQueryInsertForMSTraditionalTIP;
 };
 
 } // namespace widget
 } // namespace mozilla
 
 #endif // #ifndef TSFTextStore_h_