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/. */
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/BrowserParent.h"
14 #include "mozilla/dom/BrowserChild.h"
15 #include "mozilla/EnumeratedRange.h"
16 #include "mozilla/HalWakeLock.h"
17 #include "mozilla/Observer.h"
18 #include "mozilla/Unused.h"
19 #include "WindowIdentifier.h"
21 using namespace mozilla
;
22 using namespace mozilla::dom
;
23 using namespace mozilla::hal
;
26 namespace hal_sandbox
{
28 static bool sHalChildDestroyed
= false;
30 bool HalChildDestroyed() { return sHalChildDestroyed
; }
32 static PHalChild
* sHal
;
33 static PHalChild
* Hal() {
35 sHal
= ContentChild::GetSingleton()->SendPHalConstructor();
40 void Vibrate(const nsTArray
<uint32_t>& pattern
, WindowIdentifier
&& id
) {
41 HAL_LOG("Vibrate: Sending to parent process.");
43 WindowIdentifier
newID(std::move(id
));
44 newID
.AppendProcessID();
45 if (BrowserChild
* bc
= BrowserChild::GetFrom(newID
.GetWindow())) {
46 Hal()->SendVibrate(pattern
, newID
.AsArray(), WrapNotNull(bc
));
50 void CancelVibrate(WindowIdentifier
&& id
) {
51 HAL_LOG("CancelVibrate: Sending to parent process.");
53 WindowIdentifier
newID(std::move(id
));
54 newID
.AppendProcessID();
55 if (BrowserChild
* bc
= BrowserChild::GetFrom(newID
.GetWindow())) {
56 Hal()->SendCancelVibrate(newID
.AsArray(), WrapNotNull(bc
));
60 void EnableBatteryNotifications() { Hal()->SendEnableBatteryNotifications(); }
62 void DisableBatteryNotifications() { Hal()->SendDisableBatteryNotifications(); }
64 void GetCurrentBatteryInformation(BatteryInformation
* aBatteryInfo
) {
65 Hal()->SendGetCurrentBatteryInformation(aBatteryInfo
);
68 void EnableNetworkNotifications() { Hal()->SendEnableNetworkNotifications(); }
70 void DisableNetworkNotifications() { Hal()->SendDisableNetworkNotifications(); }
72 void GetCurrentNetworkInformation(NetworkInformation
* aNetworkInfo
) {
73 Hal()->SendGetCurrentNetworkInformation(aNetworkInfo
);
76 RefPtr
<GenericNonExclusivePromise
> LockScreenOrientation(
77 const hal::ScreenOrientation
& aOrientation
) {
79 ->SendLockScreenOrientation(aOrientation
)
80 ->Then(GetCurrentSerialEventTarget(), __func__
,
81 [](const mozilla::MozPromise
<nsresult
, ipc::ResponseRejectReason
,
82 true>::ResolveOrRejectValue
& aValue
) {
83 if (aValue
.IsResolve()) {
84 if (NS_SUCCEEDED(aValue
.ResolveValue())) {
85 return GenericNonExclusivePromise::CreateAndResolve(
88 return GenericNonExclusivePromise::CreateAndReject(
89 aValue
.ResolveValue(), __func__
);
91 return GenericNonExclusivePromise::CreateAndReject(
92 NS_ERROR_FAILURE
, __func__
);
96 void UnlockScreenOrientation() { Hal()->SendUnlockScreenOrientation(); }
98 void EnableSensorNotifications(SensorType aSensor
) {
99 Hal()->SendEnableSensorNotifications(aSensor
);
102 void DisableSensorNotifications(SensorType aSensor
) {
103 Hal()->SendDisableSensorNotifications(aSensor
);
106 void EnableWakeLockNotifications() { Hal()->SendEnableWakeLockNotifications(); }
108 void DisableWakeLockNotifications() {
109 Hal()->SendDisableWakeLockNotifications();
112 void ModifyWakeLock(const nsAString
& aTopic
, WakeLockControl aLockAdjust
,
113 WakeLockControl aHiddenAdjust
) {
114 Hal()->SendModifyWakeLock(aTopic
, aLockAdjust
, aHiddenAdjust
);
117 void GetWakeLockInfo(const nsAString
& aTopic
,
118 WakeLockInformation
* aWakeLockInfo
) {
119 Hal()->SendGetWakeLockInfo(aTopic
, aWakeLockInfo
);
123 MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet.");
126 void DisableAlarm() {
127 MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet.");
130 bool SetAlarm(int32_t aSeconds
, int32_t aNanoseconds
) {
131 MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet.");
134 void SetProcessPriority(int aPid
, ProcessPriority aPriority
) {
135 MOZ_CRASH("Only the main process may set processes' priorities.");
138 class HalParent
: public PHalParent
,
139 public BatteryObserver
,
140 public NetworkObserver
,
141 public ISensorObserver
,
142 public WakeLockObserver
{
144 virtual void ActorDestroy(ActorDestroyReason aWhy
) override
{
145 // NB: you *must* unconditionally unregister your observer here,
146 // if it *may* be registered below.
147 hal::UnregisterBatteryObserver(this);
148 hal::UnregisterNetworkObserver(this);
149 for (auto sensor
: MakeEnumeratedRange(NUM_SENSOR_TYPE
)) {
150 hal::UnregisterSensorObserver(sensor
, this);
152 hal::UnregisterWakeLockObserver(this);
155 virtual mozilla::ipc::IPCResult
RecvVibrate(
156 nsTArray
<unsigned int>&& pattern
, nsTArray
<uint64_t>&& id
,
157 NotNull
<PBrowserParent
*> browserParent
) override
{
158 // We give all content vibration permission.
159 // BrowserParent *browserParent = BrowserParent::GetFrom(browserParent);
161 nsCOMPtr<nsIDOMWindow> window =
162 do_QueryInterface(browserParent->GetBrowserDOMWindow());
164 hal::Vibrate(pattern
, WindowIdentifier(std::move(id
), nullptr));
168 virtual mozilla::ipc::IPCResult
RecvCancelVibrate(
169 nsTArray
<uint64_t>&& id
,
170 NotNull
<PBrowserParent
*> browserParent
) override
{
171 // BrowserParent *browserParent = BrowserParent::GetFrom(browserParent);
173 nsCOMPtr<nsIDOMWindow> window =
174 browserParent->GetBrowserDOMWindow();
176 hal::CancelVibrate(WindowIdentifier(std::move(id
), nullptr));
180 virtual mozilla::ipc::IPCResult
RecvEnableBatteryNotifications() override
{
181 // We give all content battery-status permission.
182 hal::RegisterBatteryObserver(this);
186 virtual mozilla::ipc::IPCResult
RecvDisableBatteryNotifications() override
{
187 hal::UnregisterBatteryObserver(this);
191 virtual mozilla::ipc::IPCResult
RecvGetCurrentBatteryInformation(
192 BatteryInformation
* aBatteryInfo
) override
{
193 // We give all content battery-status permission.
194 hal::GetCurrentBatteryInformation(aBatteryInfo
);
198 void Notify(const BatteryInformation
& aBatteryInfo
) override
{
199 Unused
<< SendNotifyBatteryChange(aBatteryInfo
);
202 virtual mozilla::ipc::IPCResult
RecvEnableNetworkNotifications() override
{
203 // We give all content access to this network-status information.
204 hal::RegisterNetworkObserver(this);
208 virtual mozilla::ipc::IPCResult
RecvDisableNetworkNotifications() override
{
209 hal::UnregisterNetworkObserver(this);
213 virtual mozilla::ipc::IPCResult
RecvGetCurrentNetworkInformation(
214 NetworkInformation
* aNetworkInfo
) override
{
215 hal::GetCurrentNetworkInformation(aNetworkInfo
);
219 void Notify(const NetworkInformation
& aNetworkInfo
) override
{
220 Unused
<< SendNotifyNetworkChange(aNetworkInfo
);
223 virtual mozilla::ipc::IPCResult
RecvLockScreenOrientation(
224 const ScreenOrientation
& aOrientation
,
225 LockScreenOrientationResolver
&& aResolve
) override
{
226 // FIXME/bug 777980: unprivileged content may only lock
227 // orientation while fullscreen. We should check whether the
228 // request comes from an actor in a process that might be
229 // fullscreen. We don't have that information currently.
231 hal::LockScreenOrientation(aOrientation
)
233 GetMainThreadSerialEventTarget(), __func__
,
234 [aResolve
](const GenericNonExclusivePromise::ResolveOrRejectValue
&
236 if (aValue
.IsResolve()) {
237 MOZ_ASSERT(aValue
.ResolveValue());
241 aResolve(aValue
.RejectValue());
246 virtual mozilla::ipc::IPCResult
RecvUnlockScreenOrientation() override
{
247 hal::UnlockScreenOrientation();
251 virtual mozilla::ipc::IPCResult
RecvEnableSensorNotifications(
252 const SensorType
& aSensor
) override
{
253 // We currently allow any content to register device-sensor
255 hal::RegisterSensorObserver(aSensor
, this);
259 virtual mozilla::ipc::IPCResult
RecvDisableSensorNotifications(
260 const SensorType
& aSensor
) override
{
261 hal::UnregisterSensorObserver(aSensor
, this);
265 void Notify(const SensorData
& aSensorData
) override
{
266 Unused
<< SendNotifySensorChange(aSensorData
);
269 virtual mozilla::ipc::IPCResult
RecvModifyWakeLock(
270 const nsAString
& aTopic
, const WakeLockControl
& aLockAdjust
,
271 const WakeLockControl
& aHiddenAdjust
) override
{
272 // We allow arbitrary content to use wake locks.
273 uint64_t id
= static_cast<ContentParent
*>(Manager())->ChildID();
274 hal_impl::ModifyWakeLockWithChildID(aTopic
, aLockAdjust
, aHiddenAdjust
, id
);
278 virtual mozilla::ipc::IPCResult
RecvEnableWakeLockNotifications() override
{
279 // We allow arbitrary content to use wake locks.
280 hal::RegisterWakeLockObserver(this);
284 virtual mozilla::ipc::IPCResult
RecvDisableWakeLockNotifications() override
{
285 hal::UnregisterWakeLockObserver(this);
289 virtual mozilla::ipc::IPCResult
RecvGetWakeLockInfo(
290 const nsAString
& aTopic
, WakeLockInformation
* aWakeLockInfo
) override
{
291 hal::GetWakeLockInfo(aTopic
, aWakeLockInfo
);
295 void Notify(const WakeLockInformation
& aWakeLockInfo
) override
{
296 Unused
<< SendNotifyWakeLockChange(aWakeLockInfo
);
300 class HalChild
: public PHalChild
{
302 virtual void ActorDestroy(ActorDestroyReason aWhy
) override
{
303 sHalChildDestroyed
= true;
306 virtual mozilla::ipc::IPCResult
RecvNotifyBatteryChange(
307 const BatteryInformation
& aBatteryInfo
) override
{
308 hal::NotifyBatteryChange(aBatteryInfo
);
312 virtual mozilla::ipc::IPCResult
RecvNotifySensorChange(
313 const hal::SensorData
& aSensorData
) override
;
315 virtual mozilla::ipc::IPCResult
RecvNotifyNetworkChange(
316 const NetworkInformation
& aNetworkInfo
) override
{
317 hal::NotifyNetworkChange(aNetworkInfo
);
321 virtual mozilla::ipc::IPCResult
RecvNotifyWakeLockChange(
322 const WakeLockInformation
& aWakeLockInfo
) override
{
323 hal::NotifyWakeLockChange(aWakeLockInfo
);
328 mozilla::ipc::IPCResult
HalChild::RecvNotifySensorChange(
329 const hal::SensorData
& aSensorData
) {
330 hal::NotifySensorChange(aSensorData
);
335 PHalChild
* CreateHalChild() { return new HalChild(); }
337 PHalParent
* CreateHalParent() { return new HalParent(); }
339 } // namespace hal_sandbox
340 } // namespace mozilla