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