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

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

Issue 2381913002: [sensors] Dispatch sensor events using postTask (Closed)
Patch Set: Created 4 years, 2 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
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/Sensor.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/inspector/ConsoleMessage.h" 10 #include "core/inspector/ConsoleMessage.h"
10 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
11 #include "modules/sensor/SensorErrorEvent.h" 12 #include "modules/sensor/SensorErrorEvent.h"
12 #include "modules/sensor/SensorPollingStrategy.h" 13 #include "modules/sensor/SensorPollingStrategy.h"
13 #include "modules/sensor/SensorProviderProxy.h" 14 #include "modules/sensor/SensorProviderProxy.h"
14 #include "modules/sensor/SensorReading.h" 15 #include "modules/sensor/SensorReading.h"
15 #include "modules/sensor/SensorReadingEvent.h" 16 #include "modules/sensor/SensorReadingEvent.h"
16 17
17 using namespace device::mojom::blink; 18 using namespace device::mojom::blink;
18 19
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 DCHECK(m_polling); 257 DCHECK(m_polling);
257 m_polling->stopPolling(); 258 m_polling->stopPolling();
258 return; 259 return;
259 } 260 }
260 261
261 DCHECK(m_sensorProxy); 262 DCHECK(m_sensorProxy);
262 DCHECK(m_sensorProxy->isInitialized()); 263 DCHECK(m_sensorProxy->isInitialized());
263 m_sensorProxy->updateInternalReading(); 264 m_sensorProxy->updateInternalReading();
264 265
265 DCHECK(m_sensorReading); 266 DCHECK(m_sensorReading);
266 if (m_sensorReading->isReadingUpdated(m_storedData)) 267 if (getExecutionContext() && m_sensorReading->isReadingUpdated(m_storedData) ) {
267 dispatchEvent(SensorReadingEvent::create(EventTypeNames::change, m_senso rReading)); 268 getExecutionContext()->postTask(BLINK_FROM_HERE,
269 createSameThreadTask(&Sensor::notifySensorReadingChanged, wrapWeakPe rsistent(this)));
haraken 2016/09/30 13:26:45 wrapWeakPersistent means that the event may not be
shalamov 2016/09/30 13:51:44 Yes, that was intentional. If sensor goes out of s
270 }
268 271
269 m_storedData = m_sensorProxy->reading(); 272 m_storedData = m_sensorProxy->reading();
270 } 273 }
271 274
272 void Sensor::updateState(Sensor::SensorState newState) 275 void Sensor::updateState(Sensor::SensorState newState)
273 { 276 {
274 if (newState == m_state) 277 if (newState == m_state)
275 return; 278 return;
276 m_state = newState; 279 m_state = newState;
277 dispatchEvent(Event::create(EventTypeNames::statechange)); 280 if (getExecutionContext()) {
281 getExecutionContext()->postTask(BLINK_FROM_HERE,
282 createSameThreadTask(&Sensor::notifyStateChanged, wrapWeakPersistent (this)));
283 }
284
278 updatePollingStatus(); 285 updatePollingStatus();
279 } 286 }
280 287
281 void Sensor::reportError() 288 void Sensor::reportError()
282 { 289 {
283 updateState(Sensor::SensorState::ERRORED); 290 updateState(Sensor::SensorState::ERRORED);
284 // TODO(Mikhail) : Dispatch Sensor Error event. 291 // TODO(Mikhail) : Dispatch Sensor Error event.
285 } 292 }
286 293
287 void Sensor::updatePollingStatus() 294 void Sensor::updatePollingStatus()
288 { 295 {
289 if (!m_polling) 296 if (!m_polling)
290 return; 297 return;
291 298
292 if (m_state != Sensor::SensorState::ACTIVE 299 if (m_state != Sensor::SensorState::ACTIVE
293 || page()->visibilityState() != PageVisibilityStateVisible) { 300 || page()->visibilityState() != PageVisibilityStateVisible) {
294 m_polling->stopPolling(); 301 m_polling->stopPolling();
295 } else { 302 } else {
296 m_polling->startPolling(); 303 m_polling->startPolling();
297 } 304 }
298 } 305 }
299 306
307 void Sensor::notifySensorReadingChanged()
308 {
309 dispatchEvent(SensorReadingEvent::create(EventTypeNames::change, m_sensorRea ding));
310 }
311
312 void Sensor::notifyStateChanged()
313 {
314 dispatchEvent(Event::create(EventTypeNames::statechange));
315 }
316
300 } // namespace blink 317 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/Sensor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698