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

Unified Diff: device/generic_sensor/platform_sensor_provider.cc

Issue 2144623003: [sensors] Introduce Generic Sensor API interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from Reilly 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/platform_sensor_provider.cc
diff --git a/device/generic_sensor/platform_sensor_provider.cc b/device/generic_sensor/platform_sensor_provider.cc
new file mode 100644
index 0000000000000000000000000000000000000000..29010dc8c87bf803585a62adbb98c5397d951a31
--- /dev/null
+++ b/device/generic_sensor/platform_sensor_provider.cc
@@ -0,0 +1,86 @@
+// 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/platform_sensor_provider.h"
+
+#include <utility>
+
+#include "base/stl_util.h"
+
+namespace device {
+
+// static
+PlatformSensorProvider* PlatformSensorProvider::Create(
+ uint64_t shared_buffer_size) {
+ // TODO(shalamov): Add platform-specific PlatformSensorProvider
+ // implementations here.
timvolodine 2016/08/18 22:52:14 NOTREACHED()?
Mikhail 2016/08/19 09:29:55 'nullptr' just means that there is no available im
+ return nullptr;
+}
+
+PlatformSensorProvider::PlatformSensorProvider(uint64_t shared_buffer_size)
+ : shared_buffer_size_(shared_buffer_size) {}
+
+PlatformSensorProvider::~PlatformSensorProvider() = default;
+
+scoped_refptr<PlatformSensor> PlatformSensorProvider::CreateSensor(
+ mojom::SensorType type,
+ uint64_t size,
+ uint64_t offset) {
+ DCHECK(CalledOnValidThread());
+
+ if (!CreateSharedBufferIfNeeded())
+ return nullptr;
+
+ mojo::ScopedSharedBufferMapping mapping =
+ shared_buffer_handle_->MapAtOffset(size, offset);
+ if (!mapping)
+ return nullptr;
+
+ scoped_refptr<PlatformSensor> new_sensor =
+ CreateSensorInternal(type, std::move(mapping), size);
+ if (!new_sensor)
+ return nullptr;
+
+ DCHECK(!ContainsKey(sensor_map_, type));
+ sensor_map_[type] = new_sensor.get();
+
+ return new_sensor;
+}
+
+scoped_refptr<PlatformSensor> PlatformSensorProvider::GetSensor(
+ mojom::SensorType type) {
+ DCHECK(CalledOnValidThread());
+
+ auto it = sensor_map_.find(type);
+ if (it != sensor_map_.end())
+ return it->second;
+ return nullptr;
+}
+
+bool PlatformSensorProvider::CreateSharedBufferIfNeeded() {
+ DCHECK(CalledOnValidThread());
+ if (shared_buffer_handle_.is_valid())
+ return true;
+
+ shared_buffer_handle_ = mojo::SharedBufferHandle::Create(shared_buffer_size_);
+ return shared_buffer_handle_.is_valid();
+}
+
+void PlatformSensorProvider::RemoveSensor(mojom::SensorType type) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(ContainsKey(sensor_map_, type));
+ sensor_map_.erase(type);
+
+ if (sensor_map_.empty())
+ shared_buffer_handle_.reset();
+}
+
+mojo::ScopedSharedBufferHandle
+PlatformSensorProvider::CloneSharedBufferHandle() {
+ DCHECK(CalledOnValidThread());
+ CreateSharedBufferIfNeeded();
+ return shared_buffer_handle_->Clone();
+}
+
+} // namespace device

Powered by Google App Engine
This is Rietveld 408576698