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 bool GamepadSharedMemoryReader::StartPollingIfNecessary() { |
| 28 if (is_polling_) |
| 29 return true; |
| 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 false; |
| 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 return true; |
| 52 } |
| 53 |
| 54 void GamepadSharedMemoryReader::StopPollingIfNecessary() { |
| 55 if (is_polling_) { |
| 56 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); |
| 57 is_polling_ = false; |
| 58 } |
36 } | 59 } |
37 | 60 |
38 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { | 61 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { |
| 62 // Blink should set the listener before start sampling. |
| 63 CHECK(gamepad_listener_); |
| 64 |
| 65 StartPollingIfNecessary(); |
| 66 if (!is_polling_) |
| 67 return; |
| 68 |
39 // ========== | 69 // ========== |
40 // DANGER | 70 // DANGER |
41 // ========== | 71 // ========== |
42 // | 72 // |
43 // This logic is duplicated in Pepper as well. If you change it, that also | 73 // 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. | 74 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. |
45 blink::WebGamepads read_into; | 75 blink::WebGamepads read_into; |
46 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); | 76 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); |
47 | 77 |
48 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 78 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 | 111 // 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. | 112 // gamepads to prevent fingerprinting. The actual data is not cleared. |
83 // WebKit will only copy out data into the JS buffers for connected | 113 // WebKit will only copy out data into the JS buffers for connected |
84 // gamepads so this is sufficient. | 114 // gamepads so this is sufficient. |
85 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) | 115 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) |
86 gamepads.items[i].connected = false; | 116 gamepads.items[i].connected = false; |
87 } | 117 } |
88 } | 118 } |
89 } | 119 } |
90 | 120 |
| 121 void GamepadSharedMemoryReader::SetGamepadListener( |
| 122 blink::WebGamepadListener* listener) { |
| 123 gamepad_listener_ = listener; |
| 124 if (gamepad_listener_) { |
| 125 // Polling has to be started rigth now and not just on the first sampling |
| 126 // because want to get connection events from now. |
| 127 StartPollingIfNecessary(); |
| 128 } else if (!gamepad_listener_) { |
| 129 StopPollingIfNecessary(); |
| 130 } |
| 131 } |
| 132 |
91 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | 133 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
92 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); | 134 StopPollingIfNecessary(); |
| 135 } |
| 136 |
| 137 bool GamepadSharedMemoryReader::OnMessageReceived(const IPC::Message& message) { |
| 138 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 139 bool handled = true; |
| 140 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message) |
| 141 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected) |
| 142 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected) |
| 143 IPC_MESSAGE_UNHANDLED(handled = false) |
| 144 IPC_END_MESSAGE_MAP() |
| 145 return handled; |
| 146 } |
| 147 |
| 148 void GamepadSharedMemoryReader::OnGamepadConnected( |
| 149 const GamepadConnectionEventMessageParams& params) { |
| 150 int index = params.index; |
| 151 blink::WebGamepad gamepad; |
| 152 params.GetWebGamepad(&gamepad); |
| 153 |
| 154 // Dispatch on the main JS thread. |
| 155 main_message_loop_->PostTask( |
| 156 FROM_HERE, |
| 157 base::Bind(&GamepadSharedMemoryReader::DispatchGamepadConnected, this, |
| 158 index, gamepad)); |
| 159 } |
| 160 |
| 161 void GamepadSharedMemoryReader::OnGamepadDisconnected( |
| 162 const GamepadConnectionEventMessageParams& params) { |
| 163 int index = params.index; |
| 164 blink::WebGamepad gamepad; |
| 165 params.GetWebGamepad(&gamepad); |
| 166 |
| 167 // Dispatch on the main JS thread. |
| 168 main_message_loop_->PostTask( |
| 169 FROM_HERE, |
| 170 base::Bind(&GamepadSharedMemoryReader::DispatchGamepadDisconnected, this, |
| 171 index, gamepad)); |
| 172 } |
| 173 |
| 174 void GamepadSharedMemoryReader::DispatchGamepadConnected( |
| 175 int index, |
| 176 const blink::WebGamepad& gamepad) { |
| 177 if (gamepad_listener_) |
| 178 gamepad_listener_->didConnectGamepad(index, gamepad); |
| 179 } |
| 180 |
| 181 void GamepadSharedMemoryReader::DispatchGamepadDisconnected( |
| 182 int index, |
| 183 const blink::WebGamepad& gamepad) { |
| 184 if (gamepad_listener_) |
| 185 gamepad_listener_->didDisconnectGamepad(index, gamepad); |
93 } | 186 } |
94 | 187 |
95 } // namespace content | 188 } // namespace content |
OLD | NEW |