Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: third_party/WebKit/Source/modules/sensor/Sensor.cpp

Issue 2465363004: [Sensors] Consider maximum supported frequency (Closed)
Patch Set: Comment from Tim Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 result->frequency = frequency;
155 return result;
156 }
157
141 void Sensor::initSensorProxyIfNeeded() { 158 void Sensor::initSensorProxyIfNeeded() {
142 if (m_sensorProxy) 159 if (m_sensorProxy)
143 return; 160 return;
144 161
145 Document* document = toDocument(getExecutionContext()); 162 Document* document = toDocument(getExecutionContext());
146 if (!document || !document->frame()) 163 if (!document || !document->frame())
147 return; 164 return;
148 165
149 auto provider = SensorProviderProxy::from(document->frame()); 166 auto provider = SensorProviderProxy::from(document->frame());
150 m_sensorProxy = provider->getSensor(m_type); 167 m_sensorProxy = provider->getSensor(m_type);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 DCHECK(m_sensorProxy); 245 DCHECK(m_sensorProxy);
229 updateState(Sensor::SensorState::ACTIVATING); 246 updateState(Sensor::SensorState::ACTIVATING);
230 247
231 m_sensorProxy->addObserver(this); 248 m_sensorProxy->addObserver(this);
232 if (!m_sensorProxy->isInitialized()) { 249 if (!m_sensorProxy->isInitialized()) {
233 m_sensorProxy->initialize(); 250 m_sensorProxy->initialize();
234 return; 251 return;
235 } 252 }
236 253
237 if (!m_configuration) { 254 if (!m_configuration) {
238 m_configuration = 255 m_configuration = createSensorConfig();
239 createSensorConfig(m_sensorOptions, *m_sensorProxy->defaultConfig());
240 DCHECK(m_configuration); 256 DCHECK(m_configuration);
257 DCHECK(m_configuration->frequency > 0 &&
258 m_configuration->frequency <= m_sensorProxy->maximumFrequency());
241 } 259 }
242 260
243 auto startCallback = 261 auto startCallback =
244 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); 262 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this));
245 m_sensorProxy->addConfiguration(m_configuration->Clone(), 263 m_sensorProxy->addConfiguration(m_configuration->Clone(),
246 std::move(startCallback)); 264 std::move(startCallback));
247 } 265 }
248 266
249 void Sensor::stopListening() { 267 void Sensor::stopListening() {
250 DCHECK(m_sensorProxy); 268 DCHECK(m_sensorProxy);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 void Sensor::notifyStateChanged() { 348 void Sensor::notifyStateChanged() {
331 dispatchEvent(Event::create(EventTypeNames::statechange)); 349 dispatchEvent(Event::create(EventTypeNames::statechange));
332 } 350 }
333 351
334 void Sensor::notifyError(DOMException* error) { 352 void Sensor::notifyError(DOMException* error) {
335 dispatchEvent( 353 dispatchEvent(
336 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); 354 SensorErrorEvent::create(EventTypeNames::error, std::move(error)));
337 } 355 }
338 356
339 } // namespace blink 357 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/Sensor.h ('k') | third_party/WebKit/Source/modules/sensor/SensorProxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698