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.
5 #include "content/renderer/gamepad_shared_memory_reader.h"
7 #include "base/debug/trace_event.h"
8 #include "base/metrics/histogram.h"
9 #include "content/common/gamepad_user_gesture.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "content/common/gamepad_hardware_buffer.h"
12 #include "ipc/ipc_sync_message_filter.h"
13 #include "third_party/WebKit/public/platform/WebGamepadListener.h"
17 GamepadSharedMemoryReader::GamepadSharedMemoryReader()
18 : gamepad_hardware_buffer_(NULL
),
19 gamepad_listener_(NULL
),
21 ever_interacted_with_(false) {
24 void GamepadSharedMemoryReader::StartPollingIfNecessary() {
28 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling(
29 &renderer_shared_memory_handle_
)));
31 // If we don't get a valid handle from the browser, don't try to Map (we're
32 // probably out of memory or file handles).
33 bool valid_handle
= base::SharedMemory::IsHandleValid(
34 renderer_shared_memory_handle_
);
35 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle
);
39 renderer_shared_memory_
.reset(
40 new base::SharedMemory(renderer_shared_memory_handle_
, true));
41 CHECK(renderer_shared_memory_
->Map(sizeof(GamepadHardwareBuffer
)));
42 void *memory
= renderer_shared_memory_
->memory();
44 gamepad_hardware_buffer_
=
45 static_cast<GamepadHardwareBuffer
*>(memory
);
50 void GamepadSharedMemoryReader::StopPollingIfNecessary() {
52 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
57 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads
& gamepads
) {
58 // Blink should set the listener before start sampling.
59 CHECK(gamepad_listener_
);
61 StartPollingIfNecessary();
69 // This logic is duplicated in Pepper as well. If you change it, that also
70 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc.
71 blink::WebGamepads read_into
;
72 TRACE_EVENT0("GAMEPAD", "SampleGamepads");
74 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_
))
77 // Only try to read this many times before failing to avoid waiting here
78 // very long in case of contention with the writer. TODO(scottmg) Tune this
79 // number (as low as 1?) if histogram shows distribution as mostly
81 const int kMaximumContentionCount
= 10;
82 int contention_count
= -1;
83 base::subtle::Atomic32 version
;
85 version
= gamepad_hardware_buffer_
->sequence
.ReadBegin();
86 memcpy(&read_into
, &gamepad_hardware_buffer_
->buffer
, sizeof(read_into
));
88 if (contention_count
== kMaximumContentionCount
)
90 } while (gamepad_hardware_buffer_
->sequence
.ReadRetry(version
));
91 UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count
);
93 if (contention_count
>= kMaximumContentionCount
) {
94 // We failed to successfully read, presumably because the hardware
95 // thread was taking unusually long. Don't copy the data to the output
96 // buffer, and simply leave what was there before.
100 // New data was read successfully, copy it into the output buffer.
101 memcpy(&gamepads
, &read_into
, sizeof(gamepads
));
103 if (!ever_interacted_with_
) {
104 if (GamepadsHaveUserGesture(gamepads
)) {
105 ever_interacted_with_
= true;
107 // Clear the connected flag if the user hasn't interacted with any of the
108 // gamepads to prevent fingerprinting. The actual data is not cleared.
109 // WebKit will only copy out data into the JS buffers for connected
110 // gamepads so this is sufficient.
111 for (unsigned i
= 0; i
< blink::WebGamepads::itemsLengthCap
; i
++)
112 gamepads
.items
[i
].connected
= false;
117 void GamepadSharedMemoryReader::SetGamepadListener(
118 blink::WebGamepadListener
* listener
) {
119 gamepad_listener_
= listener
;
120 if (gamepad_listener_
) {
121 // Polling has to be started rigth now and not just on the first sampling
122 // because want to get connection events from now.
123 StartPollingIfNecessary();
125 StopPollingIfNecessary();
129 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
130 StopPollingIfNecessary();
133 bool GamepadSharedMemoryReader::OnControlMessageReceived(
134 const IPC::Message
& message
) {
136 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader
, message
)
137 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected
, OnGamepadConnected
)
138 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected
, OnGamepadDisconnected
)
139 IPC_MESSAGE_UNHANDLED(handled
= false)
140 IPC_END_MESSAGE_MAP()
144 void GamepadSharedMemoryReader::OnGamepadConnected(
146 const blink::WebGamepad
& gamepad
) {
147 if (gamepad_listener_
)
148 gamepad_listener_
->didConnectGamepad(index
, gamepad
);
151 void GamepadSharedMemoryReader::OnGamepadDisconnected(
153 const blink::WebGamepad
& gamepad
) {
154 if (gamepad_listener_
)
155 gamepad_listener_
->didDisconnectGamepad(index
, gamepad
);
158 } // namespace content