SandboxHal.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "Hal.h"
  6. #include "HalLog.h"
  7. #include "mozilla/AppProcessChecker.h"
  8. #include "mozilla/dom/ContentChild.h"
  9. #include "mozilla/dom/ContentParent.h"
  10. #include "mozilla/hal_sandbox/PHalChild.h"
  11. #include "mozilla/hal_sandbox/PHalParent.h"
  12. #include "mozilla/dom/TabParent.h"
  13. #include "mozilla/dom/TabChild.h"
  14. #include "mozilla/dom/network/Types.h"
  15. #include "mozilla/dom/ScreenOrientation.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. using namespace mozilla;
  22. using namespace mozilla::dom;
  23. using namespace mozilla::hal;
  24. namespace mozilla {
  25. namespace hal_sandbox {
  26. static bool sHalChildDestroyed = false;
  27. bool
  28. HalChildDestroyed()
  29. {
  30. return sHalChildDestroyed;
  31. }
  32. static PHalChild* sHal;
  33. static PHalChild*
  34. Hal()
  35. {
  36. if (!sHal) {
  37. sHal = ContentChild::GetSingleton()->SendPHalConstructor();
  38. }
  39. return sHal;
  40. }
  41. void
  42. EnableNetworkNotifications()
  43. {
  44. Hal()->SendEnableNetworkNotifications();
  45. }
  46. void
  47. DisableNetworkNotifications()
  48. {
  49. Hal()->SendDisableNetworkNotifications();
  50. }
  51. void
  52. GetCurrentNetworkInformation(NetworkInformation* aNetworkInfo)
  53. {
  54. Hal()->SendGetCurrentNetworkInformation(aNetworkInfo);
  55. }
  56. void
  57. EnableScreenConfigurationNotifications()
  58. {
  59. Hal()->SendEnableScreenConfigurationNotifications();
  60. }
  61. void
  62. DisableScreenConfigurationNotifications()
  63. {
  64. Hal()->SendDisableScreenConfigurationNotifications();
  65. }
  66. void
  67. GetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration)
  68. {
  69. Hal()->SendGetCurrentScreenConfiguration(aScreenConfiguration);
  70. }
  71. bool
  72. LockScreenOrientation(const dom::ScreenOrientationInternal& aOrientation)
  73. {
  74. bool allowed;
  75. Hal()->SendLockScreenOrientation(aOrientation, &allowed);
  76. return allowed;
  77. }
  78. void
  79. UnlockScreenOrientation()
  80. {
  81. Hal()->SendUnlockScreenOrientation();
  82. }
  83. bool
  84. GetScreenEnabled()
  85. {
  86. bool enabled = false;
  87. Hal()->SendGetScreenEnabled(&enabled);
  88. return enabled;
  89. }
  90. void
  91. SetScreenEnabled(bool aEnabled)
  92. {
  93. Hal()->SendSetScreenEnabled(aEnabled);
  94. }
  95. bool
  96. GetKeyLightEnabled()
  97. {
  98. bool enabled = false;
  99. Hal()->SendGetKeyLightEnabled(&enabled);
  100. return enabled;
  101. }
  102. void
  103. SetKeyLightEnabled(bool aEnabled)
  104. {
  105. Hal()->SendSetKeyLightEnabled(aEnabled);
  106. }
  107. bool
  108. GetCpuSleepAllowed()
  109. {
  110. bool allowed = true;
  111. Hal()->SendGetCpuSleepAllowed(&allowed);
  112. return allowed;
  113. }
  114. void
  115. SetCpuSleepAllowed(bool aAllowed)
  116. {
  117. Hal()->SendSetCpuSleepAllowed(aAllowed);
  118. }
  119. double
  120. GetScreenBrightness()
  121. {
  122. double brightness = 0;
  123. Hal()->SendGetScreenBrightness(&brightness);
  124. return brightness;
  125. }
  126. void
  127. SetScreenBrightness(double aBrightness)
  128. {
  129. Hal()->SendSetScreenBrightness(aBrightness);
  130. }
  131. void
  132. AdjustSystemClock(int64_t aDeltaMilliseconds)
  133. {
  134. Hal()->SendAdjustSystemClock(aDeltaMilliseconds);
  135. }
  136. void
  137. SetTimezone(const nsCString& aTimezoneSpec)
  138. {
  139. Hal()->SendSetTimezone(nsCString(aTimezoneSpec));
  140. }
  141. nsCString
  142. GetTimezone()
  143. {
  144. nsCString timezone;
  145. Hal()->SendGetTimezone(&timezone);
  146. return timezone;
  147. }
  148. int32_t
  149. GetTimezoneOffset()
  150. {
  151. int32_t timezoneOffset;
  152. Hal()->SendGetTimezoneOffset(&timezoneOffset);
  153. return timezoneOffset;
  154. }
  155. void
  156. EnableSystemClockChangeNotifications()
  157. {
  158. Hal()->SendEnableSystemClockChangeNotifications();
  159. }
  160. void
  161. DisableSystemClockChangeNotifications()
  162. {
  163. Hal()->SendDisableSystemClockChangeNotifications();
  164. }
  165. void
  166. EnableSystemTimezoneChangeNotifications()
  167. {
  168. Hal()->SendEnableSystemTimezoneChangeNotifications();
  169. }
  170. void
  171. DisableSystemTimezoneChangeNotifications()
  172. {
  173. Hal()->SendDisableSystemTimezoneChangeNotifications();
  174. }
  175. void
  176. Reboot()
  177. {
  178. NS_RUNTIMEABORT("Reboot() can't be called from sandboxed contexts.");
  179. }
  180. void
  181. PowerOff()
  182. {
  183. NS_RUNTIMEABORT("PowerOff() can't be called from sandboxed contexts.");
  184. }
  185. void
  186. StartForceQuitWatchdog(ShutdownMode aMode, int32_t aTimeoutSecs)
  187. {
  188. NS_RUNTIMEABORT("StartForceQuitWatchdog() can't be called from sandboxed contexts.");
  189. }
  190. void
  191. EnableSensorNotifications(SensorType aSensor) {
  192. Hal()->SendEnableSensorNotifications(aSensor);
  193. }
  194. void
  195. DisableSensorNotifications(SensorType aSensor) {
  196. Hal()->SendDisableSensorNotifications(aSensor);
  197. }
  198. void
  199. EnableWakeLockNotifications()
  200. {
  201. Hal()->SendEnableWakeLockNotifications();
  202. }
  203. void
  204. DisableWakeLockNotifications()
  205. {
  206. Hal()->SendDisableWakeLockNotifications();
  207. }
  208. void
  209. ModifyWakeLock(const nsAString &aTopic,
  210. WakeLockControl aLockAdjust,
  211. WakeLockControl aHiddenAdjust,
  212. uint64_t aProcessID)
  213. {
  214. MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
  215. Hal()->SendModifyWakeLock(nsString(aTopic), aLockAdjust, aHiddenAdjust, aProcessID);
  216. }
  217. void
  218. GetWakeLockInfo(const nsAString &aTopic, WakeLockInformation *aWakeLockInfo)
  219. {
  220. Hal()->SendGetWakeLockInfo(nsString(aTopic), aWakeLockInfo);
  221. }
  222. bool
  223. EnableAlarm()
  224. {
  225. NS_RUNTIMEABORT("Alarms can't be programmed from sandboxed contexts. Yet.");
  226. return false;
  227. }
  228. void
  229. DisableAlarm()
  230. {
  231. NS_RUNTIMEABORT("Alarms can't be programmed from sandboxed contexts. Yet.");
  232. }
  233. bool
  234. SetAlarm(int32_t aSeconds, int32_t aNanoseconds)
  235. {
  236. NS_RUNTIMEABORT("Alarms can't be programmed from sandboxed contexts. Yet.");
  237. return false;
  238. }
  239. void
  240. SetProcessPriority(int aPid, ProcessPriority aPriority, uint32_t aLRU)
  241. {
  242. NS_RUNTIMEABORT("Only the main process may set processes' priorities.");
  243. }
  244. void
  245. SetCurrentThreadPriority(ThreadPriority aThreadPriority)
  246. {
  247. NS_RUNTIMEABORT("Setting current thread priority cannot be called from sandboxed contexts.");
  248. }
  249. void
  250. SetThreadPriority(PlatformThreadId aThreadId,
  251. ThreadPriority aThreadPriority)
  252. {
  253. NS_RUNTIMEABORT("Setting thread priority cannot be called from sandboxed contexts.");
  254. }
  255. void
  256. FactoryReset(FactoryResetReason& aReason)
  257. {
  258. if (aReason == FactoryResetReason::Normal) {
  259. Hal()->SendFactoryReset(NS_LITERAL_STRING("normal"));
  260. } else if (aReason == FactoryResetReason::Wipe) {
  261. Hal()->SendFactoryReset(NS_LITERAL_STRING("wipe"));
  262. } else if (aReason == FactoryResetReason::Root) {
  263. Hal()->SendFactoryReset(NS_LITERAL_STRING("root"));
  264. }
  265. }
  266. bool IsHeadphoneEventFromInputDev()
  267. {
  268. NS_RUNTIMEABORT("IsHeadphoneEventFromInputDev() cannot be called from sandboxed contexts.");
  269. return false;
  270. }
  271. nsresult StartSystemService(const char* aSvcName, const char* aArgs)
  272. {
  273. NS_RUNTIMEABORT("System services cannot be controlled from sandboxed contexts.");
  274. return NS_ERROR_NOT_IMPLEMENTED;
  275. }
  276. void StopSystemService(const char* aSvcName)
  277. {
  278. NS_RUNTIMEABORT("System services cannot be controlled from sandboxed contexts.");
  279. }
  280. bool SystemServiceIsRunning(const char* aSvcName)
  281. {
  282. NS_RUNTIMEABORT("System services cannot be controlled from sandboxed contexts.");
  283. return false;
  284. }
  285. class HalParent : public PHalParent
  286. , public NetworkObserver
  287. , public ISensorObserver
  288. , public WakeLockObserver
  289. , public ScreenConfigurationObserver
  290. , public SystemClockChangeObserver
  291. , public SystemTimezoneChangeObserver
  292. {
  293. public:
  294. virtual void
  295. ActorDestroy(ActorDestroyReason aWhy) override
  296. {
  297. // NB: you *must* unconditionally unregister your observer here,
  298. // if it *may* be registered below.
  299. hal::UnregisterNetworkObserver(this);
  300. hal::UnregisterScreenConfigurationObserver(this);
  301. for (auto sensor : MakeEnumeratedRange(NUM_SENSOR_TYPE)) {
  302. hal::UnregisterSensorObserver(sensor, this);
  303. }
  304. hal::UnregisterWakeLockObserver(this);
  305. hal::UnregisterSystemClockChangeObserver(this);
  306. hal::UnregisterSystemTimezoneChangeObserver(this);
  307. }
  308. virtual bool
  309. RecvEnableNetworkNotifications() override {
  310. // We give all content access to this network-status information.
  311. hal::RegisterNetworkObserver(this);
  312. return true;
  313. }
  314. virtual bool
  315. RecvDisableNetworkNotifications() override {
  316. hal::UnregisterNetworkObserver(this);
  317. return true;
  318. }
  319. virtual bool
  320. RecvGetCurrentNetworkInformation(NetworkInformation* aNetworkInfo) override {
  321. hal::GetCurrentNetworkInformation(aNetworkInfo);
  322. return true;
  323. }
  324. void Notify(const NetworkInformation& aNetworkInfo) override {
  325. Unused << SendNotifyNetworkChange(aNetworkInfo);
  326. }
  327. virtual bool
  328. RecvEnableScreenConfigurationNotifications() override {
  329. // Screen configuration is used to implement CSS and DOM
  330. // properties, so all content already has access to this.
  331. hal::RegisterScreenConfigurationObserver(this);
  332. return true;
  333. }
  334. virtual bool
  335. RecvDisableScreenConfigurationNotifications() override {
  336. hal::UnregisterScreenConfigurationObserver(this);
  337. return true;
  338. }
  339. virtual bool
  340. RecvGetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration) override {
  341. hal::GetCurrentScreenConfiguration(aScreenConfiguration);
  342. return true;
  343. }
  344. virtual bool
  345. RecvLockScreenOrientation(const dom::ScreenOrientationInternal& aOrientation, bool* aAllowed) override
  346. {
  347. // FIXME/bug 777980: unprivileged content may only lock
  348. // orientation while fullscreen. We should check whether the
  349. // request comes from an actor in a process that might be
  350. // fullscreen. We don't have that information currently.
  351. *aAllowed = hal::LockScreenOrientation(aOrientation);
  352. return true;
  353. }
  354. virtual bool
  355. RecvUnlockScreenOrientation() override
  356. {
  357. hal::UnlockScreenOrientation();
  358. return true;
  359. }
  360. void Notify(const ScreenConfiguration& aScreenConfiguration) override {
  361. Unused << SendNotifyScreenConfigurationChange(aScreenConfiguration);
  362. }
  363. virtual bool
  364. RecvGetScreenEnabled(bool* aEnabled) override
  365. {
  366. if (!AssertAppProcessPermission(this, "power")) {
  367. return false;
  368. }
  369. *aEnabled = hal::GetScreenEnabled();
  370. return true;
  371. }
  372. virtual bool
  373. RecvSetScreenEnabled(const bool& aEnabled) override
  374. {
  375. if (!AssertAppProcessPermission(this, "power")) {
  376. return false;
  377. }
  378. hal::SetScreenEnabled(aEnabled);
  379. return true;
  380. }
  381. virtual bool
  382. RecvGetKeyLightEnabled(bool* aEnabled) override
  383. {
  384. if (!AssertAppProcessPermission(this, "power")) {
  385. return false;
  386. }
  387. *aEnabled = hal::GetKeyLightEnabled();
  388. return true;
  389. }
  390. virtual bool
  391. RecvSetKeyLightEnabled(const bool& aEnabled) override
  392. {
  393. if (!AssertAppProcessPermission(this, "power")) {
  394. return false;
  395. }
  396. hal::SetKeyLightEnabled(aEnabled);
  397. return true;
  398. }
  399. virtual bool
  400. RecvGetCpuSleepAllowed(bool* aAllowed) override
  401. {
  402. if (!AssertAppProcessPermission(this, "power")) {
  403. return false;
  404. }
  405. *aAllowed = hal::GetCpuSleepAllowed();
  406. return true;
  407. }
  408. virtual bool
  409. RecvSetCpuSleepAllowed(const bool& aAllowed) override
  410. {
  411. if (!AssertAppProcessPermission(this, "power")) {
  412. return false;
  413. }
  414. hal::SetCpuSleepAllowed(aAllowed);
  415. return true;
  416. }
  417. virtual bool
  418. RecvGetScreenBrightness(double* aBrightness) override
  419. {
  420. if (!AssertAppProcessPermission(this, "power")) {
  421. return false;
  422. }
  423. *aBrightness = hal::GetScreenBrightness();
  424. return true;
  425. }
  426. virtual bool
  427. RecvSetScreenBrightness(const double& aBrightness) override
  428. {
  429. if (!AssertAppProcessPermission(this, "power")) {
  430. return false;
  431. }
  432. hal::SetScreenBrightness(aBrightness);
  433. return true;
  434. }
  435. virtual bool
  436. RecvAdjustSystemClock(const int64_t &aDeltaMilliseconds) override
  437. {
  438. if (!AssertAppProcessPermission(this, "time")) {
  439. return false;
  440. }
  441. hal::AdjustSystemClock(aDeltaMilliseconds);
  442. return true;
  443. }
  444. virtual bool
  445. RecvSetTimezone(const nsCString& aTimezoneSpec) override
  446. {
  447. if (!AssertAppProcessPermission(this, "time")) {
  448. return false;
  449. }
  450. hal::SetTimezone(aTimezoneSpec);
  451. return true;
  452. }
  453. virtual bool
  454. RecvGetTimezone(nsCString *aTimezoneSpec) override
  455. {
  456. if (!AssertAppProcessPermission(this, "time")) {
  457. return false;
  458. }
  459. *aTimezoneSpec = hal::GetTimezone();
  460. return true;
  461. }
  462. virtual bool
  463. RecvGetTimezoneOffset(int32_t *aTimezoneOffset) override
  464. {
  465. if (!AssertAppProcessPermission(this, "time")) {
  466. return false;
  467. }
  468. *aTimezoneOffset = hal::GetTimezoneOffset();
  469. return true;
  470. }
  471. virtual bool
  472. RecvEnableSystemClockChangeNotifications() override
  473. {
  474. hal::RegisterSystemClockChangeObserver(this);
  475. return true;
  476. }
  477. virtual bool
  478. RecvDisableSystemClockChangeNotifications() override
  479. {
  480. hal::UnregisterSystemClockChangeObserver(this);
  481. return true;
  482. }
  483. virtual bool
  484. RecvEnableSystemTimezoneChangeNotifications() override
  485. {
  486. hal::RegisterSystemTimezoneChangeObserver(this);
  487. return true;
  488. }
  489. virtual bool
  490. RecvDisableSystemTimezoneChangeNotifications() override
  491. {
  492. hal::UnregisterSystemTimezoneChangeObserver(this);
  493. return true;
  494. }
  495. virtual bool
  496. RecvEnableSensorNotifications(const SensorType &aSensor) override {
  497. // We currently allow any content to register device-sensor
  498. // listeners.
  499. hal::RegisterSensorObserver(aSensor, this);
  500. return true;
  501. }
  502. virtual bool
  503. RecvDisableSensorNotifications(const SensorType &aSensor) override {
  504. hal::UnregisterSensorObserver(aSensor, this);
  505. return true;
  506. }
  507. void Notify(const SensorData& aSensorData) override {
  508. Unused << SendNotifySensorChange(aSensorData);
  509. }
  510. virtual bool
  511. RecvModifyWakeLock(const nsString& aTopic,
  512. const WakeLockControl& aLockAdjust,
  513. const WakeLockControl& aHiddenAdjust,
  514. const uint64_t& aProcessID) override
  515. {
  516. MOZ_ASSERT(aProcessID != CONTENT_PROCESS_ID_UNKNOWN);
  517. // We allow arbitrary content to use wake locks.
  518. hal::ModifyWakeLock(aTopic, aLockAdjust, aHiddenAdjust, aProcessID);
  519. return true;
  520. }
  521. virtual bool
  522. RecvEnableWakeLockNotifications() override
  523. {
  524. // We allow arbitrary content to use wake locks.
  525. hal::RegisterWakeLockObserver(this);
  526. return true;
  527. }
  528. virtual bool
  529. RecvDisableWakeLockNotifications() override
  530. {
  531. hal::UnregisterWakeLockObserver(this);
  532. return true;
  533. }
  534. virtual bool
  535. RecvGetWakeLockInfo(const nsString &aTopic, WakeLockInformation *aWakeLockInfo) override
  536. {
  537. hal::GetWakeLockInfo(aTopic, aWakeLockInfo);
  538. return true;
  539. }
  540. void Notify(const WakeLockInformation& aWakeLockInfo) override
  541. {
  542. Unused << SendNotifyWakeLockChange(aWakeLockInfo);
  543. }
  544. void Notify(const int64_t& aClockDeltaMS) override
  545. {
  546. Unused << SendNotifySystemClockChange(aClockDeltaMS);
  547. }
  548. void Notify(const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo) override
  549. {
  550. Unused << SendNotifySystemTimezoneChange(aSystemTimezoneChangeInfo);
  551. }
  552. virtual bool
  553. RecvFactoryReset(const nsString& aReason) override
  554. {
  555. if (!AssertAppProcessPermission(this, "power")) {
  556. return false;
  557. }
  558. FactoryResetReason reason = FactoryResetReason::Normal;
  559. if (aReason.EqualsLiteral("normal")) {
  560. reason = FactoryResetReason::Normal;
  561. } else if (aReason.EqualsLiteral("wipe")) {
  562. reason = FactoryResetReason::Wipe;
  563. } else if (aReason.EqualsLiteral("root")) {
  564. reason = FactoryResetReason::Root;
  565. } else {
  566. // Invalid factory reset reason. That should never happen.
  567. return false;
  568. }
  569. hal::FactoryReset(reason);
  570. return true;
  571. }
  572. };
  573. class HalChild : public PHalChild {
  574. public:
  575. virtual void
  576. ActorDestroy(ActorDestroyReason aWhy) override
  577. {
  578. sHalChildDestroyed = true;
  579. }
  580. virtual bool
  581. RecvNotifySensorChange(const hal::SensorData &aSensorData) override;
  582. virtual bool
  583. RecvNotifyNetworkChange(const NetworkInformation& aNetworkInfo) override {
  584. hal::NotifyNetworkChange(aNetworkInfo);
  585. return true;
  586. }
  587. virtual bool
  588. RecvNotifyWakeLockChange(const WakeLockInformation& aWakeLockInfo) override {
  589. hal::NotifyWakeLockChange(aWakeLockInfo);
  590. return true;
  591. }
  592. virtual bool
  593. RecvNotifyScreenConfigurationChange(const ScreenConfiguration& aScreenConfiguration) override {
  594. hal::NotifyScreenConfigurationChange(aScreenConfiguration);
  595. return true;
  596. }
  597. virtual bool
  598. RecvNotifySystemClockChange(const int64_t& aClockDeltaMS) override {
  599. hal::NotifySystemClockChange(aClockDeltaMS);
  600. return true;
  601. }
  602. virtual bool
  603. RecvNotifySystemTimezoneChange(
  604. const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo) override {
  605. hal::NotifySystemTimezoneChange(aSystemTimezoneChangeInfo);
  606. return true;
  607. }
  608. };
  609. bool
  610. HalChild::RecvNotifySensorChange(const hal::SensorData &aSensorData) {
  611. hal::NotifySensorChange(aSensorData);
  612. return true;
  613. }
  614. PHalChild* CreateHalChild() {
  615. return new HalChild();
  616. }
  617. PHalParent* CreateHalParent() {
  618. return new HalParent();
  619. }
  620. } // namespace hal_sandbox
  621. } // namespace mozilla