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