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 = MapSharedBufferForType(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); | |
47 | |
48 requests_map_[type] = CallbackQueue({callback}); | 45 requests_map_[type] = CallbackQueue({callback}); |
49 | 46 |
50 CreateSensorInternal( | 47 CreateSensorInternal( |
51 type, std::move(mapping), | 48 type, std::move(mapping), |
52 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated, | 49 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated, |
53 base::Unretained(this), type)); | 50 base::Unretained(this), type)); |
54 } | 51 } |
55 } | 52 } |
56 | 53 |
57 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( | 54 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 107 |
111 // Inform subscribers about the sensor. | 108 // Inform subscribers about the sensor. |
112 // |sensor| can be nullptr here. | 109 // |sensor| can be nullptr here. |
113 auto it = requests_map_.find(type); | 110 auto it = requests_map_.find(type); |
114 for (auto& callback : it->second) | 111 for (auto& callback : it->second) |
115 callback.Run(sensor); | 112 callback.Run(sensor); |
116 | 113 |
117 requests_map_.erase(type); | 114 requests_map_.erase(type); |
118 } | 115 } |
119 | 116 |
| 117 std::vector<mojom::SensorType> |
| 118 PlatformSensorProviderBase::GetPendingRequestTypes() { |
| 119 std::vector<mojom::SensorType> request_types; |
| 120 for (auto const& entry : requests_map_) |
| 121 request_types.push_back(entry.first); |
| 122 return request_types; |
| 123 } |
| 124 |
| 125 mojo::ScopedSharedBufferMapping |
| 126 PlatformSensorProviderBase::MapSharedBufferForType(mojom::SensorType type) { |
| 127 mojo::ScopedSharedBufferMapping mapping = shared_buffer_handle_->MapAtOffset( |
| 128 kReadingBufferSize, SensorReadingSharedBuffer::GetOffset(type)); |
| 129 memset(mapping.get(), 0, kReadingBufferSize); |
| 130 return mapping; |
| 131 } |
| 132 |
120 } // namespace device | 133 } // namespace device |
OLD | NEW |