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

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

Issue 1942663003: [sensors]: Introduce the Generic Sensor API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: re entrancy fix Created 4 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "modules/sensor/Sensor.h"
6
7 #include "core/dom/Document.h"
8 #include "core/dom/ExceptionCode.h"
9 #include "core/events/Event.h"
10
11 #include "modules/sensor/SensorReading.h"
12
13 namespace blink {
14
15 Sensor::~Sensor()
16 {
17 }
18
19 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp tions)
20 : ActiveScriptWrappable(this)
21 , ActiveDOMObject(executionContext)
22 , PlatformEventController(toDocument(executionContext)->page())
23 , m_sensorState(SensorState::Idle)
24 , m_sensorReading(nullptr)
25 , m_sensorOptions(sensorOptions)
26 {
27 }
28
29 // Getters
30 String Sensor::state() const
31 {
32 switch (m_sensorState) {
33 case SensorState::Idle:
34 return "idle";
35 case SensorState::Activating:
36 return "activating";
37 case SensorState::Active:
38 return "active";
39 case SensorState::Errored:
40 return "errored";
41 }
42 NOTREACHED();
43 return "idle";
44 }
45
46 SensorReading* Sensor::reading() const
47 {
48 return m_sensorReading.get();
49 }
50
51 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState)
52 {
53 if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Erro red) {
54 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is not idle or errored");
55
56 // Avoid re-entrancy. sensor.start() inside 'onstatechange' event handle r.
57 m_hasEventListener = false;
58 stopUpdating();
59
60 return;
61 }
62
63 updateState(SensorState::Activating);
64
65 // TODO(riju) : Add Permissions stuff later.
66
67 m_hasEventListener = true;
68 startUpdating();
69 updateState(SensorState::Active);
timvolodine 2016/05/26 15:49:01 is this correct? startUpdating is async, so it cou
riju_ 2016/05/26 17:32:21 No, removing the active statechange from here.
timvolodine 2016/05/27 18:40:57 ok, could you pls add a TODO regarding this in ord
riju_ 2016/05/30 10:15:13 Done.
70
71 }
72
73 void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState)
74 {
75 if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Erro red) {
76 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is either idle or errored");
77 return;
78 }
79
80 // Avoid re-entrancy. sensor.stop() inside 'onstatechange' event handler.
81 if (m_sensorState == SensorState::Activating) {
timvolodine 2016/05/26 15:49:01 so this means that we cannot stop the sensor while
riju_ 2016/05/26 17:32:20 Wrongly done , fixing it.
82 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState Activating");
83 return;
84 }
85
86 m_hasEventListener = false;
87 stopUpdating();
88
89 m_sensorReading.clear();
90 updateState(SensorState::Idle);
91 }
92
93 void Sensor::updateState(SensorState newState)
94 {
95 if (m_sensorState == newState)
96 return;
97
98 m_sensorState = newState;
99 dispatchEvent(Event::create(EventTypeNames::statechange));
100 }
101
102 void Sensor::suspend()
103 {
104 m_hasEventListener = false;
105 stopUpdating();
106 }
107
108 void Sensor::resume()
109 {
110 m_hasEventListener = true;
111 startUpdating();
112 }
113
114 void Sensor::stop()
115 {
116 m_hasEventListener = false;
117 stopUpdating();
118 }
119
120 bool Sensor::hasPendingActivity() const
121 {
122 // Prevent V8 from garbage collecting the wrapper object if there are
123 // event listeners attached to it.
124 return hasEventListeners();
125 }
126
127 DEFINE_TRACE(Sensor)
128 {
129 ActiveDOMObject::trace(visitor);
130 EventTargetWithInlineData::trace(visitor);
131 PlatformEventController::trace(visitor);
132 visitor->trace(m_sensorReading);
133 }
134
135 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/Sensor.h ('k') | third_party/WebKit/Source/modules/sensor/Sensor.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698