Index: content/renderer/gamepad_shared_memory_reader.cc |
diff --git a/content/renderer/gamepad_shared_memory_reader.cc b/content/renderer/gamepad_shared_memory_reader.cc |
index 23edeb3a22368251b5614e58c0ed136c505024e9..f1b1e835144c85ab26a7ee33fb1599d07a809e0a 100644 |
--- a/content/renderer/gamepad_shared_memory_reader.cc |
+++ b/content/renderer/gamepad_shared_memory_reader.cc |
@@ -6,19 +6,28 @@ |
#include "base/debug/trace_event.h" |
#include "base/metrics/histogram.h" |
-#include "content/common/gamepad_messages.h" |
#include "content/common/gamepad_user_gesture.h" |
#include "content/public/renderer/render_thread.h" |
#include "content/common/gamepad_hardware_buffer.h" |
#include "ipc/ipc_sync_message_filter.h" |
+#include "third_party/WebKit/public/platform/WebGamepadListener.h" |
namespace content { |
GamepadSharedMemoryReader::GamepadSharedMemoryReader() |
: gamepad_hardware_buffer_(NULL), |
+ gamepad_listener_(NULL), |
+ is_polling_(false), |
ever_interacted_with_(false) { |
+} |
+ |
+void GamepadSharedMemoryReader::StartPollingIfNecessary() { |
+ if (is_polling_) |
+ return; |
+ |
CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( |
&renderer_shared_memory_handle_))); |
+ |
// If we don't get a valid handle from the browser, don't try to Map (we're |
// probably out of memory or file handles). |
bool valid_handle = base::SharedMemory::IsHandleValid( |
@@ -26,6 +35,7 @@ GamepadSharedMemoryReader::GamepadSharedMemoryReader() |
UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); |
if (!valid_handle) |
return; |
+ |
renderer_shared_memory_.reset( |
new base::SharedMemory(renderer_shared_memory_handle_, true)); |
CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); |
@@ -33,9 +43,25 @@ GamepadSharedMemoryReader::GamepadSharedMemoryReader() |
CHECK(memory); |
gamepad_hardware_buffer_ = |
static_cast<GamepadHardwareBuffer*>(memory); |
+ |
+ is_polling_ = true; |
+} |
+ |
+void GamepadSharedMemoryReader::StopPollingIfNecessary() { |
+ if (is_polling_) { |
+ RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); |
+ is_polling_ = false; |
+ } |
} |
void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { |
+ // Blink should set the listener before start sampling. |
+ CHECK(gamepad_listener_); |
+ |
+ StartPollingIfNecessary(); |
+ if (!is_polling_) |
+ return; |
+ |
// ========== |
// DANGER |
// ========== |
@@ -88,8 +114,45 @@ void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { |
} |
} |
+void GamepadSharedMemoryReader::SetGamepadListener( |
+ blink::WebGamepadListener* listener) { |
+ gamepad_listener_ = listener; |
+ if (gamepad_listener_) { |
+ // Polling has to be started rigth now and not just on the first sampling |
+ // because want to get connection events from now. |
+ StartPollingIfNecessary(); |
+ } else { |
+ StopPollingIfNecessary(); |
+ } |
+} |
+ |
GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
- RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); |
+ StopPollingIfNecessary(); |
+} |
+ |
+bool GamepadSharedMemoryReader::OnControlMessageReceived( |
+ const IPC::Message& message) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message) |
+ IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected) |
+ IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ return handled; |
+} |
+ |
+void GamepadSharedMemoryReader::OnGamepadConnected( |
+ int index, |
+ const blink::WebGamepad& gamepad) { |
+ if (gamepad_listener_) |
+ gamepad_listener_->didConnectGamepad(index, gamepad); |
+} |
+ |
+void GamepadSharedMemoryReader::OnGamepadDisconnected( |
+ int index, |
+ const blink::WebGamepad& gamepad) { |
+ if (gamepad_listener_) |
+ gamepad_listener_->didDisconnectGamepad(index, gamepad); |
} |
} // namespace content |