OLD | NEW |
---|---|
(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.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 kSharedBufferSizeInBytes = | |
17 mojom::SensorReadBuffer::kReadBufferSize * | |
18 static_cast<uint64_t>(mojom::SensorType::LAST); | |
19 | |
20 } // namespace | |
21 | |
22 PlatformSensorProvider* PlatformSensorProvider::s_instance_ = nullptr; | |
23 | |
24 // static | |
25 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { | |
26 // TODO(shalamov): Lazily initialize s_instance_ with platform-specific | |
27 // PlatformSensorProvider implementations here. | |
28 return s_instance_; | |
timvolodine
2016/08/19 16:02:30
we usually use return base::Singleton<.... instead
Mikhail
2016/08/19 19:10:02
I was considering using of Singleton, but here I b
timvolodine
2016/08/22 18:42:46
There is no need for #ifdefs when you follow the s
Mikhail
2016/08/22 19:16:15
Thanks for the links! so the solution could be to
timvolodine
2016/08/22 19:42:25
yes please, thanks ;)
| |
29 } | |
30 | |
31 PlatformSensorProvider::PlatformSensorProvider() = default; | |
32 PlatformSensorProvider::~PlatformSensorProvider() = default; | |
33 | |
34 scoped_refptr<PlatformSensor> PlatformSensorProvider::CreateSensor( | |
35 mojom::SensorType type, | |
36 uint64_t size, | |
37 uint64_t offset) { | |
38 DCHECK(CalledOnValidThread()); | |
39 | |
40 if (!CreateSharedBufferIfNeeded()) | |
41 return nullptr; | |
42 | |
43 mojo::ScopedSharedBufferMapping mapping = | |
44 shared_buffer_handle_->MapAtOffset(size, offset); | |
45 if (!mapping) | |
46 return nullptr; | |
47 | |
48 scoped_refptr<PlatformSensor> new_sensor = | |
49 CreateSensorInternal(type, std::move(mapping), size); | |
50 if (!new_sensor) | |
51 return nullptr; | |
52 | |
53 DCHECK(!ContainsKey(sensor_map_, type)); | |
54 sensor_map_[type] = new_sensor.get(); | |
55 | |
56 return new_sensor; | |
57 } | |
58 | |
59 scoped_refptr<PlatformSensor> PlatformSensorProvider::GetSensor( | |
60 mojom::SensorType type) { | |
61 DCHECK(CalledOnValidThread()); | |
62 | |
63 auto it = sensor_map_.find(type); | |
64 if (it != sensor_map_.end()) | |
65 return it->second; | |
66 return nullptr; | |
67 } | |
68 | |
69 bool PlatformSensorProvider::CreateSharedBufferIfNeeded() { | |
70 DCHECK(CalledOnValidThread()); | |
71 if (shared_buffer_handle_.is_valid()) | |
72 return true; | |
73 | |
74 shared_buffer_handle_ = | |
75 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes); | |
76 return shared_buffer_handle_.is_valid(); | |
77 } | |
78 | |
79 void PlatformSensorProvider::RemoveSensor(mojom::SensorType type) { | |
80 DCHECK(CalledOnValidThread()); | |
81 DCHECK(ContainsKey(sensor_map_, type)); | |
82 sensor_map_.erase(type); | |
83 | |
84 if (sensor_map_.empty()) | |
85 shared_buffer_handle_.reset(); | |
86 } | |
87 | |
88 mojo::ScopedSharedBufferHandle | |
89 PlatformSensorProvider::CloneSharedBufferHandle() { | |
90 DCHECK(CalledOnValidThread()); | |
91 CreateSharedBufferIfNeeded(); | |
92 return shared_buffer_handle_->Clone(); | |
93 } | |
94 | |
95 } // namespace device | |
OLD | NEW |