|
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 |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "DiskSpaceWatcher.h" |
|
6 #include "nsIObserverService.h" |
|
7 #include "nsXULAppAPI.h" |
|
8 #include "mozilla/Hal.h" |
|
9 #include "mozilla/ModuleUtils.h" |
|
10 #include "mozilla/Preferences.h" |
|
11 #include "mozilla/ClearOnShutdown.h" |
|
12 #include "mozilla/Services.h" |
|
13 |
|
14 #define NS_DISKSPACEWATCHER_CID \ |
|
15 { 0xab218518, 0xf197, 0x4fb4, { 0x8b, 0x0f, 0x8b, 0xb3, 0x4d, 0xf2, 0x4b, 0xf4 } } |
|
16 |
|
17 using namespace mozilla; |
|
18 |
|
19 StaticRefPtr<DiskSpaceWatcher> gDiskSpaceWatcher; |
|
20 |
|
21 NS_IMPL_ISUPPORTS(DiskSpaceWatcher, nsIDiskSpaceWatcher, nsIObserver) |
|
22 |
|
23 uint64_t DiskSpaceWatcher::sFreeSpace = 0; |
|
24 bool DiskSpaceWatcher::sIsDiskFull = false; |
|
25 |
|
26 DiskSpaceWatcher::DiskSpaceWatcher() |
|
27 { |
|
28 MOZ_ASSERT(NS_IsMainThread()); |
|
29 MOZ_ASSERT(!gDiskSpaceWatcher); |
|
30 } |
|
31 |
|
32 DiskSpaceWatcher::~DiskSpaceWatcher() |
|
33 { |
|
34 MOZ_ASSERT(!gDiskSpaceWatcher); |
|
35 } |
|
36 |
|
37 already_AddRefed<DiskSpaceWatcher> |
|
38 DiskSpaceWatcher::FactoryCreate() |
|
39 { |
|
40 if (XRE_GetProcessType() != GeckoProcessType_Default) { |
|
41 return nullptr; |
|
42 } |
|
43 |
|
44 MOZ_ASSERT(NS_IsMainThread()); |
|
45 |
|
46 if (!Preferences::GetBool("disk_space_watcher.enabled", false)) { |
|
47 return nullptr; |
|
48 } |
|
49 |
|
50 if (!gDiskSpaceWatcher) { |
|
51 gDiskSpaceWatcher = new DiskSpaceWatcher(); |
|
52 ClearOnShutdown(&gDiskSpaceWatcher); |
|
53 } |
|
54 |
|
55 nsRefPtr<DiskSpaceWatcher> service = gDiskSpaceWatcher.get(); |
|
56 return service.forget(); |
|
57 } |
|
58 |
|
59 NS_IMETHODIMP |
|
60 DiskSpaceWatcher::Observe(nsISupports* aSubject, const char* aTopic, |
|
61 const char16_t* aData) |
|
62 { |
|
63 MOZ_ASSERT(NS_IsMainThread()); |
|
64 |
|
65 if (!strcmp(aTopic, "profile-after-change")) { |
|
66 nsCOMPtr<nsIObserverService> observerService = |
|
67 mozilla::services::GetObserverService(); |
|
68 observerService->AddObserver(this, "profile-before-change", false); |
|
69 mozilla::hal::StartDiskSpaceWatcher(); |
|
70 return NS_OK; |
|
71 } |
|
72 |
|
73 if (!strcmp(aTopic, "profile-before-change")) { |
|
74 nsCOMPtr<nsIObserverService> observerService = |
|
75 mozilla::services::GetObserverService(); |
|
76 observerService->RemoveObserver(this, "profile-before-change"); |
|
77 mozilla::hal::StopDiskSpaceWatcher(); |
|
78 return NS_OK; |
|
79 } |
|
80 |
|
81 MOZ_ASSERT(false, "DiskSpaceWatcher got unexpected topic!"); |
|
82 return NS_ERROR_UNEXPECTED; |
|
83 } |
|
84 |
|
85 /* readonly attribute bool isDiskFull; */ |
|
86 NS_IMETHODIMP DiskSpaceWatcher::GetIsDiskFull(bool* aIsDiskFull) |
|
87 { |
|
88 *aIsDiskFull = sIsDiskFull; |
|
89 return NS_OK; |
|
90 } |
|
91 |
|
92 // GetFreeSpace is a macro on windows, and that messes up with the c++ |
|
93 // compiler. |
|
94 #ifdef XP_WIN |
|
95 #undef GetFreeSpace |
|
96 #endif |
|
97 /* readonly attribute long freeSpace; */ |
|
98 NS_IMETHODIMP DiskSpaceWatcher::GetFreeSpace(uint64_t* aFreeSpace) |
|
99 { |
|
100 *aFreeSpace = sFreeSpace; |
|
101 return NS_OK; |
|
102 } |
|
103 |
|
104 // static |
|
105 void DiskSpaceWatcher::UpdateState(bool aIsDiskFull, uint64_t aFreeSpace) |
|
106 { |
|
107 MOZ_ASSERT(NS_IsMainThread()); |
|
108 if (!gDiskSpaceWatcher) { |
|
109 return; |
|
110 } |
|
111 |
|
112 nsCOMPtr<nsIObserverService> observerService = |
|
113 mozilla::services::GetObserverService(); |
|
114 |
|
115 sIsDiskFull = aIsDiskFull; |
|
116 sFreeSpace = aFreeSpace; |
|
117 |
|
118 if (!observerService) { |
|
119 return; |
|
120 } |
|
121 |
|
122 const char16_t stateFull[] = { 'f', 'u', 'l', 'l', 0 }; |
|
123 const char16_t stateFree[] = { 'f', 'r', 'e', 'e', 0 }; |
|
124 |
|
125 nsCOMPtr<nsISupports> subject; |
|
126 CallQueryInterface(gDiskSpaceWatcher.get(), getter_AddRefs(subject)); |
|
127 MOZ_ASSERT(subject); |
|
128 observerService->NotifyObservers(subject, |
|
129 DISKSPACEWATCHER_OBSERVER_TOPIC, |
|
130 sIsDiskFull ? stateFull : stateFree); |
|
131 return; |
|
132 } |
|
133 |
|
134 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(DiskSpaceWatcher, |
|
135 DiskSpaceWatcher::FactoryCreate) |
|
136 |
|
137 NS_DEFINE_NAMED_CID(NS_DISKSPACEWATCHER_CID); |
|
138 |
|
139 static const mozilla::Module::CIDEntry kDiskSpaceWatcherCIDs[] = { |
|
140 { &kNS_DISKSPACEWATCHER_CID, false, nullptr, DiskSpaceWatcherConstructor }, |
|
141 { nullptr } |
|
142 }; |
|
143 |
|
144 static const mozilla::Module::ContractIDEntry kDiskSpaceWatcherContracts[] = { |
|
145 { "@mozilla.org/toolkit/disk-space-watcher;1", &kNS_DISKSPACEWATCHER_CID }, |
|
146 { nullptr } |
|
147 }; |
|
148 |
|
149 static const mozilla::Module::CategoryEntry kDiskSpaceWatcherCategories[] = { |
|
150 #ifdef MOZ_WIDGET_GONK |
|
151 { "profile-after-change", "Disk Space Watcher Service", DISKSPACEWATCHER_CONTRACTID }, |
|
152 #endif |
|
153 { nullptr } |
|
154 }; |
|
155 |
|
156 static const mozilla::Module kDiskSpaceWatcherModule = { |
|
157 mozilla::Module::kVersion, |
|
158 kDiskSpaceWatcherCIDs, |
|
159 kDiskSpaceWatcherContracts, |
|
160 kDiskSpaceWatcherCategories |
|
161 }; |
|
162 |
|
163 NSMODULE_DEFN(DiskSpaceWatcherModule) = &kDiskSpaceWatcherModule; |