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/Sensor.h" | 5 #include "modules/sensor/Sensor.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
9 #include "core/dom/ExecutionContextTask.h" | 9 #include "core/dom/ExecutionContextTask.h" |
10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 EventTargetWithInlineData::trace(visitor); | 131 EventTargetWithInlineData::trace(visitor); |
132 } | 132 } |
133 | 133 |
134 bool Sensor::hasPendingActivity() const { | 134 bool Sensor::hasPendingActivity() const { |
135 if (m_state == Sensor::SensorState::IDLE || | 135 if (m_state == Sensor::SensorState::IDLE || |
136 m_state == Sensor::SensorState::ERRORED) | 136 m_state == Sensor::SensorState::ERRORED) |
137 return false; | 137 return false; |
138 return hasEventListeners(); | 138 return hasEventListeners(); |
139 } | 139 } |
140 | 140 |
| 141 auto Sensor::createSensorConfig() -> SensorConfigurationPtr { |
| 142 auto result = SensorConfiguration::New(); |
| 143 |
| 144 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency; |
| 145 double maximumFrequency = m_sensorProxy->maximumFrequency(); |
| 146 |
| 147 double frequency = m_sensorOptions.hasFrequency() |
| 148 ? m_sensorOptions.frequency() |
| 149 : defaultFrequency; |
| 150 |
| 151 if (frequency > maximumFrequency) |
| 152 frequency = maximumFrequency; |
| 153 |
| 154 if (frequency > maximumFrequency) |
| 155 frequency = maximumFrequency; |
| 156 |
| 157 result->frequency = frequency; |
| 158 return result; |
| 159 } |
| 160 |
141 void Sensor::initSensorProxyIfNeeded() { | 161 void Sensor::initSensorProxyIfNeeded() { |
142 if (m_sensorProxy) | 162 if (m_sensorProxy) |
143 return; | 163 return; |
144 | 164 |
145 Document* document = toDocument(getExecutionContext()); | 165 Document* document = toDocument(getExecutionContext()); |
146 if (!document || !document->frame()) | 166 if (!document || !document->frame()) |
147 return; | 167 return; |
148 | 168 |
149 m_sensorProxy = | 169 m_sensorProxy = |
150 SensorProviderProxy::from(document->frame())->getOrCreateSensor(m_type); | 170 SensorProviderProxy::from(document->frame())->getOrCreateSensor(m_type); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 DCHECK(m_sensorReading); | 247 DCHECK(m_sensorReading); |
228 } | 248 } |
229 | 249 |
230 m_sensorProxy->addObserver(this); | 250 m_sensorProxy->addObserver(this); |
231 if (!m_sensorProxy->isInitialized()) { | 251 if (!m_sensorProxy->isInitialized()) { |
232 m_sensorProxy->initialize(); | 252 m_sensorProxy->initialize(); |
233 return; | 253 return; |
234 } | 254 } |
235 | 255 |
236 if (!m_configuration) { | 256 if (!m_configuration) { |
237 m_configuration = | 257 m_configuration = createSensorConfig(); |
238 createSensorConfig(m_sensorOptions, *m_sensorProxy->defaultConfig()); | |
239 DCHECK(m_configuration); | 258 DCHECK(m_configuration); |
| 259 DCHECK(m_configuration->frequency && |
| 260 m_configuration->frequency <= m_sensorProxy->maximumFrequency()); |
240 } | 261 } |
241 | 262 |
242 auto startCallback = | 263 auto startCallback = |
243 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); | 264 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); |
244 m_sensorProxy->addConfiguration(m_configuration->Clone(), | 265 m_sensorProxy->addConfiguration(m_configuration->Clone(), |
245 std::move(startCallback)); | 266 std::move(startCallback)); |
246 } | 267 } |
247 | 268 |
248 void Sensor::stopListening() { | 269 void Sensor::stopListening() { |
249 DCHECK(m_sensorProxy); | 270 DCHECK(m_sensorProxy); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 void Sensor::notifyStateChanged() { | 352 void Sensor::notifyStateChanged() { |
332 dispatchEvent(Event::create(EventTypeNames::statechange)); | 353 dispatchEvent(Event::create(EventTypeNames::statechange)); |
333 } | 354 } |
334 | 355 |
335 void Sensor::notifyError(DOMException* error) { | 356 void Sensor::notifyError(DOMException* error) { |
336 dispatchEvent( | 357 dispatchEvent( |
337 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 358 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
338 } | 359 } |
339 | 360 |
340 } // namespace blink | 361 } // namespace blink |
OLD | NEW |