/hal/sandbox/SandboxHal.cpp

http://github.com/zpao/v8monkey · C++ · 356 lines · 287 code · 55 blank · 14 comment · 2 complexity · 98a3cdf13c88fb93545ccfa1740c3c1d MD5 · raw file

  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. #include "Hal.h"
  7. #include "mozilla/dom/ContentChild.h"
  8. #include "mozilla/hal_sandbox/PHalChild.h"
  9. #include "mozilla/hal_sandbox/PHalParent.h"
  10. #include "mozilla/dom/TabParent.h"
  11. #include "mozilla/dom/TabChild.h"
  12. #include "mozilla/dom/battery/Types.h"
  13. #include "mozilla/dom/network/Types.h"
  14. #include "mozilla/Observer.h"
  15. #include "mozilla/unused.h"
  16. #include "WindowIdentifier.h"
  17. using namespace mozilla;
  18. using namespace mozilla::dom;
  19. using namespace mozilla::hal;
  20. namespace mozilla {
  21. namespace hal_sandbox {
  22. static PHalChild* sHal;
  23. static PHalChild*
  24. Hal()
  25. {
  26. if (!sHal) {
  27. sHal = ContentChild::GetSingleton()->SendPHalConstructor();
  28. }
  29. return sHal;
  30. }
  31. void
  32. Vibrate(const nsTArray<uint32>& pattern, const WindowIdentifier &id)
  33. {
  34. HAL_LOG(("Vibrate: Sending to parent process."));
  35. AutoInfallibleTArray<uint32, 8> p(pattern);
  36. WindowIdentifier newID(id);
  37. newID.AppendProcessID();
  38. Hal()->SendVibrate(p, newID.AsArray(), GetTabChildFrom(newID.GetWindow()));
  39. }
  40. void
  41. CancelVibrate(const WindowIdentifier &id)
  42. {
  43. HAL_LOG(("CancelVibrate: Sending to parent process."));
  44. WindowIdentifier newID(id);
  45. newID.AppendProcessID();
  46. Hal()->SendCancelVibrate(newID.AsArray(), GetTabChildFrom(newID.GetWindow()));
  47. }
  48. void
  49. EnableBatteryNotifications()
  50. {
  51. Hal()->SendEnableBatteryNotifications();
  52. }
  53. void
  54. DisableBatteryNotifications()
  55. {
  56. Hal()->SendDisableBatteryNotifications();
  57. }
  58. void
  59. GetCurrentBatteryInformation(BatteryInformation* aBatteryInfo)
  60. {
  61. Hal()->SendGetCurrentBatteryInformation(aBatteryInfo);
  62. }
  63. void
  64. EnableNetworkNotifications()
  65. {
  66. Hal()->SendEnableNetworkNotifications();
  67. }
  68. void
  69. DisableNetworkNotifications()
  70. {
  71. Hal()->SendDisableNetworkNotifications();
  72. }
  73. void
  74. GetCurrentNetworkInformation(NetworkInformation* aNetworkInfo)
  75. {
  76. Hal()->SendGetCurrentNetworkInformation(aNetworkInfo);
  77. }
  78. bool
  79. GetScreenEnabled()
  80. {
  81. bool enabled = false;
  82. Hal()->SendGetScreenEnabled(&enabled);
  83. return enabled;
  84. }
  85. void
  86. SetScreenEnabled(bool enabled)
  87. {
  88. Hal()->SendSetScreenEnabled(enabled);
  89. }
  90. double
  91. GetScreenBrightness()
  92. {
  93. double brightness = 0;
  94. Hal()->SendGetScreenBrightness(&brightness);
  95. return brightness;
  96. }
  97. void
  98. SetScreenBrightness(double brightness)
  99. {
  100. Hal()->SendSetScreenBrightness(brightness);
  101. }
  102. bool
  103. SetLight(hal::LightType light, const hal::LightConfiguration& aConfig)
  104. {
  105. bool status;
  106. Hal()->SendSetLight(light, aConfig, &status);
  107. return status;
  108. }
  109. bool
  110. GetLight(hal::LightType light, hal::LightConfiguration* aConfig)
  111. {
  112. bool status;
  113. Hal()->SendGetLight(light, aConfig, &status);
  114. return status;
  115. }
  116. void
  117. Reboot()
  118. {
  119. Hal()->SendReboot();
  120. }
  121. void
  122. PowerOff()
  123. {
  124. Hal()->SendPowerOff();
  125. }
  126. void
  127. EnableSensorNotifications(SensorType aSensor) {
  128. Hal()->SendEnableSensorNotifications(aSensor);
  129. }
  130. void
  131. DisableSensorNotifications(SensorType aSensor) {
  132. Hal()->SendDisableSensorNotifications(aSensor);
  133. }
  134. class HalParent : public PHalParent
  135. , public BatteryObserver
  136. , public NetworkObserver
  137. , public ISensorObserver
  138. {
  139. public:
  140. NS_OVERRIDE virtual bool
  141. RecvVibrate(const InfallibleTArray<unsigned int>& pattern,
  142. const InfallibleTArray<uint64> &id,
  143. PBrowserParent *browserParent)
  144. {
  145. // Check whether browserParent is active. We should have already
  146. // checked that the corresponding window is active, but this check
  147. // isn't redundant. A window may be inactive in an active
  148. // browser. And a window is not notified synchronously when it's
  149. // deactivated, so the window may think it's active when the tab
  150. // is actually inactive.
  151. TabParent *tabParent = static_cast<TabParent*>(browserParent);
  152. if (!tabParent->Active()) {
  153. HAL_LOG(("RecvVibrate: Tab is not active. Cancelling."));
  154. return true;
  155. }
  156. // Forward to hal::, not hal_impl::, because we might be a
  157. // subprocess of another sandboxed process. The hal:: entry point
  158. // will do the right thing.
  159. nsCOMPtr<nsIDOMWindow> window =
  160. do_QueryInterface(tabParent->GetBrowserDOMWindow());
  161. WindowIdentifier newID(id, window);
  162. hal::Vibrate(pattern, newID);
  163. return true;
  164. }
  165. NS_OVERRIDE virtual bool
  166. RecvCancelVibrate(const InfallibleTArray<uint64> &id,
  167. PBrowserParent *browserParent)
  168. {
  169. TabParent *tabParent = static_cast<TabParent*>(browserParent);
  170. nsCOMPtr<nsIDOMWindow> window =
  171. do_QueryInterface(tabParent->GetBrowserDOMWindow());
  172. WindowIdentifier newID(id, window);
  173. hal::CancelVibrate(newID);
  174. return true;
  175. }
  176. NS_OVERRIDE virtual bool
  177. RecvEnableBatteryNotifications() {
  178. hal::RegisterBatteryObserver(this);
  179. return true;
  180. }
  181. NS_OVERRIDE virtual bool
  182. RecvDisableBatteryNotifications() {
  183. hal::UnregisterBatteryObserver(this);
  184. return true;
  185. }
  186. NS_OVERRIDE virtual bool
  187. RecvGetCurrentBatteryInformation(BatteryInformation* aBatteryInfo) {
  188. hal::GetCurrentBatteryInformation(aBatteryInfo);
  189. return true;
  190. }
  191. void Notify(const BatteryInformation& aBatteryInfo) {
  192. unused << SendNotifyBatteryChange(aBatteryInfo);
  193. }
  194. NS_OVERRIDE virtual bool
  195. RecvEnableNetworkNotifications() {
  196. hal::RegisterNetworkObserver(this);
  197. return true;
  198. }
  199. NS_OVERRIDE virtual bool
  200. RecvDisableNetworkNotifications() {
  201. hal::UnregisterNetworkObserver(this);
  202. return true;
  203. }
  204. NS_OVERRIDE virtual bool
  205. RecvGetCurrentNetworkInformation(NetworkInformation* aNetworkInfo) {
  206. hal::GetCurrentNetworkInformation(aNetworkInfo);
  207. return true;
  208. }
  209. void Notify(const NetworkInformation& aNetworkInfo) {
  210. unused << SendNotifyNetworkChange(aNetworkInfo);
  211. }
  212. NS_OVERRIDE virtual bool
  213. RecvGetScreenEnabled(bool *enabled)
  214. {
  215. *enabled = hal::GetScreenEnabled();
  216. return true;
  217. }
  218. NS_OVERRIDE virtual bool
  219. RecvSetScreenEnabled(const bool &enabled)
  220. {
  221. hal::SetScreenEnabled(enabled);
  222. return true;
  223. }
  224. NS_OVERRIDE virtual bool
  225. RecvGetScreenBrightness(double *brightness)
  226. {
  227. *brightness = hal::GetScreenBrightness();
  228. return true;
  229. }
  230. NS_OVERRIDE virtual bool
  231. RecvSetScreenBrightness(const double &brightness)
  232. {
  233. hal::SetScreenBrightness(brightness);
  234. return true;
  235. }
  236. NS_OVERRIDE virtual bool
  237. RecvSetLight(const LightType& aLight, const hal::LightConfiguration& aConfig, bool *status)
  238. {
  239. *status = hal::SetLight(aLight, aConfig);
  240. return true;
  241. }
  242. NS_OVERRIDE virtual bool
  243. RecvGetLight(const LightType& aLight, LightConfiguration* aConfig, bool* status)
  244. {
  245. *status = hal::GetLight(aLight, aConfig);
  246. return true;
  247. }
  248. NS_OVERRIDE virtual bool
  249. RecvReboot()
  250. {
  251. hal::Reboot();
  252. return true;
  253. }
  254. NS_OVERRIDE virtual bool
  255. RecvPowerOff()
  256. {
  257. hal::PowerOff();
  258. return true;
  259. }
  260. NS_OVERRIDE virtual bool
  261. RecvEnableSensorNotifications(const SensorType &aSensor) {
  262. hal::RegisterSensorObserver(aSensor, this);
  263. return true;
  264. }
  265. NS_OVERRIDE virtual bool
  266. RecvDisableSensorNotifications(const SensorType &aSensor) {
  267. hal::UnregisterSensorObserver(aSensor, this);
  268. return true;
  269. }
  270. void Notify(const SensorData& aSensorData) {
  271. unused << SendNotifySensorChange(aSensorData);
  272. }
  273. };
  274. class HalChild : public PHalChild {
  275. public:
  276. NS_OVERRIDE virtual bool
  277. RecvNotifyBatteryChange(const BatteryInformation& aBatteryInfo) {
  278. hal::NotifyBatteryChange(aBatteryInfo);
  279. return true;
  280. }
  281. NS_OVERRIDE virtual bool
  282. RecvNotifySensorChange(const hal::SensorData &aSensorData);
  283. NS_OVERRIDE virtual bool
  284. RecvNotifyNetworkChange(const NetworkInformation& aNetworkInfo) {
  285. hal::NotifyNetworkChange(aNetworkInfo);
  286. return true;
  287. }
  288. };
  289. bool
  290. HalChild::RecvNotifySensorChange(const hal::SensorData &aSensorData) {
  291. hal::NotifySensorChange(aSensorData);
  292. return true;
  293. }
  294. PHalChild* CreateHalChild() {
  295. return new HalChild();
  296. }
  297. PHalParent* CreateHalParent() {
  298. return new HalParent();
  299. }
  300. } // namespace hal_sandbox
  301. } // namespace mozilla