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_DEVICE_INERTIAL_SENSOR_SERVICE_H_
6 #define CONTENT_BROWSER_DEVICE_SENSORS_DEVICE_INERTIAL_SENSOR_SERVICE_H_
8 #include "base/basictypes.h"
9 #include "base/callback_forward.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/shared_memory.h"
12 #include "base/memory/singleton.h"
13 #include "base/threading/thread_checker.h"
14 #include "content/browser/device_sensors/inertial_sensor_consts.h"
15 #include "content/common/content_export.h"
19 class DataFetcherSharedMemory
;
21 // Owns the data fetcher for Device Motion and Orientation and keeps track of
22 // the number of consumers currently using the data. The data fetcher is stopped
23 // when there are no consumers.
24 class CONTENT_EXPORT DeviceInertialSensorService
{
26 // Returns the DeviceInertialSensorService singleton.
27 static DeviceInertialSensorService
* GetInstance();
29 // Increments the number of users of the provider. The Provider is running
30 // when there's > 0 users, and is paused when the count drops to 0.
31 // Must be called on the I/O thread.
32 void AddConsumer(ConsumerType consumer_type
);
34 // Removes a consumer. Should be matched with an AddConsumer call.
35 // Must be called on the I/O thread.
36 void RemoveConsumer(ConsumerType cosumer_type
);
38 // Returns the shared memory handle of the device motion data duplicated
39 // into the given process.
40 base::SharedMemoryHandle
GetSharedMemoryHandleForProcess(
41 ConsumerType consumer_type
, base::ProcessHandle handle
);
43 // Stop/join with the background polling thread in |provider_|.
46 // Injects a custom data fetcher for testing purposes. This class takes
47 // ownership of the injected object.
48 void SetDataFetcherForTesting(DataFetcherSharedMemory
* test_data_fetcher
);
51 friend struct DefaultSingletonTraits
<DeviceInertialSensorService
>;
53 DeviceInertialSensorService();
54 virtual ~DeviceInertialSensorService();
56 bool ChangeNumberConsumers(ConsumerType consumer_type
,
58 int GetNumberConsumers(ConsumerType consumer_type
) const;
60 int num_light_readers_
;
61 int num_motion_readers_
;
62 int num_orientation_readers_
;
64 scoped_ptr
<DataFetcherSharedMemory
> data_fetcher_
;
65 base::ThreadChecker thread_checker_
;
67 DISALLOW_COPY_AND_ASSIGN(DeviceInertialSensorService
);
70 } // namespace content
72 #endif // CONTENT_BROWSER_DEVICE_SENSORS_DEVICE_INERTIAL_SENSOR_SERVICE_H_