Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: device/generic_sensor/platform_sensor_provider_base.cc

Issue 2865263002: Move //device/generic_sensor to be part of the internal implementation of the Device Service. (Closed)
Patch Set: code rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "device/generic_sensor/platform_sensor_provider_base.h"
6
7 #include <utility>
8
9 #include "base/stl_util.h"
10 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h"
11
12 namespace device {
13
14 namespace {
15
16 const uint64_t kReadingBufferSize = sizeof(SensorReadingSharedBuffer);
17 const uint64_t kSharedBufferSizeInBytes =
18 kReadingBufferSize * static_cast<uint64_t>(mojom::SensorType::LAST);
19
20 } // namespace
21
22 PlatformSensorProviderBase::PlatformSensorProviderBase() = default;
23
24 PlatformSensorProviderBase::~PlatformSensorProviderBase() {
25 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
26 }
27
28 void PlatformSensorProviderBase::CreateSensor(
29 mojom::SensorType type,
30 const CreateSensorCallback& callback) {
31 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
32
33 if (!CreateSharedBufferIfNeeded()) {
34 callback.Run(nullptr);
35 return;
36 }
37
38 mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type);
39 if (!mapping) {
40 callback.Run(nullptr);
41 return;
42 }
43
44 auto it = requests_map_.find(type);
45 if (it != requests_map_.end()) {
46 it->second.push_back(callback);
47 } else { // This is the first CreateSensor call.
48 requests_map_[type] = CallbackQueue({callback});
49
50 CreateSensorInternal(
51 type, std::move(mapping),
52 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated,
53 base::Unretained(this), type));
54 }
55 }
56
57 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor(
58 mojom::SensorType type) {
59 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
60
61 auto it = sensor_map_.find(type);
62 if (it != sensor_map_.end())
63 return it->second;
64 return nullptr;
65 }
66
67 bool PlatformSensorProviderBase::CreateSharedBufferIfNeeded() {
68 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
69 if (shared_buffer_handle_.is_valid())
70 return true;
71
72 shared_buffer_handle_ =
73 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes);
74 return shared_buffer_handle_.is_valid();
75 }
76
77 void PlatformSensorProviderBase::RemoveSensor(mojom::SensorType type) {
78 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
79 DCHECK(ContainsKey(sensor_map_, type));
80 sensor_map_.erase(type);
81
82 if (sensor_map_.empty()) {
83 AllSensorsRemoved();
84 shared_buffer_handle_.reset();
85 }
86 }
87
88 mojo::ScopedSharedBufferHandle
89 PlatformSensorProviderBase::CloneSharedBufferHandle() {
90 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
91 CreateSharedBufferIfNeeded();
92 return shared_buffer_handle_->Clone(
93 mojo::SharedBufferHandle::AccessMode::READ_ONLY);
94 }
95
96 bool PlatformSensorProviderBase::HasSensors() const {
97 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
98 return !sensor_map_.empty();
99 }
100
101 void PlatformSensorProviderBase::NotifySensorCreated(
102 mojom::SensorType type,
103 scoped_refptr<PlatformSensor> sensor) {
104 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
105 DCHECK(!ContainsKey(sensor_map_, type));
106 DCHECK(ContainsKey(requests_map_, type));
107
108 if (sensor)
109 sensor_map_[type] = sensor.get();
110
111 // Inform subscribers about the sensor.
112 // |sensor| can be nullptr here.
113 auto it = requests_map_.find(type);
114 for (auto& callback : it->second)
115 callback.Run(sensor);
116
117 requests_map_.erase(type);
118 }
119
120 std::vector<mojom::SensorType>
121 PlatformSensorProviderBase::GetPendingRequestTypes() {
122 std::vector<mojom::SensorType> request_types;
123 for (auto const& entry : requests_map_)
124 request_types.push_back(entry.first);
125 return request_types;
126 }
127
128 mojo::ScopedSharedBufferMapping
129 PlatformSensorProviderBase::MapSharedBufferForType(mojom::SensorType type) {
130 mojo::ScopedSharedBufferMapping mapping = shared_buffer_handle_->MapAtOffset(
131 kReadingBufferSize, SensorReadingSharedBuffer::GetOffset(type));
132 memset(mapping.get(), 0, kReadingBufferSize);
133 return mapping;
134 }
135
136 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/platform_sensor_provider_base.h ('k') | device/generic_sensor/platform_sensor_provider_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698