Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: content/renderer/gamepad_shared_memory_reader.cc

Issue 8689011: Renderer reading side of gamepad (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: using separate seqlock Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "content/renderer/gamepad_shared_memory_reader.h"
6
7 #include "base/debug/trace_event.h"
8 #include "base/metrics/histogram.h"
9 #include "content/common/gamepad_messages.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
14 namespace content {
15
16 GamepadSharedMemoryReader::GamepadSharedMemoryReader() {
17 memset(ever_interacted_with_, 0, sizeof(ever_interacted_with_));
18 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling(
19 &renderer_shared_memory_handle_)));
20 renderer_shared_memory_.reset(
21 new base::SharedMemory(renderer_shared_memory_handle_, true));
22 CHECK(renderer_shared_memory_->Map(sizeof(gamepad::GamepadHardwareBuffer)));
23 void *memory = renderer_shared_memory_->memory();
24 CHECK(memory);
25 gamepad_hardware_buffer_ =
26 static_cast<gamepad::GamepadHardwareBuffer*>(memory);
27 }
28
29 void GamepadSharedMemoryReader::SampleGamepads(WebKit::WebGamepads& gamepads) {
30 WebKit::WebGamepads read_into;
31 TRACE_EVENT0("GAMEPAD", "SampleGamepads");
32
33 // Only try to read this many times before failing to avoid waiting here
34 // very long in case of contention with the writer. TODO(scottmg) Tune this
35 // number (as low as 1?) if histogram shows distribution as mostly
36 // 0-and-maximum.
37 const int kMaximumContentionCount = 10;
38 int contention_count = -1;
39 base::subtle::Atomic32 version;
40 do {
41 version = gamepad_hardware_buffer_->sequence.ReadBegin();
42 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into));
43 ++contention_count;
44 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version));
45 HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count);
46
47 if (contention_count < kMaximumContentionCount) {
48 // We failed to successfully read, presumably because the hardware
49 // thread was taking unusually long. Don't copy the data to the output
50 // buffer, and simply leave what was there before.
51 return;
52 }
53
54 // New data was read successfully, copy it into the output buffer.
55 memcpy(&gamepads, &read_into, sizeof(gamepads));
56
57 // Override the "connected" with false until the user has interacted
58 // with the gamepad. This is to prevent fingerprinting on drive-by pages.
59 for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) {
60 WebKit::WebGamepad& pad = gamepads.items[i];
61 // If the device is physically connected, then check if we should
62 // keep it disabled. We track if any of the primary 4 buttons have been
63 // pressed to determine a reasonable intentional interaction from the user.
64 if (pad.connected) {
65 if (ever_interacted_with_[i])
66 continue;
67 const unsigned kPrimaryInteractionButtons = 4;
68 for (unsigned j = 0; j < kPrimaryInteractionButtons; ++j)
69 ever_interacted_with_[i] |= pad.buttons[j] > 0.5f;
70 // If we've not previously set, and the user still hasn't touched
71 // these buttons, then don't pass the data on to the Chromium port.
72 if (!ever_interacted_with_[i])
73 pad.connected = false;
74 }
75 }
76 }
77
78 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
79 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
80 }
81
82 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698