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" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 void PlatformSensorProviderBase::CreateSensor( | 25 void PlatformSensorProviderBase::CreateSensor( |
| 26 mojom::SensorType type, | 26 mojom::SensorType type, |
| 27 const CreateSensorCallback& callback) { | 27 const CreateSensorCallback& callback) { |
| 28 DCHECK(CalledOnValidThread()); | 28 DCHECK(CalledOnValidThread()); |
| 29 | 29 |
| 30 if (!CreateSharedBufferIfNeeded()) { | 30 if (!CreateSharedBufferIfNeeded()) { |
| 31 callback.Run(nullptr); | 31 callback.Run(nullptr); |
| 32 return; | 32 return; |
| 33 } | 33 } |
| 34 | 34 |
| 35 mojo::ScopedSharedBufferMapping mapping = shared_buffer_handle_->MapAtOffset( | 35 mojo::ScopedSharedBufferMapping mapping = GetScopedSharedBufferMapping(type); |
| 36 kReadingBufferSize, SensorReadingSharedBuffer::GetOffset(type)); | |
| 37 if (!mapping) { | 36 if (!mapping) { |
| 38 callback.Run(nullptr); | 37 callback.Run(nullptr); |
| 39 return; | 38 return; |
| 40 } | 39 } |
| 41 | 40 |
| 42 auto it = requests_map_.find(type); | 41 auto it = requests_map_.find(type); |
| 43 if (it != requests_map_.end()) { | 42 if (it != requests_map_.end()) { |
| 44 it->second.push_back(callback); | 43 it->second.push_back(callback); |
| 45 } else { // This is the first CreateSensor call. | 44 } else { // This is the first CreateSensor call. |
| 46 memset(mapping.get(), 0, kReadingBufferSize); | 45 memset(mapping.get(), 0, kReadingBufferSize); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 71 | 70 |
| 72 shared_buffer_handle_ = | 71 shared_buffer_handle_ = |
| 73 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes); | 72 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes); |
| 74 return shared_buffer_handle_.is_valid(); | 73 return shared_buffer_handle_.is_valid(); |
| 75 } | 74 } |
| 76 | 75 |
| 77 void PlatformSensorProviderBase::RemoveSensor(mojom::SensorType type) { | 76 void PlatformSensorProviderBase::RemoveSensor(mojom::SensorType type) { |
| 78 DCHECK(CalledOnValidThread()); | 77 DCHECK(CalledOnValidThread()); |
| 79 DCHECK(ContainsKey(sensor_map_, type)); | 78 DCHECK(ContainsKey(sensor_map_, type)); |
| 80 sensor_map_.erase(type); | 79 sensor_map_.erase(type); |
| 80 SensorRemoved(type); | |
| 81 | 81 |
| 82 if (sensor_map_.empty()) { | 82 if (sensor_map_.empty()) { |
| 83 AllSensorsRemoved(); | 83 AllSensorsRemoved(); |
| 84 shared_buffer_handle_.reset(); | 84 shared_buffer_handle_.reset(); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 mojo::ScopedSharedBufferHandle | 88 mojo::ScopedSharedBufferHandle |
| 89 PlatformSensorProviderBase::CloneSharedBufferHandle() { | 89 PlatformSensorProviderBase::CloneSharedBufferHandle() { |
| 90 DCHECK(CalledOnValidThread()); | 90 DCHECK(CalledOnValidThread()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 110 | 110 |
| 111 // Inform subscribers about the sensor. | 111 // Inform subscribers about the sensor. |
| 112 // |sensor| can be nullptr here. | 112 // |sensor| can be nullptr here. |
| 113 auto it = requests_map_.find(type); | 113 auto it = requests_map_.find(type); |
| 114 for (auto& callback : it->second) | 114 for (auto& callback : it->second) |
| 115 callback.Run(sensor); | 115 callback.Run(sensor); |
| 116 | 116 |
| 117 requests_map_.erase(type); | 117 requests_map_.erase(type); |
| 118 } | 118 } |
| 119 | 119 |
| 120 std::vector<mojom::SensorType> PlatformSensorProviderBase::GetRequestTypes() { | |
|
Mikhail
2016/12/08 11:15:22
GetPendingRequestedTypes
| |
| 121 std::vector<mojom::SensorType> request_types; | |
| 122 for (auto const& entry : requests_map_) | |
| 123 request_types.push_back(entry.first); | |
| 124 return request_types; | |
| 125 } | |
| 126 | |
| 127 mojo::ScopedSharedBufferMapping | |
| 128 PlatformSensorProviderBase::GetScopedSharedBufferMapping( | |
| 129 mojom::SensorType type) { | |
| 130 mojo::ScopedSharedBufferMapping mapping = shared_buffer_handle_->MapAtOffset( | |
|
Mikhail
2016/12/08 11:15:22
this should not be called several times, map once
maksims (do not use this acc)
2016/12/08 18:39:17
But it does not actually overwrite existing mappin
| |
| 131 kReadingBufferSize, SensorReadingSharedBuffer::GetOffset(type)); | |
| 132 return mapping; | |
| 133 } | |
| 134 | |
| 120 } // namespace device | 135 } // namespace device |
| OLD | NEW |