diff --git a/widget/ContentCache.cpp b/widget/ContentCache.cpp index ece057c42594..df5748cb8a04 100644 --- a/widget/ContentCache.cpp +++ b/widget/ContentCache.cpp @@ -76,6 +76,41 @@ bool ContentCache::IsValid() const { return true; } +void ContentCache::AssertIfInvalid() const { +#if MOZ_DIAGNOSTIC_ASSERT_ENABLED + if (IsValid()) { + return; + } + + // This text will appear in the crash reports without any permissions. + // Do not use `ToString` here to avoid to expose unexpected data with + // changing the type or `operator<<()`. + nsPrintfCString info( + "ContentCache={ mText=%s, mSelection=%s, mCaret=%s, mTextRectArray=%s, " + "mCompositionStart=%s }\n", + // Don't expose mText.ref() value for protecting the user's privacy. + mText.isNothing() + ? "Nothing" + : nsPrintfCString("{ Length()=%zu }", mText->Length()).get(), + mSelection.isNothing() + ? "Nothing" + : nsPrintfCString("{ mAnchor=%u, mFocus=%u }", mSelection->mAnchor, + mSelection->mFocus) + .get(), + mCaret.isNothing() + ? "Nothing" + : nsPrintfCString("{ mOffset=%u }", mCaret->mOffset).get(), + mTextRectArray.isNothing() + ? "Nothing" + : nsPrintfCString("{ Length()=%u }", mTextRectArray->Length()).get(), + mCompositionStart.isNothing() + ? "Nothing" + : nsPrintfCString("%u", mCompositionStart.value()).get()); + CrashReporter::AppendAppNotesToCrashReport(info); + MOZ_DIAGNOSTIC_ASSERT(false, "Invalid ContentCache data"); +#endif // #if MOZ_DIAGNOSTIC_ASSERT_ENABLED +} + /***************************************************************************** * mozilla::ContentCacheInChild *****************************************************************************/ @@ -148,7 +183,7 @@ bool ContentCacheInChild::CacheAll(nsIWidget* aWidget, const bool textCached = CacheText(aWidget, aNotification); const bool editorRectCached = CacheEditorRect(aWidget, aNotification); - MOZ_DIAGNOSTIC_ASSERT(IsValid()); + AssertIfInvalid(); return (textCached || editorRectCached) && IsValid(); } @@ -190,7 +225,7 @@ bool ContentCacheInChild::CacheSelection(nsIWidget* aWidget, ("0x%p CacheSelection(), FAILED, editable content had already been " "blurred", this)); - MOZ_DIAGNOSTIC_ASSERT(IsValid()); + AssertIfInvalid(); return false; } else { mSelection.emplace(querySelectedTextEvent); @@ -233,7 +268,7 @@ bool ContentCacheInChild::CacheCaret(nsIWidget* aWidget, MOZ_LOG(sContentCacheLog, LogLevel::Info, ("0x%p CacheCaret(), Succeeded, mSelection=%s, mCaret=%s", this, ToString(mSelection).c_str(), ToString(mCaret).c_str())); - MOZ_DIAGNOSTIC_ASSERT(IsValid()); + AssertIfInvalid(); return IsValid(); } @@ -278,7 +313,7 @@ bool ContentCacheInChild::CacheCaretAndTextRects( const bool caretCached = CacheCaret(aWidget, aNotification); const bool textRectsCached = CacheTextRects(aWidget, aNotification); - MOZ_DIAGNOSTIC_ASSERT(IsValid()); + AssertIfInvalid(); return (caretCached || textRectsCached) && IsValid(); } @@ -340,7 +375,7 @@ bool ContentCacheInChild::CacheText(nsIWidget* aWidget, mSelection.reset(); mCaret.reset(); mTextRectArray.reset(); - MOZ_DIAGNOSTIC_ASSERT(IsValid()); + AssertIfInvalid(); return false; } @@ -615,7 +650,7 @@ bool ContentCacheInChild::CacheTextRects(nsIWidget* aWidget, ToString(mTextRectArray).c_str(), ToString(mSelection).c_str(), ToString(mFirstCharRect).c_str(), ToString(mLastCommitStringTextRectArray).c_str())); - MOZ_DIAGNOSTIC_ASSERT(IsValid()); + AssertIfInvalid(); return IsValid(); } diff --git a/widget/ContentCache.h b/widget/ContentCache.h index 332ff9ff4312..3f8b48425ede 100644 --- a/widget/ContentCache.h +++ b/widget/ContentCache.h @@ -48,6 +48,8 @@ class ContentCache { [[nodiscard]] bool IsValid() const; protected: + void AssertIfInvalid() const; + // Whole text in the target Maybe mText;