Chromium Code Reviews| Index: device/generic_sensor/platform_sensor_provider_base.cc |
| diff --git a/device/generic_sensor/platform_sensor_provider_base.cc b/device/generic_sensor/platform_sensor_provider_base.cc |
| index 51c60ac5ae25e681cefa83ed2cdec325ee9d9ccc..2b803445e92123ce954b280b64b50703ba2a2048 100644 |
| --- a/device/generic_sensor/platform_sensor_provider_base.cc |
| +++ b/device/generic_sensor/platform_sensor_provider_base.cc |
| @@ -22,29 +22,36 @@ const uint64_t kSharedBufferSizeInBytes = |
| PlatformSensorProviderBase::PlatformSensorProviderBase() = default; |
| PlatformSensorProviderBase::~PlatformSensorProviderBase() = default; |
| -scoped_refptr<PlatformSensor> PlatformSensorProviderBase::CreateSensor( |
| +void PlatformSensorProviderBase::CreateSensor( |
| mojom::SensorType type, |
| uint64_t size, |
| - uint64_t offset) { |
| + uint64_t offset, |
| + const CreateSensorCallback& callback) { |
| DCHECK(CalledOnValidThread()); |
| - if (!CreateSharedBufferIfNeeded()) |
| - return nullptr; |
| + if (!CreateSharedBufferIfNeeded()) { |
| + callback.Run(nullptr); |
| + return; |
| + } |
| 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; |
| + if (!mapping) { |
| + callback.Run(nullptr); |
| + return; |
| + } |
| + |
| + auto it = requests_map_.find(type); |
| + if (it != requests_map_.end()) { |
| + it->second.push_back(callback); |
|
timvolodine
2016/09/27 19:39:29
should we be worried about potential memory drain
maksims (do not use this acc)
2016/09/28 07:45:37
Actually, no. It takes 2-3 milliseconds for a call
timvolodine
2016/09/29 13:17:26
hmm, even when the initial sensor creation takes a
maksims (do not use this acc)
2016/09/30 06:23:06
We have one proxy per frame. This scenario is unli
timvolodine
2016/09/30 18:50:48
I see
|
| + } else { |
| + requests_map_[type] = CallbackQueue({callback}); |
| + |
| + CreateSensorInternal( |
| + type, std::move(mapping), size, |
| + base::Bind(&PlatformSensorProviderBase::NotifySensorCreated, |
| + base::Unretained(this), type)); |
| + } |
| } |
| scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( |
| @@ -83,4 +90,23 @@ PlatformSensorProviderBase::CloneSharedBufferHandle() { |
| return shared_buffer_handle_->Clone(); |
| } |
| +void PlatformSensorProviderBase::NotifySensorCreated( |
| + mojom::SensorType type, |
| + scoped_refptr<PlatformSensor> sensor) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(!ContainsKey(sensor_map_, type)); |
| + DCHECK(ContainsKey(requests_map_, type)); |
| + |
| + if (sensor) |
| + sensor_map_[type] = sensor.get(); |
| + |
| + // Inform subscribers about the sensor. |
| + // |sensor| can be nullptr here. |
| + auto it = requests_map_.find(type); |
| + for (auto& cb : it->second) |
|
timvolodine
2016/09/27 19:39:29
nit: better name for cb? is it 'callback'?
maksims (do not use this acc)
2016/09/28 07:45:37
Done.
|
| + cb.Run(sensor); |
| + |
| + requests_map_.erase(type); |
| +} |
| + |
| } // namespace device |