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

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

Issue 2704123006: [Sensors] Clamp given sampling frequency to the minimum supported one (Closed)
Patch Set: Created 3 years, 10 months 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/dom/TaskRunnerHelper.h" 10 #include "core/dom/TaskRunnerHelper.h"
(...skipping 28 matching lines...) Expand all
39 if (!toDocument(executionContext)->domWindow()->frame() || 39 if (!toDocument(executionContext)->domWindow()->frame() ||
40 !toDocument(executionContext)->frame()->isMainFrame()) { 40 !toDocument(executionContext)->frame()->isMainFrame()) {
41 exceptionState.throwSecurityError( 41 exceptionState.throwSecurityError(
42 "Must be in a top-level browsing context"); 42 "Must be in a top-level browsing context");
43 return; 43 return;
44 } 44 }
45 45
46 // Check the given frequency value. 46 // Check the given frequency value.
47 if (m_sensorOptions.hasFrequency()) { 47 if (m_sensorOptions.hasFrequency()) {
48 double frequency = m_sensorOptions.frequency(); 48 double frequency = m_sensorOptions.frequency();
49 if (frequency <= 0.0) {
50 exceptionState.throwRangeError("Frequency must be positive.");
51 return;
52 }
53
54 if (frequency > SensorConfiguration::kMaxAllowedFrequency) { 49 if (frequency > SensorConfiguration::kMaxAllowedFrequency) {
55 m_sensorOptions.setFrequency(SensorConfiguration::kMaxAllowedFrequency); 50 m_sensorOptions.setFrequency(SensorConfiguration::kMaxAllowedFrequency);
56 ConsoleMessage* consoleMessage = ConsoleMessage::create( 51 ConsoleMessage* consoleMessage = ConsoleMessage::create(
57 JSMessageSource, InfoMessageLevel, "Frequency is limited to 60 Hz."); 52 JSMessageSource, InfoMessageLevel, "Frequency is limited to 60 Hz.");
58 executionContext->addConsoleMessage(consoleMessage); 53 executionContext->addConsoleMessage(consoleMessage);
59 } 54 }
60 } 55 }
61 } 56 }
62 57
63 Sensor::~Sensor() = default; 58 Sensor::~Sensor() = default;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 m_state == Sensor::SensorState::Idle || 140 m_state == Sensor::SensorState::Idle ||
146 m_state == Sensor::SensorState::Errored) 141 m_state == Sensor::SensorState::Errored)
147 return false; 142 return false;
148 return getExecutionContext() && hasEventListeners(); 143 return getExecutionContext() && hasEventListeners();
149 } 144 }
150 145
151 auto Sensor::createSensorConfig() -> SensorConfigurationPtr { 146 auto Sensor::createSensorConfig() -> SensorConfigurationPtr {
152 auto result = SensorConfiguration::New(); 147 auto result = SensorConfiguration::New();
153 148
154 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency; 149 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency;
155 double maximumFrequency = m_sensorProxy->maximumFrequency(); 150 double minimumFrequency = m_sensorProxy->frequencyLimits().first;
151 double maximumFrequency = m_sensorProxy->frequencyLimits().second;
156 152
157 double frequency = m_sensorOptions.hasFrequency() 153 double frequency = m_sensorOptions.hasFrequency()
158 ? m_sensorOptions.frequency() 154 ? m_sensorOptions.frequency()
159 : defaultFrequency; 155 : defaultFrequency;
160 156
161 if (frequency > maximumFrequency) 157 if (frequency > maximumFrequency)
162 frequency = maximumFrequency; 158 frequency = maximumFrequency;
159 if (frequency < minimumFrequency)
160 frequency = minimumFrequency;
163 161
164 result->frequency = frequency; 162 result->frequency = frequency;
165 return result; 163 return result;
166 } 164 }
167 165
168 double Sensor::readingValue(int index, bool& isNull) const { 166 double Sensor::readingValue(int index, bool& isNull) const {
169 if (!canReturnReadings()) { 167 if (!canReturnReadings()) {
170 isNull = true; 168 isNull = true;
171 return 0.0; 169 return 0.0;
172 } 170 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 m_sensorProxy->addObserver(this); 240 m_sensorProxy->addObserver(this);
243 if (!m_sensorProxy->isInitialized()) { 241 if (!m_sensorProxy->isInitialized()) {
244 m_sensorProxy->initialize(); 242 m_sensorProxy->initialize();
245 return; 243 return;
246 } 244 }
247 245
248 if (!m_configuration) { 246 if (!m_configuration) {
249 m_configuration = createSensorConfig(); 247 m_configuration = createSensorConfig();
250 DCHECK(m_configuration); 248 DCHECK(m_configuration);
251 DCHECK(m_configuration->frequency > 0 && 249 DCHECK(m_configuration->frequency > 0 &&
252 m_configuration->frequency <= m_sensorProxy->maximumFrequency()); 250 m_configuration->frequency <=
251 SensorConfiguration::kMaxAllowedFrequency);
253 } 252 }
254 253
255 auto startCallback = 254 auto startCallback =
256 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); 255 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this));
257 m_sensorProxy->addConfiguration(m_configuration->Clone(), 256 m_sensorProxy->addConfiguration(m_configuration->Clone(),
258 std::move(startCallback)); 257 std::move(startCallback));
259 } 258 }
260 259
261 void Sensor::stopListening() { 260 void Sensor::stopListening() {
262 DCHECK(m_sensorProxy); 261 DCHECK(m_sensorProxy);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 } 320 }
322 321
323 bool Sensor::canReturnReadings() const { 322 bool Sensor::canReturnReadings() const {
324 if (m_state != Sensor::SensorState::Activated) 323 if (m_state != Sensor::SensorState::Activated)
325 return false; 324 return false;
326 DCHECK(m_sensorProxy); 325 DCHECK(m_sensorProxy);
327 return m_sensorProxy->reading().timestamp != 0.0; 326 return m_sensorProxy->reading().timestamp != 0.0;
328 } 327 }
329 328
330 } // namespace blink 329 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698