Bug 1625801 - grab the lock when creating and destroying RefMessageBodyService. r=sg

Differential Revision: https://phabricator.services.mozilla.com/D102944
This commit is contained in:
Alexis Beingessner 2021-01-26 17:15:26 +00:00
parent a4315ae315
commit 892361c25c
2 changed files with 6 additions and 3 deletions

View File

@ -17,6 +17,7 @@
namespace mozilla::dom { namespace mozilla::dom {
// Guards sService and its members.
StaticMutex sRefMessageBodyServiceMutex; StaticMutex sRefMessageBodyServiceMutex;
// Raw pointer because the service is kept alive by other objects. // Raw pointer because the service is kept alive by other objects.
@ -35,16 +36,18 @@ already_AddRefed<RefMessageBodyService> RefMessageBodyService::GetOrCreate() {
RefMessageBodyService* RefMessageBodyService::GetOrCreateInternal( RefMessageBodyService* RefMessageBodyService::GetOrCreateInternal(
const StaticMutexAutoLock& aProofOfLock) { const StaticMutexAutoLock& aProofOfLock) {
if (!sService) { if (!sService) {
sService = new RefMessageBodyService(); sService = new RefMessageBodyService(aProofOfLock);
} }
return sService; return sService;
} }
RefMessageBodyService::RefMessageBodyService() { RefMessageBodyService::RefMessageBodyService(
const StaticMutexAutoLock& aProofOfLock) {
MOZ_DIAGNOSTIC_ASSERT(sService == nullptr); MOZ_DIAGNOSTIC_ASSERT(sService == nullptr);
} }
RefMessageBodyService::~RefMessageBodyService() { RefMessageBodyService::~RefMessageBodyService() {
StaticMutexAutoLock lock(sRefMessageBodyServiceMutex);
MOZ_DIAGNOSTIC_ASSERT(sService == this); MOZ_DIAGNOSTIC_ASSERT(sService == this);
sService = nullptr; sService = nullptr;
} }

View File

@ -122,7 +122,7 @@ class RefMessageBodyService final {
void SetMaxCount(const nsID& aID, uint32_t aMaxCount); void SetMaxCount(const nsID& aID, uint32_t aMaxCount);
private: private:
RefMessageBodyService(); explicit RefMessageBodyService(const StaticMutexAutoLock& aProofOfLock);
~RefMessageBodyService(); ~RefMessageBodyService();
static RefMessageBodyService* GetOrCreateInternal( static RefMessageBodyService* GetOrCreateInternal(