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 #ifndef DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ | |
6 #define DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ | |
7 | |
8 #include "device/generic_sensor/platform_sensor_provider.h" | |
9 | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "device/generic_sensor/generic_sensor_export.h" | |
12 #include "device/generic_sensor/linux/sensor_device_manager.h" | |
13 | |
14 namespace base { | |
15 template <typename T> | |
16 struct DefaultSingletonTraits; | |
17 class Thread; | |
18 } | |
19 | |
20 namespace device { | |
21 | |
22 struct SensorInfoLinux; | |
23 | |
24 class DEVICE_GENERIC_SENSOR_EXPORT PlatformSensorProviderLinux | |
25 : public PlatformSensorProvider, | |
26 public SensorDeviceManager::Delegate { | |
27 public: | |
28 static PlatformSensorProviderLinux* GetInstance(); | |
29 | |
30 // Sets another service provided by tests. | |
31 void SetSensorDeviceManagerForTesting( | |
32 std::unique_ptr<SensorDeviceManager> sensor_device_manager); | |
33 | |
34 // Sets task runner for tests. | |
35 void SetFileTaskRunnerForTesting( | |
36 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
37 | |
38 protected: | |
39 ~PlatformSensorProviderLinux() override; | |
40 | |
41 void CreateSensorInternal(mojom::SensorType type, | |
42 mojo::ScopedSharedBufferMapping mapping, | |
43 const CreateSensorCallback& callback) override; | |
44 | |
45 void AllSensorsRemoved() override; | |
46 | |
47 void SetFileTaskRunner( | |
48 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) override; | |
49 | |
50 private: | |
51 friend struct base::DefaultSingletonTraits<PlatformSensorProviderLinux>; | |
52 | |
53 using SensorDeviceMap = | |
54 std::unordered_map<mojom::SensorType, std::unique_ptr<SensorInfoLinux>>; | |
55 | |
56 PlatformSensorProviderLinux(); | |
57 | |
58 void SensorDeviceFound( | |
59 mojom::SensorType type, | |
60 mojo::ScopedSharedBufferMapping mapping, | |
61 const PlatformSensorProviderBase::CreateSensorCallback& callback, | |
62 const SensorInfoLinux* sensor_device); | |
63 | |
64 bool StartPollingThread(); | |
65 | |
66 // Stops a polling thread if there are no sensors left. Must be called on | |
67 // a different than the polling thread which allows I/O. | |
68 void StopPollingThread(); | |
69 | |
70 // Shuts down a service that tracks events from iio subsystem. | |
71 void Shutdown(); | |
72 | |
73 // Returns SensorInfoLinux structure of a requested type. | |
74 // If a request cannot be processed immediately, returns nullptr and | |
75 // all the requests stored in |requests_map_| are processed after | |
76 // enumeration is ready. | |
77 SensorInfoLinux* GetSensorDevice(mojom::SensorType type); | |
78 | |
79 // Returns all found iio devices. Currently not implemented. | |
80 void GetAllSensorDevices(); | |
81 | |
82 // Processed stored requests in |request_map_|. | |
83 void ProcessStoredRequests(); | |
84 | |
85 // Called when sensors are created asynchronously after enumeration is done. | |
86 void CreateSensorAndNotify(mojom::SensorType type, | |
87 SensorInfoLinux* sensor_device); | |
88 | |
89 // SensorDeviceManager::Delegate implements: | |
90 void OnSensorNodesEnumerated() override; | |
91 void OnDeviceAdded(mojom::SensorType type, | |
92 std::unique_ptr<SensorInfoLinux> sensor_device) override; | |
93 void OnDeviceRemoved(mojom::SensorType type, | |
94 const std::string& device_node) override; | |
95 | |
96 // Set to true when enumeration is ready. | |
97 bool sensor_nodes_enumerated_; | |
98 | |
99 // Set to true when |sensor_device_manager_| has already started enumeration. | |
100 bool sensor_nodes_enumeration_started_; | |
101 | |
102 // Stores all available sensor devices by type. | |
103 SensorDeviceMap sensor_devices_by_type_; | |
104 | |
105 // A thread that is used by sensor readers in case of polling strategy. | |
106 std::unique_ptr<base::Thread> polling_thread_; | |
107 | |
108 // This manager is being used to get |SensorInfoLinux|, which represents | |
109 // all the information of a concrete sensor provided by OS. | |
110 std::unique_ptr<SensorDeviceManager> sensor_device_manager_; | |
111 | |
112 // Browser's file thread task runner passed from renderer. Used by this | |
113 // provider to stop a polling thread and passed to a manager that | |
114 // runs a linux device monitor service on this task runner. | |
115 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderLinux); | |
118 }; | |
119 | |
120 } // namespace device | |
121 | |
122 #endif // DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ | |
OLD | NEW |