OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_SENSORS_DEVICE_SENSOR_SERVICE_H_ | |
6 #define DEVICE_SENSORS_DEVICE_SENSOR_SERVICE_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/callback_forward.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/shared_memory.h" | |
13 #include "base/memory/singleton.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/threading/thread_checker.h" | |
16 #include "device/sensors/device_sensor_export.h" | |
17 #include "device/sensors/device_sensors_consts.h" | |
18 #include "mojo/public/cpp/system/buffer.h" | |
19 | |
20 namespace device { | |
21 | |
22 class DataFetcherSharedMemory; | |
23 | |
24 // Owns the data fetcher for Device Motion and Orientation and keeps track of | |
25 // the number of consumers currently using the data. The data fetcher is stopped | |
26 // when there are no consumers. | |
27 class DEVICE_SENSOR_EXPORT DeviceSensorService | |
28 : public base::MessageLoop::DestructionObserver { | |
29 public: | |
30 // Returns the DeviceSensorService singleton. | |
31 static DeviceSensorService* GetInstance(); | |
32 | |
33 // Increments the number of users of the provider. The Provider is running | |
34 // when there's > 0 users, and is paused when the count drops to 0. | |
35 // Must be called on the I/O thread. | |
36 void AddConsumer(ConsumerType consumer_type); | |
37 | |
38 // Removes a consumer. Should be matched with an AddConsumer call. | |
39 // Must be called on the I/O thread. | |
40 void RemoveConsumer(ConsumerType cosumer_type); | |
41 | |
42 // Returns the shared memory handle of the device motion data. | |
43 mojo::ScopedSharedBufferHandle GetSharedMemoryHandle( | |
44 ConsumerType consumer_type); | |
45 | |
46 // base::MessageLoop::DestructionObserver: | |
47 void WillDestroyCurrentMessageLoop() override; | |
48 | |
49 // Stop/join with the background polling thread in |provider_|. | |
50 void Shutdown(); | |
51 | |
52 // Injects a custom data fetcher for testing purposes. This class takes | |
53 // ownership of the injected object. | |
54 void SetDataFetcherForTesting(DataFetcherSharedMemory* test_data_fetcher); | |
55 | |
56 private: | |
57 friend struct base::DefaultSingletonTraits<DeviceSensorService>; | |
58 | |
59 DeviceSensorService(); | |
60 ~DeviceSensorService() override; | |
61 | |
62 bool ChangeNumberConsumers(ConsumerType consumer_type, int delta); | |
63 int GetNumberConsumers(ConsumerType consumer_type) const; | |
64 | |
65 int num_motion_readers_; | |
66 int num_orientation_readers_; | |
67 int num_orientation_absolute_readers_; | |
68 bool is_shutdown_; | |
69 std::unique_ptr<DataFetcherSharedMemory> data_fetcher_; | |
70 base::ThreadChecker thread_checker_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(DeviceSensorService); | |
73 }; | |
74 | |
75 } // namespace device | |
76 | |
77 #endif // DEVICE_SENSORS_DEVICE_SENSOR_SERVICE_H_ | |
OLD | NEW |