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" | |
10 #include "content/common/gamepad_user_gesture.h" | 9 #include "content/common/gamepad_user_gesture.h" |
11 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
12 #include "content/common/gamepad_hardware_buffer.h" | 11 #include "content/common/gamepad_hardware_buffer.h" |
13 #include "ipc/ipc_sync_message_filter.h" | 12 #include "ipc/ipc_sync_message_filter.h" |
| 13 #include "third_party/WebKit/public/platform/WebGamepadListener.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 GamepadSharedMemoryReader::GamepadSharedMemoryReader() | 17 GamepadSharedMemoryReader::GamepadSharedMemoryReader( |
18 : gamepad_hardware_buffer_(NULL), | 18 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) |
| 19 : io_message_loop_(io_message_loop), |
| 20 main_message_loop_(base::MessageLoopProxy::current()), |
| 21 gamepad_hardware_buffer_(NULL), |
| 22 gamepad_listener_(NULL), |
| 23 is_polling_(false), |
19 ever_interacted_with_(false) { | 24 ever_interacted_with_(false) { |
| 25 } |
| 26 |
| 27 void GamepadSharedMemoryReader::StartPollingIfNecessary() { |
| 28 if (is_polling_) |
| 29 return; |
| 30 |
20 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( | 31 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( |
21 &renderer_shared_memory_handle_))); | 32 &renderer_shared_memory_handle_))); |
| 33 |
22 // If we don't get a valid handle from the browser, don't try to Map (we're | 34 // If we don't get a valid handle from the browser, don't try to Map (we're |
23 // probably out of memory or file handles). | 35 // probably out of memory or file handles). |
24 bool valid_handle = base::SharedMemory::IsHandleValid( | 36 bool valid_handle = base::SharedMemory::IsHandleValid( |
25 renderer_shared_memory_handle_); | 37 renderer_shared_memory_handle_); |
26 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); | 38 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); |
27 if (!valid_handle) | 39 if (!valid_handle) |
28 return; | 40 return; |
| 41 |
29 renderer_shared_memory_.reset( | 42 renderer_shared_memory_.reset( |
30 new base::SharedMemory(renderer_shared_memory_handle_, true)); | 43 new base::SharedMemory(renderer_shared_memory_handle_, true)); |
31 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); | 44 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); |
32 void *memory = renderer_shared_memory_->memory(); | 45 void *memory = renderer_shared_memory_->memory(); |
33 CHECK(memory); | 46 CHECK(memory); |
34 gamepad_hardware_buffer_ = | 47 gamepad_hardware_buffer_ = |
35 static_cast<GamepadHardwareBuffer*>(memory); | 48 static_cast<GamepadHardwareBuffer*>(memory); |
| 49 |
| 50 is_polling_ = true; |
| 51 } |
| 52 |
| 53 void GamepadSharedMemoryReader::StopPollingIfNecessary() { |
| 54 if (is_polling_) { |
| 55 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); |
| 56 is_polling_ = false; |
| 57 } |
36 } | 58 } |
37 | 59 |
38 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { | 60 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { |
| 61 // Blink should set the listener before start sampling. |
| 62 CHECK(gamepad_listener_); |
| 63 |
| 64 StartPollingIfNecessary(); |
| 65 if (!is_polling_) |
| 66 return; |
| 67 |
39 // ========== | 68 // ========== |
40 // DANGER | 69 // DANGER |
41 // ========== | 70 // ========== |
42 // | 71 // |
43 // This logic is duplicated in Pepper as well. If you change it, that also | 72 // This logic is duplicated in Pepper as well. If you change it, that also |
44 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. | 73 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. |
45 blink::WebGamepads read_into; | 74 blink::WebGamepads read_into; |
46 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); | 75 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); |
47 | 76 |
48 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 77 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 // Clear the connected flag if the user hasn't interacted with any of the | 110 // Clear the connected flag if the user hasn't interacted with any of the |
82 // gamepads to prevent fingerprinting. The actual data is not cleared. | 111 // gamepads to prevent fingerprinting. The actual data is not cleared. |
83 // WebKit will only copy out data into the JS buffers for connected | 112 // WebKit will only copy out data into the JS buffers for connected |
84 // gamepads so this is sufficient. | 113 // gamepads so this is sufficient. |
85 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) | 114 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) |
86 gamepads.items[i].connected = false; | 115 gamepads.items[i].connected = false; |
87 } | 116 } |
88 } | 117 } |
89 } | 118 } |
90 | 119 |
| 120 void GamepadSharedMemoryReader::SetGamepadListener( |
| 121 blink::WebGamepadListener* listener) { |
| 122 gamepad_listener_ = listener; |
| 123 if (gamepad_listener_) { |
| 124 // Polling has to be started rigth now and not just on the first sampling |
| 125 // because want to get connection events from now. |
| 126 StartPollingIfNecessary(); |
| 127 } else { |
| 128 StopPollingIfNecessary(); |
| 129 } |
| 130 } |
| 131 |
91 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | 132 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
92 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); | 133 StopPollingIfNecessary(); |
| 134 } |
| 135 |
| 136 bool GamepadSharedMemoryReader::OnMessageReceived(const IPC::Message& message) { |
| 137 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 138 bool handled = true; |
| 139 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message) |
| 140 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected) |
| 141 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected) |
| 142 IPC_MESSAGE_UNHANDLED(handled = false) |
| 143 IPC_END_MESSAGE_MAP() |
| 144 return handled; |
| 145 } |
| 146 |
| 147 void GamepadSharedMemoryReader::OnGamepadConnected( |
| 148 int index, |
| 149 const blink::WebGamepad& gamepad) { |
| 150 // Dispatch on the main JS thread. |
| 151 main_message_loop_->PostTask( |
| 152 FROM_HERE, |
| 153 base::Bind(&GamepadSharedMemoryReader::DispatchGamepadConnected, |
| 154 this, |
| 155 index, |
| 156 gamepad)); |
| 157 } |
| 158 |
| 159 void GamepadSharedMemoryReader::OnGamepadDisconnected( |
| 160 int index, |
| 161 const blink::WebGamepad& gamepad) { |
| 162 // Dispatch on the main JS thread. |
| 163 main_message_loop_->PostTask( |
| 164 FROM_HERE, |
| 165 base::Bind(&GamepadSharedMemoryReader::DispatchGamepadDisconnected, |
| 166 this, |
| 167 index, |
| 168 gamepad)); |
| 169 } |
| 170 |
| 171 void GamepadSharedMemoryReader::DispatchGamepadConnected( |
| 172 int index, |
| 173 const blink::WebGamepad& gamepad) { |
| 174 if (gamepad_listener_) |
| 175 gamepad_listener_->didConnectGamepad(index, gamepad); |
| 176 } |
| 177 |
| 178 void GamepadSharedMemoryReader::DispatchGamepadDisconnected( |
| 179 int index, |
| 180 const blink::WebGamepad& gamepad) { |
| 181 if (gamepad_listener_) |
| 182 gamepad_listener_->didDisconnectGamepad(index, gamepad); |
93 } | 183 } |
94 | 184 |
95 } // namespace content | 185 } // namespace content |
OLD | NEW |