Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/generic_sensor/platform_sensor_provider_base.h" | 5 #include "device/generic_sensor/platform_sensor_provider_base.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" | 10 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" |
| 11 | 11 |
| 12 namespace device { | 12 namespace device { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 const uint64_t kReadingBufferSize = sizeof(SensorReadingSharedBuffer); | |
| 16 const uint64_t kSharedBufferSizeInBytes = | 17 const uint64_t kSharedBufferSizeInBytes = |
| 17 mojom::SensorInitParams::kReadBufferSize * | 18 kReadingBufferSize * static_cast<uint64_t>(mojom::SensorType::LAST); |
| 18 static_cast<uint64_t>(mojom::SensorType::LAST); | |
| 19 | 19 |
| 20 } // namespace | 20 } // namespace |
| 21 | 21 |
| 22 PlatformSensorProviderBase::PlatformSensorProviderBase() = default; | 22 PlatformSensorProviderBase::PlatformSensorProviderBase() = default; |
| 23 PlatformSensorProviderBase::~PlatformSensorProviderBase() = default; | 23 PlatformSensorProviderBase::~PlatformSensorProviderBase() = default; |
| 24 | 24 |
| 25 void PlatformSensorProviderBase::CreateSensor( | 25 void PlatformSensorProviderBase::CreateSensor( |
| 26 mojom::SensorType type, | 26 mojom::SensorType type, |
| 27 uint64_t size, | |
| 28 uint64_t offset, | |
| 29 const CreateSensorCallback& callback) { | 27 const CreateSensorCallback& callback) { |
| 30 DCHECK(CalledOnValidThread()); | 28 DCHECK(CalledOnValidThread()); |
| 31 | 29 |
| 32 if (!CreateSharedBufferIfNeeded()) { | 30 if (!CreateSharedBufferIfNeeded()) { |
| 33 callback.Run(nullptr); | 31 callback.Run(nullptr); |
| 34 return; | 32 return; |
| 35 } | 33 } |
| 36 | 34 |
| 37 mojo::ScopedSharedBufferMapping mapping = | 35 mojo::ScopedSharedBufferMapping mapping = shared_buffer_handle_->MapAtOffset( |
| 38 shared_buffer_handle_->MapAtOffset(size, offset); | 36 kReadingBufferSize, GetSharedBufferOffset(type)); |
| 39 if (!mapping) { | 37 if (!mapping) { |
| 40 callback.Run(nullptr); | 38 callback.Run(nullptr); |
| 41 return; | 39 return; |
| 42 } | 40 } |
| 43 | 41 |
| 44 auto it = requests_map_.find(type); | 42 auto it = requests_map_.find(type); |
| 45 if (it != requests_map_.end()) { | 43 if (it != requests_map_.end()) { |
| 46 it->second.push_back(callback); | 44 it->second.push_back(callback); |
| 47 } else { | 45 } else { // This is the first CreateSensor call. |
| 46 memset(mapping.get(), 0, kReadingBufferSize); | |
| 47 | |
| 48 requests_map_[type] = CallbackQueue({callback}); | 48 requests_map_[type] = CallbackQueue({callback}); |
| 49 | 49 |
| 50 CreateSensorInternal( | 50 CreateSensorInternal( |
| 51 type, std::move(mapping), size, | 51 type, std::move(mapping), |
| 52 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated, | 52 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated, |
| 53 base::Unretained(this), type)); | 53 base::Unretained(this), type)); |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( | 57 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( |
| 58 mojom::SensorType type) { | 58 mojom::SensorType type) { |
| 59 DCHECK(CalledOnValidThread()); | 59 DCHECK(CalledOnValidThread()); |
| 60 | 60 |
| 61 auto it = sensor_map_.find(type); | 61 auto it = sensor_map_.find(type); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 | 102 |
| 103 // Inform subscribers about the sensor. | 103 // Inform subscribers about the sensor. |
| 104 // |sensor| can be nullptr here. | 104 // |sensor| can be nullptr here. |
| 105 auto it = requests_map_.find(type); | 105 auto it = requests_map_.find(type); |
| 106 for (auto& callback : it->second) | 106 for (auto& callback : it->second) |
| 107 callback.Run(sensor); | 107 callback.Run(sensor); |
| 108 | 108 |
| 109 requests_map_.erase(type); | 109 requests_map_.erase(type); |
| 110 } | 110 } |
| 111 | 111 |
| 112 uint64_t PlatformSensorProviderBase::GetSharedBufferOffset( | |
|
Ken Rockot(use gerrit already)
2016/10/10 16:02:08
nit: This doesn't need to be a member function at
Mikhail
2016/10/10 18:29:20
Thanks for hint! I've moved it here from sensor_im
| |
| 113 mojom::SensorType type) { | |
| 114 return (static_cast<uint64_t>(mojom::SensorType::LAST) - | |
| 115 static_cast<uint64_t>(type)) * | |
| 116 kReadingBufferSize; | |
| 117 } | |
| 118 | |
| 112 } // namespace device | 119 } // namespace device |
| OLD | NEW |