michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "VolumeServiceIOThread.h" michael@0: #include "base/message_loop.h" michael@0: #include "nsVolumeService.h" michael@0: #include "nsXULAppAPI.h" michael@0: #include "Volume.h" michael@0: #include "VolumeManager.h" michael@0: michael@0: namespace mozilla { michael@0: namespace system { michael@0: michael@0: VolumeServiceIOThread::VolumeServiceIOThread(nsVolumeService* aVolumeService) michael@0: : mVolumeService(aVolumeService) michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: michael@0: VolumeManager::RegisterStateObserver(this); michael@0: Volume::RegisterObserver(this); michael@0: UpdateAllVolumes(); michael@0: } michael@0: michael@0: VolumeServiceIOThread::~VolumeServiceIOThread() michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: Volume::UnregisterObserver(this); michael@0: VolumeManager::UnregisterStateObserver(this); michael@0: } michael@0: michael@0: void michael@0: VolumeServiceIOThread::Notify(Volume* const & aVolume) michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: if (VolumeManager::State() != VolumeManager::VOLUMES_READY) { michael@0: return; michael@0: } michael@0: mVolumeService->UpdateVolumeIOThread(aVolume); michael@0: } michael@0: michael@0: void michael@0: VolumeServiceIOThread::Notify(const VolumeManager::StateChangedEvent& aEvent) michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: UpdateAllVolumes(); michael@0: } michael@0: michael@0: void michael@0: VolumeServiceIOThread::UpdateAllVolumes() michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: if (VolumeManager::State() != VolumeManager::VOLUMES_READY) { michael@0: return; michael@0: } michael@0: VolumeManager::VolumeArray::size_type numVolumes = VolumeManager::NumVolumes(); michael@0: VolumeManager::VolumeArray::index_type volIndex; michael@0: michael@0: for (volIndex = 0; volIndex < numVolumes; volIndex++) { michael@0: RefPtr vol = VolumeManager::GetVolume(volIndex); michael@0: mVolumeService->UpdateVolumeIOThread(vol); michael@0: } michael@0: } michael@0: michael@0: static StaticRefPtr sVolumeServiceIOThread; michael@0: michael@0: void michael@0: InitVolumeServiceIOThread(nsVolumeService* const & aVolumeService) michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: sVolumeServiceIOThread = new VolumeServiceIOThread(aVolumeService); michael@0: } michael@0: michael@0: void michael@0: ShutdownVolumeServiceIOThread() michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: sVolumeServiceIOThread = nullptr; michael@0: } michael@0: michael@0: } // system michael@0: } // mozilla