forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamepad_shared_memory_reader.cc
138 lines (114 loc) · 4.51 KB
/
gamepad_shared_memory_reader.cc
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
// 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 "content/renderer/gamepad_shared_memory_reader.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/trace_event.h"
#include "content/renderer/renderer_blink_platform_impl.h"
#include "third_party/blink/public/platform/interface_provider.h"
#include "third_party/blink/public/platform/platform.h"
namespace content {
GamepadSharedMemoryReader::GamepadSharedMemoryReader() : binding_(this) {
blink::Platform::Current()->GetInterfaceProvider()->GetInterface(
mojo::MakeRequest(&gamepad_monitor_));
device::mojom::GamepadObserverPtr observer;
binding_.Bind(mojo::MakeRequest(&observer));
gamepad_monitor_->SetObserver(std::move(observer));
}
void GamepadSharedMemoryReader::SendStartMessage() {
if (gamepad_monitor_) {
gamepad_monitor_->GamepadStartPolling(&renderer_shared_buffer_handle_);
}
}
void GamepadSharedMemoryReader::SendStopMessage() {
if (gamepad_monitor_) {
gamepad_monitor_->GamepadStopPolling();
}
}
void GamepadSharedMemoryReader::Start(blink::WebGamepadListener* listener) {
DCHECK(!listener_);
listener_ = listener;
SendStartMessage();
// If we don't get a valid handle from the browser, don't try to Map (we're
// probably out of memory or file handles).
bool valid_handle = renderer_shared_buffer_handle_.is_valid();
UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle);
if (!valid_handle)
return;
renderer_shared_buffer_mapping_ = renderer_shared_buffer_handle_->Map(
sizeof(device::GamepadHardwareBuffer));
CHECK(renderer_shared_buffer_mapping_);
void* memory = renderer_shared_buffer_mapping_.get();
CHECK(memory);
gamepad_hardware_buffer_ =
static_cast<device::GamepadHardwareBuffer*>(memory);
}
void GamepadSharedMemoryReader::Stop() {
DCHECK(listener_);
listener_ = nullptr;
SendStopMessage();
}
void GamepadSharedMemoryReader::SampleGamepads(device::Gamepads& gamepads) {
// Blink should have started observing at this point.
CHECK(listener_);
// ==========
// DANGER
// ==========
//
// This logic is duplicated in Pepper as well. If you change it, that also
// needs to be in sync. See ppapi/proxy/gamepad_resource.cc.
device::Gamepads read_into;
TRACE_EVENT0("GAMEPAD", "SampleGamepads");
if (!renderer_shared_buffer_handle_.is_valid())
return;
// Only try to read this many times before failing to avoid waiting here
// very long in case of contention with the writer. TODO(scottmg) Tune this
// number (as low as 1?) if histogram shows distribution as mostly
// 0-and-maximum.
const int kMaximumContentionCount = 10;
int contention_count = -1;
base::subtle::Atomic32 version;
do {
version = gamepad_hardware_buffer_->seqlock.ReadBegin();
memcpy(&read_into, &gamepad_hardware_buffer_->data, sizeof(read_into));
++contention_count;
if (contention_count == kMaximumContentionCount)
break;
} while (gamepad_hardware_buffer_->seqlock.ReadRetry(version));
UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count);
if (contention_count >= kMaximumContentionCount) {
// We failed to successfully read, presumably because the hardware
// thread was taking unusually long. Don't copy the data to the output
// buffer, and simply leave what was there before.
return;
}
// New data was read successfully, copy it into the output buffer.
memcpy(&gamepads, &read_into, sizeof(gamepads));
if (!ever_interacted_with_) {
// Clear the connected flag if the user hasn't interacted with any of the
// gamepads to prevent fingerprinting. The actual data is not cleared.
// WebKit will only copy out data into the JS buffers for connected
// gamepads so this is sufficient.
for (unsigned i = 0; i < device::Gamepads::kItemsLengthCap; i++)
gamepads.items[i].connected = false;
}
}
GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
if (listener_)
Stop();
}
void GamepadSharedMemoryReader::GamepadConnected(
int index,
const device::Gamepad& gamepad) {
// The browser already checks if the user actually interacted with a device.
ever_interacted_with_ = true;
if (listener_)
listener_->DidConnectGamepad(index, gamepad);
}
void GamepadSharedMemoryReader::GamepadDisconnected(
int index,
const device::Gamepad& gamepad) {
if (listener_)
listener_->DidDisconnectGamepad(index, gamepad);
}
} // namespace content