• 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 <set>
9 
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/shared_memory.h"
14 #include "base/memory/singleton.h"
15 #include "base/threading/thread_checker.h"
16 #include "content/common/content_export.h"
17 
18 namespace blink {
19 class WebGamepad;
20 }
21 
22 namespace content {
23 
24 class GamepadConsumer;
25 class GamepadDataFetcher;
26 class GamepadProvider;
27 class GamepadServiceTestConstructor;
28 class RenderProcessHost;
29 
30 // Owns the GamepadProvider (the background polling thread) and keeps track of
31 // the number of consumers currently using the data (and pausing the provider
32 // when not in use).
33 class CONTENT_EXPORT GamepadService {
34  public:
35   // Returns the GamepadService singleton.
36   static GamepadService* GetInstance();
37 
38   // Increments the number of users of the provider. The Provider is running
39   // when there's > 0 users, and is paused when the count drops to 0.
40   // consumer is registered to listen for gamepad connections. If this is the
41   // first time it is added to the set of consumers it will be treated
42   // specially: it will not be informed about connections before a new user
43   // gesture is observed at which point it will be notified for every connected
44   // gamepads.
45   //
46   // Must be called on the I/O thread.
47   void ConsumerBecameActive(GamepadConsumer* consumer);
48 
49   // Decrements the number of users of the provider. consumer will not be
50   // informed about connections until it's added back via ConsumerBecameActive.
51   // Must be matched with a ConsumerBecameActive call.
52   //
53   // Must be called on the I/O thread.
54   void ConsumerBecameInactive(GamepadConsumer* consumer);
55 
56   // Decrements the number of users of the provider and removes consumer from
57   // the set of consumers. Should be matched with a a ConsumerBecameActive
58   // call.
59   //
60   // Must be called on the I/O thread.
61   void RemoveConsumer(GamepadConsumer* consumer);
62 
63   // Registers the given closure for calling when the user has interacted with
64   // the device. This callback will only be issued once. Should only be called
65   // while a consumer is active.
66   void RegisterForUserGesture(const base::Closure& closure);
67 
68   // Returns the shared memory handle of the gamepad data duplicated into the
69   // given process.
70   base::SharedMemoryHandle GetSharedMemoryHandleForProcess(
71       base::ProcessHandle handle);
72 
73   // Stop/join with the background thread in GamepadProvider |provider_|.
74   void Terminate();
75 
76   // Called on IO thread when a gamepad is connected.
77   void OnGamepadConnected(int index, const blink::WebGamepad& pad);
78 
79   // Called on IO thread when a gamepad is disconnected.
80   void OnGamepadDisconnected(int index, const blink::WebGamepad& pad);
81 
82  private:
83   friend struct DefaultSingletonTraits<GamepadService>;
84   friend class GamepadServiceTestConstructor;
85 
86   GamepadService();
87 
88   // Constructor for testing. This specifies the data fetcher to use for a
89   // provider, bypassing the default platform one.
90   GamepadService(scoped_ptr<GamepadDataFetcher> fetcher);
91 
92   virtual ~GamepadService();
93 
94   void OnUserGesture();
95 
96   struct ConsumerInfo {
97     ConsumerInfo(GamepadConsumer* consumer)
98         : consumer(consumer),
99           did_observe_user_gesture(false) {
100     }
101 
102     bool operator<(const ConsumerInfo& other) const {
103       return consumer < other.consumer;
104     }
105 
106     GamepadConsumer* consumer;
107     mutable bool is_active;
108     mutable bool did_observe_user_gesture;
109   };
110 
111   scoped_ptr<GamepadProvider> provider_;
112 
113   base::ThreadChecker thread_checker_;
114 
115   typedef std::set<ConsumerInfo> ConsumerSet;
116   ConsumerSet consumers_;
117 
118   int num_active_consumers_;
119 
120   bool gesture_callback_pending_;
121 
122   DISALLOW_COPY_AND_ASSIGN(GamepadService);
123 };
124 
125 }  // namespace content
126 
127 #endif  // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_
128