OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/generic_sensor/sensor_provider_impl.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "device/generic_sensor/platform_sensor_provider.h" |
| 10 #include "device/generic_sensor/sensor_impl.h" |
| 11 |
| 12 namespace device { |
| 13 |
| 14 namespace { |
| 15 |
| 16 const uint64_t kSharedBufferSizeInBytes = |
| 17 mojom::SensorReadBuffer::kReadBufferSize * |
| 18 static_cast<uint64_t>(mojom::SensorType::LAST); |
| 19 |
| 20 uint64_t GetBufferOffset(mojom::SensorType type) { |
| 21 return (static_cast<uint64_t>(mojom::SensorType::LAST) - |
| 22 static_cast<uint64_t>(type)) * |
| 23 mojom::SensorReadBuffer::kReadBufferSize; |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 PlatformSensorProvider* SensorProviderImpl::s_provider_ = nullptr; |
| 29 |
| 30 // static |
| 31 void SensorProviderImpl::Create( |
| 32 mojo::InterfaceRequest<mojom::SensorProvider> request) { |
| 33 if (!s_provider_) |
| 34 s_provider_ = PlatformSensorProvider::Create(kSharedBufferSizeInBytes); |
| 35 |
| 36 if (s_provider_) |
| 37 new SensorProviderImpl(std::move(request)); |
| 38 } |
| 39 |
| 40 SensorProviderImpl::SensorProviderImpl( |
| 41 mojo::InterfaceRequest<mojom::SensorProvider> request) |
| 42 : binding_(this, std::move(request)) {} |
| 43 |
| 44 SensorProviderImpl::~SensorProviderImpl() {} |
| 45 |
| 46 void SensorProviderImpl::GetSensor(mojom::SensorType type, |
| 47 mojom::SensorRequest sensor_request, |
| 48 const GetSensorCallback& callback) { |
| 49 auto cloned_handle = s_provider_->CloneSharedBufferHandle(); |
| 50 if (!cloned_handle.is_valid()) { |
| 51 callback.Run(nullptr, nullptr); |
| 52 return; |
| 53 } |
| 54 |
| 55 scoped_refptr<PlatformSensor> sensor = s_provider_->GetSensor(type); |
| 56 if (!sensor) { |
| 57 sensor = s_provider_->CreateSensor( |
| 58 type, mojom::SensorReadBuffer::kReadBufferSize, GetBufferOffset(type)); |
| 59 } |
| 60 |
| 61 if (!sensor) { |
| 62 callback.Run(nullptr, nullptr); |
| 63 return; |
| 64 } |
| 65 |
| 66 auto sensor_impl = new SensorImpl(std::move(sensor_request), sensor); |
| 67 |
| 68 auto sensor_read_buffer = mojom::SensorReadBuffer::New(); |
| 69 sensor_read_buffer->memory = std::move(cloned_handle); |
| 70 sensor_read_buffer->offset = GetBufferOffset(type); |
| 71 sensor_read_buffer->mode = sensor->GetReportingMode(); |
| 72 |
| 73 callback.Run(std::move(sensor_read_buffer), sensor_impl->GetClient()); |
| 74 } |
| 75 |
| 76 } // namespace device |
OLD | NEW |