| 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/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/frame/LocalFrame.h" | 8 #include "core/frame/LocalFrame.h" |
| 9 #include "modules/sensor/SensorProviderProxy.h" | 9 #include "modules/sensor/SensorProviderProxy.h" |
| 10 #include "modules/sensor/SensorReadingUpdater.h" | 10 #include "modules/sensor/SensorReadingUpdater.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 m_suspended(false) {} | 27 m_suspended(false) {} |
| 28 | 28 |
| 29 SensorProxy::~SensorProxy() {} | 29 SensorProxy::~SensorProxy() {} |
| 30 | 30 |
| 31 void SensorProxy::dispose() { | 31 void SensorProxy::dispose() { |
| 32 m_clientBinding.Close(); | 32 m_clientBinding.Close(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 DEFINE_TRACE(SensorProxy) { | 35 DEFINE_TRACE(SensorProxy) { |
| 36 visitor->trace(m_readingUpdater); | 36 visitor->trace(m_readingUpdater); |
| 37 visitor->trace(m_observers); | 37 visitor->trace(m_clients); |
| 38 visitor->trace(m_provider); | 38 visitor->trace(m_provider); |
| 39 PageVisibilityObserver::trace(visitor); | 39 PageVisibilityObserver::trace(visitor); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void SensorProxy::addObserver(Observer* observer) { | 42 void SensorProxy::addClient(Client* client) { |
| 43 if (!m_observers.contains(observer)) | 43 if (!m_clients.contains(client)) |
| 44 m_observers.insert(observer); | 44 m_clients.insert(client); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void SensorProxy::removeObserver(Observer* observer) { | 47 void SensorProxy::removeClient(Client* client) { |
| 48 m_observers.erase(observer); | 48 m_clients.erase(client); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void SensorProxy::initialize() { | 51 void SensorProxy::initialize() { |
| 52 if (m_state != Uninitialized) | 52 if (m_state != Uninitialized) |
| 53 return; | 53 return; |
| 54 | 54 |
| 55 if (!m_provider->getSensorProvider()) { | 55 if (!m_provider->getSensorProvider()) { |
| 56 handleSensorError(); | 56 handleSensorError(); |
| 57 return; | 57 return; |
| 58 } | 58 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 int readAttempts = 0; | 122 int readAttempts = 0; |
| 123 const int kMaxReadAttemptsCount = 10; | 123 const int kMaxReadAttemptsCount = 10; |
| 124 device::SensorReading readingData; | 124 device::SensorReading readingData; |
| 125 while (!tryReadFromBuffer(readingData)) { | 125 while (!tryReadFromBuffer(readingData)) { |
| 126 if (++readAttempts == kMaxReadAttemptsCount) { | 126 if (++readAttempts == kMaxReadAttemptsCount) { |
| 127 handleSensorError(); | 127 handleSensorError(); |
| 128 return; | 128 return; |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 | 131 |
| 132 m_reading = readingData; | 132 if (m_reading.timestamp != readingData.timestamp) { |
| 133 m_reading = readingData; |
| 134 for (Client* client : m_clients) |
| 135 client->onSensorReadingChanged(); |
| 136 } |
| 133 } | 137 } |
| 134 | 138 |
| 135 void SensorProxy::notifySensorChanged(double timestamp) { | 139 void SensorProxy::notifySensorChanged(double timestamp) { |
| 136 // This notification leads to sync 'onchange' event sending, so | 140 // This notification leads to sync 'onchange' event sending, so |
| 137 // we must cache m_observers as it can be modified within event handlers. | 141 // we must cache m_clients as it can be modified within event handlers. |
| 138 auto copy = m_observers; | 142 auto copy = m_clients; |
| 139 for (Observer* observer : copy) | 143 for (Client* client : copy) |
| 140 observer->onSensorReadingChanged(timestamp); | 144 client->notifySensorChanged(timestamp); |
| 141 } | 145 } |
| 142 | 146 |
| 143 void SensorProxy::RaiseError() { | 147 void SensorProxy::RaiseError() { |
| 144 handleSensorError(); | 148 handleSensorError(); |
| 145 } | 149 } |
| 146 | 150 |
| 147 void SensorProxy::SensorReadingChanged() { | 151 void SensorProxy::SensorReadingChanged() { |
| 148 DCHECK_EQ(ReportingMode::ON_CHANGE, m_mode); | 152 DCHECK_EQ(ReportingMode::ON_CHANGE, m_mode); |
| 149 if (isActive()) | 153 if (isActive()) |
| 150 m_readingUpdater->start(); | 154 m_readingUpdater->start(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 167 m_reading = device::SensorReading(); | 171 m_reading = device::SensorReading(); |
| 168 | 172 |
| 169 // The m_sensor.reset() will release all callbacks and its bound parameters, | 173 // The m_sensor.reset() will release all callbacks and its bound parameters, |
| 170 // therefore, handleSensorError accepts messages by value. | 174 // therefore, handleSensorError accepts messages by value. |
| 171 m_sensor.reset(); | 175 m_sensor.reset(); |
| 172 m_sharedBuffer.reset(); | 176 m_sharedBuffer.reset(); |
| 173 m_sharedBufferHandle.reset(); | 177 m_sharedBufferHandle.reset(); |
| 174 m_defaultConfig.reset(); | 178 m_defaultConfig.reset(); |
| 175 m_clientBinding.Close(); | 179 m_clientBinding.Close(); |
| 176 | 180 |
| 177 for (Observer* observer : m_observers) { | 181 for (Client* client : m_clients) { |
| 178 observer->onSensorError(NotReadableError, "Could not connect to a sensor", | 182 client->onSensorError(NotReadableError, "Could not connect to a sensor", |
| 179 String()); | 183 String()); |
| 180 } | 184 } |
| 181 } | 185 } |
| 182 | 186 |
| 183 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, | 187 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, |
| 184 SensorClientRequest clientRequest) { | 188 SensorClientRequest clientRequest) { |
| 185 DCHECK_EQ(Initializing, m_state); | 189 DCHECK_EQ(Initializing, m_state); |
| 186 if (!params) { | 190 if (!params) { |
| 187 handleSensorError(); | 191 handleSensorError(); |
| 188 return; | 192 return; |
| 189 } | 193 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 | 225 |
| 222 auto errorCallback = | 226 auto errorCallback = |
| 223 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this)); | 227 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this)); |
| 224 m_sensor.set_connection_error_handler( | 228 m_sensor.set_connection_error_handler( |
| 225 convertToBaseCallback(std::move(errorCallback))); | 229 convertToBaseCallback(std::move(errorCallback))); |
| 226 | 230 |
| 227 m_readingUpdater = SensorReadingUpdater::create(this, m_mode); | 231 m_readingUpdater = SensorReadingUpdater::create(this, m_mode); |
| 228 | 232 |
| 229 m_state = Initialized; | 233 m_state = Initialized; |
| 230 | 234 |
| 231 for (Observer* observer : m_observers) | 235 for (Client* client : m_clients) |
| 232 observer->onSensorInitialized(); | 236 client->onSensorInitialized(); |
| 233 } | 237 } |
| 234 | 238 |
| 235 void SensorProxy::onAddConfigurationCompleted( | 239 void SensorProxy::onAddConfigurationCompleted( |
| 236 double frequency, | 240 double frequency, |
| 237 std::unique_ptr<Function<void(bool)>> callback, | 241 std::unique_ptr<Function<void(bool)>> callback, |
| 238 bool result) { | 242 bool result) { |
| 239 if (result) { | 243 if (result) { |
| 240 m_frequenciesUsed.push_back(frequency); | 244 m_frequenciesUsed.push_back(frequency); |
| 241 std::sort(m_frequenciesUsed.begin(), m_frequenciesUsed.end()); | 245 std::sort(m_frequenciesUsed.begin(), m_frequenciesUsed.end()); |
| 242 if (isActive()) | 246 if (isActive()) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 267 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | 271 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); |
| 268 auto version = seqlock.ReadBegin(); | 272 auto version = seqlock.ReadBegin(); |
| 269 auto readingData = buffer->reading; | 273 auto readingData = buffer->reading; |
| 270 if (seqlock.ReadRetry(version)) | 274 if (seqlock.ReadRetry(version)) |
| 271 return false; | 275 return false; |
| 272 result = readingData; | 276 result = readingData; |
| 273 return true; | 277 return true; |
| 274 } | 278 } |
| 275 | 279 |
| 276 } // namespace blink | 280 } // namespace blink |
| OLD | NEW |