Commit 28cd84c5 authored by alexander.shalamov's avatar alexander.shalamov Committed by Commit bot

[sensors] Dispatch sensor events using postTask

In order to avoid re-entrancy issues, this CL modifies sensor event
dispatching code and uses postTask to schedule event dispatch.
New test is added to ambient-light-sensor.html that checks if generic
sensor code works correctly when sensor is not supported

BUG=606766

Review-Url: https://codereview.chromium.org/2381913002
Cr-Commit-Position: refs/heads/master@{#422761}
parent e9c97a0e
...@@ -22,6 +22,19 @@ test(() => assert_throws( ...@@ -22,6 +22,19 @@ test(() => assert_throws(
() => new AmbientLightSensor({frequency: -60})), () => new AmbientLightSensor({frequency: -60})),
'Test that negative frequency causes exception from constructor.'); 'Test that negative frequency causes exception from constructor.');
sensor_test(sensor => {
sensor.mockSensorProvider.setGetSensorShouldFail(true);
let ambientLightSensor = new AmbientLightSensor();
ambientLightSensor.start();
return new Promise((resolve, reject) => {
ambientLightSensor.onstatechange = event => {
if(ambientLightSensor.state == 'errored') {
resolve();
}
};
});
}, 'Test that sensor state changes to "errored" when sensor is not supported.');
sensor_test(sensor => { sensor_test(sensor => {
let ambientLightSensor = new AmbientLightSensor({frequency: 560}); let ambientLightSensor = new AmbientLightSensor({frequency: 560});
ambientLightSensor.start(); ambientLightSensor.start();
......
...@@ -194,7 +194,8 @@ function sensor_mocks(mojo) { ...@@ -194,7 +194,8 @@ function sensor_mocks(mojo) {
// Returns initialized Sensor proxy to the client. // Returns initialized Sensor proxy to the client.
getSensor(type, stub) { getSensor(type, stub) {
if (this.get_sensor_should_fail_) { if (this.get_sensor_should_fail_) {
return getSensorResponse(null, null); return getSensorResponse(null,
connection.bindProxy(null, sensor.SensorClient));
} }
let offset = let offset =
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "core/dom/Document.h" #include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h" #include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContextTask.h"
#include "core/inspector/ConsoleMessage.h" #include "core/inspector/ConsoleMessage.h"
#include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
#include "modules/sensor/SensorErrorEvent.h" #include "modules/sensor/SensorErrorEvent.h"
...@@ -268,9 +269,13 @@ void Sensor::pollForData() { ...@@ -268,9 +269,13 @@ void Sensor::pollForData() {
m_sensorProxy->updateInternalReading(); m_sensorProxy->updateInternalReading();
DCHECK(m_sensorReading); DCHECK(m_sensorReading);
if (m_sensorReading->isReadingUpdated(m_storedData)) if (getExecutionContext() &&
dispatchEvent( m_sensorReading->isReadingUpdated(m_storedData)) {
SensorReadingEvent::create(EventTypeNames::change, m_sensorReading)); getExecutionContext()->postTask(
BLINK_FROM_HERE,
createSameThreadTask(&Sensor::notifySensorReadingChanged,
wrapWeakPersistent(this)));
}
m_storedData = m_sensorProxy->reading(); m_storedData = m_sensorProxy->reading();
} }
...@@ -279,7 +284,12 @@ void Sensor::updateState(Sensor::SensorState newState) { ...@@ -279,7 +284,12 @@ void Sensor::updateState(Sensor::SensorState newState) {
if (newState == m_state) if (newState == m_state)
return; return;
m_state = newState; m_state = newState;
dispatchEvent(Event::create(EventTypeNames::statechange)); if (getExecutionContext()) {
getExecutionContext()->postTask(
BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChanged,
wrapWeakPersistent(this)));
}
updatePollingStatus(); updatePollingStatus();
} }
...@@ -300,4 +310,13 @@ void Sensor::updatePollingStatus() { ...@@ -300,4 +310,13 @@ void Sensor::updatePollingStatus() {
} }
} }
void Sensor::notifySensorReadingChanged() {
dispatchEvent(
SensorReadingEvent::create(EventTypeNames::change, m_sensorReading));
}
void Sensor::notifyStateChanged() {
dispatchEvent(Event::create(EventTypeNames::statechange));
}
} // namespace blink } // namespace blink
...@@ -102,6 +102,9 @@ class Sensor : public EventTargetWithInlineData, ...@@ -102,6 +102,9 @@ class Sensor : public EventTargetWithInlineData,
void updatePollingStatus(); void updatePollingStatus();
void notifySensorReadingChanged();
void notifyStateChanged();
private: private:
Member<SensorReading> m_sensorReading; Member<SensorReading> m_sensorReading;
SensorOptions m_sensorOptions; SensorOptions m_sensorOptions;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment