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

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

Powered by Google App Engine
This is Rietveld 408576698