| 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 { | 18 namespace { |
| 19 GamepadService* g_gamepad_service = 0; | 19 GamepadService* g_gamepad_service = 0; |
| 20 } | 20 } |
| 21 | 21 |
| 22 GamepadService::GamepadService() | 22 GamepadService::GamepadService() |
| 23 : num_active_consumers_(0), | 23 : num_active_consumers_(0), |
| 24 gesture_callback_pending_(false) { | 24 gesture_callback_pending_(false) { |
| 25 SetInstance(); | 25 SetInstance(this); |
| 26 } | 26 } |
| 27 | 27 |
| 28 GamepadService::GamepadService(scoped_ptr<GamepadDataFetcher> fetcher) | 28 GamepadService::GamepadService(scoped_ptr<GamepadDataFetcher> fetcher) |
| 29 : provider_(new GamepadProvider(fetcher.Pass())), | 29 : provider_(new GamepadProvider(fetcher.Pass())), |
| 30 num_active_consumers_(0), | 30 num_active_consumers_(0), |
| 31 gesture_callback_pending_(false) { | 31 gesture_callback_pending_(false) { |
| 32 SetInstance(); | 32 SetInstance(this); |
| 33 thread_checker_.DetachFromThread(); | 33 thread_checker_.DetachFromThread(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 GamepadService::~GamepadService() { | 36 GamepadService::~GamepadService() { |
| 37 SetInstance(NULL); |
| 37 } | 38 } |
| 38 | 39 |
| 39 void GamepadService::SetInstance() { | 40 void GamepadService::SetInstance(GamepadService* instance) { |
| 40 CHECK(!g_gamepad_service); | 41 // Unit tests can create multiple instances but only one should exist at any |
| 41 g_gamepad_service = this; | 42 // given time so g_gamepad_service should only go from NULL to non-NULL and |
| 43 // vica versa. |
| 44 CHECK(!!instance != !!g_gamepad_service); |
| 45 g_gamepad_service = instance; |
| 42 } | 46 } |
| 43 | 47 |
| 44 GamepadService* GamepadService::GetInstance() { | 48 GamepadService* GamepadService::GetInstance() { |
| 45 if (!g_gamepad_service) | 49 if (!g_gamepad_service) |
| 46 g_gamepad_service = new GamepadService; | 50 g_gamepad_service = new GamepadService; |
| 47 return g_gamepad_service; | 51 return g_gamepad_service; |
| 48 } | 52 } |
| 49 | 53 |
| 50 void GamepadService::ConsumerBecameActive(GamepadConsumer* consumer) { | 54 void GamepadService::ConsumerBecameActive(GamepadConsumer* consumer) { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 55 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { | 151 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { |
| 148 const blink::WebGamepad& pad = gamepads.items[i]; | 152 const blink::WebGamepad& pad = gamepads.items[i]; |
| 149 if (pad.connected) | 153 if (pad.connected) |
| 150 info.consumer->OnGamepadConnected(i, pad); | 154 info.consumer->OnGamepadConnected(i, pad); |
| 151 } | 155 } |
| 152 } | 156 } |
| 153 } | 157 } |
| 154 } | 158 } |
| 155 | 159 |
| 156 } // namespace content | 160 } // namespace content |
| OLD | NEW |