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

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

Issue 2885373002: [Sensors] Simplify calculation of the reporting period (Closed)
Patch Set: Created 3 years, 7 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 15 matching lines...) Expand all
26 } // namespace 26 } // namespace
27 27
28 Sensor::Sensor(ExecutionContext* execution_context, 28 Sensor::Sensor(ExecutionContext* execution_context,
29 const SensorOptions& sensor_options, 29 const SensorOptions& sensor_options,
30 ExceptionState& exception_state, 30 ExceptionState& exception_state,
31 SensorType type) 31 SensorType type)
32 : ContextLifecycleObserver(execution_context), 32 : ContextLifecycleObserver(execution_context),
33 sensor_options_(sensor_options), 33 sensor_options_(sensor_options),
34 type_(type), 34 type_(type),
35 state_(SensorState::kIdle), 35 state_(SensorState::kIdle),
36 last_update_timestamp_(0.0),
37 pending_reading_update_(false) { 36 pending_reading_update_(false) {
38 // Check secure context. 37 // Check secure context.
39 String error_message; 38 String error_message;
40 if (!execution_context->IsSecureContext(error_message)) { 39 if (!execution_context->IsSecureContext(error_message)) {
41 exception_state.ThrowDOMException(kSecurityError, error_message); 40 exception_state.ThrowDOMException(kSecurityError, error_message);
42 return; 41 return;
43 } 42 }
44 43
45 // Check top-level browsing context. 44 // Check top-level browsing context.
46 if (!ToDocument(execution_context)->domWindow()->GetFrame() || 45 if (!ToDocument(execution_context)->domWindow()->GetFrame() ||
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 StopListening(); 162 StopListening();
164 } 163 }
165 164
166 void Sensor::OnSensorInitialized() { 165 void Sensor::OnSensorInitialized() {
167 if (state_ != Sensor::SensorState::kActivating) 166 if (state_ != Sensor::SensorState::kActivating)
168 return; 167 return;
169 168
170 RequestAddConfiguration(); 169 RequestAddConfiguration();
171 } 170 }
172 171
173 void Sensor::OnSensorReadingChanged(double timestamp) { 172 void Sensor::OnSensorReadingChanged() {
174 if (state_ != Sensor::SensorState::kActivated) 173 if (state_ != Sensor::SensorState::kActivated)
175 return; 174 return;
176 175
177 // Return if reading update is already scheduled or the cached 176 // Return if reading update is already scheduled or the cached
178 // reading is up-to-date. 177 // reading is up-to-date.
179 if (pending_reading_update_ || 178 if (pending_reading_update_)
180 sensor_proxy_->reading().timestamp == reading_.timestamp)
181 return; 179 return;
182 180
183 pending_reading_update_ = true; 181 pending_reading_update_ = true;
184 182
185 double elapsedTime = timestamp - last_update_timestamp_; 183 double elapsedTime = sensor_proxy_->reading().timestamp - reading_.timestamp;
184 DCHECK_GT(elapsedTime, 0.0);
186 185
187 DCHECK_GT(configuration_->frequency, 0.0); 186 DCHECK_GT(configuration_->frequency, 0.0);
188 double waitingTime = 1 / configuration_->frequency - elapsedTime; 187 double waitingTime = 1 / configuration_->frequency - elapsedTime;
189 188
190 // Negative or zero 'waitingTime' means that polling period has elapsed. 189 // Negative or zero 'waitingTime' means that polling period has elapsed.
191 // We also avoid scheduling if the elapsed time is slightly behind the 190 // We also avoid scheduling if the elapsed time is slightly behind the
192 // polling period. 191 // polling period.
193 auto sensor_reading_changed = 192 auto sensor_reading_changed =
194 WTF::Bind(&Sensor::UpdateReading, WrapWeakPersistent(this)); 193 WTF::Bind(&Sensor::UpdateReading, WrapWeakPersistent(this));
195 if (waitingTime < kMinWaitingInterval) { 194 if (waitingTime < kMinWaitingInterval) {
(...skipping 17 matching lines...) Expand all
213 212
214 void Sensor::OnAddConfigurationRequestCompleted(bool result) { 213 void Sensor::OnAddConfigurationRequestCompleted(bool result) {
215 if (state_ != SensorState::kActivating) 214 if (state_ != SensorState::kActivating)
216 return; 215 return;
217 216
218 if (!result) { 217 if (!result) {
219 HandleError(kNotReadableError, "start() call has failed."); 218 HandleError(kNotReadableError, "start() call has failed.");
220 return; 219 return;
221 } 220 }
222 221
223 // The initial value for m_lastUpdateTimestamp is set to current time,
224 // so that the first reading update will be notified considering the given
225 // frequency hint.
226 last_update_timestamp_ = WTF::MonotonicallyIncreasingTime();
227
228 UpdateState(Sensor::SensorState::kActivated); 222 UpdateState(Sensor::SensorState::kActivated);
229 223
230 if (GetExecutionContext()) { 224 if (GetExecutionContext()) {
231 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) 225 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext())
232 ->PostTask(BLINK_FROM_HERE, WTF::Bind(&Sensor::NotifyOnActivate, 226 ->PostTask(BLINK_FROM_HERE, WTF::Bind(&Sensor::NotifyOnActivate,
233 WrapWeakPersistent(this))); 227 WrapWeakPersistent(this)));
234 } 228 }
235 } 229 }
236 230
237 void Sensor::StartListening() { 231 void Sensor::StartListening() {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 auto error = 291 auto error =
298 DOMException::Create(code, sanitized_message, unsanitized_message); 292 DOMException::Create(code, sanitized_message, unsanitized_message);
299 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) 293 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext())
300 ->PostTask(BLINK_FROM_HERE, 294 ->PostTask(BLINK_FROM_HERE,
301 WTF::Bind(&Sensor::NotifyError, WrapWeakPersistent(this), 295 WTF::Bind(&Sensor::NotifyError, WrapWeakPersistent(this),
302 WrapPersistent(error))); 296 WrapPersistent(error)));
303 } 297 }
304 } 298 }
305 299
306 void Sensor::UpdateReading() { 300 void Sensor::UpdateReading() {
307 last_update_timestamp_ = WTF::MonotonicallyIncreasingTime();
308 reading_ = sensor_proxy_->reading(); 301 reading_ = sensor_proxy_->reading();
309 pending_reading_update_ = false; 302 pending_reading_update_ = false;
310 DispatchEvent(Event::Create(EventTypeNames::change)); 303 DispatchEvent(Event::Create(EventTypeNames::change));
311 } 304 }
312 305
313 void Sensor::NotifyOnActivate() { 306 void Sensor::NotifyOnActivate() {
314 DispatchEvent(Event::Create(EventTypeNames::activate)); 307 DispatchEvent(Event::Create(EventTypeNames::activate));
315 } 308 }
316 309
317 void Sensor::NotifyError(DOMException* error) { 310 void Sensor::NotifyError(DOMException* error) {
318 DispatchEvent( 311 DispatchEvent(
319 SensorErrorEvent::Create(EventTypeNames::error, std::move(error))); 312 SensorErrorEvent::Create(EventTypeNames::error, std::move(error)));
320 } 313 }
321 314
322 bool Sensor::CanReturnReadings() const { 315 bool Sensor::CanReturnReadings() const {
323 if (!IsActivated()) 316 if (!IsActivated())
324 return false; 317 return false;
325 DCHECK(sensor_proxy_); 318 DCHECK(sensor_proxy_);
326 return sensor_proxy_->reading().timestamp != 0.0; 319 return sensor_proxy_->reading().timestamp != 0.0;
327 } 320 }
328 321
329 } // namespace blink 322 } // 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