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..6098fd508940464c4ebcca9f20e9cb1611fcd7ed |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp |
| @@ -0,0 +1,143 @@ |
| +// 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) |
| +{ |
| +} |
| + |
| +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)) |
|
timvolodine
2016/08/25 17:52:33
no need for contains
Mikhail
2016/08/26 16:42:42
Done.
|
| + 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::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) |
| +{ |
| + if (!buffer) { |
|
timvolodine
2016/08/25 17:52:32
DCHECK?
Mikhail
2016/08/26 16:42:42
(!buffer) just means that platform could not creat
timvolodine
2016/09/01 19:02:05
Would it be better to go through handleSensorError
Mikhail
2016/09/02 08:23:43
handleSensorError() is a connection_error_handler
|
| + handleSensorError(); |
| + return; |
| + } |
| + |
| + if (buffer->offset % SensorReadBuffer::kReadBufferSize != 0) { |
|
timvolodine
2016/08/25 17:52:32
DCHECK?
Mikhail
2016/08/26 16:42:42
we've NOTREACHED(); below + some exit logic for re
timvolodine
2016/09/01 19:02:06
still looks DCHECK to me.. in other words if the c
Mikhail
2016/09/02 08:23:43
Agreed. Done.
|
| + 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; |
| + } |
| + |
| + m_state = Initialized; |
| + |
| + for (Observer* observer : m_observers) |
| + observer->onSensorInitialized(); |
| +} |
| + |
| +} // namespace blink |