| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/sensor/SensorProxy.h" | 5 #include "modules/sensor/SensorProxy.h" |
| 6 | 6 |
| 7 #include "core/frame/LocalFrame.h" | 7 #include "core/frame/LocalFrame.h" |
| 8 #include "modules/sensor/SensorProviderProxy.h" | 8 #include "modules/sensor/SensorProviderProxy.h" |
| 9 #include "modules/sensor/SensorReading.h" |
| 9 #include "platform/mojo/MojoHelper.h" | 10 #include "platform/mojo/MojoHelper.h" |
| 10 #include "public/platform/Platform.h" | 11 #include "public/platform/Platform.h" |
| 11 | 12 |
| 12 using namespace device::mojom::blink; | 13 using namespace device::mojom::blink; |
| 13 | 14 |
| 14 namespace blink { | 15 namespace blink { |
| 15 | 16 |
| 16 SensorProxy::SensorProxy(SensorType sensorType, SensorProviderProxy* provider) | 17 SensorProxy::SensorProxy(SensorType sensorType, |
| 18 SensorProviderProxy* provider, |
| 19 std::unique_ptr<SensorReadingFactory> readingFactory) |
| 17 : m_type(sensorType), | 20 : m_type(sensorType), |
| 18 m_mode(ReportingMode::CONTINUOUS), | 21 m_mode(ReportingMode::CONTINUOUS), |
| 19 m_provider(provider), | 22 m_provider(provider), |
| 20 m_clientBinding(this), | 23 m_clientBinding(this), |
| 21 m_state(SensorProxy::Uninitialized), | 24 m_state(SensorProxy::Uninitialized), |
| 22 m_reading(), | 25 m_suspended(false), |
| 23 m_suspended(false) {} | 26 m_readingFactory(std::move(readingFactory)) {} |
| 24 | 27 |
| 25 SensorProxy::~SensorProxy() {} | 28 SensorProxy::~SensorProxy() {} |
| 26 | 29 |
| 27 void SensorProxy::dispose() { | 30 void SensorProxy::dispose() { |
| 28 m_clientBinding.Close(); | 31 m_clientBinding.Close(); |
| 29 } | 32 } |
| 30 | 33 |
| 31 DEFINE_TRACE(SensorProxy) { | 34 DEFINE_TRACE(SensorProxy) { |
| 35 visitor->trace(m_reading); |
| 32 visitor->trace(m_observers); | 36 visitor->trace(m_observers); |
| 33 visitor->trace(m_provider); | 37 visitor->trace(m_provider); |
| 34 } | 38 } |
| 35 | 39 |
| 36 void SensorProxy::addObserver(Observer* observer) { | 40 void SensorProxy::addObserver(Observer* observer) { |
| 37 if (!m_observers.contains(observer)) | 41 if (!m_observers.contains(observer)) |
| 38 m_observers.add(observer); | 42 m_observers.add(observer); |
| 39 } | 43 } |
| 40 | 44 |
| 41 void SensorProxy::removeObserver(Observer* observer) { | 45 void SensorProxy::removeObserver(Observer* observer) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 m_sensor->Resume(); | 95 m_sensor->Resume(); |
| 92 m_suspended = false; | 96 m_suspended = false; |
| 93 } | 97 } |
| 94 | 98 |
| 95 const device::mojom::blink::SensorConfiguration* SensorProxy::defaultConfig() | 99 const device::mojom::blink::SensorConfiguration* SensorProxy::defaultConfig() |
| 96 const { | 100 const { |
| 97 DCHECK(isInitialized()); | 101 DCHECK(isInitialized()); |
| 98 return m_defaultConfig.get(); | 102 return m_defaultConfig.get(); |
| 99 } | 103 } |
| 100 | 104 |
| 101 void SensorProxy::updateInternalReading() { | 105 void SensorProxy::updateSensorReading() { |
| 102 DCHECK(isInitialized()); | 106 DCHECK(isInitialized()); |
| 107 DCHECK(m_readingFactory); |
| 103 int readAttempts = 0; | 108 int readAttempts = 0; |
| 104 const int kMaxReadAttemptsCount = 10; | 109 const int kMaxReadAttemptsCount = 10; |
| 105 while (!tryReadFromBuffer()) { | 110 device::SensorReading readingData; |
| 111 while (!tryReadFromBuffer(readingData)) { |
| 106 if (++readAttempts == kMaxReadAttemptsCount) { | 112 if (++readAttempts == kMaxReadAttemptsCount) { |
| 107 handleSensorError(); | 113 handleSensorError(); |
| 108 return; | 114 return; |
| 109 } | 115 } |
| 110 } | 116 } |
| 117 |
| 118 m_reading = m_readingFactory->createSensorReading(readingData); |
| 111 } | 119 } |
| 112 | 120 |
| 113 void SensorProxy::RaiseError() { | 121 void SensorProxy::RaiseError() { |
| 114 handleSensorError(); | 122 handleSensorError(); |
| 115 } | 123 } |
| 116 | 124 |
| 117 void SensorProxy::SensorReadingChanged() { | 125 void SensorProxy::SensorReadingChanged() { |
| 118 for (Observer* observer : m_observers) | 126 for (Observer* observer : m_observers) |
| 119 observer->onSensorReadingChanged(); | 127 observer->onSensorReadingChanged(); |
| 120 } | 128 } |
| 121 | 129 |
| 122 void SensorProxy::handleSensorError(ExceptionCode code, | 130 void SensorProxy::handleSensorError(ExceptionCode code, |
| 123 const String& sanitizedMessage, | 131 const String& sanitizedMessage, |
| 124 const String& unsanitizedMessage) { | 132 const String& unsanitizedMessage) { |
| 125 if (!Platform::current()) { | 133 if (!Platform::current()) { |
| 126 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. | 134 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. |
| 127 return; | 135 return; |
| 128 } | 136 } |
| 129 m_state = Uninitialized; | 137 m_state = Uninitialized; |
| 130 m_sensor.reset(); | 138 m_sensor.reset(); |
| 131 m_sharedBuffer.reset(); | 139 m_sharedBuffer.reset(); |
| 132 m_sharedBufferHandle.reset(); | 140 m_sharedBufferHandle.reset(); |
| 133 m_defaultConfig.reset(); | 141 m_defaultConfig.reset(); |
| 134 m_clientBinding.Close(); | 142 m_clientBinding.Close(); |
| 143 m_reading = nullptr; |
| 135 | 144 |
| 136 for (Observer* observer : m_observers) | 145 for (Observer* observer : m_observers) |
| 137 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); | 146 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); |
| 138 } | 147 } |
| 139 | 148 |
| 140 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, | 149 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, |
| 141 SensorClientRequest clientRequest) { | 150 SensorClientRequest clientRequest) { |
| 142 DCHECK_EQ(Initializing, m_state); | 151 DCHECK_EQ(Initializing, m_state); |
| 143 if (!params) { | 152 if (!params) { |
| 144 handleSensorError(NotFoundError, "Sensor is not present on the platform."); | 153 handleSensorError(NotFoundError, "Sensor is not present on the platform."); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 172 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this), | 181 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this), |
| 173 UnknownError, String("Internal error"), String()); | 182 UnknownError, String("Internal error"), String()); |
| 174 m_sensor.set_connection_error_handler( | 183 m_sensor.set_connection_error_handler( |
| 175 convertToBaseCallback(std::move(errorCallback))); | 184 convertToBaseCallback(std::move(errorCallback))); |
| 176 | 185 |
| 177 m_state = Initialized; | 186 m_state = Initialized; |
| 178 for (Observer* observer : m_observers) | 187 for (Observer* observer : m_observers) |
| 179 observer->onSensorInitialized(); | 188 observer->onSensorInitialized(); |
| 180 } | 189 } |
| 181 | 190 |
| 182 bool SensorProxy::tryReadFromBuffer() { | 191 bool SensorProxy::tryReadFromBuffer(device::SensorReading& result) { |
| 183 DCHECK(isInitialized()); | 192 DCHECK(isInitialized()); |
| 184 const ReadingBuffer* buffer = | 193 const ReadingBuffer* buffer = |
| 185 static_cast<const ReadingBuffer*>(m_sharedBuffer.get()); | 194 static_cast<const ReadingBuffer*>(m_sharedBuffer.get()); |
| 186 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | 195 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); |
| 187 auto version = seqlock.ReadBegin(); | 196 auto version = seqlock.ReadBegin(); |
| 188 auto reading = buffer->reading; | 197 auto readingData = buffer->reading; |
| 189 if (seqlock.ReadRetry(version)) | 198 if (seqlock.ReadRetry(version)) |
| 190 return false; | 199 return false; |
| 191 m_reading = reading; | 200 result = readingData; |
| 192 return true; | 201 return true; |
| 193 } | 202 } |
| 194 | 203 |
| 195 } // namespace blink | 204 } // namespace blink |
| OLD | NEW |