Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "VolumeServiceIOThread.h"
6 #include "base/message_loop.h"
7 #include "nsVolumeService.h"
8 #include "nsXULAppAPI.h"
9 #include "Volume.h"
10 #include "VolumeManager.h"
12 namespace mozilla {
13 namespace system {
15 VolumeServiceIOThread::VolumeServiceIOThread(nsVolumeService* aVolumeService)
16 : mVolumeService(aVolumeService)
17 {
18 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
20 VolumeManager::RegisterStateObserver(this);
21 Volume::RegisterObserver(this);
22 UpdateAllVolumes();
23 }
25 VolumeServiceIOThread::~VolumeServiceIOThread()
26 {
27 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
28 Volume::UnregisterObserver(this);
29 VolumeManager::UnregisterStateObserver(this);
30 }
32 void
33 VolumeServiceIOThread::Notify(Volume* const & aVolume)
34 {
35 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
36 if (VolumeManager::State() != VolumeManager::VOLUMES_READY) {
37 return;
38 }
39 mVolumeService->UpdateVolumeIOThread(aVolume);
40 }
42 void
43 VolumeServiceIOThread::Notify(const VolumeManager::StateChangedEvent& aEvent)
44 {
45 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
46 UpdateAllVolumes();
47 }
49 void
50 VolumeServiceIOThread::UpdateAllVolumes()
51 {
52 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
53 if (VolumeManager::State() != VolumeManager::VOLUMES_READY) {
54 return;
55 }
56 VolumeManager::VolumeArray::size_type numVolumes = VolumeManager::NumVolumes();
57 VolumeManager::VolumeArray::index_type volIndex;
59 for (volIndex = 0; volIndex < numVolumes; volIndex++) {
60 RefPtr<Volume> vol = VolumeManager::GetVolume(volIndex);
61 mVolumeService->UpdateVolumeIOThread(vol);
62 }
63 }
65 static StaticRefPtr<VolumeServiceIOThread> sVolumeServiceIOThread;
67 void
68 InitVolumeServiceIOThread(nsVolumeService* const & aVolumeService)
69 {
70 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
71 sVolumeServiceIOThread = new VolumeServiceIOThread(aVolumeService);
72 }
74 void
75 ShutdownVolumeServiceIOThread()
76 {
77 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
78 sVolumeServiceIOThread = nullptr;
79 }
81 } // system
82 } // mozilla