Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(778)

Side by Side Diff: device/gamepad/gamepad_service.cc

Issue 2466073002: Movw GamepadService out of content/browser/ and into device/ (Closed)
Patch Set: Fixed everything but one unittest Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/gamepad/gamepad_service.h ('k') | device/gamepad/gamepad_service_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "device/gamepad/gamepad_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/memory/singleton.h" 12 #include "base/memory/singleton.h"
13 #include "content/browser/gamepad/gamepad_shared_buffer_impl.h" 13 #include "base/single_thread_task_runner.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "base/threading/thread_task_runner_handle.h"
15 #include "content/public/browser/render_process_host.h" 15 //#include "content/public/browser/browser_thread.h"
16 //#include "content/public/browser/render_process_host.h"
16 #include "device/gamepad/gamepad_consumer.h" 17 #include "device/gamepad/gamepad_consumer.h"
17 #include "device/gamepad/gamepad_data_fetcher.h" 18 #include "device/gamepad/gamepad_data_fetcher.h"
18 #include "device/gamepad/gamepad_provider.h" 19 #include "device/gamepad/gamepad_provider.h"
20 #include "device/gamepad/gamepad_shared_buffer.h"
19 21
20 namespace content { 22 namespace device {
21 23
22 namespace { 24 namespace {
23 GamepadService* g_gamepad_service = 0; 25 GamepadService* g_gamepad_service = 0;
24 } 26 }
25 27
26 GamepadService::GamepadService() 28 GamepadService::GamepadService()
27 : num_active_consumers_(0), 29 : main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
30 num_active_consumers_(0),
28 gesture_callback_pending_(false) { 31 gesture_callback_pending_(false) {
29 SetInstance(this); 32 SetInstance(this);
30 } 33 }
31 34
32 GamepadService::GamepadService( 35 GamepadService::GamepadService(
33 std::unique_ptr<device::GamepadDataFetcher> fetcher) 36 std::unique_ptr<device::GamepadDataFetcher> fetcher)
34 : provider_(new device::GamepadProvider( 37 : provider_(new device::GamepadProvider(this, std::move(fetcher))),
35 base::MakeUnique<GamepadSharedBufferImpl>(), this, std::move(fetcher))), 38 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
36 num_active_consumers_(0), 39 num_active_consumers_(0),
37 gesture_callback_pending_(false) { 40 gesture_callback_pending_(false) {
38 SetInstance(this); 41 SetInstance(this);
39 thread_checker_.DetachFromThread();
40 } 42 }
41 43
42 GamepadService::~GamepadService() { 44 GamepadService::~GamepadService() {
43 SetInstance(NULL); 45 SetInstance(NULL);
44 } 46 }
45 47
46 void GamepadService::SetInstance(GamepadService* instance) { 48 void GamepadService::SetInstance(GamepadService* instance) {
47 // Unit tests can create multiple instances but only one should exist at any 49 // Unit tests can create multiple instances but only one should exist at any
48 // given time so g_gamepad_service should only go from NULL to non-NULL and 50 // given time so g_gamepad_service should only go from NULL to non-NULL and
49 // vica versa. 51 // vica versa.
50 CHECK(!!instance != !!g_gamepad_service); 52 CHECK(!!instance != !!g_gamepad_service);
51 g_gamepad_service = instance; 53 g_gamepad_service = instance;
52 } 54 }
53 55
54 GamepadService* GamepadService::GetInstance() { 56 GamepadService* GamepadService::GetInstance() {
55 if (!g_gamepad_service) 57 if (!g_gamepad_service)
56 g_gamepad_service = new GamepadService; 58 g_gamepad_service = new GamepadService;
57 return g_gamepad_service; 59 return g_gamepad_service;
58 } 60 }
59 61
60 void GamepadService::ConsumerBecameActive(device::GamepadConsumer* consumer) { 62 void GamepadService::ConsumerBecameActive(device::GamepadConsumer* consumer) {
61 DCHECK(thread_checker_.CalledOnValidThread()); 63 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
62 64
63 if (!provider_) 65 if (!provider_)
64 provider_.reset(new device::GamepadProvider( 66 provider_.reset(new device::GamepadProvider(this));
65 base::MakeUnique<GamepadSharedBufferImpl>(), this));
66 67
67 std::pair<ConsumerSet::iterator, bool> insert_result = 68 std::pair<ConsumerSet::iterator, bool> insert_result =
68 consumers_.insert(consumer); 69 consumers_.insert(consumer);
69 insert_result.first->is_active = true; 70 insert_result.first->is_active = true;
70 if (!insert_result.first->did_observe_user_gesture && 71 if (!insert_result.first->did_observe_user_gesture &&
71 !gesture_callback_pending_) { 72 !gesture_callback_pending_) {
72 gesture_callback_pending_ = true; 73 gesture_callback_pending_ = true;
73 provider_->RegisterForUserGesture( 74 provider_->RegisterForUserGesture(
74 base::Bind(&GamepadService::OnUserGesture, 75 base::Bind(&GamepadService::OnUserGesture, base::Unretained(this)));
75 base::Unretained(this)));
76 } 76 }
77 77
78 if (num_active_consumers_++ == 0) 78 if (num_active_consumers_++ == 0)
79 provider_->Resume(); 79 provider_->Resume();
80 } 80 }
81 81
82 void GamepadService::ConsumerBecameInactive(device::GamepadConsumer* consumer) { 82 void GamepadService::ConsumerBecameInactive(device::GamepadConsumer* consumer) {
83 DCHECK(provider_); 83 DCHECK(provider_);
84 DCHECK(num_active_consumers_ > 0); 84 DCHECK(num_active_consumers_ > 0);
85 DCHECK(consumers_.count(consumer) > 0); 85 DCHECK(consumers_.count(consumer) > 0);
86 DCHECK(consumers_.find(consumer)->is_active); 86 DCHECK(consumers_.find(consumer)->is_active);
87 87
88 consumers_.find(consumer)->is_active = false; 88 consumers_.find(consumer)->is_active = false;
89 if (--num_active_consumers_ == 0) 89 if (--num_active_consumers_ == 0)
90 provider_->Pause(); 90 provider_->Pause();
91 } 91 }
92 92
93 void GamepadService::RemoveConsumer(device::GamepadConsumer* consumer) { 93 void GamepadService::RemoveConsumer(device::GamepadConsumer* consumer) {
94 DCHECK(thread_checker_.CalledOnValidThread()); 94 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
95 95
96 ConsumerSet::iterator it = consumers_.find(consumer); 96 ConsumerSet::iterator it = consumers_.find(consumer);
97 if (it->is_active && --num_active_consumers_ == 0) 97 if (it->is_active && --num_active_consumers_ == 0)
98 provider_->Pause(); 98 provider_->Pause();
99 consumers_.erase(it); 99 consumers_.erase(it);
100 } 100 }
101 101
102 void GamepadService::RegisterForUserGesture(const base::Closure& closure) { 102 void GamepadService::RegisterForUserGesture(const base::Closure& closure) {
103 DCHECK(consumers_.size() > 0); 103 DCHECK(consumers_.size() > 0);
104 DCHECK(thread_checker_.CalledOnValidThread()); 104 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
105 provider_->RegisterForUserGesture(closure); 105 provider_->RegisterForUserGesture(closure);
106 } 106 }
107 107
108 void GamepadService::Terminate() { 108 void GamepadService::Terminate() {
109 provider_.reset(); 109 provider_.reset();
110 } 110 }
111 111
112 void GamepadService::OnGamepadConnectionChange(bool connected, 112 void GamepadService::OnGamepadConnectionChange(bool connected,
113 int index, 113 int index,
114 const blink::WebGamepad& pad) { 114 const blink::WebGamepad& pad) {
115 if (connected) { 115 if (connected) {
116 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 116 main_thread_task_runner_->PostTask(
117 base::Bind(&GamepadService::OnGamepadConnected, 117 FROM_HERE, base::Bind(&GamepadService::OnGamepadConnected,
118 base::Unretained(this), index, pad)); 118 base::Unretained(this), index, pad));
119 } else { 119 } else {
120 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 120 main_thread_task_runner_->PostTask(
121 base::Bind(&GamepadService::OnGamepadDisconnected, 121 FROM_HERE, base::Bind(&GamepadService::OnGamepadDisconnected,
122 base::Unretained(this), index, pad)); 122 base::Unretained(this), index, pad));
123 } 123 }
124 } 124 }
125 125
126 void GamepadService::OnGamepadConnected( 126 void GamepadService::OnGamepadConnected(int index,
127 int index, 127 const blink::WebGamepad& pad) {
128 const blink::WebGamepad& pad) { 128 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
129 DCHECK(thread_checker_.CalledOnValidThread());
130 129
131 for (ConsumerSet::iterator it = consumers_.begin(); 130 for (ConsumerSet::iterator it = consumers_.begin(); it != consumers_.end();
132 it != consumers_.end(); ++it) { 131 ++it) {
133 if (it->did_observe_user_gesture && it->is_active) 132 if (it->did_observe_user_gesture && it->is_active)
134 it->consumer->OnGamepadConnected(index, pad); 133 it->consumer->OnGamepadConnected(index, pad);
135 } 134 }
136 } 135 }
137 136
138 void GamepadService::OnGamepadDisconnected( 137 void GamepadService::OnGamepadDisconnected(int index,
139 int index, 138 const blink::WebGamepad& pad) {
140 const blink::WebGamepad& pad) { 139 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
141 DCHECK(thread_checker_.CalledOnValidThread());
142 140
143 for (ConsumerSet::iterator it = consumers_.begin(); 141 for (ConsumerSet::iterator it = consumers_.begin(); it != consumers_.end();
144 it != consumers_.end(); ++it) { 142 ++it) {
145 if (it->did_observe_user_gesture && it->is_active) 143 if (it->did_observe_user_gesture && it->is_active)
146 it->consumer->OnGamepadDisconnected(index, pad); 144 it->consumer->OnGamepadDisconnected(index, pad);
147 } 145 }
148 } 146 }
149 147
150 base::SharedMemoryHandle GamepadService::GetSharedMemoryHandleForProcess( 148 base::SharedMemoryHandle GamepadService::GetSharedMemoryHandleForProcess(
151 base::ProcessHandle handle) { 149 base::ProcessHandle handle) {
152 DCHECK(thread_checker_.CalledOnValidThread()); 150 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
153 return provider_->GetSharedMemoryHandleForProcess(handle); 151 return provider_->GetSharedMemoryHandleForProcess(handle);
154 } 152 }
155 153
156 void GamepadService::OnUserGesture() { 154 void GamepadService::OnUserGesture() {
157 DCHECK(thread_checker_.CalledOnValidThread()); 155 DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
158 156
159 gesture_callback_pending_ = false; 157 gesture_callback_pending_ = false;
160 158
161 if (!provider_ || 159 if (!provider_ || num_active_consumers_ == 0)
162 num_active_consumers_ == 0)
163 return; 160 return;
164 161
165 for (ConsumerSet::iterator it = consumers_.begin(); 162 for (ConsumerSet::iterator it = consumers_.begin(); it != consumers_.end();
166 it != consumers_.end(); ++it) { 163 ++it) {
167 if (!it->did_observe_user_gesture && it->is_active) { 164 if (!it->did_observe_user_gesture && it->is_active) {
168 const ConsumerInfo& info = *it; 165 const ConsumerInfo& info = *it;
169 info.did_observe_user_gesture = true; 166 info.did_observe_user_gesture = true;
170 blink::WebGamepads gamepads; 167 blink::WebGamepads gamepads;
171 provider_->GetCurrentGamepadData(&gamepads); 168 provider_->GetCurrentGamepadData(&gamepads);
172 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { 169 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
173 const blink::WebGamepad& pad = gamepads.items[i]; 170 const blink::WebGamepad& pad = gamepads.items[i];
174 if (pad.connected) 171 if (pad.connected)
175 info.consumer->OnGamepadConnected(i, pad); 172 info.consumer->OnGamepadConnected(i, pad);
176 } 173 }
177 } 174 }
178 } 175 }
179 } 176 }
180 177
181 void GamepadService::SetSanitizationEnabled(bool sanitize) { 178 void GamepadService::SetSanitizationEnabled(bool sanitize) {
182 provider_->SetSanitizationEnabled(sanitize); 179 provider_->SetSanitizationEnabled(sanitize);
183 } 180 }
184 181
185 } // namespace content 182 } // namespace device
OLDNEW
« no previous file with comments | « device/gamepad/gamepad_service.h ('k') | device/gamepad/gamepad_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698