Skip to content

Commit

Permalink
Bug 1825142 - part9 : fix string leaking in testing. r=media-playback…
Browse files Browse the repository at this point in the history
…-reviewers,aosmond

Differential Revision: https://phabricator.services.mozilla.com/D197404
  • Loading branch information
alastor0325 committed Jan 4, 2024
1 parent d5a1d5c commit 6a1da27
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions dom/media/eme/mediafoundation/WMFCDMImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,62 @@

#include <unordered_map>

#include "mozilla/AppShutdown.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/dom/MediaKeySession.h"
#include "mozilla/dom/KeySystemNames.h"

namespace mozilla {

/* static */
bool WMFCDMImpl::Supports(const nsAString& aKeySystem) {
static std::map<nsString, bool> supports;
MOZ_ASSERT(NS_IsMainThread());
if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
return false;
}

static std::map<nsString, bool> 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<WMFCDMImpl> cdm = MakeRefPtr<WMFCDMImpl>(aKeySystem);
nsTArray<KeySystemConfig> configs;
bool s = cdm->GetCapabilities(configs);
supports[key] = s;
sSupports[key] = s;
return s;
}

bool WMFCDMImpl::GetCapabilities(nsTArray<KeySystemConfig>& aOutConfigs) {
nsCOMPtr<nsISerialEventTarget> 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<std::string, nsTArray<KeySystemConfig>>
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()) {
Expand All @@ -49,6 +77,8 @@ bool WMFCDMImpl::GetCapabilities(nsTArray<KeySystemConfig>& aOutConfigs) {
}

// Not cached result, ask the remote process.
nsCOMPtr<nsISerialEventTarget> backgroundTaskQueue;
NS_CreateBackgroundTaskQueue(__func__, getter_AddRefs(backgroundTaskQueue));
if (!mCDM) {
mCDM = MakeRefPtr<MFCDMChild>(mKeySystem);
}
Expand Down

0 comments on commit 6a1da27

Please sign in to comment.