Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
| 10 #include "core/events/Event.h" | 10 #include "modules/sensor/SensorErrorEvent.h" |
| 11 | 11 #include "modules/sensor/SensorPollingStrategy.h" |
| 12 #include "modules/sensor/SensorProviderProxy.h" | |
| 12 #include "modules/sensor/SensorReading.h" | 13 #include "modules/sensor/SensorReading.h" |
| 14 #include "modules/sensor/SensorReadingEvent.h" | |
| 15 | |
| 16 using namespace device::mojom::blink; | |
| 13 | 17 |
| 14 namespace blink { | 18 namespace blink { |
| 15 | 19 |
| 16 Sensor::~Sensor() | 20 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp tions, device::mojom::blink::SensorType type) |
|
riju_
2016/08/31 10:35:49
since you are using "using namespace device::mojom
Mikhail
2016/08/31 12:01:07
Right, that can be shortened. Thanks for noticing
| |
| 17 { | |
| 18 } | |
| 19 | |
| 20 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp tions) | |
| 21 : ActiveScriptWrappable(this) | 21 : ActiveScriptWrappable(this) |
| 22 , ActiveDOMObject(executionContext) | 22 , ActiveDOMObject(executionContext) |
| 23 , PlatformEventController(toDocument(executionContext)->page()) | 23 , PageVisibilityObserver(toDocument(executionContext)->page()) |
| 24 , m_sensorState(SensorState::Idle) | |
| 25 , m_sensorReading(nullptr) | |
| 26 , m_sensorOptions(sensorOptions) | 24 , m_sensorOptions(sensorOptions) |
| 27 { | 25 , m_type(type) |
| 26 , m_state(Sensor::SensorState::IDLE) | |
| 27 , m_storedData() | |
| 28 { | |
| 29 DCHECK(executionContext->isDocument()); | |
| 30 } | |
| 31 | |
| 32 void Sensor::dispose() | |
| 33 { | |
| 34 stopListening(); | |
| 35 } | |
| 36 | |
| 37 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) | |
| 38 { | |
| 39 if (m_state != Sensor::SensorState::IDLE && m_state != Sensor::SensorState:: ERRORED) { | |
| 40 exceptionState.throwDOMException(InvalidStateError, "Cannot start becaus e SensorState is not idle or errored"); | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 initSensorProxyIfNeeded(); | |
| 45 | |
| 46 if (!m_sensorProxy) { | |
| 47 exceptionState.throwDOMException(InvalidStateError, "The Sensor is no lo nger associated to a frame."); | |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 updateState(Sensor::SensorState::ACTIVATING); | |
| 52 | |
| 53 startListening(); | |
| 54 } | |
| 55 | |
| 56 void Sensor::stop(ScriptState*, ExceptionState& exceptionState) | |
| 57 { | |
| 58 if (m_state == Sensor::SensorState::IDLE || m_state == Sensor::SensorState:: ERRORED) { | |
| 59 exceptionState.throwDOMException(InvalidStateError, "Cannot stop because SensorState is either idle or errored"); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 if (!m_sensorProxy) { | |
| 64 exceptionState.throwDOMException(InvalidStateError, "The Sensor is no lo nger associated to a frame."); | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 stopListening(); | |
| 69 } | |
| 70 | |
| 71 static String ToString(Sensor::SensorState state) | |
| 72 { | |
| 73 switch (state) { | |
| 74 case Sensor::SensorState::IDLE: | |
| 75 return "idle"; | |
| 76 case Sensor::SensorState::ACTIVATING: | |
| 77 return "activating"; | |
| 78 case Sensor::SensorState::ACTIVE: | |
| 79 return "active"; | |
| 80 case Sensor::SensorState::ERRORED: | |
| 81 return "errored"; | |
| 82 default: | |
| 83 NOTREACHED(); | |
| 84 } | |
| 85 return "idle"; | |
| 28 } | 86 } |
| 29 | 87 |
| 30 // Getters | 88 // Getters |
| 31 String Sensor::state() const | 89 String Sensor::state() const |
| 32 { | 90 { |
| 33 // TODO(riju): Validate the transitions. | 91 return ToString(m_state); |
| 34 switch (m_sensorState) { | |
| 35 case SensorState::Idle: | |
| 36 return "idle"; | |
| 37 case SensorState::Activating: | |
| 38 return "activating"; | |
| 39 case SensorState::Active: | |
| 40 return "active"; | |
| 41 case SensorState::Errored: | |
| 42 return "errored"; | |
| 43 } | |
| 44 NOTREACHED(); | |
| 45 return "idle"; | |
| 46 } | 92 } |
| 47 | 93 |
| 48 SensorReading* Sensor::reading() const | 94 SensorReading* Sensor::reading() const |
| 49 { | 95 { |
| 50 return m_sensorReading.get(); | 96 return m_sensorReading.get(); |
| 51 } | 97 } |
| 52 | 98 |
| 53 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) | 99 DEFINE_TRACE(Sensor) |
| 54 { | 100 { |
| 55 | 101 visitor->trace(m_polling); |
| 56 if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Erro red) { | 102 visitor->trace(m_sensorProxy); |
| 57 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is not idle or errored"); | 103 visitor->trace(m_sensorReading); |
| 58 return; | 104 ActiveScriptWrappable::trace(visitor); |
| 59 } | 105 ActiveDOMObject::trace(visitor); |
| 60 | 106 PageVisibilityObserver::trace(visitor); |
| 61 updateState(SensorState::Activating); | 107 EventTargetWithInlineData::trace(visitor); |
| 62 | 108 } |
| 63 // TODO(riju) : Add Permissions stuff later. | 109 |
| 64 | 110 bool Sensor::hasPendingActivity() const |
| 65 m_hasEventListener = true; | 111 { |
| 66 | 112 if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop ped()) |
| 67 // TODO(riju): verify the correct order of onstatechange(active) and the fir st onchange(event). | 113 return false; |
| 68 startUpdating(); | 114 return hasEventListeners(); |
| 69 } | 115 } |
| 70 | 116 |
| 71 void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState) | 117 void Sensor::initSensorProxyIfNeeded() |
| 72 { | 118 { |
| 73 if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Erro red) { | 119 if (m_sensorProxy) |
| 74 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is either idle or errored"); | 120 return; |
| 75 return; | 121 |
| 76 } | 122 Document* document = toDocument(getExecutionContext()); |
| 77 | 123 if (!document->frame()) |
| 78 m_hasEventListener = false; | 124 return; |
| 79 stopUpdating(); | 125 |
| 80 | 126 m_sensorProxy = SensorProviderProxy::getOrCreateForFrame(document->frame())- >getOrCreateSensor(m_type); |
| 81 m_sensorReading.clear(); | 127 } |
| 82 updateState(SensorState::Idle); | 128 |
| 83 } | 129 void Sensor::stop() |
| 84 | 130 { |
| 85 void Sensor::updateState(SensorState newState) | 131 stopListening(); |
| 86 { | 132 } |
| 87 DCHECK(isMainThread()); | 133 |
| 88 if (m_sensorState == newState) | 134 void Sensor::onSensorInitialized() |
| 89 return; | 135 { |
| 90 | 136 if (m_state != Sensor::SensorState::ACTIVATING) |
| 91 m_sensorState = newState; | 137 return; |
| 92 // Notify context that state changed. | 138 |
| 93 if (getExecutionContext()) | 139 if (!m_sensorProxy) { |
| 94 getExecutionContext()->postTask(BLINK_FROM_HERE, createSameThreadTask(&S ensor::notifyStateChange, wrapPersistent(this))); | 140 reportError(); |
| 95 } | 141 return; |
| 96 | 142 } |
| 97 void Sensor::notifyStateChange() | 143 |
| 98 { | 144 m_configuration = createSensorConfig(m_sensorOptions, m_sensorProxy->default Configuration()); |
| 145 if (!m_configuration) { | |
| 146 reportError(); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 auto startCallback = WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPer sistent(this)); | |
| 151 m_sensorProxy->addConfiguration(m_configuration->Clone(), std::move(startCal lback)); | |
| 152 } | |
| 153 | |
| 154 void Sensor::onSensorReadingChanged() | |
| 155 { | |
| 156 if (m_polling) | |
| 157 m_polling->onSensorReadingChanged(); | |
| 158 } | |
| 159 | |
| 160 void Sensor::onSensorError() | |
| 161 { | |
| 162 reportError(); | |
| 163 } | |
| 164 | |
| 165 void Sensor::onStartRequestCompleted(bool result) | |
| 166 { | |
| 167 if (m_state != Sensor::SensorState::ACTIVATING) | |
| 168 return; | |
| 169 | |
| 170 if (!result || !m_sensorProxy) { | |
| 171 reportError(); | |
| 172 return; | |
| 173 } | |
| 174 | |
| 175 updateState(Sensor::SensorState::ACTIVE); | |
| 176 | |
| 177 DCHECK(m_configuration); | |
| 178 auto pollCallback = WTF::bind(&Sensor::pollForData, wrapWeakPersistent(this) ); | |
| 179 m_polling = SensorPollingStrategy::create(m_configuration->frequency, std::m ove(pollCallback), m_sensorProxy->reportingMode()); | |
| 180 updatePollingStatus(); | |
| 181 } | |
| 182 | |
| 183 void Sensor::onStopRequestCompleted(bool result) | |
| 184 { | |
| 185 if (m_state == Sensor::SensorState::IDLE) | |
| 186 return; | |
| 187 | |
| 188 if (!result) | |
| 189 reportError(); | |
| 190 | |
| 191 if (m_sensorProxy) | |
| 192 m_sensorProxy->removeObserver(this); | |
| 193 } | |
| 194 | |
| 195 void Sensor::pageVisibilityChanged() | |
| 196 { | |
| 197 updatePollingStatus(); | |
| 198 } | |
| 199 | |
| 200 void Sensor::startListening() | |
| 201 { | |
| 202 if (!m_sensorProxy) | |
| 203 return; | |
| 204 | |
| 205 if (!m_sensorReading) { | |
| 206 m_sensorReading = createSensorReading(getExecutionContext()); | |
| 207 m_sensorReading->attach(m_sensorProxy); | |
| 208 } | |
| 209 | |
| 210 m_sensorProxy->addObserver(this); | |
| 211 if (m_sensorProxy->isInitialized()) { | |
| 212 auto callback = WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPers istent(this)); | |
| 213 DCHECK(m_configuration); | |
| 214 m_sensorProxy->addConfiguration(m_configuration->Clone(), std::move(call back)); | |
| 215 } else { | |
| 216 m_sensorProxy->initialize(); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 void Sensor::stopListening() | |
| 221 { | |
| 222 if (m_sensorReading) { | |
| 223 m_sensorReading->detach(); | |
| 224 m_sensorReading = nullptr; | |
| 225 } | |
| 226 | |
| 227 updateState(Sensor::SensorState::IDLE); | |
| 228 | |
| 229 if (!m_sensorProxy) | |
| 230 return; | |
| 231 | |
| 232 if (m_sensorProxy->isInitialized()) { | |
| 233 auto callback = WTF::bind(&Sensor::onStopRequestCompleted, wrapWeakPersi stent(this)); | |
| 234 DCHECK(m_configuration); | |
| 235 m_sensorProxy->removeConfiguration(m_configuration->Clone(), std::move(c allback)); | |
| 236 } else { | |
| 237 m_sensorProxy->removeObserver(this); | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 void Sensor::pollForData() | |
| 242 { | |
| 243 if (m_state != Sensor::SensorState::ACTIVE) { | |
| 244 DCHECK(m_polling); | |
| 245 m_polling->stopPolling(); | |
| 246 return; | |
| 247 } | |
| 248 | |
| 249 DCHECK(m_sensorProxy); | |
| 250 m_sensorProxy->updateReading(); | |
| 251 | |
| 252 DCHECK(m_sensorReading); | |
| 253 if (m_sensorReading->isReadingUpdated(m_storedData)) | |
| 254 dispatchEvent(SensorReadingEvent::create(EventTypeNames::change, m_senso rReading)); | |
| 255 | |
| 256 m_storedData = m_sensorProxy->reading(); | |
| 257 } | |
| 258 | |
| 259 void Sensor::updateState(Sensor::SensorState newState) | |
| 260 { | |
| 261 if (newState == m_state) | |
| 262 return; | |
| 263 m_state = newState; | |
| 99 dispatchEvent(Event::create(EventTypeNames::statechange)); | 264 dispatchEvent(Event::create(EventTypeNames::statechange)); |
| 100 } | 265 updatePollingStatus(); |
| 101 | 266 } |
| 102 void Sensor::suspend() | 267 |
| 103 { | 268 void Sensor::reportError() |
| 104 m_hasEventListener = false; | 269 { |
| 105 stopUpdating(); | 270 updateState(Sensor::SensorState::ERRORED); |
| 106 } | 271 // TODO(Mikhail) : Dispatch Sensor Error event. |
| 107 | 272 } |
| 108 void Sensor::resume() | 273 |
| 109 { | 274 void Sensor::updatePollingStatus() |
| 110 m_hasEventListener = true; | 275 { |
| 111 startUpdating(); | 276 if (!m_polling) |
| 112 } | 277 return; |
| 113 | 278 |
| 114 void Sensor::stop() | 279 if (m_state != Sensor::SensorState::ACTIVE |
| 115 { | 280 || page()->visibilityState() != PageVisibilityStateVisible) { |
| 116 m_hasEventListener = false; | 281 m_polling->stopPolling(); |
| 117 stopUpdating(); | 282 } else { |
| 118 } | 283 m_polling->startPolling(); |
| 119 | 284 } |
| 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 } | 285 } |
| 134 | 286 |
| 135 } // namespace blink | 287 } // namespace blink |
| OLD | NEW |