Chromium Code Reviews| Index: third_party/WebKit/Source/modules/sensor/SensorProxy.cpp |
| diff --git a/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp b/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..66944df1c8baee39c4871630ef1ddb88679fb68d |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp |
| @@ -0,0 +1,164 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/sensor/SensorProxy.h" |
| + |
| +#include "core/frame/LocalFrame.h" |
| +#include "modules/sensor/SensorProviderProxy.h" |
| +#include "platform/mojo/MojoHelper.h" |
| + |
| +using namespace device::mojom::blink; |
| + |
| +namespace blink { |
| + |
| +SensorProxy::SensorProxy(SensorType sensorType, SensorProviderProxy* provider) |
| + : m_type(sensorType) |
| + , m_mode(ReportingMode::CONTINUOUS) |
| + , m_provider(provider) |
| + , m_clientBinding(this) |
| + , m_state(SensorProxy::Uninitialized) |
| + , m_reading() |
| +{ |
| +} |
| + |
| +SensorProxy::~SensorProxy() |
| +{ |
| +} |
| + |
| +void SensorProxy::dispose() |
| +{ |
| + m_clientBinding.Close(); |
| +} |
| + |
| +DEFINE_TRACE(SensorProxy) |
| +{ |
| + visitor->trace(m_observers); |
| + visitor->trace(m_provider); |
| +} |
| + |
| +void SensorProxy::addObserver(Observer* observer) |
| +{ |
| + if (!m_observers.contains(observer)) |
| + m_observers.add(observer); |
| +} |
| + |
| +void SensorProxy::removeObserver(Observer* observer) |
| +{ |
| + if (m_observers.contains(observer)) |
| + m_observers.remove(observer); |
| + |
| + if (m_observers.isEmpty()) |
| + m_provider->removeSensor(this); |
| +} |
| + |
| +void SensorProxy::initialize() |
| +{ |
| + if (m_state != Uninitialized) |
| + return; |
| + m_state = Initializing; |
| + auto callback = convertToBaseCallback(WTF::bind(&SensorProxy::onSensorCreated, wrapWeakPersistent(this))); |
| + m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), callback); |
| +} |
| + |
| +void SensorProxy::addConfiguration(SensorConfigurationPtr configuration, std::unique_ptr<Function<void(bool)>> callback) |
| +{ |
| + DCHECK(isInitialized()); |
| + m_sensor->AddConfiguration(std::move(configuration), convertToBaseCallback(std::move(callback))); |
| +} |
| + |
| +void SensorProxy::removeConfiguration(SensorConfigurationPtr configuration, std::unique_ptr<Function<void(bool)>> callback) |
| +{ |
| + DCHECK(isInitialized()); |
| + m_sensor->RemoveConfiguration(std::move(configuration), convertToBaseCallback(std::move(callback))); |
| +} |
| + |
| +void SensorProxy::suspend() |
| +{ |
| + DCHECK(isInitialized()); |
| + m_sensor->Suspend(); |
| +} |
| + |
| +void SensorProxy::resume() |
| +{ |
| + DCHECK(isInitialized()); |
| + m_sensor->Resume(); |
| +} |
| + |
| +void SensorProxy::updateReading() |
| +{ |
| + DCHECK(isInitialized()); |
| + Reading* reading = static_cast<Reading*>(m_sharedBuffer.get()); |
| + m_reading = *reading; |
| +} |
| + |
| +void SensorProxy::RaiseError() |
| +{ |
| + handleSensorError(); |
| +} |
| + |
| +void SensorProxy::SensorReadingChanged() |
| +{ |
| + for (Observer* observer : m_observers) |
| + observer->onSensorReadingChanged(); |
| +} |
| + |
| +void SensorProxy::handleSensorError() |
| +{ |
| + m_state = Uninitialized; |
| + |
| + ObserversSet observersCopy(m_observers); |
| + for (Observer* observer : observersCopy) |
| + observer->onSensorError(); |
| + |
| + m_observers.clear(); |
| + m_provider->removeSensor(this); |
| +} |
| + |
| +void SensorProxy::onSensorCreated(SensorReadBufferPtr buffer, SensorClientRequest clientRequest) |
| +{ |
| + DCHECK_EQ(Initializing, m_state); |
| + if (!buffer) { |
| + handleSensorError(); |
| + return; |
| + } |
| + |
| + if (buffer->offset % SensorReadBuffer::kReadBufferSize != 0) { |
| + NOTREACHED(); |
| + handleSensorError(); |
| + return; |
| + } |
| + |
| + m_mode = buffer->mode; |
| + |
| + m_clientBinding.Bind(std::move(clientRequest)); |
| + m_sensor.set_connection_error_handler(convertToBaseCallback(WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this)))); |
| + |
| + m_sharedBufferHandle = std::move(buffer->memory); |
| + DCHECK(!m_sharedBuffer); |
| + m_sharedBuffer = m_sharedBufferHandle->MapAtOffset(buffer->offset, SensorReadBuffer::kReadBufferSize); |
| + |
| + if (!m_sharedBuffer) { |
| + handleSensorError(); |
| + return; |
| + } |
| + |
| + auto callback = convertToBaseCallback(WTF::bind(&SensorProxy::onDefaultConfiguration, wrapWeakPersistent(this))); |
| + m_sensor->GetDefaultConfiguration(callback); |
|
timvolodine
2016/09/01 19:02:06
why is this needed? i.e. wouldn't a sensor always
Mikhail
2016/09/02 08:23:43
It is better to know the default config beforehand
timvolodine
2016/09/02 19:41:28
In that case I think my question still stands: why
Mikhail
2016/09/05 10:26:27
Oh sorry, I probably misunderstood your comment. I
|
| +} |
| + |
| +void SensorProxy::onDefaultConfiguration(device::mojom::blink::SensorConfigurationPtr config) |
| +{ |
| + DCHECK_EQ(Initializing, m_state); |
| + if (!config) { |
| + handleSensorError(); |
| + return; |
| + } |
| + m_defaultConfiguration = std::move(config); |
|
timvolodine
2016/09/02 19:41:28
Is m_defaultConfiguration used anywhere? looks lik
|
| + |
| + m_state = Initialized; |
| + for (Observer* observer : m_observers) |
| + observer->onSensorInitialized(); |
| +} |
| + |
| +} // namespace blink |