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