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

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

Issue 1892083002: Generic Sensor API : Ambient Light Sensor API. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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/AmbientLightSensor.cpp
diff --git a/third_party/WebKit/Source/modules/sensor/AmbientLightSensor.cpp b/third_party/WebKit/Source/modules/sensor/AmbientLightSensor.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..f08493c053870406dbfb49d012b7caf029e3405f
--- /dev/null
+++ b/third_party/WebKit/Source/modules/sensor/AmbientLightSensor.cpp
@@ -0,0 +1,150 @@
+// 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/AmbientLightSensor.h"
+
+#include "bindings/core/v8/ScriptPromise.h"
+#include "bindings/core/v8/ScriptPromiseResolver.h"
+#include "core/dom/DOMException.h"
+#include "core/dom/Document.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/events/Event.h"
+
+#include "modules/sensor/AmbientLightSensorDispatcher.h"
+#include "modules/sensor/AmbientLightSensorReading.h"
+#include "modules/sensor/SensorReadingEvent.h"
+#include "modules/sensor/sensor_state_type.h"
+
+namespace blink {
+
+// static
+AmbientLightSensor* AmbientLightSensor::create(ExecutionContext* context, SensorOptions sensorOptions, ExceptionState& exceptionState)
+{
+ AmbientLightSensor* sensor = new AmbientLightSensor(context, sensorOptions);
+ sensor->suspendIfNeeded();
+ return sensor;
+}
+
+// static
+AmbientLightSensor* AmbientLightSensor::create(ExecutionContext* context, ExceptionState& exceptionState)
+{
+ return create(context, SensorOptions(), exceptionState);
+}
+
+AmbientLightSensor::~AmbientLightSensor()
+{
+ stopUpdating();
+}
+
+AmbientLightSensor::AmbientLightSensor(ExecutionContext* executionContext, SensorOptions sensorOptions)
+ : Sensor(executionContext, sensorOptions)
+ , PlatformEventController(toDocument(executionContext)->page())
+{
+}
+
+void AmbientLightSensor::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 AmbientLightSensor::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 AmbientLightSensor::updateState(SensorState newState)
+{
+ m_sensorState = newState;
+ dispatchEvent(Event::create(EventTypeNames::statechange));
+}
+
+AmbientLightSensorReading& AmbientLightSensor::reading()
+{
+ return *m_lightReading.get();
+}
+
+void AmbientLightSensor::didUpdateData()
+{
+ if (!m_lightReading)
+ m_lightReading = AmbientLightSensorReading::create();
+
+ AmbientLightSensorReadingProxy oldReading(m_lightReading.get()->timeStamp(), m_lightReading.get()->illuminance());
+ AmbientLightSensorReadingProxy currentReading = *AmbientLightSensorDispatcher::instance().latestData();
+
+ Document* document = toDocument(getExecutionContext());
+ ASSERT(document);
+
+ if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAreStopped())
+ return;
+
+ m_lightReading.get()->setTimeStamp(currentReading.timeStamp);
+ m_lightReading.get()->setIlluminance(currentReading.illuminance);
+
+ // TODO(riju) : Implement threshold when it is specs.
+ if (currentReading.illuminance != oldReading.illuminance)
+ dispatchEvent(SensorReadingEvent::create(EventTypeNames::change, *m_lightReading.get()));
+}
+
+void AmbientLightSensor::registerWithDispatcher()
+{
+ AmbientLightSensorDispatcher::instance().addController(this);
+}
+
+void AmbientLightSensor::unregisterWithDispatcher()
+{
+ AmbientLightSensorDispatcher::instance().removeController(this);
+}
+
+bool AmbientLightSensor::hasLastData()
+{
+ return AmbientLightSensorDispatcher::instance().latestData();
+}
+
+void AmbientLightSensor::suspend()
+{
+ m_hasEventListener = false;
timvolodine 2016/05/06 17:05:41 could this go to the Sensor superclass?
riju_ 2016/05/16 16:58:51 I could add the that to Sensor superclass, But th
+ stopUpdating();
+}
+
+void AmbientLightSensor::resume()
+{
+ m_hasEventListener = true;
+ startUpdating();
+}
+
+void AmbientLightSensor::stop()
+{
+ m_hasEventListener = false;
+ stopUpdating();
+}
+
+bool AmbientLightSensor::hasPendingActivity() const
+{
+ // Prevent V8 from garbage collecting the wrapper object if there are
+ // event listeners attached to it.
+ return hasEventListeners();
+}
+
+DEFINE_TRACE(AmbientLightSensor)
+{
+ PlatformEventController::trace(visitor);
+ ActiveDOMObject::trace(visitor);
+ Sensor::trace(visitor);
+ visitor->trace(m_lightReading);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698