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

Unified Diff: device/sensor/sensor_impl.cc

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
« no previous file with comments | « device/sensor/sensor_impl.h ('k') | device/sensor/sensor_manager.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/sensor/sensor_impl.cc
diff --git a/device/sensor/sensor_impl.cc b/device/sensor/sensor_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f8d9dfd5ca46a1f5419feba5d9aad3b4b003ad4e
--- /dev/null
+++ b/device/sensor/sensor_impl.cc
@@ -0,0 +1,63 @@
+// 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 "device/sensor/sensor_impl.h"
+
+#include <utility>
+
+#include "base/bind.h"
+
+namespace device {
+
+// static
+void SensorImpl::Create(mojo::InterfaceRequest<AmbientLightSensor> request) {
+ new SensorImpl(std::move(request));
+}
+
+SensorImpl::SensorImpl(mojo::InterfaceRequest<AmbientLightSensor> request)
+ : binding_(this, std::move(request)), reading_to_report_(false) {
+ // NOTE: DidChange may be called before AddCallback returns. This is done to
+ // report current reading.
+ subscription_ = SensorService::GetInstance()->AddCallback(
+ base::Bind(&SensorImpl::DidChange, base::Unretained(this)));
+}
+
+SensorImpl::~SensorImpl() {}
+
+void SensorImpl::QueryNextReading(const SensorReadingCallback& callback) {
+ if (!callback_.is_null()) {
+ DVLOG(1) << "Overlapped call to QueryNextReading!";
+ delete this;
+ return;
+ }
+ callback_ = callback;
+
+ if (reading_to_report_)
+ ReportReading();
+}
+
+void SensorImpl::StartReading() {
+ // TODO(riju): To be implemented.
+}
+
+void SensorImpl::StopReading() {
+ // TODO(riju): To be implemented.
+}
+
+void SensorImpl::DidChange(const AmbientLightSensorReading& sensor_reading) {
+ reading_ = sensor_reading;
+ reading_to_report_ = true;
+
+ if (!callback_.is_null())
+ ReportReading();
+}
+
+void SensorImpl::ReportReading() {
+ callback_.Run(reading_.Clone());
+ callback_.reset();
+
+ reading_to_report_ = false;
+}
+
+} // namespace device
« no previous file with comments | « device/sensor/sensor_impl.h ('k') | device/sensor/sensor_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698