1 // Copyright 2013 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 CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_INERTIAL_SENSOR_SERVICE_H_ 6 #define CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_INERTIAL_SENSOR_SERVICE_H_ 7 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_orientation/inertial_sensor_consts.h" 15 #include "content/common/content_export.h" 16 17 namespace content { 18 19 class DataFetcherSharedMemory; 20 class RenderProcessHost; 21 22 // Owns the DeviceMotionProvider (the background polling thread) and keeps 23 // track of the number of consumers currently using the data (and pausing 24 // the provider when not in use). 25 class CONTENT_EXPORT DeviceInertialSensorService { 26 public: 27 // Returns the DeviceInertialSensorService singleton. 28 static DeviceInertialSensorService* GetInstance(); 29 30 // Increments the number of users of the provider. The Provider is running 31 // when there's > 0 users, and is paused when the count drops to 0. 32 // Must be called on the I/O thread. 33 void AddConsumer(ConsumerType consumer_type); 34 35 // Removes a consumer. Should be matched with an AddConsumer call. 36 // Must be called on the I/O thread. 37 void RemoveConsumer(ConsumerType cosumer_type); 38 39 // Returns the shared memory handle of the device motion data duplicated 40 // into the given process. 41 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( 42 ConsumerType consumer_type, base::ProcessHandle handle); 43 44 // Stop/join with the background polling thread in |provider_|. 45 void Shutdown(); 46 47 // Injects a custom data fetcher for testing purposes. This class takes 48 // ownership of the injected object. 49 void SetDataFetcherForTests(DataFetcherSharedMemory* test_data_fetcher); 50 51 private: 52 friend struct DefaultSingletonTraits<DeviceInertialSensorService>; 53 54 DeviceInertialSensorService(); 55 virtual ~DeviceInertialSensorService(); 56 57 bool ChangeNumberConsumers(ConsumerType consumer_type, 58 int delta); 59 int GetNumberConsumers(ConsumerType consumer_type) const; 60 61 int num_motion_readers_; 62 int num_orientation_readers_; 63 bool is_shutdown_; 64 scoped_ptr<DataFetcherSharedMemory> data_fetcher_; 65 base::ThreadChecker thread_checker_; 66 67 DISALLOW_COPY_AND_ASSIGN(DeviceInertialSensorService); 68 }; 69 70 } // namespace content 71 72 #endif // CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_INERTIAL_SENSOR_SERVICE_H_ 73