/src/mozilla-central/hal/sandbox/SandboxHal.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set sw=2 ts=8 et ft=cpp : */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "Hal.h" |
8 | | #include "HalLog.h" |
9 | | #include "mozilla/dom/ContentChild.h" |
10 | | #include "mozilla/dom/ContentParent.h" |
11 | | #include "mozilla/hal_sandbox/PHalChild.h" |
12 | | #include "mozilla/hal_sandbox/PHalParent.h" |
13 | | #include "mozilla/dom/TabParent.h" |
14 | | #include "mozilla/dom/TabChild.h" |
15 | | #include "mozilla/fallback/FallbackScreenConfiguration.h" |
16 | | #include "mozilla/EnumeratedRange.h" |
17 | | #include "mozilla/Observer.h" |
18 | | #include "mozilla/Unused.h" |
19 | | #include "nsAutoPtr.h" |
20 | | #include "WindowIdentifier.h" |
21 | | |
22 | | using namespace mozilla; |
23 | | using namespace mozilla::dom; |
24 | | using namespace mozilla::hal; |
25 | | |
26 | | namespace mozilla { |
27 | | namespace hal_sandbox { |
28 | | |
29 | | static bool sHalChildDestroyed = false; |
30 | | |
31 | | bool |
32 | | HalChildDestroyed() |
33 | 0 | { |
34 | 0 | return sHalChildDestroyed; |
35 | 0 | } |
36 | | |
37 | | static PHalChild* sHal; |
38 | | static PHalChild* |
39 | | Hal() |
40 | 0 | { |
41 | 0 | if (!sHal) { |
42 | 0 | sHal = ContentChild::GetSingleton()->SendPHalConstructor(); |
43 | 0 | } |
44 | 0 | return sHal; |
45 | 0 | } |
46 | | |
47 | | void |
48 | | Vibrate(const nsTArray<uint32_t>& pattern, const WindowIdentifier &id) |
49 | 0 | { |
50 | 0 | HAL_LOG("Vibrate: Sending to parent process."); |
51 | 0 |
|
52 | 0 | AutoTArray<uint32_t, 8> p(pattern); |
53 | 0 |
|
54 | 0 | WindowIdentifier newID(id); |
55 | 0 | newID.AppendProcessID(); |
56 | 0 | Hal()->SendVibrate(p, newID.AsArray(), TabChild::GetFrom(newID.GetWindow())); |
57 | 0 | } |
58 | | |
59 | | void |
60 | | CancelVibrate(const WindowIdentifier &id) |
61 | 0 | { |
62 | 0 | HAL_LOG("CancelVibrate: Sending to parent process."); |
63 | 0 |
|
64 | 0 | WindowIdentifier newID(id); |
65 | 0 | newID.AppendProcessID(); |
66 | 0 | Hal()->SendCancelVibrate(newID.AsArray(), TabChild::GetFrom(newID.GetWindow())); |
67 | 0 | } |
68 | | |
69 | | void |
70 | | EnableBatteryNotifications() |
71 | 0 | { |
72 | 0 | Hal()->SendEnableBatteryNotifications(); |
73 | 0 | } |
74 | | |
75 | | void |
76 | | DisableBatteryNotifications() |
77 | 0 | { |
78 | 0 | Hal()->SendDisableBatteryNotifications(); |
79 | 0 | } |
80 | | |
81 | | void |
82 | | GetCurrentBatteryInformation(BatteryInformation* aBatteryInfo) |
83 | 0 | { |
84 | 0 | Hal()->SendGetCurrentBatteryInformation(aBatteryInfo); |
85 | 0 | } |
86 | | |
87 | | void |
88 | | EnableNetworkNotifications() |
89 | 0 | { |
90 | 0 | Hal()->SendEnableNetworkNotifications(); |
91 | 0 | } |
92 | | |
93 | | void |
94 | | DisableNetworkNotifications() |
95 | 0 | { |
96 | 0 | Hal()->SendDisableNetworkNotifications(); |
97 | 0 | } |
98 | | |
99 | | void |
100 | | GetCurrentNetworkInformation(NetworkInformation* aNetworkInfo) |
101 | 0 | { |
102 | 0 | Hal()->SendGetCurrentNetworkInformation(aNetworkInfo); |
103 | 0 | } |
104 | | |
105 | | void |
106 | | EnableScreenConfigurationNotifications() |
107 | 0 | { |
108 | 0 | Hal()->SendEnableScreenConfigurationNotifications(); |
109 | 0 | } |
110 | | |
111 | | void |
112 | | DisableScreenConfigurationNotifications() |
113 | 0 | { |
114 | 0 | Hal()->SendDisableScreenConfigurationNotifications(); |
115 | 0 | } |
116 | | |
117 | | void |
118 | | GetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration) |
119 | 0 | { |
120 | 0 | fallback::GetCurrentScreenConfiguration(aScreenConfiguration); |
121 | 0 | } |
122 | | |
123 | | bool |
124 | | LockScreenOrientation(const ScreenOrientation& aOrientation) |
125 | 0 | { |
126 | 0 | bool allowed; |
127 | 0 | Hal()->SendLockScreenOrientation(aOrientation, &allowed); |
128 | 0 | return allowed; |
129 | 0 | } |
130 | | |
131 | | void |
132 | | UnlockScreenOrientation() |
133 | 0 | { |
134 | 0 | // Don't send this message from both the middleman and recording processes. |
135 | 0 | if (!recordreplay::IsMiddleman()) { |
136 | 0 | Hal()->SendUnlockScreenOrientation(); |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | | void |
141 | 0 | EnableSensorNotifications(SensorType aSensor) { |
142 | 0 | Hal()->SendEnableSensorNotifications(aSensor); |
143 | 0 | } |
144 | | |
145 | | void |
146 | 0 | DisableSensorNotifications(SensorType aSensor) { |
147 | 0 | Hal()->SendDisableSensorNotifications(aSensor); |
148 | 0 | } |
149 | | |
150 | | void |
151 | | EnableWakeLockNotifications() |
152 | 0 | { |
153 | 0 | Hal()->SendEnableWakeLockNotifications(); |
154 | 0 | } |
155 | | |
156 | | void |
157 | | DisableWakeLockNotifications() |
158 | 0 | { |
159 | 0 | Hal()->SendDisableWakeLockNotifications(); |
160 | 0 | } |
161 | | |
162 | | void |
163 | | ModifyWakeLock(const nsAString &aTopic, |
164 | | WakeLockControl aLockAdjust, |
165 | | WakeLockControl aHiddenAdjust, |
166 | | uint64_t aProcessID) |
167 | 0 | { |
168 | 0 | MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN); |
169 | 0 | Hal()->SendModifyWakeLock(nsString(aTopic), aLockAdjust, aHiddenAdjust, aProcessID); |
170 | 0 | } |
171 | | |
172 | | void |
173 | | GetWakeLockInfo(const nsAString &aTopic, WakeLockInformation *aWakeLockInfo) |
174 | 0 | { |
175 | 0 | Hal()->SendGetWakeLockInfo(nsString(aTopic), aWakeLockInfo); |
176 | 0 | } |
177 | | |
178 | | bool |
179 | | EnableAlarm() |
180 | 0 | { |
181 | 0 | MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet."); |
182 | 0 | } |
183 | | |
184 | | void |
185 | | DisableAlarm() |
186 | 0 | { |
187 | 0 | MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet."); |
188 | 0 | } |
189 | | |
190 | | bool |
191 | | SetAlarm(int32_t aSeconds, int32_t aNanoseconds) |
192 | 0 | { |
193 | 0 | MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet."); |
194 | 0 | } |
195 | | |
196 | | void |
197 | | SetProcessPriority(int aPid, ProcessPriority aPriority) |
198 | 0 | { |
199 | 0 | MOZ_CRASH("Only the main process may set processes' priorities."); |
200 | 0 | } |
201 | | |
202 | | bool |
203 | | SetProcessPrioritySupported() |
204 | 0 | { |
205 | 0 | MOZ_CRASH("Only the main process may call SetProcessPrioritySupported()."); |
206 | 0 | } |
207 | | |
208 | | class HalParent : public PHalParent |
209 | | , public BatteryObserver |
210 | | , public NetworkObserver |
211 | | , public ISensorObserver |
212 | | , public WakeLockObserver |
213 | | , public ScreenConfigurationObserver |
214 | | { |
215 | | public: |
216 | | virtual void |
217 | | ActorDestroy(ActorDestroyReason aWhy) override |
218 | 0 | { |
219 | 0 | // NB: you *must* unconditionally unregister your observer here, |
220 | 0 | // if it *may* be registered below. |
221 | 0 | hal::UnregisterBatteryObserver(this); |
222 | 0 | hal::UnregisterNetworkObserver(this); |
223 | 0 | hal::UnregisterScreenConfigurationObserver(this); |
224 | 0 | for (auto sensor : MakeEnumeratedRange(NUM_SENSOR_TYPE)) { |
225 | 0 | hal::UnregisterSensorObserver(sensor, this); |
226 | 0 | } |
227 | 0 | hal::UnregisterWakeLockObserver(this); |
228 | 0 | } |
229 | | |
230 | | virtual mozilla::ipc::IPCResult |
231 | | RecvVibrate(InfallibleTArray<unsigned int>&& pattern, |
232 | | InfallibleTArray<uint64_t>&& id, |
233 | | PBrowserParent *browserParent) override |
234 | 0 | { |
235 | 0 | // We give all content vibration permission. |
236 | 0 | // TabParent *tabParent = TabParent::GetFrom(browserParent); |
237 | 0 | /* xxxkhuey wtf |
238 | 0 | nsCOMPtr<nsIDOMWindow> window = |
239 | 0 | do_QueryInterface(tabParent->GetBrowserDOMWindow()); |
240 | 0 | */ |
241 | 0 | WindowIdentifier newID(id, nullptr); |
242 | 0 | hal::Vibrate(pattern, newID); |
243 | 0 | return IPC_OK(); |
244 | 0 | } |
245 | | |
246 | | virtual mozilla::ipc::IPCResult |
247 | | RecvCancelVibrate(InfallibleTArray<uint64_t> &&id, |
248 | | PBrowserParent *browserParent) override |
249 | 0 | { |
250 | 0 | //TabParent *tabParent = TabParent::GetFrom(browserParent); |
251 | 0 | /* XXXkhuey wtf |
252 | 0 | nsCOMPtr<nsIDOMWindow> window = |
253 | 0 | tabParent->GetBrowserDOMWindow(); |
254 | 0 | */ |
255 | 0 | WindowIdentifier newID(id, nullptr); |
256 | 0 | hal::CancelVibrate(newID); |
257 | 0 | return IPC_OK(); |
258 | 0 | } |
259 | | |
260 | | virtual mozilla::ipc::IPCResult |
261 | 0 | RecvEnableBatteryNotifications() override { |
262 | 0 | // We give all content battery-status permission. |
263 | 0 | hal::RegisterBatteryObserver(this); |
264 | 0 | return IPC_OK(); |
265 | 0 | } |
266 | | |
267 | | virtual mozilla::ipc::IPCResult |
268 | 0 | RecvDisableBatteryNotifications() override { |
269 | 0 | hal::UnregisterBatteryObserver(this); |
270 | 0 | return IPC_OK(); |
271 | 0 | } |
272 | | |
273 | | virtual mozilla::ipc::IPCResult |
274 | 0 | RecvGetCurrentBatteryInformation(BatteryInformation* aBatteryInfo) override { |
275 | 0 | // We give all content battery-status permission. |
276 | 0 | hal::GetCurrentBatteryInformation(aBatteryInfo); |
277 | 0 | return IPC_OK(); |
278 | 0 | } |
279 | | |
280 | 0 | void Notify(const BatteryInformation& aBatteryInfo) override { |
281 | 0 | Unused << SendNotifyBatteryChange(aBatteryInfo); |
282 | 0 | } |
283 | | |
284 | | virtual mozilla::ipc::IPCResult |
285 | 0 | RecvEnableNetworkNotifications() override { |
286 | 0 | // We give all content access to this network-status information. |
287 | 0 | hal::RegisterNetworkObserver(this); |
288 | 0 | return IPC_OK(); |
289 | 0 | } |
290 | | |
291 | | virtual mozilla::ipc::IPCResult |
292 | 0 | RecvDisableNetworkNotifications() override { |
293 | 0 | hal::UnregisterNetworkObserver(this); |
294 | 0 | return IPC_OK(); |
295 | 0 | } |
296 | | |
297 | | virtual mozilla::ipc::IPCResult |
298 | 0 | RecvGetCurrentNetworkInformation(NetworkInformation* aNetworkInfo) override { |
299 | 0 | hal::GetCurrentNetworkInformation(aNetworkInfo); |
300 | 0 | return IPC_OK(); |
301 | 0 | } |
302 | | |
303 | 0 | void Notify(const NetworkInformation& aNetworkInfo) override { |
304 | 0 | Unused << SendNotifyNetworkChange(aNetworkInfo); |
305 | 0 | } |
306 | | |
307 | | virtual mozilla::ipc::IPCResult |
308 | 0 | RecvEnableScreenConfigurationNotifications() override { |
309 | 0 | // Screen configuration is used to implement CSS and DOM |
310 | 0 | // properties, so all content already has access to this. |
311 | 0 | hal::RegisterScreenConfigurationObserver(this); |
312 | 0 | return IPC_OK(); |
313 | 0 | } |
314 | | |
315 | | virtual mozilla::ipc::IPCResult |
316 | 0 | RecvDisableScreenConfigurationNotifications() override { |
317 | 0 | hal::UnregisterScreenConfigurationObserver(this); |
318 | 0 | return IPC_OK(); |
319 | 0 | } |
320 | | |
321 | | virtual mozilla::ipc::IPCResult |
322 | | RecvLockScreenOrientation(const ScreenOrientation& aOrientation, bool* aAllowed) override |
323 | 0 | { |
324 | 0 | // FIXME/bug 777980: unprivileged content may only lock |
325 | 0 | // orientation while fullscreen. We should check whether the |
326 | 0 | // request comes from an actor in a process that might be |
327 | 0 | // fullscreen. We don't have that information currently. |
328 | 0 | *aAllowed = hal::LockScreenOrientation(aOrientation); |
329 | 0 | return IPC_OK(); |
330 | 0 | } |
331 | | |
332 | | virtual mozilla::ipc::IPCResult |
333 | | RecvUnlockScreenOrientation() override |
334 | 0 | { |
335 | 0 | hal::UnlockScreenOrientation(); |
336 | 0 | return IPC_OK(); |
337 | 0 | } |
338 | | |
339 | 0 | void Notify(const ScreenConfiguration& aScreenConfiguration) override { |
340 | 0 | Unused << SendNotifyScreenConfigurationChange(aScreenConfiguration); |
341 | 0 | } |
342 | | |
343 | | virtual mozilla::ipc::IPCResult |
344 | 0 | RecvEnableSensorNotifications(const SensorType &aSensor) override { |
345 | 0 | // We currently allow any content to register device-sensor |
346 | 0 | // listeners. |
347 | 0 | hal::RegisterSensorObserver(aSensor, this); |
348 | 0 | return IPC_OK(); |
349 | 0 | } |
350 | | |
351 | | virtual mozilla::ipc::IPCResult |
352 | 0 | RecvDisableSensorNotifications(const SensorType &aSensor) override { |
353 | 0 | hal::UnregisterSensorObserver(aSensor, this); |
354 | 0 | return IPC_OK(); |
355 | 0 | } |
356 | | |
357 | 0 | void Notify(const SensorData& aSensorData) override { |
358 | 0 | Unused << SendNotifySensorChange(aSensorData); |
359 | 0 | } |
360 | | |
361 | | virtual mozilla::ipc::IPCResult |
362 | | RecvModifyWakeLock(const nsString& aTopic, |
363 | | const WakeLockControl& aLockAdjust, |
364 | | const WakeLockControl& aHiddenAdjust, |
365 | | const uint64_t& aProcessID) override |
366 | 0 | { |
367 | 0 | MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN); |
368 | 0 |
|
369 | 0 | // We allow arbitrary content to use wake locks. |
370 | 0 | hal::ModifyWakeLock(aTopic, aLockAdjust, aHiddenAdjust, aProcessID); |
371 | 0 | return IPC_OK(); |
372 | 0 | } |
373 | | |
374 | | virtual mozilla::ipc::IPCResult |
375 | | RecvEnableWakeLockNotifications() override |
376 | 0 | { |
377 | 0 | // We allow arbitrary content to use wake locks. |
378 | 0 | hal::RegisterWakeLockObserver(this); |
379 | 0 | return IPC_OK(); |
380 | 0 | } |
381 | | |
382 | | virtual mozilla::ipc::IPCResult |
383 | | RecvDisableWakeLockNotifications() override |
384 | 0 | { |
385 | 0 | hal::UnregisterWakeLockObserver(this); |
386 | 0 | return IPC_OK(); |
387 | 0 | } |
388 | | |
389 | | virtual mozilla::ipc::IPCResult |
390 | | RecvGetWakeLockInfo(const nsString &aTopic, WakeLockInformation *aWakeLockInfo) override |
391 | 0 | { |
392 | 0 | hal::GetWakeLockInfo(aTopic, aWakeLockInfo); |
393 | 0 | return IPC_OK(); |
394 | 0 | } |
395 | | |
396 | | void Notify(const WakeLockInformation& aWakeLockInfo) override |
397 | 0 | { |
398 | 0 | Unused << SendNotifyWakeLockChange(aWakeLockInfo); |
399 | 0 | } |
400 | | }; |
401 | | |
402 | | class HalChild : public PHalChild { |
403 | | public: |
404 | | virtual void |
405 | | ActorDestroy(ActorDestroyReason aWhy) override |
406 | 0 | { |
407 | 0 | sHalChildDestroyed = true; |
408 | 0 | } |
409 | | |
410 | | virtual mozilla::ipc::IPCResult |
411 | 0 | RecvNotifyBatteryChange(const BatteryInformation& aBatteryInfo) override { |
412 | 0 | hal::NotifyBatteryChange(aBatteryInfo); |
413 | 0 | return IPC_OK(); |
414 | 0 | } |
415 | | |
416 | | virtual mozilla::ipc::IPCResult |
417 | | RecvNotifySensorChange(const hal::SensorData &aSensorData) override; |
418 | | |
419 | | virtual mozilla::ipc::IPCResult |
420 | 0 | RecvNotifyNetworkChange(const NetworkInformation& aNetworkInfo) override { |
421 | 0 | hal::NotifyNetworkChange(aNetworkInfo); |
422 | 0 | return IPC_OK(); |
423 | 0 | } |
424 | | |
425 | | virtual mozilla::ipc::IPCResult |
426 | 0 | RecvNotifyWakeLockChange(const WakeLockInformation& aWakeLockInfo) override { |
427 | 0 | hal::NotifyWakeLockChange(aWakeLockInfo); |
428 | 0 | return IPC_OK(); |
429 | 0 | } |
430 | | |
431 | | virtual mozilla::ipc::IPCResult |
432 | 0 | RecvNotifyScreenConfigurationChange(const ScreenConfiguration& aScreenConfiguration) override { |
433 | 0 | hal::NotifyScreenConfigurationChange(aScreenConfiguration); |
434 | 0 | return IPC_OK(); |
435 | 0 | } |
436 | | }; |
437 | | |
438 | | mozilla::ipc::IPCResult |
439 | 0 | HalChild::RecvNotifySensorChange(const hal::SensorData &aSensorData) { |
440 | 0 | hal::NotifySensorChange(aSensorData); |
441 | 0 |
|
442 | 0 | return IPC_OK(); |
443 | 0 | } |
444 | | |
445 | 0 | PHalChild* CreateHalChild() { |
446 | 0 | return new HalChild(); |
447 | 0 | } |
448 | | |
449 | 0 | PHalParent* CreateHalParent() { |
450 | 0 | return new HalParent(); |
451 | 0 | } |
452 | | |
453 | | } // namespace hal_sandbox |
454 | | } // namespace mozilla |