summaryrefslogtreecommitdiffstats
path: root/content/renderer/gamepad_shared_memory_reader.cc
diff options
context:
space:
mode:
authorb.kelemen@samsung.com <b.kelemen@samsung.com@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-09 09:09:47 +0000
committerb.kelemen@samsung.com <b.kelemen@samsung.com@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-09 09:09:47 +0000
commit0ff7365690e19be1f20fe83ab9001205989ae13f (patch)
tree5781167bec440aeaec1be61d65010b17f91bdc45 /content/renderer/gamepad_shared_memory_reader.cc
parent32080c3798792f0f2a46b3da88971a4125690a08 (diff)
downloadchromium_src-0ff7365690e19be1f20fe83ab9001205989ae13f.zip
chromium_src-0ff7365690e19be1f20fe83ab9001205989ae13f.tar.gz
chromium_src-0ff7365690e19be1f20fe83ab9001205989ae13f.tar.bz2
Gamepad API: add support for connection events
Co-authored with Brandon Jones. This CL continues the work of updating the Gamepad API to latest spec. IPC's added for connected and disconnected events. The connection state is observed by GamepadProvider together with polling. The other option would be to add logic to each platform fetcher. Doing it in GamepadProvider avoids duplicated logic and I think it is a bit simpler and more consistent with the polling base nature of the gamepad implementation. Extra care has been taken to make it consistent with the policy of not exposing gamepad data to the page before a user gesture is observed. When a new page starts listening it will not get any events until we see a user gesture. When we see it we notify it about all the connected pads. Now we stop polling as soon as blink is no more interested in gamepad data. BUG=344556 Review URL: https://codereview.chromium.org/195873019 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269189 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/renderer/gamepad_shared_memory_reader.cc')
-rw-r--r--content/renderer/gamepad_shared_memory_reader.cc67
1 files changed, 65 insertions, 2 deletions
diff --git a/content/renderer/gamepad_shared_memory_reader.cc b/content/renderer/gamepad_shared_memory_reader.cc
index 23edeb3..f1b1e83 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