Index: third_party/WebKit/Source/modules/sensor/Sensor.cpp |
diff --git a/third_party/WebKit/Source/modules/sensor/Sensor.cpp b/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
index 250615652f5b3a0dfd550a89ed5892bb23d1d23f..9af16311161ab769e0af3c07e8117f665fa0fb55 100644 |
--- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
+++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
@@ -4,132 +4,251 @@ |
#include "modules/sensor/Sensor.h" |
+#include "bindings/core/v8/ScriptPromise.h" |
+#include "bindings/core/v8/ScriptPromiseResolver.h" |
#include "core/dom/Document.h" |
#include "core/dom/ExceptionCode.h" |
-#include "core/dom/ExecutionContextTask.h" |
-#include "core/events/Event.h" |
- |
+#include "modules/sensor/SensorErrorEvent.h" |
+#include "modules/sensor/SensorPollingStrategy.h" |
#include "modules/sensor/SensorReading.h" |
+#include "modules/sensor/SensorReadingEvent.h" |
namespace blink { |
-Sensor::~Sensor() |
-{ |
-} |
- |
-Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOptions) |
+Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOptions, device::sensors::blink::SensorType type) |
: ActiveScriptWrappable(this) |
, ActiveDOMObject(executionContext) |
- , PlatformEventController(toDocument(executionContext)->page()) |
- , m_sensorState(SensorState::Idle) |
- , m_sensorReading(nullptr) |
, m_sensorOptions(sensorOptions) |
+ , m_type(type) |
+ , m_state(Sensor::SensorState::IDLE) |
{ |
} |
-// Getters |
-String Sensor::state() const |
+void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) |
+{ |
+ // 1. Check if sensor_instance’s state is neither "idle" nor "errored" |
+ if (m_state != Sensor::SensorState::IDLE && m_state != Sensor::SensorState::ERRORED) { |
+ exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is not idle or errored"); |
+ return; |
+ } |
+ |
+ InitControllerIfNeeded(); |
+ |
+ if (!m_controller) { |
+ exceptionState.throwDOMException(InvalidStateError, "The Sensor is no longer associated to a frame."); |
+ return; |
+ } |
+ |
+ // 2. Invoke the update state algorithm passing sensor_instance and "activating" as the arguments. |
+ updateState(Sensor::SensorState::ACTIVATING); |
+ |
+ m_sensorReading = createSensorReading(); |
+ |
+ m_controller->addObserver(this); |
+ if (m_controller->isInitialized()) { |
+ auto callback = bind<bool>(&Sensor::onStartRequestCompleted, WeakPersistentThisPointer<Sensor>(this)); |
+ m_controller->startConfiguration(createSensorOptions(), std::move(callback)); |
+ } else { |
+ m_controller->initialize(); |
+ } |
+} |
+ |
+void Sensor::stop(ScriptState*, ExceptionState& exceptionState) |
{ |
- // TODO(riju): Validate the transitions. |
- switch (m_sensorState) { |
- case SensorState::Idle: |
+ if (m_state == Sensor::SensorState::IDLE || m_state == Sensor::SensorState::ERRORED) { |
+ exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is either idle or errored"); |
+ return; |
+ } |
+ |
+ if (!m_controller) { |
+ exceptionState.throwDOMException(InvalidStateError, "The Sensor is no longer associated to a frame."); |
+ return; |
+ } |
+ m_sensorReading = nullptr; |
+ |
+ updateState(Sensor::SensorState::IDLE); |
+ |
+ if (m_controller->isInitialized()) { |
+ auto callback = bind<bool>(&Sensor::onStopRequestCompleted, WeakPersistentThisPointer<Sensor>(this)); |
+ m_controller->stopConfiguration(createSensorOptions(), std::move(callback)); |
+ } else { |
+ m_controller->removeObserver(this); |
+ } |
+} |
+ |
+static String ToString(Sensor::SensorState state) { |
+ switch (state) { |
+ case Sensor::SensorState::IDLE: |
return "idle"; |
- case SensorState::Activating: |
+ case Sensor::SensorState::ACTIVATING: |
return "activating"; |
- case SensorState::Active: |
+ case Sensor::SensorState::ACTIVE: |
return "active"; |
- case SensorState::Errored: |
+ case Sensor::SensorState::ERRORED: |
return "errored"; |
+ default: |
+ ASSERT_NOT_REACHED(); |
} |
- NOTREACHED(); |
return "idle"; |
} |
+// Getters |
+String Sensor::state() const |
+{ |
+ return ToString(m_state); |
+} |
+ |
SensorReading* Sensor::reading() const |
{ |
return m_sensorReading.get(); |
} |
-void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) |
+DEFINE_TRACE(Sensor) |
{ |
+ visitor->trace(m_polling); |
+ visitor->trace(m_controller); |
+ visitor->trace(m_sensorReading); |
+ ActiveDOMObject::trace(visitor); |
+ EventTargetWithInlineData::trace(visitor); |
+} |
- if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Errored) { |
- exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is not idle or errored"); |
+void Sensor::InitControllerIfNeeded() |
+{ |
+ if (m_controller) |
return; |
- } |
- updateState(SensorState::Activating); |
+ ExecutionContext* executionContext = getExecutionContext(); |
+ ASSERT(executionContext && executionContext->isDocument()); |
- // TODO(riju) : Add Permissions stuff later. |
+ Document* document = toDocument(executionContext); |
+ if (!document->frame()) |
+ return; |
- m_hasEventListener = true; |
+ m_controller = SensorProvider::getOrCreateForFrame(*document->frame())->getOrCreateSensor(m_type); |
+} |
- // TODO(riju): verify the correct order of onstatechange(active) and the first onchange(event). |
- startUpdating(); |
+void Sensor::addedEventListener(const AtomicString& eventType, RegisteredEventListener&) |
+{ |
+ if (EventTypeNames::change == eventType) |
+ updatePollingStatus(); |
} |
-void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState) |
+void Sensor::removedEventListener(const AtomicString& eventType, const RegisteredEventListener&) |
{ |
- if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Errored) { |
- exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is either idle or errored"); |
+ if (EventTypeNames::change == eventType) |
+ updatePollingStatus(); |
+} |
+ |
+void Sensor::onSensorInitialized() |
+{ |
+ if (m_state != Sensor::SensorState::ACTIVATING) |
+ return; |
+ |
+ if (!m_controller) |
+ { |
+ reportError(); |
return; |
} |
- m_hasEventListener = false; |
- stopUpdating(); |
+ auto startCallback = bind<bool>(&Sensor::onStartRequestCompleted, WeakPersistentThisPointer<Sensor>(this)); |
+ m_controller->startConfiguration(createSensorOptions(), std::move(startCallback)); |
+} |
- m_sensorReading.clear(); |
- updateState(SensorState::Idle); |
+void Sensor::onSensorReadingChanged() |
+{ |
+ if (m_polling) |
+ m_polling->onSensorReadingChanged(); |
} |
-void Sensor::updateState(SensorState newState) |
+void Sensor::onSensorError() |
{ |
- DCHECK(isMainThread()); |
- if (m_sensorState == newState) |
- return; |
+ reportError(); |
+} |
- m_sensorState = newState; |
- // Notify context that state changed. |
- if (getExecutionContext()) |
- getExecutionContext()->postTask(BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChange, this)); |
+bool Sensor::hasPendingActivity() const { |
+ if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStopped()) |
+ return false; |
+ return hasEventListeners(); |
} |
-void Sensor::notifyStateChange() |
+void Sensor::onStartRequestCompleted(bool success) |
{ |
- dispatchEvent(Event::create(EventTypeNames::statechange)); |
+ if (m_state != Sensor::SensorState::ACTIVATING) |
+ return; |
+ |
+ if (!success || !m_controller) { |
+ reportError(); |
+ return; |
+ } |
+ |
+ updateState(Sensor::SensorState::ACTIVE); |
+ |
+ double frequency = options().hasFrequency() ? options().frequency() : 1; |
+ auto pollCallback = bind(&Sensor::pollForData, WeakPersistentThisPointer<Sensor>(this)); |
+ m_polling = SensorPollingStrategy::create(frequency, std::move(pollCallback), m_controller->reportingMode()); |
+ updatePollingStatus(); |
} |
-void Sensor::suspend() |
+void Sensor::onStopRequestCompleted(bool success) |
{ |
- m_hasEventListener = false; |
- stopUpdating(); |
+ if (m_state == Sensor::SensorState::IDLE) |
+ return; |
+ |
+ if (!success) |
+ reportError(); |
+ |
+ if (m_controller) |
+ m_controller->removeObserver(this); |
} |
-void Sensor::resume() |
+void Sensor::pollForData() |
{ |
- m_hasEventListener = true; |
- startUpdating(); |
+ if (m_state != Sensor::SensorState::ACTIVE) |
+ { |
+ ASSERT(m_polling); |
+ m_polling->stopPolling(); |
+ return; |
+ } |
+ |
+ switch (m_sensorReading->updateInternalData()) |
+ { |
+ case SensorReading::Updated: |
+ dispatchEvent(SensorReadingEvent::create(EventTypeNames::change, *m_sensorReading)); |
+ break; |
+ case SensorReading::Error: |
+ reportError(); |
+ break; |
+ case SensorReading::Same: |
+ break; |
+ default: |
+ ASSERT_NOT_REACHED(); |
+ } |
} |
-void Sensor::stop() |
-{ |
- m_hasEventListener = false; |
- stopUpdating(); |
+void Sensor::updateState(Sensor::SensorState newState) { |
+ if (newState == m_state) |
+ return; |
+ m_state = newState; |
+ dispatchEvent(Event::create(EventTypeNames::statechange)); |
} |
-bool Sensor::hasPendingActivity() const |
+void Sensor::reportError() |
{ |
- // Prevent V8 from garbage collecting the wrapper object if there are |
- // event listeners attached to it. |
- return hasEventListeners(); |
+ updateState(Sensor::SensorState::ERRORED); |
+ dispatchEvent(SensorErrorEvent::create(EventTypeNames::error)); |
} |
-DEFINE_TRACE(Sensor) |
+void Sensor::updatePollingStatus() |
{ |
- ActiveDOMObject::trace(visitor); |
- EventTargetWithInlineData::trace(visitor); |
- PlatformEventController::trace(visitor); |
- visitor->trace(m_sensorReading); |
+ if (!m_polling) |
+ return; |
+ |
+ if (m_state != Sensor::SensorState::ACTIVE || !hasEventListeners(EventTypeNames::change)) { |
+ // Do not poll if noone is listening 'onchange' event. |
+ m_polling->stopPolling(); |
+ } else { |
+ m_polling->startPolling(); |
+ } |
} |
} // namespace blink |