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 kSharedBufferSizeInBytes = | 16 const uint64_t kSharedBufferSizeInBytes = |
| 17 mojom::SensorInitParams::kReadBufferSize * | 17 mojom::SensorInitParams::kReadBufferSize * |
| 18 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 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::CreateSensor( | 25 void PlatformSensorProviderBase::CreateSensor( |
| 26 mojom::SensorType type, | 26 mojom::SensorType type, |
| 27 uint64_t size, | 27 uint64_t size, |
| 28 uint64_t offset) { | 28 uint64_t offset, |
| 29 const CreateSensorCallback& callback) { | |
| 29 DCHECK(CalledOnValidThread()); | 30 DCHECK(CalledOnValidThread()); |
| 30 | 31 |
| 31 if (!CreateSharedBufferIfNeeded()) | 32 if (!CreateSharedBufferIfNeeded()) { |
| 32 return nullptr; | 33 callback.Run(nullptr); |
| 34 return; | |
| 35 } | |
| 33 | 36 |
| 34 mojo::ScopedSharedBufferMapping mapping = | 37 mojo::ScopedSharedBufferMapping mapping = |
| 35 shared_buffer_handle_->MapAtOffset(size, offset); | 38 shared_buffer_handle_->MapAtOffset(size, offset); |
| 36 if (!mapping) | 39 if (!mapping) { |
| 37 return nullptr; | 40 callback.Run(nullptr); |
| 41 return; | |
| 42 } | |
| 38 | 43 |
| 39 scoped_refptr<PlatformSensor> new_sensor = | 44 auto it = requests_map_.find(type); |
| 40 CreateSensorInternal(type, std::move(mapping), size); | 45 if (it != requests_map_.end()) { |
| 41 if (!new_sensor) | 46 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
| |
| 42 return nullptr; | 47 } else { |
| 48 requests_map_[type] = CallbackQueue({callback}); | |
| 43 | 49 |
| 44 DCHECK(!ContainsKey(sensor_map_, type)); | 50 CreateSensorInternal( |
| 45 sensor_map_[type] = new_sensor.get(); | 51 type, std::move(mapping), size, |
| 46 | 52 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated, |
| 47 return new_sensor; | 53 base::Unretained(this), type)); |
| 54 } | |
| 48 } | 55 } |
| 49 | 56 |
| 50 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( | 57 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( |
| 51 mojom::SensorType type) { | 58 mojom::SensorType type) { |
| 52 DCHECK(CalledOnValidThread()); | 59 DCHECK(CalledOnValidThread()); |
| 53 | 60 |
| 54 auto it = sensor_map_.find(type); | 61 auto it = sensor_map_.find(type); |
| 55 if (it != sensor_map_.end()) | 62 if (it != sensor_map_.end()) |
| 56 return it->second; | 63 return it->second; |
| 57 return nullptr; | 64 return nullptr; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 76 shared_buffer_handle_.reset(); | 83 shared_buffer_handle_.reset(); |
| 77 } | 84 } |
| 78 | 85 |
| 79 mojo::ScopedSharedBufferHandle | 86 mojo::ScopedSharedBufferHandle |
| 80 PlatformSensorProviderBase::CloneSharedBufferHandle() { | 87 PlatformSensorProviderBase::CloneSharedBufferHandle() { |
| 81 DCHECK(CalledOnValidThread()); | 88 DCHECK(CalledOnValidThread()); |
| 82 CreateSharedBufferIfNeeded(); | 89 CreateSharedBufferIfNeeded(); |
| 83 return shared_buffer_handle_->Clone(); | 90 return shared_buffer_handle_->Clone(); |
| 84 } | 91 } |
| 85 | 92 |
| 93 void PlatformSensorProviderBase::NotifySensorCreated( | |
| 94 mojom::SensorType type, | |
| 95 scoped_refptr<PlatformSensor> sensor) { | |
| 96 DCHECK(CalledOnValidThread()); | |
| 97 DCHECK(!ContainsKey(sensor_map_, type)); | |
| 98 DCHECK(ContainsKey(requests_map_, type)); | |
| 99 | |
| 100 if (sensor) | |
| 101 sensor_map_[type] = sensor.get(); | |
| 102 | |
| 103 // Inform subscribers about the sensor. | |
| 104 // |sensor| can be nullptr here. | |
| 105 auto it = requests_map_.find(type); | |
| 106 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.
| |
| 107 cb.Run(sensor); | |
| 108 | |
| 109 requests_map_.erase(type); | |
| 110 } | |
| 111 | |
| 86 } // namespace device | 112 } // namespace device |
| OLD | NEW |