services/device/generic_sensor
contains the platform-specific parts of the Sensor APIs implementation.
Sensors Mojo interfaces are defined in the services/device/public/mojom
subdirectory.
The Generic Sensors API is implemented in third_party/blink/renderer/modules/sensor
and exposes the following sensor types as JavaScript objects:
The DeviceOrientation Events API is implemented in third_party/blink/renderer/modules/device_orientation
and exposes two events based on the following sensors:
accelerationIncludingGravity
fieldacceleration
fieldrotationRate
field'deviceorientationabsolute'
event is added)'deviceorientation'
event is added)The content renderer layer is located in third_party/blink/renderer/modules/device_orientation
.
Testing:
content/browser/device_sensors
.third_party/WebKit/LayoutTests/device_orientation
.third_party/WebKit/LayoutTests/external/wpt/orientation-event
and are a mirror of the web-platform-tests GitHub repository.The device service provides no support for permission checks. When the render process requests access to a sensor type this request is proxied through the browser process by SensorProviderProxyImpl which is responsible for checking the permissions granted to the requesting origin.
Support for the SensorTypes defined by the Mojo interface is summarized in this table. An empty cell indicates that the sensor type is not supported on that platform.
SensorType | Android | Linux | macOS | Windows |
---|---|---|---|---|
AMBIENT_LIGHT | TYPE_LIGHT | in_illuminance | AppleLMUController | SENSOR_TYPE_AMBIENT_LIGHT |
PROXIMITY | ||||
ACCELEROMETER | TYPE_ACCELEROMETER | in_accel | SMCMotionSensor | SENSOR_TYPE_ACCELEROMETER_3D |
LINEAR_ACCELEROMETER | See below | ACCELEROMETER (*) | ACCELEROMETER (*) | |
GYROSCOPE | TYPE_GYROSCOPE | in_anglvel | SENSOR_TYPE_GYROMETER_3D | |
MAGNETOMETER | TYPE_MAGNETIC_FIELD | in_magn | SENSOR_TYPE_COMPASS_3D | |
PRESSURE | ||||
ABSOLUTE_ORIENTATION_EULER_ANGLES | See below | ACCELEROMETER and MAGNETOMETER (*) | SENSOR_TYPE_INCLINOMETER_3D | |
ABSOLUTE_ORIENTATION_QUATERNION | See below | ABSOLUTE_ORIENTATION_EULER_ANGLES (*) | SENSOR_TYPE_AGGREGATED_DEVICE_ORIENTATION | |
RELATIVE_ORIENTATION_EULER_ANGLES | See below | ACCELEROMETER and GYROSCOPE (*) | ACCELEROMETER (*) | |
or ACCELEROMETER (*) | ||||
RELATIVE_ORIENTATION_QUATERNION | TYPE_GAME_ROTATION_VECTOR | RELATIVE_ORIENTATION_EULER_ANGLES (*) | RELATIVE_ORIENTATION_EULER_ANGLES (*) |
(Note: “*” means the sensor type is provided by sensor fusion.)
Sensors are implemented by passing through values provided by the Sensor class. The TYPE_* values in the below descriptions correspond to the integer constants from the android.hardware.Sensor used to provide data for a SensorType.
For LINEAR_ACCELEROMETER, the following sensor fallback is used:
For ABSOLUTE_ORIENTATION_EULER_ANGLES, the following sensor fallback is used:
For ABSOLUTE_ORIENTATION_QUATERNION, the following sensor fallback is used:
For RELATIVE_ORIENTATION_EULER_ANGLES, converts the data produced by RELATIVE_ORIENTATION_QUATERNION to euler angles.
Sensors are implemented by reading values from the IIO subsystem. The values in the “Linux” column of the table above are the prefix of the sysfs files Chrome searches for to provide data for a SensorType. The ABSOLUTE_ORIENTATION_EULER_ANGLES sensor type is provided by interpreting the value that can be read from the ACCELEROMETER and MAGNETOMETER. The ABSOLUTE_ORIENTATION_QUATERNION sensor type is provided by interpreting the value that can be read from the ABSOLUTE_ORIENTATION_EULER_ANGLES. The RELATIVE_ORIENTATION_EULER_ANGLES sensor type is provided by interpreting the value that can be read from the ACCELEROMETER and GYROSCOPE, or ACCELEROMETER. The RELATIVE_ORIENTATION_QUATERNION sensor type is provided by interpreting the value that can be read from the RELATIVE_ORIENTATION_EULER_ANGLES. LINEAR_ACCELEROMETER sensor type is provided by implementing a low-pass-filter over the values returned by the ACCELEROMETER in order to remove the contribution of the gravitational force.
On this platform there is limited support for sensors. The AMBIENT_LIGHT sensor type is provided by interpreting the value that can be read from the LMU. The ACCELEROMETER sensor type is provided by interpreting the value that can be read from the SMCMotionSensor. The RELATIVE_ORIENTATION_EULER_ANGLES sensor type is provided by interpreting the value that can be read from the ACCELEROMETER. The RELATIVE_ORIENTATION_QUATERNION sensor type is provided by interpreting the value that can be read from the RELATIVE_ORIENTATION_EULER_ANGLES.
Sensors are implemented by passing through values provided by the Sensor API. The values in the “Windows” column of the table above correspond to the names of the sensor type GUIDs used to provide data for a SensorType. The LINEAR_ACCELEROMETER sensor type is provided by implementing a low-pass-filter over the values returned by the ACCELEROMETER in order to remove the contribution of the gravitational force.
Sensors platform unit tests are located in the current directory and its subdirectories.
The sensors unit tests file for Android is android/junit/src/org/chromium/device/sensors/PlatformSensorAndProviderTest.java
.
Sensors browser tests are located in content/test/data/generic_sensor
.
Please refer to the design documentation for more details.