1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cmath>
#include <set>
#include <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "content/browser/gamepad/gamepad_data_fetcher.h"
#include "content/browser/gamepad/gamepad_provider.h"
#include "content/browser/gamepad/gamepad_platform_data_fetcher.h"
#include "content/common/gamepad_hardware_buffer.h"
#include "content/common/gamepad_messages.h"
#include "content/common/gamepad_user_gesture.h"
namespace content {
GamepadProvider::ClosureAndThread::ClosureAndThread(
const base::Closure& c,
const scoped_refptr<base::MessageLoopProxy>& m)
: closure(c),
message_loop(m) {
}
GamepadProvider::ClosureAndThread::~ClosureAndThread() {
}
GamepadProvider::GamepadProvider()
: is_paused_(true),
have_scheduled_do_poll_(false),
devices_changed_(true) {
Initialize(scoped_ptr<GamepadDataFetcher>());
}
GamepadProvider::GamepadProvider(scoped_ptr<GamepadDataFetcher> fetcher)
: is_paused_(true),
have_scheduled_do_poll_(false),
devices_changed_(true) {
Initialize(fetcher.Pass());
}
GamepadProvider::~GamepadProvider() {
base::SystemMonitor* monitor = base::SystemMonitor::Get();
if (monitor)
monitor->RemoveDevicesChangedObserver(this);
// Use Stop() to join the polling thread, as there may be pending callbacks
// which dereference |polling_thread_|.
polling_thread_->Stop();
data_fetcher_.reset();
}
base::SharedMemoryHandle GamepadProvider::GetSharedMemoryHandleForProcess(
base::ProcessHandle process) {
base::SharedMemoryHandle renderer_handle;
gamepad_shared_memory_.ShareToProcess(process, &renderer_handle);
return renderer_handle;
}
void GamepadProvider::Pause() {
{
base::AutoLock lock(is_paused_lock_);
is_paused_ = true;
}
MessageLoop* polling_loop = polling_thread_->message_loop();
polling_loop->PostTask(
FROM_HERE,
base::Bind(&GamepadProvider::SendPauseHint, Unretained(this), true));
}
void GamepadProvider::Resume() {
{
base::AutoLock lock(is_paused_lock_);
if (!is_paused_)
return;
is_paused_ = false;
}
MessageLoop* polling_loop = polling_thread_->message_loop();
polling_loop->PostTask(
FROM_HERE,
base::Bind(&GamepadProvider::SendPauseHint, Unretained(this), false));
polling_loop->PostTask(
FROM_HERE,
base::Bind(&GamepadProvider::ScheduleDoPoll, Unretained(this)));
}
void GamepadProvider::RegisterForUserGesture(const base::Closure& closure) {
base::AutoLock lock(user_gesture_lock_);
user_gesture_observers_.push_back(
ClosureAndThread(closure, MessageLoop::current()->message_loop_proxy()));
}
void GamepadProvider::OnDevicesChanged(base::SystemMonitor::DeviceType type) {
base::AutoLock lock(devices_changed_lock_);
devices_changed_ = true;
}
void GamepadProvider::Initialize(scoped_ptr<GamepadDataFetcher> fetcher) {
size_t data_size = sizeof(GamepadHardwareBuffer);
base::SystemMonitor* monitor = base::SystemMonitor::Get();
if (monitor)
monitor->AddDevicesChangedObserver(this);
bool res = gamepad_shared_memory_.CreateAndMapAnonymous(data_size);
CHECK(res);
GamepadHardwareBuffer* hwbuf = SharedMemoryAsHardwareBuffer();
memset(hwbuf, 0, sizeof(GamepadHardwareBuffer));
polling_thread_.reset(new base::Thread("Gamepad polling thread"));
polling_thread_->StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0));
polling_thread_->message_loop()->PostTask(
FROM_HERE,
base::Bind(&GamepadProvider::DoInitializePollingThread,
base::Unretained(this),
base::Passed(&fetcher)));
}
void GamepadProvider::DoInitializePollingThread(
scoped_ptr<GamepadDataFetcher> fetcher) {
DCHECK(MessageLoop::current() == polling_thread_->message_loop());
DCHECK(!data_fetcher_.get()); // Should only initialize once.
if (!fetcher)
fetcher.reset(new GamepadPlatformDataFetcher);
data_fetcher_ = fetcher.Pass();
}
void GamepadProvider::SendPauseHint(bool paused) {
DCHECK(MessageLoop::current() == polling_thread_->message_loop());
if (data_fetcher_)
data_fetcher_->PauseHint(paused);
}
void GamepadProvider::DoPoll() {
DCHECK(MessageLoop::current() == polling_thread_->message_loop());
DCHECK(have_scheduled_do_poll_);
have_scheduled_do_poll_ = false;
bool changed;
GamepadHardwareBuffer* hwbuf = SharedMemoryAsHardwareBuffer();
ANNOTATE_BENIGN_RACE_SIZED(
&hwbuf->buffer,
sizeof(WebKit::WebGamepads),
"Racey reads are discarded");
{
base::AutoLock lock(devices_changed_lock_);
changed = devices_changed_;
devices_changed_ = false;
}
// Acquire the SeqLock. There is only ever one writer to this data.
// See gamepad_hardware_buffer.h.
hwbuf->sequence.WriteBegin();
data_fetcher_->GetGamepadData(&hwbuf->buffer, changed);
hwbuf->sequence.WriteEnd();
CheckForUserGesture();
// Schedule our next interval of polling.
ScheduleDoPoll();
}
void GamepadProvider::ScheduleDoPoll() {
DCHECK(MessageLoop::current() == polling_thread_->message_loop());
if (have_scheduled_do_poll_)
return;
{
base::AutoLock lock(is_paused_lock_);
if (is_paused_)
return;
}
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&GamepadProvider::DoPoll, Unretained(this)),
base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs));
have_scheduled_do_poll_ = true;
}
GamepadHardwareBuffer* GamepadProvider::SharedMemoryAsHardwareBuffer() {
void* mem = gamepad_shared_memory_.memory();
CHECK(mem);
return static_cast<GamepadHardwareBuffer*>(mem);
}
void GamepadProvider::CheckForUserGesture() {
base::AutoLock lock(user_gesture_lock_);
if (user_gesture_observers_.empty())
return; // Don't need to check if nobody is listening.
if (GamepadsHaveUserGesture(SharedMemoryAsHardwareBuffer()->buffer)) {
for (size_t i = 0; i < user_gesture_observers_.size(); i++) {
user_gesture_observers_[i].message_loop->PostTask(FROM_HERE,
user_gesture_observers_[i].closure);
}
user_gesture_observers_.clear();
}
}
} // namespace content
|