Rename Animate as Begin(Main)Frame
[chromium-blink-merge.git] / content / browser / gamepad / gamepad_service.h
blob726558936af07214df05af83ea55d1fefb2594f8
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.
5 #ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H
6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H
8 #include <set>
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"
18 namespace blink {
19 class WebGamepad;
22 namespace content {
24 class GamepadConsumer;
25 class GamepadDataFetcher;
26 class GamepadProvider;
27 class GamepadServiceTestConstructor;
28 class RenderProcessHost;
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();
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.
46 // Must be called on the I/O thread.
47 void ConsumerBecameActive(GamepadConsumer* consumer);
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.
53 // Must be called on the I/O thread.
54 void ConsumerBecameInactive(GamepadConsumer* consumer);
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.
60 // Must be called on the I/O thread.
61 void RemoveConsumer(GamepadConsumer* consumer);
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);
68 // Returns the shared memory handle of the gamepad data duplicated into the
69 // given process.
70 base::SharedMemoryHandle GetSharedMemoryHandleForProcess(
71 base::ProcessHandle handle);
73 // Stop/join with the background thread in GamepadProvider |provider_|.
74 void Terminate();
76 // Called on IO thread when a gamepad is connected.
77 void OnGamepadConnected(int index, const blink::WebGamepad& pad);
79 // Called on IO thread when a gamepad is disconnected.
80 void OnGamepadDisconnected(int index, const blink::WebGamepad& pad);
82 private:
83 friend struct DefaultSingletonTraits<GamepadService>;
84 friend class GamepadServiceTestConstructor;
85 friend class GamepadServiceTest;
87 GamepadService();
89 // Constructor for testing. This specifies the data fetcher to use for a
90 // provider, bypassing the default platform one.
91 GamepadService(scoped_ptr<GamepadDataFetcher> fetcher);
93 virtual ~GamepadService();
95 static void SetInstance(GamepadService*);
97 void OnUserGesture();
99 struct ConsumerInfo {
100 ConsumerInfo(GamepadConsumer* consumer)
101 : consumer(consumer),
102 did_observe_user_gesture(false) {
105 bool operator<(const ConsumerInfo& other) const {
106 return consumer < other.consumer;
109 GamepadConsumer* consumer;
110 mutable bool is_active;
111 mutable bool did_observe_user_gesture;
114 scoped_ptr<GamepadProvider> provider_;
116 base::ThreadChecker thread_checker_;
118 typedef std::set<ConsumerInfo> ConsumerSet;
119 ConsumerSet consumers_;
121 int num_active_consumers_;
123 bool gesture_callback_pending_;
125 DISALLOW_COPY_AND_ASSIGN(GamepadService);
128 } // namespace content
130 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_