Bug 697641, part 3: Make Sensor API available for Sandbox. r=cjones
authorDão Gottwald <dao@mozilla.com>
Mon, 16 Jan 2012 14:37:49 +0100
changeset 87030 1bea53f2d067a78872a51160125730b625f59b1b
parent 87029 577cba6021d2d6a66aa3b293bb6827dd1498314e
child 87031 789f244f34f8c45d9102cc9d7e59da07cb7862e5
push id674
push userffxbld
push dateTue, 13 Mar 2012 21:17:50 +0000
treeherdermozilla-beta@e3c4c92dec31 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones
bugs697641
milestone12.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 697641, part 3: Make Sensor API available for Sandbox. r=cjones Add Sensor API to PHal protocol that Sandbox can access sensors.
hal/Hal.cpp
hal/sandbox/PHal.ipdl
hal/sandbox/SandboxHal.cpp
--- a/hal/Hal.cpp
+++ b/hal/Hal.cpp
@@ -321,10 +321,68 @@ double GetScreenBrightness()
 }
 
 void SetScreenBrightness(double brightness)
 {
   AssertMainThread();
   PROXY_IF_SANDBOXED(SetScreenBrightness(clamped(brightness, 0.0, 1.0)));
 }
 
+
+void
+EnableSensorNotifications(SensorType aSensor) {
+  AssertMainThread();
+  PROXY_IF_SANDBOXED(EnableSensorNotifications(aSensor));
+}
+
+void
+DisableSensorNotifications(SensorType aSensor) {
+  AssertMainThread();
+  PROXY_IF_SANDBOXED(DisableSensorNotifications(aSensor));
+}
+
+typedef ObserverList<SensorData> SensorObserverList;
+static SensorObserverList *gSensorObservers = NULL;
+
+static SensorObserverList &
+GetSensorObservers(SensorType sensor_type) {
+  MOZ_ASSERT(sensor_type < NUM_SENSOR_TYPE);
+  
+  if(gSensorObservers == NULL)
+    gSensorObservers = new SensorObserverList[NUM_SENSOR_TYPE];
+  return gSensorObservers[sensor_type];
+}
+
+void
+RegisterSensorObserver(SensorType aSensor, ISensorObserver *aObserver) {
+  SensorObserverList &observers = GetSensorObservers(aSensor);
+
+  AssertMainThread();
+  
+  observers.AddObserver(aObserver);
+  if(observers.Length() == 1) {
+    EnableSensorNotifications(aSensor);
+  }
+}
+
+void
+UnregisterSensorObserver(SensorType aSensor, ISensorObserver *aObserver) {
+  SensorObserverList &observers = GetSensorObservers(aSensor);
+
+  AssertMainThread();
+  
+  observers.RemoveObserver(aObserver);
+  if(observers.Length() == 0) {
+    DisableSensorNotifications(aSensor);
+  }
+}
+
+void
+NotifySensorChange(const SensorData &aSensorData) {
+  SensorObserverList &observers = GetSensorObservers(aSensorData.sensor());
+
+  AssertMainThread();
+  
+  observers.Broadcast(aSensorData);
+}
+
 } // namespace hal
 } // namespace mozilla
--- a/hal/sandbox/PHal.ipdl
+++ b/hal/sandbox/PHal.ipdl
@@ -79,13 +79,20 @@ parent:
       returns (BatteryInformation aBatteryInfo);
 
     sync GetScreenEnabled() returns (bool enabled);
     SetScreenEnabled(bool enabled);
 
     sync GetScreenBrightness() returns (double brightness);
     SetScreenBrightness(double brightness);
 
+child:
+    NotifySensorChange(SensorData aSensorData);
+
+parent:    
+    EnableSensorNotifications(SensorType aSensor);
+    DisableSensorNotifications(SensorType aSensor);
+
     __delete__();
 };
 
 } // namespace hal
 } // namespace mozilla
--- a/hal/sandbox/SandboxHal.cpp
+++ b/hal/sandbox/SandboxHal.cpp
@@ -128,18 +128,31 @@ GetScreenBrightness()
 }
 
 void
 SetScreenBrightness(double brightness)
 {
   Hal()->SendSetScreenBrightness(brightness);
 }
 
+
+void
+EnableSensorNotifications(SensorType aSensor) {
+  Hal()->SendEnableSensorNotifications(aSensor);
+}
+
+void
+DisableSensorNotifications(SensorType aSensor) {
+  Hal()->SendDisableSensorNotifications(aSensor);
+}
+
+
 class HalParent : public PHalParent
-                , public BatteryObserver {
+                , public BatteryObserver
+                , public ISensorObserver {
 public:
   NS_OVERRIDE virtual bool
   RecvVibrate(const InfallibleTArray<unsigned int>& pattern,
               const InfallibleTArray<uint64> &id,
               PBrowserParent *browserParent)
   {
     // Check whether browserParent is active.  We should have already
     // checked that the corresponding window is active, but this check
@@ -219,27 +232,55 @@ public:
   }
 
   NS_OVERRIDE virtual bool
   RecvSetScreenBrightness(const double &brightness)
   {
     hal::SetScreenBrightness(brightness);
     return true;
   }
+
+  NS_OVERRIDE virtual bool
+  RecvEnableSensorNotifications(const SensorType &aSensor) {
+    hal::RegisterSensorObserver(aSensor, this);
+    return true;
+  }
+   
+  NS_OVERRIDE virtual bool
+  RecvDisableSensorNotifications(const SensorType &aSensor) {
+    hal::UnregisterSensorObserver(aSensor, this);
+    return true;
+  }
+  
+  void Notify(const SensorData& aSensorData) {
+    unused << SendNotifySensorChange(aSensorData);
+  }
+
 };
 
 class HalChild : public PHalChild {
 public:
   NS_OVERRIDE virtual bool
   RecvNotifyBatteryChange(const BatteryInformation& aBatteryInfo) {
     hal::NotifyBatteryChange(aBatteryInfo);
     return true;
   }
+  
+  NS_OVERRIDE virtual bool
+  RecvNotifySensorChange(const hal::SensorData &aSensorData);
 };
 
+bool
+HalChild::RecvNotifySensorChange(const hal::SensorData &aSensorData) {
+  hal::NotifySensorChange(aSensorData);
+  
+  return true;
+}
+
+
 PHalChild* CreateHalChild() {
   return new HalChild();
 }
 
 PHalParent* CreateHalParent() {
   return new HalParent();
 }