OLD | NEW |
---|---|
(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/dom/ExecutionContextTask.h" | |
10 #include "core/events/Event.h" | |
11 | |
12 #include "modules/sensor/SensorReading.h" | |
13 | |
14 namespace blink { | |
15 | |
16 Sensor::~Sensor() | |
17 { | |
18 } | |
19 | |
20 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp tions) | |
21 : ActiveScriptWrappable(this) | |
22 , ActiveDOMObject(executionContext) | |
23 , PlatformEventController(toDocument(executionContext)->page()) | |
24 , m_sensorState(SensorState::Idle) | |
25 , m_sensorReading(nullptr) | |
26 , m_sensorOptions(sensorOptions) | |
27 { | |
28 } | |
29 | |
30 // Getters | |
31 String Sensor::state() const | |
32 { | |
33 switch (m_sensorState) { | |
34 case SensorState::Idle: | |
35 return "idle"; | |
36 case SensorState::Activating: | |
37 return "activating"; | |
38 case SensorState::Active: | |
39 return "active"; | |
40 case SensorState::Errored: | |
41 return "errored"; | |
42 } | |
43 NOTREACHED(); | |
44 return "idle"; | |
45 } | |
46 | |
47 SensorReading* Sensor::reading() const | |
48 { | |
49 return m_sensorReading.get(); | |
50 } | |
51 | |
52 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) | |
53 { | |
54 | |
55 if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Erro red) { | |
56 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is not idle or errored"); | |
57 | |
58 m_hasEventListener = false; | |
timvolodine
2016/05/27 18:40:57
Don't think this is needed? if sensor state is idl
riju_
2016/05/30 10:15:13
Done.
| |
59 stopUpdating(); | |
60 return; | |
61 } | |
62 | |
63 updateState(SensorState::Activating); | |
64 | |
65 // TODO(riju) : Add Permissions stuff later. | |
66 | |
67 m_hasEventListener = true; | |
68 startUpdating(); | |
69 } | |
70 | |
71 void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState) | |
72 { | |
73 if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Erro red) { | |
74 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is either idle or errored"); | |
75 return; | |
76 } | |
77 | |
78 m_hasEventListener = false; | |
79 stopUpdating(); | |
80 | |
81 m_sensorReading.clear(); | |
82 updateState(SensorState::Idle); | |
83 } | |
84 | |
85 void Sensor::updateState(SensorState newState) | |
86 { | |
87 DCHECK(isMainThread()); | |
88 if (m_sensorState == newState) | |
89 return; | |
90 | |
91 m_sensorState = newState; | |
timvolodine
2016/05/27 18:40:57
Would be nice to have a state transitions check an
riju_
2016/05/30 10:15:13
Done.
| |
92 // Notify context that state changed | |
93 if (getExecutionContext()) | |
94 getExecutionContext()->postTask(BLINK_FROM_HERE, createSameThreadTask(&S ensor::notifyStateChange, this)); | |
95 } | |
96 | |
97 void Sensor::notifyStateChange() | |
98 { | |
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 | |
OLD | NEW |