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

Unified Diff: device/generic_sensor/sensor_provider_impl.cc

Issue 2144623003: [sensors] Introduce Generic Sensor API interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Definition for PlatformSensorProvider ctor/dtor Created 4 years, 4 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: device/generic_sensor/sensor_provider_impl.cc
diff --git a/device/generic_sensor/sensor_provider_impl.cc b/device/generic_sensor/sensor_provider_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1a2f79a82b726a972ff9370bdf7c003aa9a75eb5
--- /dev/null
+++ b/device/generic_sensor/sensor_provider_impl.cc
@@ -0,0 +1,71 @@
+// 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/generic_sensor/sensor_provider_impl.h"
+
+#include <utility>
+
+#include "device/generic_sensor/platform_sensor_provider.h"
+#include "device/generic_sensor/sensor_impl.h"
+
+namespace device {
+
+namespace {
+
+uint64_t GetBufferOffset(mojom::SensorType type) {
+ return (static_cast<uint64_t>(mojom::SensorType::LAST) -
+ static_cast<uint64_t>(type)) *
+ mojom::SensorReadBuffer::kReadBufferSize;
+}
+
+} // namespace
+
+// static
+void SensorProviderImpl::Create(
+ mojo::InterfaceRequest<mojom::SensorProvider> request) {
+ PlatformSensorProvider* provider = PlatformSensorProvider::GetInstance();
+ if (provider)
+ new SensorProviderImpl(std::move(request), provider);
+}
+
+SensorProviderImpl::SensorProviderImpl(
+ mojo::InterfaceRequest<mojom::SensorProvider> request,
+ PlatformSensorProvider* provider)
+ : binding_(this, std::move(request)), provider_(provider) {
+ DCHECK(provider_);
+}
+
+SensorProviderImpl::~SensorProviderImpl() {}
+
+void SensorProviderImpl::GetSensor(mojom::SensorType type,
+ mojom::SensorRequest sensor_request,
+ const GetSensorCallback& callback) {
+ auto cloned_handle = provider_->CloneSharedBufferHandle();
+ if (!cloned_handle.is_valid()) {
+ callback.Run(nullptr, nullptr);
+ return;
+ }
+
+ scoped_refptr<PlatformSensor> sensor = provider_->GetSensor(type);
+ if (!sensor) {
+ sensor = provider_->CreateSensor(
+ type, mojom::SensorReadBuffer::kReadBufferSize, GetBufferOffset(type));
+ }
+
+ if (!sensor) {
+ callback.Run(nullptr, nullptr);
+ return;
+ }
+
+ auto sensor_impl = new SensorImpl(std::move(sensor_request), sensor);
+
+ auto sensor_read_buffer = mojom::SensorReadBuffer::New();
+ sensor_read_buffer->memory = std::move(cloned_handle);
+ sensor_read_buffer->offset = GetBufferOffset(type);
+ sensor_read_buffer->mode = sensor->GetReportingMode();
+
+ callback.Run(std::move(sensor_read_buffer), sensor_impl->GetClient());
+}
+
+} // namespace device
« no previous file with comments | « device/generic_sensor/sensor_provider_impl.h ('k') | mojo/public/tools/bindings/chromium_bindings_configuration.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698