This source file includes following definitions.
- ever_interacted_with_
- SampleGamepads
#include "content/renderer/gamepad_shared_memory_reader.h"
#include "base/debug/trace_event.h"
#include "base/metrics/histogram.h"
#include "content/common/gamepad_messages.h"
#include "content/common/gamepad_user_gesture.h"
#include "content/public/renderer/render_thread.h"
#include "content/common/gamepad_hardware_buffer.h"
#include "ipc/ipc_sync_message_filter.h"
namespace content {
GamepadSharedMemoryReader::GamepadSharedMemoryReader()
: gamepad_hardware_buffer_(NULL),
ever_interacted_with_(false) {
CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling(
&renderer_shared_memory_handle_)));
bool valid_handle = base::SharedMemory::IsHandleValid(
renderer_shared_memory_handle_);
UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle);
if (!valid_handle)
return;
renderer_shared_memory_.reset(
new base::SharedMemory(renderer_shared_memory_handle_, true));
CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer)));
void *memory = renderer_shared_memory_->memory();
CHECK(memory);
gamepad_hardware_buffer_ =
static_cast<GamepadHardwareBuffer*>(memory);
}
void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) {
blink::WebGamepads read_into;
TRACE_EVENT0("GAMEPAD", "SampleGamepads");
if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
return;
const int kMaximumContentionCount = 10;
int contention_count = -1;
base::subtle::Atomic32 version;
do {
version = gamepad_hardware_buffer_->sequence.ReadBegin();
memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into));
++contention_count;
if (contention_count == kMaximumContentionCount)
break;
} while (gamepad_hardware_buffer_->sequence.ReadRetry(version));
UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count);
if (contention_count >= kMaximumContentionCount) {
return;
}
memcpy(&gamepads, &read_into, sizeof(gamepads));
if (!ever_interacted_with_) {
if (GamepadsHaveUserGesture(gamepads)) {
ever_interacted_with_ = true;
} else {
for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++)
gamepads.items[i].connected = false;
}
}
}
GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
}
}