OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/sensor/SensorProxy.h" |
| 6 |
| 7 #include "core/frame/LocalFrame.h" |
| 8 #include "modules/sensor/SensorProviderProxy.h" |
| 9 #include "platform/mojo/MojoHelper.h" |
| 10 |
| 11 using namespace device::mojom::blink; |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 SensorProxy::SensorProxy(SensorType sensorType, SensorProviderProxy* provider) |
| 16 : m_type(sensorType) |
| 17 , m_mode(ReportingMode::CONTINUOUS) |
| 18 , m_provider(provider) |
| 19 , m_clientBinding(this) |
| 20 , m_state(SensorProxy::Uninitialized) |
| 21 , m_reading() |
| 22 { |
| 23 } |
| 24 |
| 25 SensorProxy::~SensorProxy() |
| 26 { |
| 27 } |
| 28 |
| 29 void SensorProxy::dispose() |
| 30 { |
| 31 m_clientBinding.Close(); |
| 32 } |
| 33 |
| 34 DEFINE_TRACE(SensorProxy) |
| 35 { |
| 36 visitor->trace(m_observers); |
| 37 visitor->trace(m_provider); |
| 38 } |
| 39 |
| 40 void SensorProxy::addObserver(Observer* observer) |
| 41 { |
| 42 if (!m_observers.contains(observer)) |
| 43 m_observers.add(observer); |
| 44 } |
| 45 |
| 46 void SensorProxy::removeObserver(Observer* observer) |
| 47 { |
| 48 m_observers.remove(observer); |
| 49 } |
| 50 |
| 51 void SensorProxy::initialize() |
| 52 { |
| 53 if (m_state != Uninitialized) |
| 54 return; |
| 55 |
| 56 if (!m_provider->sensorProvider()) { |
| 57 handleSensorError(); |
| 58 return; |
| 59 } |
| 60 |
| 61 m_state = Initializing; |
| 62 auto callback = convertToBaseCallback(WTF::bind(&SensorProxy::onSensorCreate
d, wrapWeakPersistent(this))); |
| 63 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), c
allback); |
| 64 m_sensor.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Senso
rProxy::handleSensorError, wrapWeakPersistent(this)))); |
| 65 } |
| 66 |
| 67 void SensorProxy::addConfiguration(SensorConfigurationPtr configuration, std::un
ique_ptr<Function<void(bool)>> callback) |
| 68 { |
| 69 DCHECK(isInitialized()); |
| 70 m_sensor->AddConfiguration(std::move(configuration), convertToBaseCallback(s
td::move(callback))); |
| 71 } |
| 72 |
| 73 void SensorProxy::removeConfiguration(SensorConfigurationPtr configuration, std:
:unique_ptr<Function<void(bool)>> callback) |
| 74 { |
| 75 DCHECK(isInitialized()); |
| 76 m_sensor->RemoveConfiguration(std::move(configuration), convertToBaseCallbac
k(std::move(callback))); |
| 77 } |
| 78 |
| 79 void SensorProxy::updateInternalReading() |
| 80 { |
| 81 DCHECK(isInitialized()); |
| 82 Reading* reading = static_cast<Reading*>(m_sharedBuffer.get()); |
| 83 m_reading = *reading; |
| 84 } |
| 85 |
| 86 void SensorProxy::RaiseError() |
| 87 { |
| 88 handleSensorError(); |
| 89 } |
| 90 |
| 91 void SensorProxy::SensorReadingChanged() |
| 92 { |
| 93 for (Observer* observer : m_observers) |
| 94 observer->onSensorReadingChanged(); |
| 95 } |
| 96 |
| 97 void SensorProxy::handleSensorError() |
| 98 { |
| 99 m_state = Uninitialized; |
| 100 m_sensor.reset(); |
| 101 for (Observer* observer : m_observers) |
| 102 observer->onSensorError(); |
| 103 } |
| 104 |
| 105 void SensorProxy::onSensorCreated(SensorReadBufferPtr buffer, SensorClientReques
t clientRequest) |
| 106 { |
| 107 DCHECK_EQ(Initializing, m_state); |
| 108 if (!buffer) { |
| 109 handleSensorError(); |
| 110 return; |
| 111 } |
| 112 |
| 113 DCHECK_EQ(0u, buffer->offset % SensorReadBuffer::kReadBufferSize); |
| 114 |
| 115 m_mode = buffer->mode; |
| 116 |
| 117 DCHECK(m_sensor.is_bound()); |
| 118 m_clientBinding.Bind(std::move(clientRequest)); |
| 119 |
| 120 m_sharedBufferHandle = std::move(buffer->memory); |
| 121 DCHECK(!m_sharedBuffer); |
| 122 m_sharedBuffer = m_sharedBufferHandle->MapAtOffset(buffer->offset, SensorRea
dBuffer::kReadBufferSize); |
| 123 |
| 124 if (!m_sharedBuffer) { |
| 125 handleSensorError(); |
| 126 return; |
| 127 } |
| 128 |
| 129 m_state = Initialized; |
| 130 for (Observer* observer : m_observers) |
| 131 observer->onSensorInitialized(); |
| 132 } |
| 133 |
| 134 } // namespace blink |
OLD | NEW |