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

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

Issue 1892083002: Generic Sensor API : Ambient Light Sensor API. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: re entrancy fix 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..e7a4d098ab0becb0905e72810bb62c9736336720
--- /dev/null
+++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
@@ -0,0 +1,136 @@
+// 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 "core/dom/Document.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/events/Event.h"
+
+#include "modules/sensor/SensorReading.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_sensorReading(nullptr)
+ , 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");
+
+ // Avoid re-entrancy. sensor.start() inside 'onstatechange' event handler.
+ m_hasEventListener = false;
+ stopUpdating();
+
+ return;
+ }
+
+ updateState(SensorState::Activating);
+
+ // TODO(riju) : Add Permissions stuff later.
+
+ m_hasEventListener = true;
+ startUpdating();
+ updateState(SensorState::Active);
+
+}
+
+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");
+ return;
+ }
+
+ // Avoid re-entrancy. sensor.stop() inside 'onstatechange' event handler.
+ if (m_sensorState == SensorState::Activating) {
+ exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState Activating");
+ return;
+ }
+
+ m_hasEventListener = false;
+ stopUpdating();
+
+ m_sensorReading.clear();
+ updateState(SensorState::Idle);
+}
+
+void Sensor::updateState(SensorState newState)
+{
+ if (m_sensorState == newState)
+ return;
+
+ m_sensorState = newState;
+ dispatchEvent(Event::create(EventTypeNames::statechange));
+}
+
+void Sensor::suspend()
+{
+ m_hasEventListener = false;
+ stopUpdating();
+}
+
+void Sensor::resume()
+{
+ m_hasEventListener = true;
+ startUpdating();
+}
+
+void Sensor::stop()
+{
+ m_hasEventListener = false;
+ stopUpdating();
+}
+
+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
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/Sensor.h ('k') | third_party/WebKit/Source/modules/sensor/Sensor.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698