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/sensors/platform_sensor_provider.h" | |
6 | |
7 namespace device { | |
8 | |
9 // static | |
10 PlatformSensorProvider* PlatformSensorProvider::Create( | |
11 uint64_t shared_buffer_size) { | |
12 // TODO(shalamov): Add platform-specific PlatformSensorProvider | |
13 // implementations here. | |
14 return nullptr; | |
15 } | |
16 | |
17 PlatformSensorProvider::PlatformSensorProvider(uint64_t shared_buffer_size) | |
18 : shared_buffer_size_(shared_buffer_size) {} | |
19 | |
20 PlatformSensorProvider::~PlatformSensorProvider() = default; | |
21 | |
22 scoped_refptr<PlatformSensor> PlatformSensorProvider::CreateSensor( | |
23 SensorType type, | |
24 uint64_t size, | |
25 uint64_t offset) { | |
26 DCHECK(CalledOnValidThread()); | |
27 | |
28 if (!CreateSharedBufferIfNeeded()) | |
29 return nullptr; | |
30 | |
31 mojo::ScopedSharedBufferMapping mapping = | |
32 shared_buffer_handle_->MapAtOffset(size, offset); | |
33 if (!mapping) | |
34 return nullptr; | |
35 | |
36 // Create new platform sensor instance. | |
37 auto new_sensor = CreateSensor(type, std::move(mapping), size); | |
dcheng
2016/07/01 09:17:24
Note the current guidance is not to use auto to ho
shalamov
2016/07/01 14:22:47
Done.
| |
38 if (!new_sensor) | |
39 return nullptr; | |
40 | |
41 const auto& add_result = | |
42 sensor_map_.insert(std::make_pair(type, new_sensor.get())); | |
43 DCHECK(add_result.second); | |
44 | |
45 return add_result.first->second; | |
dcheng
2016/07/01 09:17:24
return new_sensor is slightly more efficient here.
shalamov
2016/07/01 14:22:47
Done.
| |
46 } | |
47 | |
48 scoped_refptr<PlatformSensor> PlatformSensorProvider::GetSensor( | |
49 SensorType type) { | |
50 DCHECK(CalledOnValidThread()); | |
51 | |
52 auto it = sensor_map_.find(type); | |
53 if (it != sensor_map_.end()) | |
54 return it->second; | |
55 return nullptr; | |
56 } | |
57 | |
58 bool PlatformSensorProvider::CreateSharedBufferIfNeeded() { | |
59 DCHECK(CalledOnValidThread()); | |
60 if (shared_buffer_handle_.is_valid()) | |
61 return true; | |
62 | |
63 return mojo::CreateSharedBuffer(nullptr, shared_buffer_size_, | |
64 &shared_buffer_handle_) == MOJO_RESULT_OK; | |
65 } | |
66 | |
67 void PlatformSensorProvider::RemoveSensor(SensorType type) { | |
68 DCHECK(CalledOnValidThread()); | |
69 auto it = sensor_map_.find(type); | |
70 if (it != sensor_map_.end()) | |
71 sensor_map_.erase(it); | |
72 | |
73 if (sensor_map_.empty() && shared_buffer_handle_->is_valid()) | |
74 shared_buffer_handle_.reset(); | |
75 } | |
76 | |
77 mojo::ScopedSharedBufferHandle | |
78 PlatformSensorProvider::GetClonedSharedBufferHandle() { | |
79 DCHECK(CalledOnValidThread()); | |
80 CreateSharedBufferIfNeeded(); | |
81 return shared_buffer_handle_->Clone(); | |
82 } | |
83 | |
84 } // namespace device | |
OLD | NEW |