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 return; |
|
Mikhail
2016/09/26 09:52:23
it has to run callback with nullptr, right?
maksims (do not use this acc)
2016/09/27 10:17:25
Done.
| |
| 33 | 34 |
| 34 mojo::ScopedSharedBufferMapping mapping = | 35 mojo::ScopedSharedBufferMapping mapping = |
| 35 shared_buffer_handle_->MapAtOffset(size, offset); | 36 shared_buffer_handle_->MapAtOffset(size, offset); |
| 36 if (!mapping) | 37 if (!mapping) |
| 37 return nullptr; | 38 return; |
|
Mikhail
2016/09/26 09:52:23
ditto
maksims (do not use this acc)
2016/09/27 10:17:25
Done.
| |
| 38 | 39 |
| 39 scoped_refptr<PlatformSensor> new_sensor = | 40 auto it = requests_map_.find(type); |
| 40 CreateSensorInternal(type, std::move(mapping), size); | 41 if (it != requests_map_.end()) { |
| 41 if (!new_sensor) | 42 it->second.push_back(callback); |
| 42 return nullptr; | 43 } else { |
| 44 DCHECK(!ContainsKey(requests_map_, type)); | |
|
shalamov
2016/09/27 11:27:01
This DCHECK would never fail, right? Maybe it is b
maksims (do not use this acc)
2016/09/27 12:19:37
Done.
| |
| 43 | 45 |
| 44 DCHECK(!ContainsKey(sensor_map_, type)); | 46 CallbackQueue pending_requests; |
|
Mikhail
2016/09/27 11:29:19
nit: could be 'requests_map_[type] = CallbackQueue
maksims (do not use this acc)
2016/09/27 12:19:37
Done.
| |
| 45 sensor_map_[type] = new_sensor.get(); | 47 pending_requests.push_back(callback); |
| 48 requests_map_[type] = pending_requests; | |
| 46 | 49 |
| 47 return new_sensor; | 50 CreateSensorInternal( |
| 51 type, std::move(mapping), size, | |
| 52 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated, | |
| 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 if (it != requests_map_.end()) { | |
|
Mikhail
2016/09/27 11:29:19
it can never be 'it == requests_map_.end()', condi
maksims (do not use this acc)
2016/09/27 12:19:37
Right. Done!
| |
| 107 for (auto& cb : it->second) { | |
| 108 cb.Run(sensor); | |
| 109 } | |
| 110 } | |
| 111 requests_map_.erase(type); | |
| 112 } | |
| 113 | |
| 86 } // namespace device | 114 } // namespace device |
| OLD | NEW |