From 6a1da274be2c9b2e7d8d3a63a181cd882a016a75 Mon Sep 17 00:00:00 2001 From: alwu Date: Thu, 4 Jan 2024 04:13:05 +0000 Subject: [PATCH] Bug 1825142 - part9 : fix string leaking in testing. r=media-playback-reviewers,aosmond Differential Revision: https://phabricator.services.mozilla.com/D197404 --- dom/media/eme/mediafoundation/WMFCDMImpl.cpp | 42 +++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/dom/media/eme/mediafoundation/WMFCDMImpl.cpp b/dom/media/eme/mediafoundation/WMFCDMImpl.cpp index 496960e9fe6a5..1fe42aa8e279f 100644 --- a/dom/media/eme/mediafoundation/WMFCDMImpl.cpp +++ b/dom/media/eme/mediafoundation/WMFCDMImpl.cpp @@ -8,6 +8,8 @@ #include +#include "mozilla/AppShutdown.h" +#include "mozilla/ClearOnShutdown.h" #include "mozilla/dom/MediaKeySession.h" #include "mozilla/dom/KeySystemNames.h" @@ -15,27 +17,53 @@ namespace mozilla { /* static */ bool WMFCDMImpl::Supports(const nsAString& aKeySystem) { - static std::map supports; + MOZ_ASSERT(NS_IsMainThread()); + if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) { + return false; + } + + static std::map sSupports; + static bool sSetRunOnShutdown = false; + if (!sSetRunOnShutdown) { + GetMainThreadSerialEventTarget()->Dispatch( + NS_NewRunnableFunction("WMFCDMImpl::Supports", [&] { + RunOnShutdown([&] { sSupports.clear(); }, + ShutdownPhase::XPCOMShutdown); + })); + sSetRunOnShutdown = true; + } nsString key(aKeySystem); - if (const auto& s = supports.find(key); s != supports.end()) { + if (const auto& s = sSupports.find(key); s != sSupports.end()) { return s->second; } RefPtr cdm = MakeRefPtr(aKeySystem); nsTArray configs; bool s = cdm->GetCapabilities(configs); - supports[key] = s; + sSupports[key] = s; return s; } bool WMFCDMImpl::GetCapabilities(nsTArray& aOutConfigs) { - nsCOMPtr backgroundTaskQueue; - NS_CreateBackgroundTaskQueue(__func__, getter_AddRefs(backgroundTaskQueue)); + MOZ_ASSERT(NS_IsMainThread()); + if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) { + return false; + } - // Retrieve result from our cached key system static std::unordered_map> sKeySystemConfigs{}; + static bool sSetRunOnShutdown = false; + if (!sSetRunOnShutdown) { + GetMainThreadSerialEventTarget()->Dispatch( + NS_NewRunnableFunction("WMFCDMImpl::GetCapabilities", [&] { + RunOnShutdown([&] { sKeySystemConfigs.clear(); }, + ShutdownPhase::XPCOMShutdown); + })); + sSetRunOnShutdown = true; + } + + // Retrieve result from our cached key system auto keySystem = std::string{NS_ConvertUTF16toUTF8(mKeySystem).get()}; if (auto rv = sKeySystemConfigs.find(keySystem); rv != sKeySystemConfigs.end()) { @@ -49,6 +77,8 @@ bool WMFCDMImpl::GetCapabilities(nsTArray& aOutConfigs) { } // Not cached result, ask the remote process. + nsCOMPtr backgroundTaskQueue; + NS_CreateBackgroundTaskQueue(__func__, getter_AddRefs(backgroundTaskQueue)); if (!mCDM) { mCDM = MakeRefPtr(mKeySystem); }