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

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: rebase to HEAD 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(GamepadHardwareBuffer)));
23 void *memory = renderer_shared_memory_->memory();
24 CHECK(memory);
25 gamepad_hardware_buffer_ =
26 static_cast<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 if (contention_count == kMaximumContentionCount)
45 break;
46 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version));
47 HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count);
48
49 if (contention_count >= kMaximumContentionCount) {
50 // We failed to successfully read, presumably because the hardware
51 // thread was taking unusually long. Don't copy the data to the output
52 // buffer, and simply leave what was there before.
53 return;
54 }
55
56 // New data was read successfully, copy it into the output buffer.
57 memcpy(&gamepads, &read_into, sizeof(gamepads));
58
59 // Override the "connected" with false until the user has interacted
60 // with the gamepad. This is to prevent fingerprinting on drive-by pages.
61 for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) {
62 WebKit::WebGamepad& pad = gamepads.items[i];
63 // If the device is physically connected, then check if we should
64 // keep it disabled. We track if any of the primary 4 buttons have been
65 // pressed to determine a reasonable intentional interaction from the user.
66 if (pad.connected) {
67 if (ever_interacted_with_[i])
68 continue;
69 const unsigned kPrimaryInteractionButtons = 4;
70 for (unsigned j = 0; j < kPrimaryInteractionButtons; ++j)
71 ever_interacted_with_[i] |= pad.buttons[j] > 0.5f;
72 // If we've not previously set, and the user still hasn't touched
73 // these buttons, then don't pass the data on to the Chromium port.
74 if (!ever_interacted_with_[i])
75 pad.connected = false;
76 }
77 }
78 }
79
80 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
81 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
82 }
83
84 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/gamepad_shared_memory_reader.h ('k') | content/renderer/renderer_webkitplatformsupport_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698