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

Unified 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: Rebase + add Tim as owner Created 4 years, 7 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
new file mode 100644
index 0000000000000000000000000000000000000000..3630a26b1a36e9be95b46eb0185c6a7f12195119
--- /dev/null
+++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
@@ -0,0 +1,114 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#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 "modules/sensor/SensorReading.h"
+#include "modules/sensor/sensor_state_type.h"
+
+namespace blink {
+
+Sensor::~Sensor()
+{
+}
+
+Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOptions)
+ : ActiveScriptWrappable(this)
+ , ActiveDOMObject(executionContext)
+ , PlatformEventController(toDocument(executionContext)->page())
+ , m_sensorState(SensorState::Idle)
+ , m_sensorOptions(sensorOptions)
+{
+}
+
+// Getters
+String Sensor::state() const
+{
+ switch (m_sensorState) {
+ case SensorState::Idle:
+ return "idle";
+ case SensorState::Activating:
+ return "activating";
+ case SensorState::Active:
+ return "active";
+ case SensorState::Errored:
+ return "errored";
+ }
+ NOTREACHED();
+ return "idle";
+}
+
+SensorReading* Sensor::reading() const
+{
+ return m_sensorReading.get();
+}
+
+void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState)
+{
+ if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Errored)
+ exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is not idle or errored");
+
+ updateState(SensorState::Activating);
+
+ // TODO(riju) : Add Permissions stuff later.
+
+ m_hasEventListener = true;
+ startUpdating();
+}
+
+void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState)
+{
+ if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Errored)
+ exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is either idle or errored");
+
+ m_hasEventListener = false;
+ stopUpdating();
+ updateState(SensorState::Idle);
+}
+
+void Sensor::updateState(SensorState newState)
+{
+ m_sensorState = newState;
+ dispatchEvent(Event::create(EventTypeNames::statechange));
timvolodine 2016/05/24 15:30:35 this may need a bit of thinking regarding re-entra
riju_ 2016/05/25 15:07:17 Adding, if (m_sensorState == newState) return;
timvolodine 2016/05/25 15:40:22 don't think this is sufficient. if I am not overlo
riju_ 2016/05/26 13:21:01 Yes, you are right. Now I have checked with adding
+}
+
+void Sensor::suspend()
+{
+ m_hasEventListener = false;
+ stopUpdating();
+}
+
+void Sensor::resume()
+{
+ m_hasEventListener = true;
+ startUpdating();
+}
+
+void Sensor::stop()
+{
+ m_hasEventListener = false;
+ stopUpdating();
timvolodine 2016/05/24 15:30:35 clear m_sensorReading?
riju_ 2016/05/25 15:07:17 for some sensors say geolocation, it is useful to
timvolodine 2016/05/25 15:40:22 caching is nice, but I was thinking in terms of us
riju_ 2016/05/26 13:21:01 Ok, I am clearing the reading in sensor.stop() now
+}
+
+bool Sensor::hasPendingActivity() const
+{
+ // Prevent V8 from garbage collecting the wrapper object if there are
+ // event listeners attached to it.
+ return hasEventListeners();
+}
+
+DEFINE_TRACE(Sensor)
+{
+ ActiveDOMObject::trace(visitor);
+ EventTargetWithInlineData::trace(visitor);
+ PlatformEventController::trace(visitor);
+ visitor->trace(m_sensorReading);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698