• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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_GAMEPAD_GAMEPAD_SERVICE_H
6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_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/common/content_export.h"
15 
16 namespace content {
17 
18 class GamepadDataFetcher;
19 class GamepadProvider;
20 class GamepadServiceTestConstructor;
21 class RenderProcessHost;
22 
23 // Owns the GamepadProvider (the background polling thread) and keeps track of
24 // the number of consumers currently using the data (and pausing the provider
25 // when not in use).
26 class CONTENT_EXPORT GamepadService {
27  public:
28   // Returns the GamepadService singleton.
29   static GamepadService* GetInstance();
30 
31   // 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.
33   //
34   // Must be called on the I/O thread.
35   void AddConsumer();
36 
37   // Removes a consumer. Should be matched with an AddConsumer call.
38   //
39   // Must be called on the I/O thread.
40   void RemoveConsumer();
41 
42   // Registers the given closure for calling when the user has interacted with
43   // the device. This callback will only be issued once. Should only be called
44   // while a consumer is active.
45   void RegisterForUserGesture(const base::Closure& closure);
46 
47   // Returns the shared memory handle of the gamepad data duplicated into the
48   // given process.
49   base::SharedMemoryHandle GetSharedMemoryHandleForProcess(
50       base::ProcessHandle handle);
51 
52   // Stop/join with the background thread in GamepadProvider |provider_|.
53   void Terminate();
54 
55  private:
56   friend struct DefaultSingletonTraits<GamepadService>;
57   friend class GamepadServiceTestConstructor;
58 
59   GamepadService();
60 
61   // Constructor for testing. This specifies the data fetcher to use for a
62   // provider, bypassing the default platform one.
63   GamepadService(scoped_ptr<GamepadDataFetcher> fetcher);
64 
65   virtual ~GamepadService();
66 
67   int num_readers_;
68   scoped_ptr<GamepadProvider> provider_;
69 
70   base::ThreadChecker thread_checker_;
71 
72   DISALLOW_COPY_AND_ASSIGN(GamepadService);
73 };
74 
75 }  // namespace content
76 
77 #endif  // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_
78