OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/gamepad_shared_memory_reader.h" | 5 #include "content/renderer/gamepad_shared_memory_reader.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "content/common/gamepad_messages.h" | 9 #include "content/common/gamepad_messages.h" |
10 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 static_cast<GamepadHardwareBuffer*>(memory); | 34 static_cast<GamepadHardwareBuffer*>(memory); |
35 } | 35 } |
36 | 36 |
37 void GamepadSharedMemoryReader::SampleGamepads(WebKit::WebGamepads& gamepads) { | 37 void GamepadSharedMemoryReader::SampleGamepads(WebKit::WebGamepads& gamepads) { |
38 WebKit::WebGamepads read_into; | 38 WebKit::WebGamepads read_into; |
39 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); | 39 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); |
40 | 40 |
41 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 41 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) |
42 return; | 42 return; |
43 | 43 |
44 // Only try to read this many times before failing to avoid waiting here | 44 gamepad_hardware_buffer_->gamepads.ReadTo(&gamepads); |
45 // very long in case of contention with the writer. TODO(scottmg) Tune this | |
46 // number (as low as 1?) if histogram shows distribution as mostly | |
47 // 0-and-maximum. | |
48 const int kMaximumContentionCount = 10; | |
49 int contention_count = -1; | |
50 base::subtle::Atomic32 version; | |
51 do { | |
52 version = gamepad_hardware_buffer_->sequence.ReadBegin(); | |
53 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into)); | |
54 ++contention_count; | |
55 if (contention_count == kMaximumContentionCount) | |
56 break; | |
57 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version)); | |
58 UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count); | |
59 | |
60 if (contention_count >= kMaximumContentionCount) { | |
61 // We failed to successfully read, presumably because the hardware | |
62 // thread was taking unusually long. Don't copy the data to the output | |
63 // buffer, and simply leave what was there before. | |
64 return; | |
65 } | |
66 | |
67 // New data was read successfully, copy it into the output buffer. | |
68 memcpy(&gamepads, &read_into, sizeof(gamepads)); | |
69 | 45 |
70 // Override the "connected" with false until the user has interacted | 46 // Override the "connected" with false until the user has interacted |
71 // with the gamepad. This is to prevent fingerprinting on drive-by pages. | 47 // with the gamepad. This is to prevent fingerprinting on drive-by pages. |
72 for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) { | 48 for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) { |
73 WebKit::WebGamepad& pad = gamepads.items[i]; | 49 WebKit::WebGamepad& pad = gamepads.items[i]; |
74 // If the device is physically connected, then check if we should | 50 // If the device is physically connected, then check if we should |
75 // keep it disabled. We track if any of the primary 4 buttons have been | 51 // keep it disabled. We track if any of the primary 4 buttons have been |
76 // pressed to determine a reasonable intentional interaction from the user. | 52 // pressed to determine a reasonable intentional interaction from the user. |
77 if (pad.connected) { | 53 if (pad.connected) { |
78 if (ever_interacted_with_[i]) | 54 if (ever_interacted_with_[i]) |
79 continue; | 55 continue; |
80 const unsigned kPrimaryInteractionButtons = 4; | 56 const unsigned kPrimaryInteractionButtons = 4; |
81 for (unsigned j = 0; j < kPrimaryInteractionButtons; ++j) | 57 for (unsigned j = 0; j < kPrimaryInteractionButtons; ++j) |
82 ever_interacted_with_[i] |= pad.buttons[j] > 0.5f; | 58 ever_interacted_with_[i] |= pad.buttons[j] > 0.5f; |
83 // If we've not previously set, and the user still hasn't touched | 59 // If we've not previously set, and the user still hasn't touched |
84 // these buttons, then don't pass the data on to the Chromium port. | 60 // these buttons, then don't pass the data on to the Chromium port. |
85 if (!ever_interacted_with_[i]) | 61 if (!ever_interacted_with_[i]) |
86 pad.connected = false; | 62 pad.connected = false; |
87 } | 63 } |
88 } | 64 } |
89 } | 65 } |
90 | 66 |
91 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | 67 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
92 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); | 68 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); |
93 } | 69 } |
94 | 70 |
95 } // namespace content | 71 } // namespace content |
OLD | NEW |