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/hal_sandbox/PHalChild.h"
11 #include "mozilla/hal_sandbox/PHalParent.h"
12 #include "mozilla/dom/BrowserParent.h"
13 #include "mozilla/dom/BrowserChild.h"
14 #include "mozilla/EnumeratedRange.h"
15 #include "mozilla/Observer.h"
16 #include "mozilla/Unused.h"
17 #include "WindowIdentifier.h"
19 using namespace mozilla
;
20 using namespace mozilla::dom
;
21 using namespace mozilla::hal
;
24 namespace hal_sandbox
{
26 static bool sHalChildDestroyed
= false;
28 bool HalChildDestroyed() { return sHalChildDestroyed
; }
30 static PHalChild
* sHal
;
31 static PHalChild
* Hal() {
33 sHal
= ContentChild::GetSingleton()->SendPHalConstructor();
38 void Vibrate(const nsTArray
<uint32_t>& pattern
, WindowIdentifier
&& id
) {
39 HAL_LOG("Vibrate: Sending to parent process.");
41 WindowIdentifier
newID(std::move(id
));
42 newID
.AppendProcessID();
43 if (BrowserChild
* bc
= BrowserChild::GetFrom(newID
.GetWindow())) {
44 Hal()->SendVibrate(pattern
, newID
.AsArray(), WrapNotNull(bc
));
48 void CancelVibrate(WindowIdentifier
&& id
) {
49 HAL_LOG("CancelVibrate: Sending to parent process.");
51 WindowIdentifier
newID(std::move(id
));
52 newID
.AppendProcessID();
53 if (BrowserChild
* bc
= BrowserChild::GetFrom(newID
.GetWindow())) {
54 Hal()->SendCancelVibrate(newID
.AsArray(), WrapNotNull(bc
));
58 void EnableBatteryNotifications() { Hal()->SendEnableBatteryNotifications(); }
60 void DisableBatteryNotifications() { Hal()->SendDisableBatteryNotifications(); }
62 void GetCurrentBatteryInformation(BatteryInformation
* aBatteryInfo
) {
63 Hal()->SendGetCurrentBatteryInformation(aBatteryInfo
);
66 void EnableNetworkNotifications() { Hal()->SendEnableNetworkNotifications(); }
68 void DisableNetworkNotifications() { Hal()->SendDisableNetworkNotifications(); }
70 void GetCurrentNetworkInformation(NetworkInformation
* aNetworkInfo
) {
71 Hal()->SendGetCurrentNetworkInformation(aNetworkInfo
);
74 RefPtr
<GenericNonExclusivePromise
> LockScreenOrientation(
75 const hal::ScreenOrientation
& aOrientation
) {
77 ->SendLockScreenOrientation(aOrientation
)
78 ->Then(GetCurrentSerialEventTarget(), __func__
,
79 [](const mozilla::MozPromise
<nsresult
, ipc::ResponseRejectReason
,
80 true>::ResolveOrRejectValue
& aValue
) {
81 if (aValue
.IsResolve()) {
82 if (NS_SUCCEEDED(aValue
.ResolveValue())) {
83 return GenericNonExclusivePromise::CreateAndResolve(
86 return GenericNonExclusivePromise::CreateAndReject(
87 aValue
.ResolveValue(), __func__
);
89 return GenericNonExclusivePromise::CreateAndReject(
90 NS_ERROR_FAILURE
, __func__
);
94 void UnlockScreenOrientation() { Hal()->SendUnlockScreenOrientation(); }
96 void EnableSensorNotifications(SensorType aSensor
) {
97 Hal()->SendEnableSensorNotifications(aSensor
);
100 void DisableSensorNotifications(SensorType aSensor
) {
101 Hal()->SendDisableSensorNotifications(aSensor
);
104 void EnableWakeLockNotifications() { Hal()->SendEnableWakeLockNotifications(); }
106 void DisableWakeLockNotifications() {
107 Hal()->SendDisableWakeLockNotifications();
110 void ModifyWakeLock(const nsAString
& aTopic
, WakeLockControl aLockAdjust
,
111 WakeLockControl aHiddenAdjust
, uint64_t aProcessID
) {
112 MOZ_ASSERT(aProcessID
!= CONTENT_PROCESS_ID_UNKNOWN
);
113 Hal()->SendModifyWakeLock(aTopic
, aLockAdjust
, aHiddenAdjust
, aProcessID
);
116 void GetWakeLockInfo(const nsAString
& aTopic
,
117 WakeLockInformation
* aWakeLockInfo
) {
118 Hal()->SendGetWakeLockInfo(aTopic
, aWakeLockInfo
);
122 MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet.");
125 void DisableAlarm() {
126 MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet.");
129 bool SetAlarm(int32_t aSeconds
, int32_t aNanoseconds
) {
130 MOZ_CRASH("Alarms can't be programmed from sandboxed contexts. Yet.");
133 void SetProcessPriority(int aPid
, ProcessPriority aPriority
) {
134 MOZ_CRASH("Only the main process may set processes' priorities.");
137 class HalParent
: public PHalParent
,
138 public BatteryObserver
,
139 public NetworkObserver
,
140 public ISensorObserver
,
141 public WakeLockObserver
{
143 virtual void ActorDestroy(ActorDestroyReason aWhy
) override
{
144 // NB: you *must* unconditionally unregister your observer here,
145 // if it *may* be registered below.
146 hal::UnregisterBatteryObserver(this);
147 hal::UnregisterNetworkObserver(this);
148 for (auto sensor
: MakeEnumeratedRange(NUM_SENSOR_TYPE
)) {
149 hal::UnregisterSensorObserver(sensor
, this);
151 hal::UnregisterWakeLockObserver(this);
154 virtual mozilla::ipc::IPCResult
RecvVibrate(
155 nsTArray
<unsigned int>&& pattern
, nsTArray
<uint64_t>&& id
,
156 NotNull
<PBrowserParent
*> browserParent
) override
{
157 // We give all content vibration permission.
158 // BrowserParent *browserParent = BrowserParent::GetFrom(browserParent);
160 nsCOMPtr<nsIDOMWindow> window =
161 do_QueryInterface(browserParent->GetBrowserDOMWindow());
163 hal::Vibrate(pattern
, WindowIdentifier(std::move(id
), nullptr));
167 virtual mozilla::ipc::IPCResult
RecvCancelVibrate(
168 nsTArray
<uint64_t>&& id
,
169 NotNull
<PBrowserParent
*> browserParent
) override
{
170 // BrowserParent *browserParent = BrowserParent::GetFrom(browserParent);
172 nsCOMPtr<nsIDOMWindow> window =
173 browserParent->GetBrowserDOMWindow();
175 hal::CancelVibrate(WindowIdentifier(std::move(id
), nullptr));
179 virtual mozilla::ipc::IPCResult
RecvEnableBatteryNotifications() override
{
180 // We give all content battery-status permission.
181 hal::RegisterBatteryObserver(this);
185 virtual mozilla::ipc::IPCResult
RecvDisableBatteryNotifications() override
{
186 hal::UnregisterBatteryObserver(this);
190 virtual mozilla::ipc::IPCResult
RecvGetCurrentBatteryInformation(
191 BatteryInformation
* aBatteryInfo
) override
{
192 // We give all content battery-status permission.
193 hal::GetCurrentBatteryInformation(aBatteryInfo
);
197 void Notify(const BatteryInformation
& aBatteryInfo
) override
{
198 Unused
<< SendNotifyBatteryChange(aBatteryInfo
);
201 virtual mozilla::ipc::IPCResult
RecvEnableNetworkNotifications() override
{
202 // We give all content access to this network-status information.
203 hal::RegisterNetworkObserver(this);
207 virtual mozilla::ipc::IPCResult
RecvDisableNetworkNotifications() override
{
208 hal::UnregisterNetworkObserver(this);
212 virtual mozilla::ipc::IPCResult
RecvGetCurrentNetworkInformation(
213 NetworkInformation
* aNetworkInfo
) override
{
214 hal::GetCurrentNetworkInformation(aNetworkInfo
);
218 void Notify(const NetworkInformation
& aNetworkInfo
) override
{
219 Unused
<< SendNotifyNetworkChange(aNetworkInfo
);
222 virtual mozilla::ipc::IPCResult
RecvLockScreenOrientation(
223 const ScreenOrientation
& aOrientation
,
224 LockScreenOrientationResolver
&& aResolve
) override
{
225 // FIXME/bug 777980: unprivileged content may only lock
226 // orientation while fullscreen. We should check whether the
227 // request comes from an actor in a process that might be
228 // fullscreen. We don't have that information currently.
230 hal::LockScreenOrientation(aOrientation
)
232 GetMainThreadSerialEventTarget(), __func__
,
233 [aResolve
](const GenericNonExclusivePromise::ResolveOrRejectValue
&
235 if (aValue
.IsResolve()) {
236 MOZ_ASSERT(aValue
.ResolveValue());
240 aResolve(aValue
.RejectValue());
245 virtual mozilla::ipc::IPCResult
RecvUnlockScreenOrientation() override
{
246 hal::UnlockScreenOrientation();
250 virtual mozilla::ipc::IPCResult
RecvEnableSensorNotifications(
251 const SensorType
& aSensor
) override
{
252 // We currently allow any content to register device-sensor
254 hal::RegisterSensorObserver(aSensor
, this);
258 virtual mozilla::ipc::IPCResult
RecvDisableSensorNotifications(
259 const SensorType
& aSensor
) override
{
260 hal::UnregisterSensorObserver(aSensor
, this);
264 void Notify(const SensorData
& aSensorData
) override
{
265 Unused
<< SendNotifySensorChange(aSensorData
);
268 virtual mozilla::ipc::IPCResult
RecvModifyWakeLock(
269 const nsAString
& aTopic
, const WakeLockControl
& aLockAdjust
,
270 const WakeLockControl
& aHiddenAdjust
,
271 const uint64_t& aProcessID
) override
{
272 MOZ_ASSERT(aProcessID
!= CONTENT_PROCESS_ID_UNKNOWN
);
274 // We allow arbitrary content to use wake locks.
275 hal::ModifyWakeLock(aTopic
, aLockAdjust
, aHiddenAdjust
, aProcessID
);
279 virtual mozilla::ipc::IPCResult
RecvEnableWakeLockNotifications() override
{
280 // We allow arbitrary content to use wake locks.
281 hal::RegisterWakeLockObserver(this);
285 virtual mozilla::ipc::IPCResult
RecvDisableWakeLockNotifications() override
{
286 hal::UnregisterWakeLockObserver(this);
290 virtual mozilla::ipc::IPCResult
RecvGetWakeLockInfo(
291 const nsAString
& aTopic
, WakeLockInformation
* aWakeLockInfo
) override
{
292 hal::GetWakeLockInfo(aTopic
, aWakeLockInfo
);
296 void Notify(const WakeLockInformation
& aWakeLockInfo
) override
{
297 Unused
<< SendNotifyWakeLockChange(aWakeLockInfo
);
301 class HalChild
: public PHalChild
{
303 virtual void ActorDestroy(ActorDestroyReason aWhy
) override
{
304 sHalChildDestroyed
= true;
307 virtual mozilla::ipc::IPCResult
RecvNotifyBatteryChange(
308 const BatteryInformation
& aBatteryInfo
) override
{
309 hal::NotifyBatteryChange(aBatteryInfo
);
313 virtual mozilla::ipc::IPCResult
RecvNotifySensorChange(
314 const hal::SensorData
& aSensorData
) override
;
316 virtual mozilla::ipc::IPCResult
RecvNotifyNetworkChange(
317 const NetworkInformation
& aNetworkInfo
) override
{
318 hal::NotifyNetworkChange(aNetworkInfo
);
322 virtual mozilla::ipc::IPCResult
RecvNotifyWakeLockChange(
323 const WakeLockInformation
& aWakeLockInfo
) override
{
324 hal::NotifyWakeLockChange(aWakeLockInfo
);
329 mozilla::ipc::IPCResult
HalChild::RecvNotifySensorChange(
330 const hal::SensorData
& aSensorData
) {
331 hal::NotifySensorChange(aSensorData
);
336 PHalChild
* CreateHalChild() { return new HalChild(); }
338 PHalParent
* CreateHalParent() { return new HalParent(); }
340 } // namespace hal_sandbox
341 } // namespace mozilla