OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/gamepad/gamepad_service.h" | 5 #include "content/browser/gamepad/gamepad_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
10 #include "content/browser/gamepad/gamepad_consumer.h" | 10 #include "content/browser/gamepad/gamepad_consumer.h" |
11 #include "content/browser/gamepad/gamepad_data_fetcher.h" | 11 #include "content/browser/gamepad/gamepad_data_fetcher.h" |
12 #include "content/browser/gamepad/gamepad_provider.h" | 12 #include "content/browser/gamepad/gamepad_provider.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
| 18 namespace { |
| 19 GamepadService* g_gamepad_service = 0; |
| 20 } |
| 21 |
18 GamepadService::GamepadService() | 22 GamepadService::GamepadService() |
19 : num_active_consumers_(0), | 23 : num_active_consumers_(0), |
20 gesture_callback_pending_(false) { | 24 gesture_callback_pending_(false) { |
| 25 SetInstance(); |
21 } | 26 } |
22 | 27 |
23 GamepadService::GamepadService(scoped_ptr<GamepadDataFetcher> fetcher) | 28 GamepadService::GamepadService(scoped_ptr<GamepadDataFetcher> fetcher) |
24 : provider_(new GamepadProvider(fetcher.Pass())), | 29 : provider_(new GamepadProvider(fetcher.Pass())), |
25 num_active_consumers_(0), | 30 num_active_consumers_(0), |
26 gesture_callback_pending_(false) { | 31 gesture_callback_pending_(false) { |
| 32 SetInstance(); |
27 thread_checker_.DetachFromThread(); | 33 thread_checker_.DetachFromThread(); |
28 } | 34 } |
29 | 35 |
30 GamepadService::~GamepadService() { | 36 GamepadService::~GamepadService() { |
31 } | 37 } |
32 | 38 |
| 39 void GamepadService::SetInstance() { |
| 40 CHECK(!g_gamepad_service); |
| 41 g_gamepad_service = this; |
| 42 } |
| 43 |
33 GamepadService* GamepadService::GetInstance() { | 44 GamepadService* GamepadService::GetInstance() { |
34 return Singleton<GamepadService, | 45 if (!g_gamepad_service) |
35 LeakySingletonTraits<GamepadService> >::get(); | 46 g_gamepad_service = new GamepadService; |
| 47 return g_gamepad_service; |
36 } | 48 } |
37 | 49 |
38 void GamepadService::ConsumerBecameActive(GamepadConsumer* consumer) { | 50 void GamepadService::ConsumerBecameActive(GamepadConsumer* consumer) { |
39 DCHECK(thread_checker_.CalledOnValidThread()); | 51 DCHECK(thread_checker_.CalledOnValidThread()); |
40 | 52 |
41 if (!provider_) | 53 if (!provider_) |
42 provider_.reset(new GamepadProvider); | 54 provider_.reset(new GamepadProvider); |
43 | 55 |
44 std::pair<ConsumerSet::iterator, bool> insert_result = | 56 std::pair<ConsumerSet::iterator, bool> insert_result = |
45 consumers_.insert(consumer); | 57 consumers_.insert(consumer); |
46 insert_result.first->is_active = true; | 58 insert_result.first->is_active = true; |
47 if (!insert_result.first->did_observe_user_gesture && | 59 if (!insert_result.first->did_observe_user_gesture && |
48 !gesture_callback_pending_) { | 60 !gesture_callback_pending_) { |
| 61 gesture_callback_pending_ = true; |
49 provider_->RegisterForUserGesture( | 62 provider_->RegisterForUserGesture( |
50 base::Bind(&GamepadService::OnUserGesture, | 63 base::Bind(&GamepadService::OnUserGesture, |
51 base::Unretained(this))); | 64 base::Unretained(this))); |
52 } | 65 } |
53 | 66 |
54 if (num_active_consumers_++ == 0) | 67 if (num_active_consumers_++ == 0) |
55 provider_->Resume(); | 68 provider_->Resume(); |
56 } | 69 } |
57 | 70 |
58 void GamepadService::ConsumerBecameInactive(GamepadConsumer* consumer) { | 71 void GamepadService::ConsumerBecameInactive(GamepadConsumer* consumer) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { | 147 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { |
135 const blink::WebGamepad& pad = gamepads.items[i]; | 148 const blink::WebGamepad& pad = gamepads.items[i]; |
136 if (pad.connected) | 149 if (pad.connected) |
137 info.consumer->OnGamepadConnected(i, pad); | 150 info.consumer->OnGamepadConnected(i, pad); |
138 } | 151 } |
139 } | 152 } |
140 } | 153 } |
141 } | 154 } |
142 | 155 |
143 } // namespace content | 156 } // namespace content |
OLD | NEW |