123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 |
- /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this file,
- * You can obtain one at http://mozilla.org/MPL/2.0/. */
- #include "Hal.h"
- #include "HalLog.h"
- #include "mozilla/AppProcessChecker.h"
- #include "mozilla/dom/ContentChild.h"
- #include "mozilla/dom/ContentParent.h"
- #include "mozilla/hal_sandbox/PHalChild.h"
- #include "mozilla/hal_sandbox/PHalParent.h"
- #include "mozilla/dom/TabParent.h"
- #include "mozilla/dom/TabChild.h"
- #include "mozilla/dom/network/Types.h"
- #include "mozilla/dom/ScreenOrientation.h"
- #include "mozilla/EnumeratedRange.h"
- #include "mozilla/Observer.h"
- #include "mozilla/Unused.h"
- #include "nsAutoPtr.h"
- #include "WindowIdentifier.h"
- using namespace mozilla;
- using namespace mozilla::dom;
- using namespace mozilla::hal;
- namespace mozilla {
- namespace hal_sandbox {
- static bool sHalChildDestroyed = false;
- bool
- HalChildDestroyed()
- {
- return sHalChildDestroyed;
- }
- static PHalChild* sHal;
- static PHalChild*
- Hal()
- {
- if (!sHal) {
- sHal = ContentChild::GetSingleton()->SendPHalConstructor();
- }
- return sHal;
- }
- void
- EnableNetworkNotifications()
- {
- Hal()->SendEnableNetworkNotifications();
- }
- void
- DisableNetworkNotifications()
- {
- Hal()->SendDisableNetworkNotifications();
- }
- void
- GetCurrentNetworkInformation(NetworkInformation* aNetworkInfo)
- {
- Hal()->SendGetCurrentNetworkInformation(aNetworkInfo);
- }
- void
- EnableScreenConfigurationNotifications()
- {
- Hal()->SendEnableScreenConfigurationNotifications();
- }
- void
- DisableScreenConfigurationNotifications()
- {
- Hal()->SendDisableScreenConfigurationNotifications();
- }
- void
- GetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration)
- {
- Hal()->SendGetCurrentScreenConfiguration(aScreenConfiguration);
- }
- bool
- LockScreenOrientation(const dom::ScreenOrientationInternal& aOrientation)
- {
- bool allowed;
- Hal()->SendLockScreenOrientation(aOrientation, &allowed);
- return allowed;
- }
- void
- UnlockScreenOrientation()
- {
- Hal()->SendUnlockScreenOrientation();
- }
- bool
- GetScreenEnabled()
- {
- bool enabled = false;
- Hal()->SendGetScreenEnabled(&enabled);
- return enabled;
- }
- void
- SetScreenEnabled(bool aEnabled)
- {
- Hal()->SendSetScreenEnabled(aEnabled);
- }
- bool
- GetKeyLightEnabled()
- {
- bool enabled = false;
- Hal()->SendGetKeyLightEnabled(&enabled);
- return enabled;
- }
- void
- SetKeyLightEnabled(bool aEnabled)
- {
- Hal()->SendSetKeyLightEnabled(aEnabled);
- }
- bool
- GetCpuSleepAllowed()
- {
- bool allowed = true;
- Hal()->SendGetCpuSleepAllowed(&allowed);
- return allowed;
- }
- void
- SetCpuSleepAllowed(bool aAllowed)
- {
- Hal()->SendSetCpuSleepAllowed(aAllowed);
- }
- double
- GetScreenBrightness()
- {
- double brightness = 0;
- Hal()->SendGetScreenBrightness(&brightness);
- return brightness;
- }
- void
- SetScreenBrightness(double aBrightness)
- {
- Hal()->SendSetScreenBrightness(aBrightness);
- }
- void
- AdjustSystemClock(int64_t aDeltaMilliseconds)
- {
- Hal()->SendAdjustSystemClock(aDeltaMilliseconds);
- }
- void
- SetTimezone(const nsCString& aTimezoneSpec)
- {
- Hal()->SendSetTimezone(nsCString(aTimezoneSpec));
- }
- nsCString
- GetTimezone()
- {
- nsCString timezone;
- Hal()->SendGetTimezone(&timezone);
- return timezone;
- }
- int32_t
- GetTimezoneOffset()
- {
- int32_t timezoneOffset;
- Hal()->SendGetTimezoneOffset(&timezoneOffset);
- return timezoneOffset;
- }
- void
- EnableSystemClockChangeNotifications()
- {
- Hal()->SendEnableSystemClockChangeNotifications();
- }
- void
- DisableSystemClockChangeNotifications()
- {
- Hal()->SendDisableSystemClockChangeNotifications();
- }
- void
- EnableSystemTimezoneChangeNotifications()
- {
- Hal()->SendEnableSystemTimezoneChangeNotifications();
- }
- void
- DisableSystemTimezoneChangeNotifications()
- {
- Hal()->SendDisableSystemTimezoneChangeNotifications();
- }
- void
- Reboot()
- {
- NS_RUNTIMEABORT("Reboot() can't be called from sandboxed contexts.");
- }
- void
- PowerOff()
- {
- NS_RUNTIMEABORT("PowerOff() can't be called from sandboxed contexts.");
- }
- void
- StartForceQuitWatchdog(ShutdownMode aMode, int32_t aTimeoutSecs)
- {
- NS_RUNTIMEABORT("StartForceQuitWatchdog() can't be called from sandboxed contexts.");
- }
- void
- EnableSensorNotifications(SensorType aSensor) {
- Hal()->SendEnableSensorNotifications(aSensor);
- }
- void
- DisableSensorNotifications(SensorType aSensor) {
- Hal()->SendDisableSensorNotifications(aSensor);
- }
- void
- EnableWakeLockNotifications()
- {
- Hal()->SendEnableWakeLockNotifications();
- }
- void
- DisableWakeLockNotifications()
- {
- Hal()->SendDisableWakeLockNotifications();
- }
- void
- ModifyWakeLock(const nsAString &aTopic,
- WakeLockControl aLockAdjust,
- WakeLockControl aHiddenAdjust,
- uint64_t aProcessID)
- {
- MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
- Hal()->SendModifyWakeLock(nsString(aTopic), aLockAdjust, aHiddenAdjust, aProcessID);
- }
- void
- GetWakeLockInfo(const nsAString &aTopic, WakeLockInformation *aWakeLockInfo)
- {
- Hal()->SendGetWakeLockInfo(nsString(aTopic), aWakeLockInfo);
- }
- bool
- EnableAlarm()
- {
- NS_RUNTIMEABORT("Alarms can't be programmed from sandboxed contexts. Yet.");
- return false;
- }
- void
- DisableAlarm()
- {
- NS_RUNTIMEABORT("Alarms can't be programmed from sandboxed contexts. Yet.");
- }
- bool
- SetAlarm(int32_t aSeconds, int32_t aNanoseconds)
- {
- NS_RUNTIMEABORT("Alarms can't be programmed from sandboxed contexts. Yet.");
- return false;
- }
- void
- SetProcessPriority(int aPid, ProcessPriority aPriority, uint32_t aLRU)
- {
- NS_RUNTIMEABORT("Only the main process may set processes' priorities.");
- }
- void
- SetCurrentThreadPriority(ThreadPriority aThreadPriority)
- {
- NS_RUNTIMEABORT("Setting current thread priority cannot be called from sandboxed contexts.");
- }
- void
- SetThreadPriority(PlatformThreadId aThreadId,
- ThreadPriority aThreadPriority)
- {
- NS_RUNTIMEABORT("Setting thread priority cannot be called from sandboxed contexts.");
- }
- void
- FactoryReset(FactoryResetReason& aReason)
- {
- if (aReason == FactoryResetReason::Normal) {
- Hal()->SendFactoryReset(NS_LITERAL_STRING("normal"));
- } else if (aReason == FactoryResetReason::Wipe) {
- Hal()->SendFactoryReset(NS_LITERAL_STRING("wipe"));
- } else if (aReason == FactoryResetReason::Root) {
- Hal()->SendFactoryReset(NS_LITERAL_STRING("root"));
- }
- }
- bool IsHeadphoneEventFromInputDev()
- {
- NS_RUNTIMEABORT("IsHeadphoneEventFromInputDev() cannot be called from sandboxed contexts.");
- return false;
- }
- nsresult StartSystemService(const char* aSvcName, const char* aArgs)
- {
- NS_RUNTIMEABORT("System services cannot be controlled from sandboxed contexts.");
- return NS_ERROR_NOT_IMPLEMENTED;
- }
- void StopSystemService(const char* aSvcName)
- {
- NS_RUNTIMEABORT("System services cannot be controlled from sandboxed contexts.");
- }
- bool SystemServiceIsRunning(const char* aSvcName)
- {
- NS_RUNTIMEABORT("System services cannot be controlled from sandboxed contexts.");
- return false;
- }
- class HalParent : public PHalParent
- , public NetworkObserver
- , public ISensorObserver
- , public WakeLockObserver
- , public ScreenConfigurationObserver
- , public SystemClockChangeObserver
- , public SystemTimezoneChangeObserver
- {
- public:
- virtual void
- ActorDestroy(ActorDestroyReason aWhy) override
- {
- // NB: you *must* unconditionally unregister your observer here,
- // if it *may* be registered below.
- hal::UnregisterNetworkObserver(this);
- hal::UnregisterScreenConfigurationObserver(this);
- for (auto sensor : MakeEnumeratedRange(NUM_SENSOR_TYPE)) {
- hal::UnregisterSensorObserver(sensor, this);
- }
- hal::UnregisterWakeLockObserver(this);
- hal::UnregisterSystemClockChangeObserver(this);
- hal::UnregisterSystemTimezoneChangeObserver(this);
- }
- virtual bool
- RecvEnableNetworkNotifications() override {
- // We give all content access to this network-status information.
- hal::RegisterNetworkObserver(this);
- return true;
- }
- virtual bool
- RecvDisableNetworkNotifications() override {
- hal::UnregisterNetworkObserver(this);
- return true;
- }
- virtual bool
- RecvGetCurrentNetworkInformation(NetworkInformation* aNetworkInfo) override {
- hal::GetCurrentNetworkInformation(aNetworkInfo);
- return true;
- }
- void Notify(const NetworkInformation& aNetworkInfo) override {
- Unused << SendNotifyNetworkChange(aNetworkInfo);
- }
- virtual bool
- RecvEnableScreenConfigurationNotifications() override {
- // Screen configuration is used to implement CSS and DOM
- // properties, so all content already has access to this.
- hal::RegisterScreenConfigurationObserver(this);
- return true;
- }
- virtual bool
- RecvDisableScreenConfigurationNotifications() override {
- hal::UnregisterScreenConfigurationObserver(this);
- return true;
- }
- virtual bool
- RecvGetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration) override {
- hal::GetCurrentScreenConfiguration(aScreenConfiguration);
- return true;
- }
- virtual bool
- RecvLockScreenOrientation(const dom::ScreenOrientationInternal& aOrientation, bool* aAllowed) override
- {
- // FIXME/bug 777980: unprivileged content may only lock
- // orientation while fullscreen. We should check whether the
- // request comes from an actor in a process that might be
- // fullscreen. We don't have that information currently.
- *aAllowed = hal::LockScreenOrientation(aOrientation);
- return true;
- }
- virtual bool
- RecvUnlockScreenOrientation() override
- {
- hal::UnlockScreenOrientation();
- return true;
- }
- void Notify(const ScreenConfiguration& aScreenConfiguration) override {
- Unused << SendNotifyScreenConfigurationChange(aScreenConfiguration);
- }
- virtual bool
- RecvGetScreenEnabled(bool* aEnabled) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- *aEnabled = hal::GetScreenEnabled();
- return true;
- }
- virtual bool
- RecvSetScreenEnabled(const bool& aEnabled) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- hal::SetScreenEnabled(aEnabled);
- return true;
- }
- virtual bool
- RecvGetKeyLightEnabled(bool* aEnabled) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- *aEnabled = hal::GetKeyLightEnabled();
- return true;
- }
- virtual bool
- RecvSetKeyLightEnabled(const bool& aEnabled) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- hal::SetKeyLightEnabled(aEnabled);
- return true;
- }
- virtual bool
- RecvGetCpuSleepAllowed(bool* aAllowed) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- *aAllowed = hal::GetCpuSleepAllowed();
- return true;
- }
- virtual bool
- RecvSetCpuSleepAllowed(const bool& aAllowed) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- hal::SetCpuSleepAllowed(aAllowed);
- return true;
- }
- virtual bool
- RecvGetScreenBrightness(double* aBrightness) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- *aBrightness = hal::GetScreenBrightness();
- return true;
- }
- virtual bool
- RecvSetScreenBrightness(const double& aBrightness) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- hal::SetScreenBrightness(aBrightness);
- return true;
- }
- virtual bool
- RecvAdjustSystemClock(const int64_t &aDeltaMilliseconds) override
- {
- if (!AssertAppProcessPermission(this, "time")) {
- return false;
- }
- hal::AdjustSystemClock(aDeltaMilliseconds);
- return true;
- }
- virtual bool
- RecvSetTimezone(const nsCString& aTimezoneSpec) override
- {
- if (!AssertAppProcessPermission(this, "time")) {
- return false;
- }
- hal::SetTimezone(aTimezoneSpec);
- return true;
- }
- virtual bool
- RecvGetTimezone(nsCString *aTimezoneSpec) override
- {
- if (!AssertAppProcessPermission(this, "time")) {
- return false;
- }
- *aTimezoneSpec = hal::GetTimezone();
- return true;
- }
- virtual bool
- RecvGetTimezoneOffset(int32_t *aTimezoneOffset) override
- {
- if (!AssertAppProcessPermission(this, "time")) {
- return false;
- }
- *aTimezoneOffset = hal::GetTimezoneOffset();
- return true;
- }
- virtual bool
- RecvEnableSystemClockChangeNotifications() override
- {
- hal::RegisterSystemClockChangeObserver(this);
- return true;
- }
- virtual bool
- RecvDisableSystemClockChangeNotifications() override
- {
- hal::UnregisterSystemClockChangeObserver(this);
- return true;
- }
- virtual bool
- RecvEnableSystemTimezoneChangeNotifications() override
- {
- hal::RegisterSystemTimezoneChangeObserver(this);
- return true;
- }
- virtual bool
- RecvDisableSystemTimezoneChangeNotifications() override
- {
- hal::UnregisterSystemTimezoneChangeObserver(this);
- return true;
- }
- virtual bool
- RecvEnableSensorNotifications(const SensorType &aSensor) override {
- // We currently allow any content to register device-sensor
- // listeners.
- hal::RegisterSensorObserver(aSensor, this);
- return true;
- }
- virtual bool
- RecvDisableSensorNotifications(const SensorType &aSensor) override {
- hal::UnregisterSensorObserver(aSensor, this);
- return true;
- }
- void Notify(const SensorData& aSensorData) override {
- Unused << SendNotifySensorChange(aSensorData);
- }
- virtual bool
- RecvModifyWakeLock(const nsString& aTopic,
- const WakeLockControl& aLockAdjust,
- const WakeLockControl& aHiddenAdjust,
- const uint64_t& aProcessID) override
- {
- MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
- // We allow arbitrary content to use wake locks.
- hal::ModifyWakeLock(aTopic, aLockAdjust, aHiddenAdjust, aProcessID);
- return true;
- }
- virtual bool
- RecvEnableWakeLockNotifications() override
- {
- // We allow arbitrary content to use wake locks.
- hal::RegisterWakeLockObserver(this);
- return true;
- }
- virtual bool
- RecvDisableWakeLockNotifications() override
- {
- hal::UnregisterWakeLockObserver(this);
- return true;
- }
- virtual bool
- RecvGetWakeLockInfo(const nsString &aTopic, WakeLockInformation *aWakeLockInfo) override
- {
- hal::GetWakeLockInfo(aTopic, aWakeLockInfo);
- return true;
- }
- void Notify(const WakeLockInformation& aWakeLockInfo) override
- {
- Unused << SendNotifyWakeLockChange(aWakeLockInfo);
- }
- void Notify(const int64_t& aClockDeltaMS) override
- {
- Unused << SendNotifySystemClockChange(aClockDeltaMS);
- }
- void Notify(const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo) override
- {
- Unused << SendNotifySystemTimezoneChange(aSystemTimezoneChangeInfo);
- }
- virtual bool
- RecvFactoryReset(const nsString& aReason) override
- {
- if (!AssertAppProcessPermission(this, "power")) {
- return false;
- }
- FactoryResetReason reason = FactoryResetReason::Normal;
- if (aReason.EqualsLiteral("normal")) {
- reason = FactoryResetReason::Normal;
- } else if (aReason.EqualsLiteral("wipe")) {
- reason = FactoryResetReason::Wipe;
- } else if (aReason.EqualsLiteral("root")) {
- reason = FactoryResetReason::Root;
- } else {
- // Invalid factory reset reason. That should never happen.
- return false;
- }
- hal::FactoryReset(reason);
- return true;
- }
- };
- class HalChild : public PHalChild {
- public:
- virtual void
- ActorDestroy(ActorDestroyReason aWhy) override
- {
- sHalChildDestroyed = true;
- }
- virtual bool
- RecvNotifySensorChange(const hal::SensorData &aSensorData) override;
- virtual bool
- RecvNotifyNetworkChange(const NetworkInformation& aNetworkInfo) override {
- hal::NotifyNetworkChange(aNetworkInfo);
- return true;
- }
- virtual bool
- RecvNotifyWakeLockChange(const WakeLockInformation& aWakeLockInfo) override {
- hal::NotifyWakeLockChange(aWakeLockInfo);
- return true;
- }
- virtual bool
- RecvNotifyScreenConfigurationChange(const ScreenConfiguration& aScreenConfiguration) override {
- hal::NotifyScreenConfigurationChange(aScreenConfiguration);
- return true;
- }
- virtual bool
- RecvNotifySystemClockChange(const int64_t& aClockDeltaMS) override {
- hal::NotifySystemClockChange(aClockDeltaMS);
- return true;
- }
- virtual bool
- RecvNotifySystemTimezoneChange(
- const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo) override {
- hal::NotifySystemTimezoneChange(aSystemTimezoneChangeInfo);
- return true;
- }
- };
- bool
- HalChild::RecvNotifySensorChange(const hal::SensorData &aSensorData) {
- hal::NotifySensorChange(aSensorData);
- return true;
- }
- PHalChild* CreateHalChild() {
- return new HalChild();
- }
- PHalParent* CreateHalParent() {
- return new HalParent();
- }
- } // namespace hal_sandbox
- } // namespace mozilla
|