Index: device/sensors/platform_sensor_provider.cc |
diff --git a/device/sensors/platform_sensor_provider.cc b/device/sensors/platform_sensor_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a66137727f4557a0ce2a841f5c695b3b1f9dbc3f |
--- /dev/null |
+++ b/device/sensors/platform_sensor_provider.cc |
@@ -0,0 +1,84 @@ |
+// 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/sensors/platform_sensor_provider.h" |
+ |
+namespace device { |
+ |
+// static |
+PlatformSensorProvider* PlatformSensorProvider::Create( |
+ uint64_t shared_buffer_size) { |
+ // TODO(shalamov): Add platform-specific PlatformSensorProvider |
+ // implementations here. |
+ return nullptr; |
+} |
+ |
+PlatformSensorProvider::PlatformSensorProvider(uint64_t shared_buffer_size) |
+ : shared_buffer_size_(shared_buffer_size) {} |
+ |
+PlatformSensorProvider::~PlatformSensorProvider() = default; |
+ |
+scoped_refptr<PlatformSensor> PlatformSensorProvider::CreateSensor( |
+ 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; |
+ |
+ // Create new platform sensor instance. |
+ auto new_sensor = CreateSensor(type, std::move(mapping), size); |
dcheng
2016/07/01 09:17:24
Note the current guidance is not to use auto to ho
shalamov
2016/07/01 14:22:47
Done.
|
+ if (!new_sensor) |
+ return nullptr; |
+ |
+ const auto& add_result = |
+ sensor_map_.insert(std::make_pair(type, new_sensor.get())); |
+ DCHECK(add_result.second); |
+ |
+ return add_result.first->second; |
dcheng
2016/07/01 09:17:24
return new_sensor is slightly more efficient here.
shalamov
2016/07/01 14:22:47
Done.
|
+} |
+ |
+scoped_refptr<PlatformSensor> PlatformSensorProvider::GetSensor( |
+ 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; |
+ |
+ return mojo::CreateSharedBuffer(nullptr, shared_buffer_size_, |
+ &shared_buffer_handle_) == MOJO_RESULT_OK; |
+} |
+ |
+void PlatformSensorProvider::RemoveSensor(SensorType type) { |
+ DCHECK(CalledOnValidThread()); |
+ auto it = sensor_map_.find(type); |
+ if (it != sensor_map_.end()) |
+ sensor_map_.erase(it); |
+ |
+ if (sensor_map_.empty() && shared_buffer_handle_->is_valid()) |
+ shared_buffer_handle_.reset(); |
+} |
+ |
+mojo::ScopedSharedBufferHandle |
+PlatformSensorProvider::GetClonedSharedBufferHandle() { |
+ DCHECK(CalledOnValidThread()); |
+ CreateSharedBufferIfNeeded(); |
+ return shared_buffer_handle_->Clone(); |
+} |
+ |
+} // namespace device |