Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / gamepad / gamepad_service.h
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   friend class GamepadServiceTest;
86
87   GamepadService();
88
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);
92
93   virtual ~GamepadService();
94
95   static void SetInstance(GamepadService*);
96
97   void OnUserGesture();
98
99   struct ConsumerInfo {
100     ConsumerInfo(GamepadConsumer* consumer)
101         : consumer(consumer),
102           did_observe_user_gesture(false) {
103     }
104
105     bool operator<(const ConsumerInfo& other) const {
106       return consumer < other.consumer;
107     }
108
109     GamepadConsumer* consumer;
110     mutable bool is_active;
111     mutable bool did_observe_user_gesture;
112   };
113
114   scoped_ptr<GamepadProvider> provider_;
115
116   base::ThreadChecker thread_checker_;
117
118   typedef std::set<ConsumerInfo> ConsumerSet;
119   ConsumerSet consumers_;
120
121   int num_active_consumers_;
122
123   bool gesture_callback_pending_;
124
125   DISALLOW_COPY_AND_ASSIGN(GamepadService);
126 };
127
128 }  // namespace content
129
130 #endif  // CONTENT_BROWSER_GAMEPAD_GAMEPAD_SERVICE_H_