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 uint64_t GetBufferOffset(mojom::SensorType type) { |
| 17 return (static_cast<uint64_t>(mojom::SensorType::LAST) - |
| 18 static_cast<uint64_t>(type)) * |
| 19 mojom::SensorReadBuffer::kReadBufferSize; |
| 20 } |
| 21 |
| 22 } // namespace |
| 23 |
| 24 // static |
| 25 void SensorProviderImpl::Create( |
| 26 mojo::InterfaceRequest<mojom::SensorProvider> request) { |
| 27 PlatformSensorProvider* provider = PlatformSensorProvider::GetInstance(); |
| 28 if (provider) |
| 29 new SensorProviderImpl(std::move(request), provider); |
| 30 } |
| 31 |
| 32 SensorProviderImpl::SensorProviderImpl( |
| 33 mojo::InterfaceRequest<mojom::SensorProvider> request, |
| 34 PlatformSensorProvider* provider) |
| 35 : binding_(this, std::move(request)), provider_(provider) { |
| 36 DCHECK(provider_); |
| 37 } |
| 38 |
| 39 SensorProviderImpl::~SensorProviderImpl() {} |
| 40 |
| 41 void SensorProviderImpl::GetSensor(mojom::SensorType type, |
| 42 mojom::SensorRequest sensor_request, |
| 43 const GetSensorCallback& callback) { |
| 44 auto cloned_handle = provider_->CloneSharedBufferHandle(); |
| 45 if (!cloned_handle.is_valid()) { |
| 46 callback.Run(nullptr, nullptr); |
| 47 return; |
| 48 } |
| 49 |
| 50 scoped_refptr<PlatformSensor> sensor = provider_->GetSensor(type); |
| 51 if (!sensor) { |
| 52 sensor = provider_->CreateSensor( |
| 53 type, mojom::SensorReadBuffer::kReadBufferSize, GetBufferOffset(type)); |
| 54 } |
| 55 |
| 56 if (!sensor) { |
| 57 callback.Run(nullptr, nullptr); |
| 58 return; |
| 59 } |
| 60 |
| 61 auto sensor_impl = new SensorImpl(std::move(sensor_request), sensor); |
| 62 |
| 63 auto sensor_read_buffer = mojom::SensorReadBuffer::New(); |
| 64 sensor_read_buffer->memory = std::move(cloned_handle); |
| 65 sensor_read_buffer->offset = GetBufferOffset(type); |
| 66 sensor_read_buffer->mode = sensor->GetReportingMode(); |
| 67 |
| 68 callback.Run(std::move(sensor_read_buffer), sensor_impl->GetClient()); |
| 69 } |
| 70 |
| 71 } // namespace device |
OLD | NEW |