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_PLATFORM_SENSOR_READER_WIN_H_ | |
6 #define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_WIN_H_ | |
7 | |
8 #include <SensorsApi.h> | |
9 | |
10 #include "base/win/scoped_comptr.h" | |
11 #include "device/generic_sensor/public/interfaces/sensor.mojom.h" | |
12 | |
13 namespace device { | |
14 | |
15 class PlatformSensorConfiguration; | |
16 struct ReaderInitParams; | |
17 struct SensorReading; | |
18 | |
19 // Generic class that uses ISensor interface to fetch sensor data. Used | |
20 // by PlatformSensorWin and delivers notifications via Client interface. | |
21 // Instances of this class must be created and destructed on the same thread. | |
22 class PlatformSensorReaderWin { | |
23 public: | |
24 // Client interface that can be used to receive notifications about sensor | |
25 // error or data change events. | |
26 class Client { | |
27 public: | |
28 virtual void OnReadingUpdated(const SensorReading& reading) = 0; | |
29 virtual void OnSensorError() = 0; | |
30 | |
31 protected: | |
32 virtual ~Client() {} | |
33 }; | |
34 | |
35 static std::unique_ptr<PlatformSensorReaderWin> Create( | |
36 mojom::SensorType type, | |
37 base::win::ScopedComPtr<ISensorManager> sensor_manager); | |
38 | |
39 // Following methods are thread safe. | |
40 void SetClient(Client* client); | |
41 unsigned long GetMinimalReportingIntervalMs() const; | |
42 bool StartSensor(const PlatformSensorConfiguration& configuration); | |
43 void StopSensor(); | |
44 | |
45 // Must be destructed on the same thread that was used during construction. | |
46 ~PlatformSensorReaderWin(); | |
47 | |
48 private: | |
49 PlatformSensorReaderWin(base::win::ScopedComPtr<ISensor> sensor, | |
50 std::unique_ptr<ReaderInitParams> params); | |
51 | |
52 static base::win::ScopedComPtr<ISensor> GetSensorForType( | |
53 REFSENSOR_TYPE_ID sensor_type, | |
54 base::win::ScopedComPtr<ISensorManager> sensor_manager); | |
55 | |
56 bool SetReportingInterval(const PlatformSensorConfiguration& configuration); | |
57 void ListenSensorEvent(); | |
58 HRESULT SensorReadingChanged(ISensorDataReport* report, | |
59 SensorReading* reading) const; | |
60 void SensorError(); | |
61 | |
62 private: | |
63 friend class EventListener; | |
64 | |
65 const std::unique_ptr<ReaderInitParams> init_params_; | |
66 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
67 // Following class members are protected by lock, because SetClient, | |
68 // StartSensor and StopSensor are called from another thread by | |
69 // PlatformSensorWin that can modify internal state of the object. | |
70 base::Lock lock_; | |
71 bool sensor_active_; | |
72 Client* client_; | |
73 base::win::ScopedComPtr<ISensor> sensor_; | |
74 scoped_refptr<EventListener> event_listener_; | |
75 base::WeakPtrFactory<PlatformSensorReaderWin> weak_factory_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(PlatformSensorReaderWin); | |
78 }; | |
79 | |
80 } // namespace device | |
81 | |
82 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_WIN_H_ | |
OLD | NEW |