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.
5 #ifndef CONTENT_BROWSER_DEVICE_SENSORS_DATA_FETCHER_SHARED_MEMORY_BASE_H_
6 #define CONTENT_BROWSER_DEVICE_SENSORS_DATA_FETCHER_SHARED_MEMORY_BASE_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/shared_memory.h"
12 #include "base/message_loop/message_loop.h"
13 #include "content/browser/device_sensors/inertial_sensor_consts.h"
14 #include "content/common/content_export.h"
18 // Sensor data fetchers should derive from this base class and implement
19 // the abstract Start() and Stop() methods.
20 // If the fetcher requires polling it should also implement IsPolling()
21 // to return true and the Fetch() method which will be called from the
22 // polling thread to fetch data at regular intervals.
23 class CONTENT_EXPORT DataFetcherSharedMemoryBase
{
25 // Starts updating the shared memory buffer with sensor data at
26 // regular intervals. Returns true if the relevant sensors could
27 // be successfully activated.
28 bool StartFetchingDeviceData(ConsumerType consumer_type
);
30 // Stops updating the shared memory buffer. Returns true if the
31 // relevant sensors could be successfully deactivated.
32 bool StopFetchingDeviceData(ConsumerType consumer_type
);
34 // Should be called before destruction to make sure all active
35 // sensors are unregistered.
36 void StopFetchingAllDeviceData();
38 // Returns the shared memory handle of the device sensor data
39 // duplicated into the given process. This method should only be
40 // called after a call to StartFetchingDeviceData method with
41 // corresponding |consumer_type| parameter.
42 base::SharedMemoryHandle
GetSharedMemoryHandleForProcess(
43 ConsumerType consumer_type
, base::ProcessHandle process
);
46 // Fetcher runs on the same thread as its creator.
48 // Fetcher runs on a separate thread calling |Fetch()| at regular intervals.
49 FETCHER_TYPE_POLLING_CALLBACK
,
50 // Fetcher runs on a separate thread, but no callbacks are executed.
51 FETCHER_TYPE_SEPARATE_THREAD
57 DataFetcherSharedMemoryBase();
58 virtual ~DataFetcherSharedMemoryBase();
60 // Returns the message loop of the polling thread.
61 // Returns NULL if there is no polling thread.
62 base::MessageLoop
* GetPollingMessageLoop() const;
64 // If IsPolling() is true this method is called from the |polling_thread_|
65 // at regular intervals.
66 virtual void Fetch(unsigned consumer_bitmask
);
68 // Returns the type of thread this fetcher runs on.
69 virtual FetcherType
GetType() const;
71 // Returns the sensor sampling interval. In particular if this fetcher
72 // GetType() == FETCHER_TYPE_POLLING_CALLBACK the interval between
73 // successive calls to Fetch().
74 virtual base::TimeDelta
GetInterval() const;
76 // Start() method should call InitSharedMemoryBuffer() to get the shared
77 // memory pointer. If IsPolling() is true both Start() and Stop() methods
78 // are called from the |polling_thread_|.
79 virtual bool Start(ConsumerType consumer_type
, void* buffer
) = 0;
80 virtual bool Stop(ConsumerType consumer_type
) = 0;
82 bool IsPollingTimerRunningForTesting() const;
85 bool InitAndStartPollingThreadIfNecessary();
86 base::SharedMemory
* GetSharedMemory(ConsumerType consumer_type
);
87 void* GetSharedMemoryBuffer(ConsumerType consumer_type
);
89 unsigned started_consumers_
;
91 scoped_ptr
<PollingThread
> polling_thread_
;
93 // Owning pointers. Objects in the map are deleted in dtor.
94 typedef std::map
<ConsumerType
, base::SharedMemory
*> SharedMemoryMap
;
95 SharedMemoryMap shared_memory_map_
;
97 DISALLOW_COPY_AND_ASSIGN(DataFetcherSharedMemoryBase
);
100 } // namespace content
102 #endif // CONTENT_BROWSER_DEVICE_SENSORS_DATA_FETCHER_SHARED_MEMORY_BASE_H_