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

Unified Diff: content/renderer/gamepad_util.cc

Issue 8689011: Renderer reading side of gamepad (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: remove unrelated change Created 9 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/gamepad_util.h ('k') | content/renderer/renderer_webkitplatformsupport_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/gamepad_util.cc
diff --git a/content/renderer/gamepad_util.cc b/content/renderer/gamepad_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..daec11d3b69a7bbf1ac59a792294d7fa7bb4b2bf
--- /dev/null
+++ b/content/renderer/gamepad_util.cc
@@ -0,0 +1,85 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/gamepad_util.h"
+
+#include "base/debug/trace_event.h"
+#include "base/metrics/histogram.h"
+#include "content/common/gamepad_messages.h"
+#include "content/public/renderer/render_thread.h"
+#include "content/common/gamepad_hardware_buffer.h"
+#include "ipc/ipc_sync_message_filter.h"
+
+using namespace content;
darin (slow to review) 2011/11/28 17:35:49 nit: the google c++ style guide forbids 'using nam
scottmg 2011/11/28 20:02:08 Done.
+using namespace gamepad;
+
+GamepadUtil::GamepadUtil() {
darin (slow to review) 2011/11/28 17:35:49 please come up with a better name for this class.
scottmg 2011/11/28 20:02:08 Done.
+ memset(ever_interacted_with_, 0, sizeof(ever_interacted_with_));
+ CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling(
+ &renderer_shared_memory_handle_)));
+ renderer_shared_memory_.reset(
+ new base::SharedMemory(renderer_shared_memory_handle_, true));
+ CHECK(renderer_shared_memory_->Map(sizeof(gamepad::GamepadHardwareBuffer)));
+ void *memory = renderer_shared_memory_->memory();
+ CHECK(memory);
+ gamepad_hardware_buffer_ = static_cast<GamepadHardwareBuffer*>(memory);
+}
+
+void GamepadUtil::SampleGamepads(WebKit::WebGamepads& gamepads) {
+ WebKit::WebGamepads read_into;
+ TRACE_EVENT0("GAMEPAD", "SampleGamepads");
+
+ // See gamepad_hardware_buffer.h for details on the read discipline.
+ base::subtle::Atomic32 start, end;
+ // Only try to read this many times before failing to avoid waiting here
+ // very long in case of contention with the writer. TODO(scottmg) Tune this
+ // number (as low as 1?) if histogram shows distribution as mostly
+ // 0-and-maximum.
+ const int maximum_contention_count = 10;
+ int contention_count = 0;
+ while (contention_count < maximum_contention_count) {
+ end = base::subtle::Acquire_Load(&gamepad_hardware_buffer_->end_marker);
darin (slow to review) 2011/11/28 17:35:49 i had expected to see a memory barrier here. i ad
scottmg 2011/11/28 20:02:08 I'm afraid that's probably a bug then. I thought I
+ memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(gamepads));
darin (slow to review) 2011/11/28 17:35:49 i'm not the expert on our dynamic annotations syst
scottmg 2011/11/28 20:02:08 OK, will do.
+ start = base::subtle::Acquire_Load(&gamepad_hardware_buffer_->start_marker);
+ if (start == end)
+ break;
+ ++contention_count;
+ base::PlatformThread::YieldCurrentThread();
+ }
+ HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count);
+
+ if (contention_count == maximum_contention_count) {
+ // We failed to successfully read, presumably because the hardware
+ // thread was taking unusually long. Don't copy the data to the output
+ // buffer, and simply leave what was there before.
+ return;
+ }
+
+ // New data was read successfully, copy it into the output buffer.
+ memcpy(&gamepads, &read_into, sizeof(gamepads));
+
+ // Override the "connected" with false until the user has interacted
+ // with the gamepad. This is to prevent fingerprinting on drive-by pages.
+ for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) {
+ WebKit::WebGamepad& pad = gamepads.items[i];
+ // If the device is physically connected, then check if we should
+ // keep it disabled. We track if any of the primary 4 buttons have been
+ // pressed to determine a reasonable intentional interaction from the user.
+ if (pad.connected) {
+ if (ever_interacted_with_[i])
+ continue;
+ const unsigned primaryInteractionButtons = 4;
darin (slow to review) 2011/11/28 17:35:49 nit: use kFooBar for google c++ style constants.
scottmg 2011/11/28 20:02:08 Done.
+ for (unsigned j = 0; j < primaryInteractionButtons; ++j)
+ ever_interacted_with_[i] |= pad.buttons[j] > 0.5f;
+ // If we've not previously set, and the user still hasn't touched
+ // these buttons, then don't pass the data on to the Chromium port.
+ if (!ever_interacted_with_[i])
+ pad.connected = false;
+ }
+ }
+}
+
+GamepadUtil::~GamepadUtil() {
+ RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
darin (slow to review) 2011/11/28 17:35:49 note: i'm not sure if you are doing this yet, but
scottmg 2011/11/28 20:02:08 After talking to John I have some other work to do
+}
« no previous file with comments | « content/renderer/gamepad_util.h ('k') | content/renderer/renderer_webkitplatformsupport_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698