PageRenderTime 20ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

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