| 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_DATA_FETCHER_SHARED_MEMORY_BASE_H_ | |
| 6 #define DEVICE_SENSORS_DATA_FETCHER_SHARED_MEMORY_BASE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/shared_memory.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "device/sensors/device_sensor_export.h" | |
| 15 #include "device/sensors/device_sensors_consts.h" | |
| 16 #include "mojo/public/cpp/system/buffer.h" | |
| 17 | |
| 18 namespace device { | |
| 19 | |
| 20 // Sensor data fetchers should derive from this base class and implement | |
| 21 // the abstract Start() and Stop() methods. | |
| 22 // If the fetcher requires polling it should also implement IsPolling() | |
| 23 // to return true and the Fetch() method which will be called from the | |
| 24 // polling thread to fetch data at regular intervals. | |
| 25 class DEVICE_SENSOR_EXPORT DataFetcherSharedMemoryBase { | |
| 26 public: | |
| 27 // Starts updating the shared memory buffer with sensor data at | |
| 28 // regular intervals. Returns true if the relevant sensors could | |
| 29 // be successfully activated. | |
| 30 bool StartFetchingDeviceData(ConsumerType consumer_type); | |
| 31 | |
| 32 // Stops updating the shared memory buffer. Returns true if the | |
| 33 // relevant sensors could be successfully deactivated. | |
| 34 bool StopFetchingDeviceData(ConsumerType consumer_type); | |
| 35 | |
| 36 // Should be called before destruction to make sure all active | |
| 37 // sensors are unregistered. | |
| 38 virtual void Shutdown(); | |
| 39 | |
| 40 // Returns the shared memory handle of the device sensor data. This method | |
| 41 // should only be called after a call to StartFetchingDeviceData method with | |
| 42 // corresponding |consumer_type| parameter. | |
| 43 mojo::ScopedSharedBufferHandle GetSharedMemoryHandle( | |
| 44 ConsumerType consumer_type); | |
| 45 | |
| 46 enum FetcherType { | |
| 47 // Fetcher runs on the same thread as its creator. | |
| 48 FETCHER_TYPE_DEFAULT, | |
| 49 // Fetcher runs on a separate thread calling |Fetch()| at regular intervals. | |
| 50 FETCHER_TYPE_POLLING_CALLBACK, | |
| 51 // Fetcher runs on a separate thread, but no callbacks are executed. | |
| 52 FETCHER_TYPE_SEPARATE_THREAD | |
| 53 }; | |
| 54 | |
| 55 protected: | |
| 56 class PollingThread; | |
| 57 | |
| 58 DataFetcherSharedMemoryBase(); | |
| 59 virtual ~DataFetcherSharedMemoryBase(); | |
| 60 | |
| 61 // Returns the message loop of the polling thread. | |
| 62 // Returns NULL if there is no polling thread. | |
| 63 base::MessageLoop* GetPollingMessageLoop() const; | |
| 64 | |
| 65 // If IsPolling() is true this method is called from the |polling_thread_| | |
| 66 // at regular intervals. | |
| 67 virtual void Fetch(unsigned consumer_bitmask); | |
| 68 | |
| 69 // Returns the type of thread this fetcher runs on. | |
| 70 virtual FetcherType GetType() const; | |
| 71 | |
| 72 // Returns the sensor sampling interval. In particular if this fetcher | |
| 73 // GetType() == FETCHER_TYPE_POLLING_CALLBACK the interval between | |
| 74 // successive calls to Fetch(). | |
| 75 virtual base::TimeDelta GetInterval() const; | |
| 76 | |
| 77 // Start() method should call InitSharedMemoryBuffer() to get the shared | |
| 78 // memory pointer. If IsPolling() is true both Start() and Stop() methods | |
| 79 // are called from the |polling_thread_|. | |
| 80 virtual bool Start(ConsumerType consumer_type, void* buffer) = 0; | |
| 81 virtual bool Stop(ConsumerType consumer_type) = 0; | |
| 82 | |
| 83 bool IsPollingTimerRunningForTesting() const; | |
| 84 | |
| 85 private: | |
| 86 bool InitAndStartPollingThreadIfNecessary(); | |
| 87 void* GetSharedMemoryBuffer(ConsumerType consumer_type); | |
| 88 | |
| 89 unsigned started_consumers_; | |
| 90 | |
| 91 std::unique_ptr<PollingThread> polling_thread_; | |
| 92 | |
| 93 using SharedMemoryMap = std::map<ConsumerType, | |
| 94 std::pair<mojo::ScopedSharedBufferHandle, | |
| 95 mojo::ScopedSharedBufferMapping>>; | |
| 96 SharedMemoryMap shared_memory_map_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(DataFetcherSharedMemoryBase); | |
| 99 }; | |
| 100 | |
| 101 } // namespace device | |
| 102 | |
| 103 #endif // DEVICE_SENSORS_DATA_FETCHER_SHARED_MEMORY_BASE_H_ | |
| OLD | NEW |