Skip to content

Commit

Permalink
sensors: Do nothing in Sensor.start() when the document is not fully …
Browse files Browse the repository at this point in the history
…active.

When a sensor is created on e.g. an iframe that is later removed from
its parent via removeChild(), we end up in a situation where a sensor
instance did get created but which does not have a valid
ExecutionContext by the time start() is invoked.

Check for a valid ExecutionContext when start() is called and bail out
early if it is null. w3c/sensors#415 tracks
handling non-fully active documents from a spec perspective; once that
one is fixed we should probably throw an error in this case rather than
silently doing nothing.

Bug: 1289924
Change-Id: I2b033252d93347ba7c91385bdb510b69b8298aa2
  • Loading branch information
rakuco authored and chromium-wpt-export-bot committed Jan 25, 2022
1 parent 1b6bcba commit 5adc7a5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions generic-sensor/generic-sensor-iframe-tests.sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,32 @@ function run_generic_sensor_iframe_tests(sensorName) {
iframe.parentNode.removeChild(iframe);
window.focus();
}, `${sensorName}: losing a document's frame with an active sensor does not crash`);

sensor_test(async t => {
assert_implements(sensorName in self, `${sensorName} is not supported.`);
const iframe = document.createElement('iframe');
iframe.allow = featurePolicies.join(';') + ';';
iframe.src = 'https://{{host}}:{{ports[https][0]}}/generic-sensor/resources/iframe_sensor_handler.html';

// Create sensor in the iframe (we do not care whether this is a
// cross-origin nested context in this test).
const iframeLoadWatcher = new EventWatcher(t, iframe, 'load');
document.body.appendChild(iframe);
await iframeLoadWatcher.wait_for('load');

// The purpose of this message is to initialize the mock backend in the
// iframe. We are not going to use the sensor created there.
await send_message_to_iframe(iframe, {command: 'create_sensor',
type: sensorName});

const iframeSensor = new iframe.contentWindow[sensorName]();
assert_not_equals(iframeSensor, null);

// Remove iframe from main document. |iframeSensor| no longer has a
// non-null browsing context. Calling start() should probably throw an
// error when called from a non-fully active document, but that depends on
// https://github.com/w3c/sensors/issues/415
iframe.parentNode.removeChild(iframe);
iframeSensor.start();
}, `${sensorName}: calling start() in a non-fully active document does not crash`);
}

0 comments on commit 5adc7a5

Please sign in to comment.