Verified Commit a8fdd5d3 authored by Jens Stutte's avatar Jens Stutte Committed by ma1
Browse files

Bug 2048795 - Guard access to the GamepadPlatformService singleton by a...

Bug 2048795 - Guard access to the GamepadPlatformService singleton by a StaticMutex.  a=RyanVM DONTBUILD

Original Revision: https://phabricator.services.mozilla.com/D308034

Differential Revision: https://phabricator.services.mozilla.com/D309819
parent 455196ce
Loading
Loading
Loading
Loading
+11 −3
Original line number Original line Diff line number Diff line
@@ -11,6 +11,7 @@
#include "mozilla/dom/GamepadTestChannelParent.h"
#include "mozilla/dom/GamepadTestChannelParent.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/Mutex.h"
#include "mozilla/Mutex.h"
#include "mozilla/StaticMutex.h"
#include "mozilla/Unused.h"
#include "mozilla/Unused.h"


#include "nsCOMPtr.h"
#include "nsCOMPtr.h"
@@ -22,9 +23,12 @@ namespace mozilla::dom {


namespace {
namespace {


StaticMutex gGamepadPlatformServiceMutex;

// This is the singleton instance of GamepadPlatformService, can be called
// This is the singleton instance of GamepadPlatformService, can be called
// by both background and monitor thread.
// by both background and monitor thread.
StaticRefPtr<GamepadPlatformService> gGamepadPlatformServiceSingleton;
StaticRefPtr<GamepadPlatformService> gGamepadPlatformServiceSingleton
    MOZ_GUARDED_BY(gGamepadPlatformServiceMutex);


}  // namespace
}  // namespace


@@ -90,6 +94,7 @@ already_AddRefed<GamepadPlatformService>
GamepadPlatformService::GetParentService() {
GamepadPlatformService::GetParentService() {
  // GamepadPlatformService can only be accessed in parent process
  // GamepadPlatformService can only be accessed in parent process
  MOZ_ASSERT(XRE_IsParentProcess());
  MOZ_ASSERT(XRE_IsParentProcess());
  StaticMutexAutoLock lock(gGamepadPlatformServiceMutex);
  if (!gGamepadPlatformServiceSingleton) {
  if (!gGamepadPlatformServiceSingleton) {
    // Only Background Thread can create new GamepadPlatformService instance.
    // Only Background Thread can create new GamepadPlatformService instance.
    if (IsOnBackgroundThread()) {
    if (IsOnBackgroundThread()) {
@@ -325,11 +330,14 @@ void GamepadPlatformService::MaybeShutdown() {
    MutexAutoLock autoLock(mMutex);
    MutexAutoLock autoLock(mMutex);
    isChannelParentEmpty = mChannelParents.IsEmpty();
    isChannelParentEmpty = mChannelParents.IsEmpty();
    if (isChannelParentEmpty) {
    if (isChannelParentEmpty) {
      kungFuDeathGrip = gGamepadPlatformServiceSingleton;
      gGamepadPlatformServiceSingleton = nullptr;
      mGamepadAdded.clear();
      mGamepadAdded.clear();
    }
    }
  }
  }
  if (isChannelParentEmpty) {
    StaticMutexAutoLock lock(gGamepadPlatformServiceMutex);
    kungFuDeathGrip = gGamepadPlatformServiceSingleton;
    gGamepadPlatformServiceSingleton = nullptr;
  }
}
}


void GamepadPlatformService::Cleanup() {
void GamepadPlatformService::Cleanup() {