diff --git a/gfx/vr/ipc/VRParent.cpp b/gfx/vr/ipc/VRParent.cpp index 3d715805c9a1..c20cfbc6aa42 100644 --- a/gfx/vr/ipc/VRParent.cpp +++ b/gfx/vr/ipc/VRParent.cpp @@ -114,6 +114,12 @@ VRParent::Init(base::ProcessId aParentPid, MessageLoop* aIOLoop, IPC::Channel* aChannel) { + // Initialize the thread manager before starting IPC. Otherwise, messages + // may be posted to the main thread and we won't be able to process them. + if (NS_WARN_IF(NS_FAILED(nsThreadManager::get().Init()))) { + return false; + } + // Now it's safe to start IPC. if (NS_WARN_IF(!Open(aChannel, aParentPid, aIOLoop))) { return false; @@ -136,6 +142,10 @@ VRParent::Init(base::ProcessId aParentPid, #if defined(XP_WIN) DeviceManagerDx::Init(); #endif + if (NS_FAILED(NS_InitMinimalXPCOM())) { + return false; + } + return true; } diff --git a/gfx/vr/service/OpenVRSession.cpp b/gfx/vr/service/OpenVRSession.cpp index 0b68813638c4..7efb148978f0 100644 --- a/gfx/vr/service/OpenVRSession.cpp +++ b/gfx/vr/service/OpenVRSession.cpp @@ -157,8 +157,10 @@ OpenVRSession::Initialize(mozilla::gfx::VRSystemState& aSystemState) return false; } - StartHapticThread(); - StartHapticTimer(); + NS_DispatchToMainThread(NS_NewRunnableFunction( + "OpenVRSession::StartHapticThread", [this]() { + StartHapticThread(); + })); // Succeeded return true; @@ -927,17 +929,23 @@ OpenVRSession::VibrateHaptic(uint32_t aControllerIdx, uint32_t aHapticIndex, void OpenVRSession::StartHapticThread() { + MOZ_ASSERT(NS_IsMainThread()); if (!mHapticThread) { mHapticThread = new VRThread(NS_LITERAL_CSTRING("VR_OpenVR_Haptics")); } mHapticThread->Start(); + StartHapticTimer(); } void OpenVRSession::StopHapticThread() { if (mHapticThread) { - mHapticThread->Shutdown(); + NS_DispatchToMainThread(NS_NewRunnableFunction( + "mHapticThread::Shutdown", + [thread = mHapticThread]() { + thread->Shutdown(); + })); mHapticThread = nullptr; } }