Bug 697641, part 3: Make Sensor API available for Sandbox. r=cjones

Add Sensor API to PHal protocol that Sandbox can access sensors.
This commit is contained in:
Sinker Li 2012-02-01 22:47:55 -08:00
parent e2c13d2e36
commit d66fd0a876
3 changed files with 96 additions and 2 deletions

View File

@ -200,7 +200,7 @@ class ObserversManager
public:
void AddObserver(Observer<InfoType>* aObserver) {
if (!mObservers) {
mObservers = new ObserverList<InfoType>();
mObservers = new mozilla::ObserverList<InfoType>();
}
mObservers->AddObserver(aObserver);
@ -250,7 +250,7 @@ protected:
virtual void GetCurrentInformationInternal(InfoType*) = 0;
private:
ObserverList<InfoType>* mObservers;
mozilla::ObserverList<InfoType>* mObservers;
InfoType mInfo;
bool mHasValidCache;
};
@ -344,6 +344,63 @@ void SetScreenBrightness(double brightness)
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 mozilla::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);
}
void
RegisterNetworkObserver(NetworkObserver* aObserver)
{

View File

@ -76,6 +76,7 @@ sync protocol PHal {
child:
NotifyBatteryChange(BatteryInformation aBatteryInfo);
NotifyNetworkChange(NetworkInformation aNetworkInfo);
NotifySensorChange(SensorData aSensorData);
parent:
Vibrate(uint32[] pattern, uint64[] id, PBrowser browser);
@ -100,6 +101,9 @@ parent:
Reboot();
PowerOff();
EnableSensorNotifications(SensorType aSensor);
DisableSensorNotifications(SensorType aSensor);
__delete__();
};

View File

@ -164,9 +164,20 @@ PowerOff()
Hal()->SendPowerOff();
}
void
EnableSensorNotifications(SensorType aSensor) {
Hal()->SendEnableSensorNotifications(aSensor);
}
void
DisableSensorNotifications(SensorType aSensor) {
Hal()->SendDisableSensorNotifications(aSensor);
}
class HalParent : public PHalParent
, public BatteryObserver
, public NetworkObserver
, public ISensorObserver
{
public:
NS_OVERRIDE virtual bool
@ -293,6 +304,22 @@ public:
hal::PowerOff();
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 {
@ -308,6 +335,12 @@ public:
hal::NotifyNetworkChange(aNetworkInfo);
return true;
}
NS_OVERRIDE virtual bool
RecvNotifySensorChange(const hal::SensorData &aSensorData) {
hal::NotifySensorChange(aSensorData);
return true;
}
};
PHalChild* CreateHalChild() {