| OLD | NEW |
| (Empty) |
| 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 #include "content/browser/gamepad/gamepad_service.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/memory/singleton.h" | |
| 13 #include "content/browser/gamepad/gamepad_shared_buffer_impl.h" | |
| 14 #include "content/common/gamepad_hardware_buffer.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/render_process_host.h" | |
| 17 #include "device/gamepad/gamepad_consumer.h" | |
| 18 #include "device/gamepad/gamepad_data_fetcher.h" | |
| 19 #include "device/gamepad/gamepad_provider.h" | |
| 20 #include "mojo/public/cpp/system/platform_handle.h" | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 namespace { | |
| 25 GamepadService* g_gamepad_service = 0; | |
| 26 } | |
| 27 | |
| 28 GamepadService::GamepadService() | |
| 29 : num_active_consumers_(0), | |
| 30 gesture_callback_pending_(false) { | |
| 31 SetInstance(this); | |
| 32 } | |
| 33 | |
| 34 GamepadService::GamepadService( | |
| 35 std::unique_ptr<device::GamepadDataFetcher> fetcher) | |
| 36 : provider_(new device::GamepadProvider( | |
| 37 base::MakeUnique<GamepadSharedBufferImpl>(), this, std::move(fetcher))), | |
| 38 num_active_consumers_(0), | |
| 39 gesture_callback_pending_(false) { | |
| 40 SetInstance(this); | |
| 41 thread_checker_.DetachFromThread(); | |
| 42 } | |
| 43 | |
| 44 GamepadService::~GamepadService() { | |
| 45 SetInstance(NULL); | |
| 46 } | |
| 47 | |
| 48 void GamepadService::SetInstance(GamepadService* instance) { | |
| 49 // Unit tests can create multiple instances but only one should exist at any | |
| 50 // given time so g_gamepad_service should only go from NULL to non-NULL and | |
| 51 // vica versa. | |
| 52 CHECK(!!instance != !!g_gamepad_service); | |
| 53 g_gamepad_service = instance; | |
| 54 } | |
| 55 | |
| 56 GamepadService* GamepadService::GetInstance() { | |
| 57 if (!g_gamepad_service) | |
| 58 g_gamepad_service = new GamepadService; | |
| 59 return g_gamepad_service; | |
| 60 } | |
| 61 | |
| 62 void GamepadService::ConsumerBecameActive(device::GamepadConsumer* consumer) { | |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 64 | |
| 65 if (!provider_) | |
| 66 provider_.reset(new device::GamepadProvider( | |
| 67 base::MakeUnique<GamepadSharedBufferImpl>(), this)); | |
| 68 | |
| 69 std::pair<ConsumerSet::iterator, bool> insert_result = | |
| 70 consumers_.insert(consumer); | |
| 71 insert_result.first->is_active = true; | |
| 72 if (!insert_result.first->did_observe_user_gesture && | |
| 73 !gesture_callback_pending_) { | |
| 74 gesture_callback_pending_ = true; | |
| 75 provider_->RegisterForUserGesture( | |
| 76 base::Bind(&GamepadService::OnUserGesture, | |
| 77 base::Unretained(this))); | |
| 78 } | |
| 79 | |
| 80 if (num_active_consumers_++ == 0) | |
| 81 provider_->Resume(); | |
| 82 } | |
| 83 | |
| 84 void GamepadService::ConsumerBecameInactive(device::GamepadConsumer* consumer) { | |
| 85 DCHECK(provider_); | |
| 86 DCHECK(num_active_consumers_ > 0); | |
| 87 DCHECK(consumers_.count(consumer) > 0); | |
| 88 DCHECK(consumers_.find(consumer)->is_active); | |
| 89 | |
| 90 consumers_.find(consumer)->is_active = false; | |
| 91 if (--num_active_consumers_ == 0) | |
| 92 provider_->Pause(); | |
| 93 } | |
| 94 | |
| 95 void GamepadService::RemoveConsumer(device::GamepadConsumer* consumer) { | |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 97 | |
| 98 ConsumerSet::iterator it = consumers_.find(consumer); | |
| 99 if (it->is_active && --num_active_consumers_ == 0) | |
| 100 provider_->Pause(); | |
| 101 consumers_.erase(it); | |
| 102 } | |
| 103 | |
| 104 void GamepadService::RegisterForUserGesture(const base::Closure& closure) { | |
| 105 DCHECK(consumers_.size() > 0); | |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 107 provider_->RegisterForUserGesture(closure); | |
| 108 } | |
| 109 | |
| 110 void GamepadService::Terminate() { | |
| 111 provider_.reset(); | |
| 112 } | |
| 113 | |
| 114 void GamepadService::OnGamepadConnectionChange(bool connected, | |
| 115 int index, | |
| 116 const blink::WebGamepad& pad) { | |
| 117 if (connected) { | |
| 118 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 119 base::Bind(&GamepadService::OnGamepadConnected, | |
| 120 base::Unretained(this), index, pad)); | |
| 121 } else { | |
| 122 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 123 base::Bind(&GamepadService::OnGamepadDisconnected, | |
| 124 base::Unretained(this), index, pad)); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void GamepadService::OnGamepadConnected( | |
| 129 int index, | |
| 130 const blink::WebGamepad& pad) { | |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 132 | |
| 133 for (ConsumerSet::iterator it = consumers_.begin(); | |
| 134 it != consumers_.end(); ++it) { | |
| 135 if (it->did_observe_user_gesture && it->is_active) | |
| 136 it->consumer->OnGamepadConnected(index, pad); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void GamepadService::OnGamepadDisconnected( | |
| 141 int index, | |
| 142 const blink::WebGamepad& pad) { | |
| 143 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 144 | |
| 145 for (ConsumerSet::iterator it = consumers_.begin(); | |
| 146 it != consumers_.end(); ++it) { | |
| 147 if (it->did_observe_user_gesture && it->is_active) | |
| 148 it->consumer->OnGamepadDisconnected(index, pad); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 base::SharedMemoryHandle GamepadService::GetSharedMemoryHandleForProcess( | |
| 153 base::ProcessHandle handle) { | |
| 154 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 155 return provider_->GetSharedMemoryHandleForProcess(handle); | |
| 156 } | |
| 157 | |
| 158 mojo::ScopedSharedBufferHandle GamepadService::GetSharedBufferHandle() { | |
| 159 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 160 | |
| 161 // TODO(heke): Use mojo::SharedBuffer rather than base::SharedMemory in | |
| 162 // GamepadSharedBuffer. See crbug.com/670655 for details. | |
| 163 return mojo::WrapSharedMemoryHandle(provider_->GetSharedMemoryHandle(), | |
| 164 sizeof(GamepadHardwareBuffer), | |
| 165 true /* read_only */); | |
| 166 } | |
| 167 | |
| 168 void GamepadService::OnUserGesture() { | |
| 169 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 170 | |
| 171 gesture_callback_pending_ = false; | |
| 172 | |
| 173 if (!provider_ || | |
| 174 num_active_consumers_ == 0) | |
| 175 return; | |
| 176 | |
| 177 for (ConsumerSet::iterator it = consumers_.begin(); | |
| 178 it != consumers_.end(); ++it) { | |
| 179 if (!it->did_observe_user_gesture && it->is_active) { | |
| 180 const ConsumerInfo& info = *it; | |
| 181 info.did_observe_user_gesture = true; | |
| 182 blink::WebGamepads gamepads; | |
| 183 provider_->GetCurrentGamepadData(&gamepads); | |
| 184 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { | |
| 185 const blink::WebGamepad& pad = gamepads.items[i]; | |
| 186 if (pad.connected) | |
| 187 info.consumer->OnGamepadConnected(i, pad); | |
| 188 } | |
| 189 } | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 void GamepadService::SetSanitizationEnabled(bool sanitize) { | |
| 194 provider_->SetSanitizationEnabled(sanitize); | |
| 195 } | |
| 196 | |
| 197 } // namespace content | |
| OLD | NEW |