Bug 1841466 - Make ContentCacheInChild assert invalid cases with better message r=m_kato

The assertion reason "MOZ_DIAGNOSTIC_ASSERT(IsValid())" does not help us to
debug.  Make it assert with the minimum data of `ContentCache` instead.

Depends on D182652

Differential Revision: https://phabricator.services.mozilla.com/D182653
This commit is contained in:
Masayuki Nakano 2023-07-07 23:13:20 +00:00
parent d111bd326e
commit 3c8d46af25
2 changed files with 43 additions and 6 deletions

View file

@ -76,6 +76,41 @@ bool ContentCache::IsValid() const {
return true; 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 * mozilla::ContentCacheInChild
*****************************************************************************/ *****************************************************************************/
@ -148,7 +183,7 @@ bool ContentCacheInChild::CacheAll(nsIWidget* aWidget,
const bool textCached = CacheText(aWidget, aNotification); const bool textCached = CacheText(aWidget, aNotification);
const bool editorRectCached = CacheEditorRect(aWidget, aNotification); const bool editorRectCached = CacheEditorRect(aWidget, aNotification);
MOZ_DIAGNOSTIC_ASSERT(IsValid()); AssertIfInvalid();
return (textCached || editorRectCached) && IsValid(); return (textCached || editorRectCached) && IsValid();
} }
@ -190,7 +225,7 @@ bool ContentCacheInChild::CacheSelection(nsIWidget* aWidget,
("0x%p CacheSelection(), FAILED, editable content had already been " ("0x%p CacheSelection(), FAILED, editable content had already been "
"blurred", "blurred",
this)); this));
MOZ_DIAGNOSTIC_ASSERT(IsValid()); AssertIfInvalid();
return false; return false;
} else { } else {
mSelection.emplace(querySelectedTextEvent); mSelection.emplace(querySelectedTextEvent);
@ -233,7 +268,7 @@ bool ContentCacheInChild::CacheCaret(nsIWidget* aWidget,
MOZ_LOG(sContentCacheLog, LogLevel::Info, MOZ_LOG(sContentCacheLog, LogLevel::Info,
("0x%p CacheCaret(), Succeeded, mSelection=%s, mCaret=%s", this, ("0x%p CacheCaret(), Succeeded, mSelection=%s, mCaret=%s", this,
ToString(mSelection).c_str(), ToString(mCaret).c_str())); ToString(mSelection).c_str(), ToString(mCaret).c_str()));
MOZ_DIAGNOSTIC_ASSERT(IsValid()); AssertIfInvalid();
return IsValid(); return IsValid();
} }
@ -278,7 +313,7 @@ bool ContentCacheInChild::CacheCaretAndTextRects(
const bool caretCached = CacheCaret(aWidget, aNotification); const bool caretCached = CacheCaret(aWidget, aNotification);
const bool textRectsCached = CacheTextRects(aWidget, aNotification); const bool textRectsCached = CacheTextRects(aWidget, aNotification);
MOZ_DIAGNOSTIC_ASSERT(IsValid()); AssertIfInvalid();
return (caretCached || textRectsCached) && IsValid(); return (caretCached || textRectsCached) && IsValid();
} }
@ -340,7 +375,7 @@ bool ContentCacheInChild::CacheText(nsIWidget* aWidget,
mSelection.reset(); mSelection.reset();
mCaret.reset(); mCaret.reset();
mTextRectArray.reset(); mTextRectArray.reset();
MOZ_DIAGNOSTIC_ASSERT(IsValid()); AssertIfInvalid();
return false; return false;
} }
@ -615,7 +650,7 @@ bool ContentCacheInChild::CacheTextRects(nsIWidget* aWidget,
ToString(mTextRectArray).c_str(), ToString(mSelection).c_str(), ToString(mTextRectArray).c_str(), ToString(mSelection).c_str(),
ToString(mFirstCharRect).c_str(), ToString(mFirstCharRect).c_str(),
ToString(mLastCommitStringTextRectArray).c_str())); ToString(mLastCommitStringTextRectArray).c_str()));
MOZ_DIAGNOSTIC_ASSERT(IsValid()); AssertIfInvalid();
return IsValid(); return IsValid();
} }

View file

@ -48,6 +48,8 @@ class ContentCache {
[[nodiscard]] bool IsValid() const; [[nodiscard]] bool IsValid() const;
protected: protected:
void AssertIfInvalid() const;
// Whole text in the target // Whole text in the target
Maybe<nsString> mText; Maybe<nsString> mText;