Bug 1818417 - Tweak nsFontFaceLoader destruction to be more consistent. r=jfkthame

Differential Revision: https://phabricator.services.mozilla.com/D170744
This commit is contained in:
Emilio Cobos Álvarez 2023-02-24 17:37:23 +00:00
Родитель ea7b7a528c
Коммит d822650349
3 изменённых файлов: 41 добавлений и 35 удалений

Просмотреть файл

@ -87,24 +87,56 @@ FontFaceSetImpl::~FontFaceSetImpl() {
Destroy();
}
void FontFaceSetImpl::DestroyLoaders() {
mMutex.AssertCurrentThreadIn();
if (mLoaders.IsEmpty()) {
return;
}
if (NS_IsMainThread()) {
for (const auto& key : mLoaders.Keys()) {
key->Cancel();
}
mLoaders.Clear();
return;
}
class DestroyLoadersRunnable final : public Runnable {
public:
explicit DestroyLoadersRunnable(FontFaceSetImpl* aFontFaceSet)
: Runnable("FontFaceSetImpl::DestroyLoaders"),
mFontFaceSet(aFontFaceSet) {}
protected:
~DestroyLoadersRunnable() override = default;
NS_IMETHOD Run() override {
RecursiveMutexAutoLock lock(mFontFaceSet->mMutex);
mFontFaceSet->DestroyLoaders();
return NS_OK;
}
// We need to save a reference to the FontFaceSetImpl because the
// loaders contain a non-owning reference to it.
RefPtr<FontFaceSetImpl> mFontFaceSet;
};
auto runnable = MakeRefPtr<DestroyLoadersRunnable>(this);
NS_DispatchToMainThread(runnable);
}
void FontFaceSetImpl::Destroy() {
nsTArray<FontFaceRecord> nonRuleFaces;
nsRefPtrHashtable<nsCStringHashKey, gfxUserFontFamily> fontFamilies;
{
RecursiveMutexAutoLock lock(mMutex);
for (const auto& key : mLoaders.Keys()) {
key->Cancel();
}
mLoaders.Clear();
DestroyLoaders();
nonRuleFaces = std::move(mNonRuleFaces);
fontFamilies = std::move(mFontFamilies);
mOwner = nullptr;
}
gfxPlatformFontList* fp = gfxPlatformFontList::PlatformFontList();
if (fp) {
if (gfxPlatformFontList* fp = gfxPlatformFontList::PlatformFontList()) {
fp->RemoveUserFontSet(this);
}
}

Просмотреть файл

@ -79,6 +79,8 @@ class FontFaceSetImpl : public nsISupports, public gfxUserFontSet {
explicit FontFaceSetImpl(FontFaceSet* aOwner);
void DestroyLoaders();
public:
virtual void Destroy();
virtual bool IsOnOwningThread() = 0;

Просмотреть файл

@ -136,34 +136,6 @@ void FontFaceSetWorkerImpl::InitializeOnMainThread() {
void FontFaceSetWorkerImpl::Destroy() {
RecursiveMutexAutoLock lock(mMutex);
class DestroyLoadersRunnable final : public Runnable {
public:
explicit DestroyLoadersRunnable(FontFaceSetWorkerImpl* aFontFaceSet)
: Runnable("FontFaceSetWorkerImpl::Destroy"),
mFontFaceSet(aFontFaceSet) {}
protected:
~DestroyLoadersRunnable() override = default;
NS_IMETHOD Run() override {
RecursiveMutexAutoLock lock(mFontFaceSet->mMutex);
for (const auto& key : mFontFaceSet->mLoaders.Keys()) {
key->Cancel();
}
mFontFaceSet->mLoaders.Clear();
return NS_OK;
}
// We need to save a reference to the FontFaceSetWorkerImpl because the
// loaders contain a non-owning reference to it.
RefPtr<FontFaceSetWorkerImpl> mFontFaceSet;
};
if (!mLoaders.IsEmpty() && !NS_IsMainThread()) {
auto runnable = MakeRefPtr<DestroyLoadersRunnable>(this);
NS_DispatchToMainThread(runnable);
}
mWorkerRef = nullptr;
FontFaceSetImpl::Destroy();
}