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 namespace base { | 10 namespace base { |
| 11 template <typename T> |
| 12 struct DefaultSingletonTraits; |
11 class Thread; | 13 class Thread; |
12 } | 14 } |
13 | 15 |
14 namespace device { | 16 namespace device { |
15 | 17 |
16 struct SensorDataLinux; | 18 struct SensorDeviceLinux; |
17 class SensorReader; | 19 class SensorDeviceManager; |
| 20 class SensorDeviceService; |
18 | 21 |
19 class PlatformSensorProviderLinux : public PlatformSensorProvider { | 22 class DEVICE_GENERIC_SENSOR_EXPORT PlatformSensorProviderLinux |
| 23 : public PlatformSensorProvider { |
20 public: | 24 public: |
21 PlatformSensorProviderLinux(); | 25 using SensorAttachedCallback = base::Callback<void(mojom::SensorType)>; |
22 ~PlatformSensorProviderLinux() override; | |
23 | 26 |
24 static PlatformSensorProviderLinux* GetInstance(); | 27 static PlatformSensorProviderLinux* GetInstance(); |
25 | 28 |
| 29 // Provides SensorDeviceManager with a mock service used for testing, because |
| 30 // there might not be sensors on a testing system. |
| 31 void SetSensorDeviceServiceForTesting( |
| 32 std::unique_ptr<SensorDeviceService> device); |
| 33 |
26 protected: | 34 protected: |
| 35 ~PlatformSensorProviderLinux() override; |
| 36 |
27 void CreateSensorInternal(mojom::SensorType type, | 37 void CreateSensorInternal(mojom::SensorType type, |
28 mojo::ScopedSharedBufferMapping mapping, | 38 mojo::ScopedSharedBufferMapping mapping, |
29 const CreateSensorCallback& callback) override; | 39 const CreateSensorCallback& callback) override; |
30 | 40 |
31 void AllSensorsRemoved() override; | 41 void AllSensorsRemoved() override; |
32 | 42 |
33 void SetFileTaskRunner( | 43 void SetFileTaskRunner( |
34 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) override; | 44 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) override; |
35 | 45 |
36 private: | 46 private: |
37 void SensorReaderFound( | 47 friend struct base::DefaultSingletonTraits<PlatformSensorProviderLinux>; |
| 48 |
| 49 PlatformSensorProviderLinux(); |
| 50 |
| 51 void SensorDeviceFound( |
38 mojom::SensorType type, | 52 mojom::SensorType type, |
39 mojo::ScopedSharedBufferMapping mapping, | 53 mojo::ScopedSharedBufferMapping mapping, |
40 const PlatformSensorProviderBase::CreateSensorCallback& callback, | 54 const PlatformSensorProviderBase::CreateSensorCallback& callback, |
41 const SensorDataLinux& data, | 55 SensorDeviceLinux* sensor_device); |
42 std::unique_ptr<SensorReader> sensor_reader); | 56 |
| 57 bool StartPollingThread(); |
43 | 58 |
44 // Stops a polling thread if there are no sensors left. Must be called on | 59 // Stops a polling thread if there are no sensors left. Must be called on |
45 // a different that polling thread that allows io. | 60 // a different than the polling thread which allows I/O. |
46 void StopPollingThread(); | 61 void StopPollingThread(); |
47 | 62 |
48 // TODO(maksims): make a separate class Manager that will | 63 void InitializeSensorDeviceManager(); |
49 // create provide sensors with a polling task runner, check sensors existence | 64 |
50 // and notify provider if a new sensor has appeared and it can be created if a | 65 // A thread that is used by sensor readers in case of polling strategy. |
51 // request comes again for the same sensor. | |
52 // A use case example: a request for a sensor X comes, manager checks if the | |
53 // sensor exists on a platform and notifies a provider it is not found. | |
54 // The provider stores this information into its cache and doesn't try to | |
55 // create this specific sensor if a request comes. But when, for example, | |
56 // the sensor X is plugged into a usb port, the manager notices that and | |
57 // notifies the provider, which updates its cache and starts handling requests | |
58 // for the sensor X. | |
59 // | |
60 // Right now, this thread is used to find sensors files and poll data. | |
61 std::unique_ptr<base::Thread> polling_thread_; | 66 std::unique_ptr<base::Thread> polling_thread_; |
62 | 67 |
63 // A task runner that is passed to polling sensors to poll data. | 68 // This manager is being used to get |SensorDeviceLinux|, which represents |
64 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner_; | 69 // all the information of a concrete sensor provided by OS. |
| 70 std::unique_ptr<SensorDeviceManager> sensor_device_manager_; |
65 | 71 |
66 // Browser's file thread task runner passed from renderer. Used to | 72 // Browser's file thread task runner passed from renderer. Used by this |
67 // stop a polling thread. | 73 // provider to stop a polling thread and passed to a manager that |
| 74 // runs a linux device monitor service on this task runner. |
68 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 75 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
69 | 76 |
70 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderLinux); | 77 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderLinux); |
71 }; | 78 }; |
72 | 79 |
73 } // namespace device | 80 } // namespace device |
74 | 81 |
75 #endif // DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ | 82 #endif // DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ |
OLD | NEW |