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

Unified Diff: third_party/WebKit/Source/modules/sensor/Sensor.cpp

Issue 2121313002: [sensors] Generic Sensors Framework blink side (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sensors_mojo_interfaces
Patch Set: Comments from Tim. Removed the default sensor configuration management (to be added with a separate… Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
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 9d716ca5e02e56ebec1fc95b3088d3d2ec82e2a2..0f456761a2f1d44a67bb5fbdd58f73803f328c01 100644
--- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp
+++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
@@ -6,130 +6,289 @@
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
-#include "core/dom/ExecutionContextTask.h"
-#include "core/events/Event.h"
-
+#include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
+#include "modules/sensor/SensorErrorEvent.h"
+#include "modules/sensor/SensorPollingStrategy.h"
+#include "modules/sensor/SensorProviderProxy.h"
#include "modules/sensor/SensorReading.h"
+#include "modules/sensor/SensorReadingEvent.h"
-namespace blink {
+using namespace device::mojom::blink;
-Sensor::~Sensor()
-{
-}
+namespace blink {
-Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOptions)
+Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOptions, SensorType type)
: ActiveScriptWrappable(this)
, ActiveDOMObject(executionContext)
- , PlatformEventController(toDocument(executionContext)->page())
- , m_sensorState(SensorState::Idle)
- , m_sensorReading(nullptr)
+ , PageVisibilityObserver(toDocument(executionContext)->page())
, m_sensorOptions(sensorOptions)
+ , m_type(type)
+ , m_state(Sensor::SensorState::IDLE)
+ , m_storedData()
{
+ DCHECK(executionContext->isDocument());
}
-// Getters
-String Sensor::state() const
+void Sensor::dispose()
+{
+ stopListening();
+}
+
+void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState)
+{
+ if (m_state != Sensor::SensorState::IDLE && m_state != Sensor::SensorState::ERRORED) {
+ exceptionState.throwDOMException(InvalidStateError, "Cannot start because SensorState is not idle or errored");
+ return;
+ }
+
+ initSensorProxyIfNeeded();
+
+ if (!m_sensorProxy) {
+ exceptionState.throwDOMException(InvalidStateError, "The Sensor is no longer associated to a frame.");
+ return;
+ }
+
+ updateState(Sensor::SensorState::ACTIVATING);
+
+ startListening();
+}
+
+void Sensor::stop(ScriptState*, ExceptionState& exceptionState)
+{
+ if (m_state == Sensor::SensorState::IDLE || m_state == Sensor::SensorState::ERRORED) {
+ exceptionState.throwDOMException(InvalidStateError, "Cannot stop because SensorState is either idle or errored");
+ return;
+ }
+
+ if (!m_sensorProxy) {
+ exceptionState.throwDOMException(InvalidStateError, "The Sensor is no longer associated to a frame.");
+ return;
+ }
+
+ stopListening();
+}
+
+static String ToString(Sensor::SensorState state)
{
- // TODO(riju): Validate the transitions.
- switch (m_sensorState) {
- case SensorState::Idle:
+ 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:
+ NOTREACHED();
}
- 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_sensorProxy);
+ visitor->trace(m_sensorReading);
+ ActiveScriptWrappable::trace(visitor);
+ ActiveDOMObject::trace(visitor);
+ PageVisibilityObserver::trace(visitor);
+ EventTargetWithInlineData::trace(visitor);
+}
+
+bool Sensor::hasPendingActivity() const
{
+ if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStopped())
haraken 2016/09/06 05:04:28 I'd change this check to if(!m_sensorReading) or s
Mikhail 2016/09/06 07:13:58 I'll change accordingly, thanks
+ return false;
+ return hasEventListeners();
+}
+
+void Sensor::initSensorProxyIfNeeded()
+{
+ if (m_sensorProxy)
+ return;
- if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Errored) {
- exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is not idle or errored");
+ Document* document = toDocument(getExecutionContext());
+ if (!document->frame())
haraken 2016/09/06 05:04:28 document may be nullptr.
Mikhail 2016/09/06 07:13:58 we've DCHECK(executionContext->isDocument()); in t
+ return;
+
+ m_sensorProxy = SensorProviderProxy::getOrCreateForFrame(document->frame())->getOrCreateSensor(m_type);
+}
+
+void Sensor::stop()
+{
+ stopListening();
+}
+
+void Sensor::onSensorInitialized()
+{
+ if (m_state != Sensor::SensorState::ACTIVATING)
+ return;
+
+ if (!m_sensorProxy) {
+ reportError();
return;
}
- updateState(SensorState::Activating);
+ // TODO(Mikhail) : Manage the default configuration properly
+ // (when provided with the actual configuration from the platform).
+ // So far assign to 5Hz which corresponds to 'SENSOR_DELAY_NORMAL'
+ // delay from Android Sensors framework.
+ SensorConfiguration defaultConfig;
+ defaultConfig.frequency = 5.0;
+
+ m_configuration = createSensorConfig(m_sensorOptions, &defaultConfig);
timvolodine 2016/09/05 23:22:40 would it make sense to drop defaultConfig here and
+ if (!m_configuration) {
+ reportError();
+ return;
+ }
- // TODO(riju) : Add Permissions stuff later.
+ auto startCallback = WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this));
+ m_sensorProxy->addConfiguration(m_configuration->Clone(), std::move(startCallback));
+}
- m_hasEventListener = true;
+void Sensor::onSensorReadingChanged()
+{
+ if (m_polling)
+ m_polling->onSensorReadingChanged();
+}
- // TODO(riju): verify the correct order of onstatechange(active) and the first onchange(event).
- startUpdating();
+void Sensor::onSensorError()
+{
+ reportError();
}
-void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState)
+void Sensor::onStartRequestCompleted(bool result)
{
- if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Errored) {
- exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is either idle or errored");
+ if (m_state != Sensor::SensorState::ACTIVATING)
+ return;
+
+ if (!result || !m_sensorProxy) {
+ reportError();
return;
}
- m_hasEventListener = false;
- stopUpdating();
+ updateState(Sensor::SensorState::ACTIVE);
- m_sensorReading.clear();
- updateState(SensorState::Idle);
+ DCHECK(m_configuration);
+ auto pollCallback = WTF::bind(&Sensor::pollForData, wrapWeakPersistent(this));
+ m_polling = SensorPollingStrategy::create(m_configuration->frequency, std::move(pollCallback), m_sensorProxy->reportingMode());
+ updatePollingStatus();
}
-void Sensor::updateState(SensorState newState)
+void Sensor::onStopRequestCompleted(bool result)
{
- DCHECK(isMainThread());
- if (m_sensorState == newState)
+ if (m_state == Sensor::SensorState::IDLE)
return;
- m_sensorState = newState;
- // Notify context that state changed.
- if (getExecutionContext())
- getExecutionContext()->postTask(BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChange, wrapPersistent(this)));
+ if (!result)
+ reportError();
+
+ if (m_sensorProxy)
+ m_sensorProxy->removeObserver(this);
}
-void Sensor::notifyStateChange()
+void Sensor::pageVisibilityChanged()
{
- dispatchEvent(Event::create(EventTypeNames::statechange));
+ updatePollingStatus();
}
-void Sensor::suspend()
+void Sensor::startListening()
{
- m_hasEventListener = false;
- stopUpdating();
+ if (!m_sensorProxy)
+ return;
+
+ if (!m_sensorReading) {
+ m_sensorReading = createSensorReading(getExecutionContext());
+ m_sensorReading->attach(m_sensorProxy);
+ }
+
+ m_sensorProxy->addObserver(this);
+ if (m_sensorProxy->isInitialized()) {
+ auto callback = WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this));
haraken 2016/09/06 05:04:28 WeakPersistent means that the callback may not run
Mikhail 2016/09/06 07:13:58 I think so, if the object is collected there is no
+ DCHECK(m_configuration);
+ m_sensorProxy->addConfiguration(m_configuration->Clone(), std::move(callback));
+ } else {
+ m_sensorProxy->initialize();
+ }
}
-void Sensor::resume()
+void Sensor::stopListening()
{
- m_hasEventListener = true;
- startUpdating();
+ if (m_sensorReading) {
+ m_sensorReading->detach();
+ m_sensorReading = nullptr;
+ }
+
+ updateState(Sensor::SensorState::IDLE);
+
+ if (!m_sensorProxy)
+ return;
+
+ if (m_sensorProxy->isInitialized()) {
+ auto callback = WTF::bind(&Sensor::onStopRequestCompleted, wrapWeakPersistent(this));
haraken 2016/09/06 05:04:28 Ditto.
+ DCHECK(m_configuration);
+ m_sensorProxy->removeConfiguration(m_configuration->Clone(), std::move(callback));
+ } else {
+ m_sensorProxy->removeObserver(this);
+ }
}
-void Sensor::stop()
+void Sensor::pollForData()
{
- m_hasEventListener = false;
- stopUpdating();
+ if (m_state != Sensor::SensorState::ACTIVE) {
+ DCHECK(m_polling);
+ m_polling->stopPolling();
+ return;
+ }
+
+ DCHECK(m_sensorProxy);
+ m_sensorProxy->updateReading();
+
+ DCHECK(m_sensorReading);
+ if (m_sensorReading->isReadingUpdated(m_storedData))
+ dispatchEvent(SensorReadingEvent::create(EventTypeNames::change, m_sensorReading));
+
+ m_storedData = m_sensorProxy->reading();
}
-bool Sensor::hasPendingActivity() const
+void Sensor::updateState(Sensor::SensorState newState)
{
- // Prevent V8 from garbage collecting the wrapper object if there are
- // event listeners attached to it.
- return hasEventListeners();
+ if (newState == m_state)
+ return;
+ m_state = newState;
+ dispatchEvent(Event::create(EventTypeNames::statechange));
+ updatePollingStatus();
}
-DEFINE_TRACE(Sensor)
+void Sensor::reportError()
{
- ActiveDOMObject::trace(visitor);
- EventTargetWithInlineData::trace(visitor);
- PlatformEventController::trace(visitor);
- visitor->trace(m_sensorReading);
+ updateState(Sensor::SensorState::ERRORED);
+ // TODO(Mikhail) : Dispatch Sensor Error event.
+}
+
+void Sensor::updatePollingStatus()
+{
+ if (!m_polling)
+ return;
+
+ if (m_state != Sensor::SensorState::ACTIVE
+ || page()->visibilityState() != PageVisibilityStateVisible) {
+ m_polling->stopPolling();
+ } else {
+ m_polling->startPolling();
+ }
}
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698