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

Unified Diff: third_party/WebKit/Source/modules/sensor/AmbientLightSensorDispatcher.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/AmbientLightSensorDispatcher.cpp
diff --git a/third_party/WebKit/Source/modules/sensor/AmbientLightSensorDispatcher.cpp b/third_party/WebKit/Source/modules/sensor/AmbientLightSensorDispatcher.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..c51c91e67c168e99c83f6abf01403b0b322d3995
--- /dev/null
+++ b/third_party/WebKit/Source/modules/sensor/AmbientLightSensorDispatcher.cpp
@@ -0,0 +1,74 @@
+// 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/AmbientLightSensorDispatcher.h"
+
+#include "platform/mojo/MojoHelper.h"
+#include "public/platform/Platform.h"
+#include "public/platform/ServiceRegistry.h"
+#include "wtf/PassOwnPtr.h"
+
+namespace blink {
+
+AmbientLightSensorDispatcher& AmbientLightSensorDispatcher::instance()
+{
+ DEFINE_STATIC_LOCAL(Persistent<AmbientLightSensorDispatcher>, lightSensorDispatcher, (new AmbientLightSensorDispatcher()));
+ return *lightSensorDispatcher;
+}
+
+AmbientLightSensorDispatcher::AmbientLightSensorDispatcher()
+ : m_hasLatestData(false)
+{
+}
+
+void AmbientLightSensorDispatcher::queryNextReading()
+{
+ m_lightSensor->QueryNextReading(createBaseCallback(bind<device::blink::AmbientLightSensorReadingPtr>(&AmbientLightSensorDispatcher::onDidChange, this)));
+}
+
+void AmbientLightSensorDispatcher::startReading()
+{
+ m_lightSensor->StartReading();
+}
+
+void AmbientLightSensorDispatcher::stopReading()
+{
+ m_lightSensor->StopReading();
+}
+
+void AmbientLightSensorDispatcher::onDidChange(device::blink::AmbientLightSensorReadingPtr alsReading)
+{
+ queryNextReading();
+
+ DCHECK(alsReading);
+
+ updateSensorReading(AmbientLightSensorReadingProxy(
+ alsReading->timeStamp, alsReading->illuminance));
+}
+
+void AmbientLightSensorDispatcher::updateSensorReading(const AmbientLightSensorReadingProxy& alsReading)
+{
+ m_alsReading = alsReading;
+ m_hasLatestData = true;
+ notifyControllers();
+}
+
+void AmbientLightSensorDispatcher::startListening()
+{
+ DCHECK(!m_lightSensor.is_bound());
+ Platform::current()->serviceRegistry()->connectToRemoteService(mojo::GetProxy(&m_lightSensor));
+ if (!m_hasLatestData)
+ startReading();
+
+ queryNextReading();
+}
+
+void AmbientLightSensorDispatcher::stopListening()
+{
+ stopReading();
+ m_lightSensor.reset();
+ m_hasLatestData = false;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698