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