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 if (m_observers.isEmpty()) |
| 51 m_provider->removeSensor(this); |
| 52 } |
| 53 |
| 54 void SensorProxy::initialize() |
| 55 { |
| 56 if (m_state != Uninitialized) |
| 57 return; |
| 58 m_state = Initializing; |
| 59 auto callback = convertToBaseCallback(WTF::bind(&SensorProxy::onSensorCreate
d, wrapWeakPersistent(this))); |
| 60 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), c
allback); |
| 61 } |
| 62 |
| 63 void SensorProxy::addConfiguration(SensorConfigurationPtr configuration, std::un
ique_ptr<Function<void(bool)>> callback) |
| 64 { |
| 65 DCHECK(isInitialized()); |
| 66 m_sensor->AddConfiguration(std::move(configuration), convertToBaseCallback(s
td::move(callback))); |
| 67 } |
| 68 |
| 69 void SensorProxy::removeConfiguration(SensorConfigurationPtr configuration, std:
:unique_ptr<Function<void(bool)>> callback) |
| 70 { |
| 71 DCHECK(isInitialized()); |
| 72 m_sensor->RemoveConfiguration(std::move(configuration), convertToBaseCallbac
k(std::move(callback))); |
| 73 } |
| 74 |
| 75 void SensorProxy::suspend() |
| 76 { |
| 77 DCHECK(isInitialized()); |
| 78 m_sensor->Suspend(); |
| 79 } |
| 80 |
| 81 void SensorProxy::resume() |
| 82 { |
| 83 DCHECK(isInitialized()); |
| 84 m_sensor->Resume(); |
| 85 } |
| 86 |
| 87 void SensorProxy::updateReading() |
| 88 { |
| 89 DCHECK(isInitialized()); |
| 90 Reading* reading = static_cast<Reading*>(m_sharedBuffer.get()); |
| 91 m_reading = *reading; |
| 92 } |
| 93 |
| 94 void SensorProxy::RaiseError() |
| 95 { |
| 96 handleSensorError(); |
| 97 } |
| 98 |
| 99 void SensorProxy::SensorReadingChanged() |
| 100 { |
| 101 for (Observer* observer : m_observers) |
| 102 observer->onSensorReadingChanged(); |
| 103 } |
| 104 |
| 105 void SensorProxy::handleSensorError() |
| 106 { |
| 107 m_state = Uninitialized; |
| 108 |
| 109 ObserversSet observersCopy(m_observers); |
| 110 for (Observer* observer : observersCopy) |
| 111 observer->onSensorError(); |
| 112 |
| 113 m_observers.clear(); |
| 114 m_provider->removeSensor(this); |
| 115 } |
| 116 |
| 117 void SensorProxy::onSensorCreated(SensorReadBufferPtr buffer, SensorClientReques
t clientRequest) |
| 118 { |
| 119 DCHECK_EQ(Initializing, m_state); |
| 120 if (!buffer) { |
| 121 handleSensorError(); |
| 122 return; |
| 123 } |
| 124 |
| 125 DCHECK_EQ(0u, buffer->offset % SensorReadBuffer::kReadBufferSize); |
| 126 |
| 127 m_mode = buffer->mode; |
| 128 |
| 129 m_clientBinding.Bind(std::move(clientRequest)); |
| 130 m_sensor.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Senso
rProxy::handleSensorError, wrapWeakPersistent(this)))); |
| 131 |
| 132 m_sharedBufferHandle = std::move(buffer->memory); |
| 133 DCHECK(!m_sharedBuffer); |
| 134 m_sharedBuffer = m_sharedBufferHandle->MapAtOffset(buffer->offset, SensorRea
dBuffer::kReadBufferSize); |
| 135 |
| 136 if (!m_sharedBuffer) { |
| 137 handleSensorError(); |
| 138 return; |
| 139 } |
| 140 |
| 141 m_state = Initialized; |
| 142 for (Observer* observer : m_observers) |
| 143 observer->onSensorInitialized(); |
| 144 } |
| 145 |
| 146 } // namespace blink |
OLD | NEW |