| 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..9a012d0b29e8b0b0ae2732796f7616e550c2a51a
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/modules/sensor/SensorProxy.cpp
|
| @@ -0,0 +1,146 @@
|
| +// 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)
|
| +{
|
| + 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;
|
| + }
|
| +
|
| + DCHECK_EQ(0u, buffer->offset % SensorReadBuffer::kReadBufferSize);
|
| +
|
| + 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
|
|
|