|
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/. */ |
|
4 |
|
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" |
|
11 |
|
12 namespace mozilla { |
|
13 namespace system { |
|
14 |
|
15 VolumeServiceIOThread::VolumeServiceIOThread(nsVolumeService* aVolumeService) |
|
16 : mVolumeService(aVolumeService) |
|
17 { |
|
18 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); |
|
19 |
|
20 VolumeManager::RegisterStateObserver(this); |
|
21 Volume::RegisterObserver(this); |
|
22 UpdateAllVolumes(); |
|
23 } |
|
24 |
|
25 VolumeServiceIOThread::~VolumeServiceIOThread() |
|
26 { |
|
27 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); |
|
28 Volume::UnregisterObserver(this); |
|
29 VolumeManager::UnregisterStateObserver(this); |
|
30 } |
|
31 |
|
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 } |
|
41 |
|
42 void |
|
43 VolumeServiceIOThread::Notify(const VolumeManager::StateChangedEvent& aEvent) |
|
44 { |
|
45 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); |
|
46 UpdateAllVolumes(); |
|
47 } |
|
48 |
|
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; |
|
58 |
|
59 for (volIndex = 0; volIndex < numVolumes; volIndex++) { |
|
60 RefPtr<Volume> vol = VolumeManager::GetVolume(volIndex); |
|
61 mVolumeService->UpdateVolumeIOThread(vol); |
|
62 } |
|
63 } |
|
64 |
|
65 static StaticRefPtr<VolumeServiceIOThread> sVolumeServiceIOThread; |
|
66 |
|
67 void |
|
68 InitVolumeServiceIOThread(nsVolumeService* const & aVolumeService) |
|
69 { |
|
70 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); |
|
71 sVolumeServiceIOThread = new VolumeServiceIOThread(aVolumeService); |
|
72 } |
|
73 |
|
74 void |
|
75 ShutdownVolumeServiceIOThread() |
|
76 { |
|
77 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); |
|
78 sVolumeServiceIOThread = nullptr; |
|
79 } |
|
80 |
|
81 } // system |
|
82 } // mozilla |