chrome.bluetoothSocket: clean-up Listen functions
[chromium-blink-merge.git] / content / renderer / gamepad_shared_memory_reader.cc
blobf1b1e835144c85ab26a7ee33fb1599d07a809e0a
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"
15 namespace content {
17 GamepadSharedMemoryReader::GamepadSharedMemoryReader()
18 : gamepad_hardware_buffer_(NULL),
19 gamepad_listener_(NULL),
20 is_polling_(false),
21 ever_interacted_with_(false) {
24 void GamepadSharedMemoryReader::StartPollingIfNecessary() {
25 if (is_polling_)
26 return;
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);
36 if (!valid_handle)
37 return;
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();
43 CHECK(memory);
44 gamepad_hardware_buffer_ =
45 static_cast<GamepadHardwareBuffer*>(memory);
47 is_polling_ = true;
50 void GamepadSharedMemoryReader::StopPollingIfNecessary() {
51 if (is_polling_) {
52 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
53 is_polling_ = false;
57 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) {
58 // Blink should set the listener before start sampling.
59 CHECK(gamepad_listener_);
61 StartPollingIfNecessary();
62 if (!is_polling_)
63 return;
65 // ==========
66 // DANGER
67 // ==========
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_))
75 return;
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
80 // 0-and-maximum.
81 const int kMaximumContentionCount = 10;
82 int contention_count = -1;
83 base::subtle::Atomic32 version;
84 do {
85 version = gamepad_hardware_buffer_->sequence.ReadBegin();
86 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into));
87 ++contention_count;
88 if (contention_count == kMaximumContentionCount)
89 break;
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.
97 return;
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;
106 } else {
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();
124 } else {
125 StopPollingIfNecessary();
129 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
130 StopPollingIfNecessary();
133 bool GamepadSharedMemoryReader::OnControlMessageReceived(
134 const IPC::Message& message) {
135 bool handled = true;
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()
141 return handled;
144 void GamepadSharedMemoryReader::OnGamepadConnected(
145 int index,
146 const blink::WebGamepad& gamepad) {
147 if (gamepad_listener_)
148 gamepad_listener_->didConnectGamepad(index, gamepad);
151 void GamepadSharedMemoryReader::OnGamepadDisconnected(
152 int index,
153 const blink::WebGamepad& gamepad) {
154 if (gamepad_listener_)
155 gamepad_listener_->didDisconnectGamepad(index, gamepad);
158 } // namespace content