Commit b49349c6 authored by mikhail.pozdnyakov's avatar mikhail.pozdnyakov Committed by Commit bot

[Sensors] Handle default sensor configuration

This patch provides platform sensor's default configuration to the Blink side at 'SensorProxy' initialization stage.
Default configuration will be used for sensors construction by default, e.g. 'let sensor = new AmbientLightSensor();'.

A few related changes are added as well:
- Rename 'SensorReadBuffer' to 'SensorInitParams'
- Remove repeated code calling 'startListening()' from 'Sensor::onSensorInitialized()'
- Call 'reset()' for all 'SensorProxy' members from 'SensorProxy::handleSensorError()'

BUG=606766

Review-Url: https://codereview.chromium.org/2330943002
Cr-Commit-Position: refs/heads/master@{#418528}
parent fd01db73
......@@ -14,7 +14,7 @@ namespace device {
namespace {
const uint64_t kSharedBufferSizeInBytes =
mojom::SensorReadBuffer::kReadBufferSize *
mojom::SensorInitParams::kReadBufferSize *
static_cast<uint64_t>(mojom::SensorType::LAST);
} // namespace
......
......@@ -6,16 +6,19 @@ module device.mojom;
import "sensor.mojom";
struct SensorReadBuffer {
struct SensorInitParams {
// The shared memory handle used to fetch the sensor reading.
handle<shared_buffer> memory;
// The offset at which shared buffer must be mapped.
uint64 offset;
uint64 buffer_offset;
// The ReportingMode supported by the sensor.
ReportingMode mode;
// Default sensor configuration.
SensorConfiguration default_configuration;
// Note: Each sensor's read buffer contains 4 tightly packed 64-bit floating
// point fields, its layout is: { double timestamp; double values[3] }.
// So it has a fixed size 4*8 = 32 bytes.
......@@ -29,12 +32,12 @@ interface SensorProvider {
//
// |sensor_request| the Sensor interface instance to be initialized.
//
// |read_buffer| on success will contain the SensorReadBuffer describing the
// sensor reading buffer details,
// |init_params| on success will contain the SensorInitParams describing the
// sensor details,
// contains null on failure.
// |client_request| on success contains a request to be bound by the client,
// contains null on failure.
GetSensor(SensorType type, Sensor& sensor_request) => (
SensorReadBuffer? read_buffer,
SensorInitParams? init_params,
SensorClient&? client_request);
};
......@@ -17,7 +17,7 @@ namespace {
uint64_t GetBufferOffset(mojom::SensorType type) {
return (static_cast<uint64_t>(mojom::SensorType::LAST) -
static_cast<uint64_t>(type)) *
mojom::SensorReadBuffer::kReadBufferSize;
mojom::SensorInitParams::kReadBufferSize;
}
} // namespace
......@@ -50,7 +50,7 @@ void SensorProviderImpl::GetSensor(mojom::SensorType type,
scoped_refptr<PlatformSensor> sensor = provider_->GetSensor(type);
if (!sensor) {
sensor = provider_->CreateSensor(
type, mojom::SensorReadBuffer::kReadBufferSize, GetBufferOffset(type));
type, mojom::SensorInitParams::kReadBufferSize, GetBufferOffset(type));
}
if (!sensor) {
......@@ -60,12 +60,13 @@ void SensorProviderImpl::GetSensor(mojom::SensorType type,
auto sensor_impl = base::MakeUnique<SensorImpl>(sensor);
auto sensor_read_buffer = mojom::SensorReadBuffer::New();
sensor_read_buffer->memory = std::move(cloned_handle);
sensor_read_buffer->offset = GetBufferOffset(type);
sensor_read_buffer->mode = sensor->GetReportingMode();
auto init_params = mojom::SensorInitParams::New();
init_params->memory = std::move(cloned_handle);
init_params->buffer_offset = GetBufferOffset(type);
init_params->mode = sensor->GetReportingMode();
init_params->default_configuration = sensor->GetDefaultConfiguration();
callback.Run(std::move(sensor_read_buffer), sensor_impl->GetClient());
callback.Run(std::move(init_params), sensor_impl->GetClient());
mojo::MakeStrongBinding(std::move(sensor_impl), std::move(sensor_request));
}
......
......@@ -125,15 +125,7 @@ void Sensor::onSensorInitialized()
if (m_state != Sensor::SensorState::ACTIVATING)
return;
m_configuration = createSensorConfig(m_sensorOptions);
if (!m_configuration) {
reportError();
return;
}
DCHECK(m_sensorProxy);
auto startCallback = WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this));
m_sensorProxy->addConfiguration(m_configuration->Clone(), std::move(startCallback));
startListening();
}
void Sensor::onSensorReadingChanged()
......@@ -195,17 +187,24 @@ void Sensor::startListening()
{
DCHECK(m_sensorProxy);
updateState(Sensor::SensorState::ACTIVATING);
if (!m_sensorReading)
if (!m_sensorReading) {
m_sensorReading = createSensorReading(m_sensorProxy);
DCHECK(m_sensorReading);
}
m_sensorProxy->addObserver(this);
if (m_sensorProxy->isInitialized()) {
auto callback = WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this));
DCHECK(m_configuration);
m_sensorProxy->addConfiguration(m_configuration->Clone(), std::move(callback));
} else {
if (!m_sensorProxy->isInitialized()) {
m_sensorProxy->initialize();
return;
}
if (!m_configuration) {
m_configuration = createSensorConfig(m_sensorOptions, *m_sensorProxy->defaultConfig());
DCHECK(m_configuration);
}
auto startCallback = WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this));
m_sensorProxy->addConfiguration(m_configuration->Clone(), std::move(startCallback));
}
void Sensor::stopListening()
......
......@@ -68,7 +68,8 @@ protected:
virtual SensorReading* createSensorReading(SensorProxy*) = 0;
using SensorConfigurationPtr = device::mojom::blink::SensorConfigurationPtr;
virtual SensorConfigurationPtr createSensorConfig(const SensorOptions&) = 0;
using SensorConfiguration = device::mojom::blink::SensorConfiguration;
virtual SensorConfigurationPtr createSensorConfig(const SensorOptions&, const SensorConfiguration& defaultConfiguration) = 0;
private:
void initSensorProxyIfNeeded();
......
......@@ -97,6 +97,12 @@ void SensorProxy::resume()
m_suspended = false;
}
const device::mojom::blink::SensorConfiguration* SensorProxy::defaultConfig() const
{
DCHECK(isInitialized());
return m_defaultConfig.get();
}
void SensorProxy::updateInternalReading()
{
DCHECK(isInitialized());
......@@ -119,28 +125,38 @@ void SensorProxy::handleSensorError()
{
m_state = Uninitialized;
m_sensor.reset();
m_sharedBuffer.reset();
m_sharedBufferHandle.reset();
m_defaultConfig.reset();
m_clientBinding.Close();
for (Observer* observer : m_observers)
observer->onSensorError();
}
void SensorProxy::onSensorCreated(SensorReadBufferPtr buffer, SensorClientRequest clientRequest)
void SensorProxy::onSensorCreated(SensorInitParamsPtr params, SensorClientRequest clientRequest)
{
DCHECK_EQ(Initializing, m_state);
if (!buffer) {
if (!params) {
handleSensorError();
return;
}
DCHECK_EQ(0u, buffer->offset % SensorReadBuffer::kReadBufferSize);
DCHECK_EQ(0u, params->buffer_offset % SensorInitParams::kReadBufferSize);
m_mode = buffer->mode;
m_mode = params->mode;
m_defaultConfig = std::move(params->default_configuration);
if (!m_defaultConfig) {
handleSensorError();
return;
}
DCHECK(m_sensor.is_bound());
m_clientBinding.Bind(std::move(clientRequest));
m_sharedBufferHandle = std::move(buffer->memory);
m_sharedBufferHandle = std::move(params->memory);
DCHECK(!m_sharedBuffer);
m_sharedBuffer = m_sharedBufferHandle->MapAtOffset(buffer->offset, SensorReadBuffer::kReadBufferSize);
m_sharedBuffer = m_sharedBufferHandle->MapAtOffset(SensorInitParams::kReadBufferSize, params->buffer_offset);
if (!m_sharedBuffer) {
handleSensorError();
......
......@@ -59,10 +59,12 @@ public:
double timestamp;
double reading[3];
};
static_assert(sizeof(Reading) == device::mojom::blink::SensorReadBuffer::kReadBufferSize, "Check reading size");
static_assert(sizeof(Reading) == device::mojom::blink::SensorInitParams::kReadBufferSize, "Check reading size");
const Reading& reading() const { return m_reading; }
const device::mojom::blink::SensorConfiguration* defaultConfig() const;
// Updates internal reading from shared buffer.
void updateInternalReading();
......@@ -79,7 +81,7 @@ private:
// Generic handler for a fatal error.
void handleSensorError();
void onSensorCreated(device::mojom::blink::SensorReadBufferPtr, device::mojom::blink::SensorClientRequest);
void onSensorCreated(device::mojom::blink::SensorInitParamsPtr, device::mojom::blink::SensorClientRequest);
device::mojom::blink::SensorType m_type;
device::mojom::blink::ReportingMode m_mode;
......@@ -88,7 +90,9 @@ private:
ObserversSet m_observers;
device::mojom::blink::SensorPtr m_sensor;
device::mojom::blink::SensorConfigurationPtr m_defaultConfig;
mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding;
enum State {
Uninitialized,
Initializing,
......
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