| 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_hardware_buffer.h" | 9 #include "content/common/gamepad_hardware_buffer.h" |
| 10 #include "content/common/gamepad_user_gesture.h" | 10 #include "content/common/gamepad_user_gesture.h" |
| 11 #include "content/public/renderer/render_thread.h" | 11 #include "content/public/renderer/render_thread.h" |
| 12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | 12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| 13 #include "ipc/ipc_sync_message_filter.h" | 13 #include "ipc/ipc_sync_message_filter.h" |
| 14 #include "third_party/WebKit/public/platform/WebGamepadListener.h" | 14 #include "third_party/WebKit/public/platform/WebGamepadListener.h" |
| 15 #include "third_party/WebKit/public/platform/WebPlatformEventListener.h" | |
| 16 | 15 |
| 17 namespace content { | 16 namespace content { |
| 18 | 17 |
| 19 GamepadSharedMemoryReader::GamepadSharedMemoryReader(RenderThread* thread) | 18 GamepadSharedMemoryReader::GamepadSharedMemoryReader( |
| 20 : RendererGamepadProvider(thread), | 19 RendererWebKitPlatformSupportImpl* webkit_platform_support) |
| 21 gamepad_hardware_buffer_(NULL), | 20 : gamepad_hardware_buffer_(NULL), |
| 21 gamepad_listener_(NULL), |
| 22 is_polling_(false), |
| 22 ever_interacted_with_(false) { | 23 ever_interacted_with_(false) { |
| 24 webkit_platform_support->set_gamepad_provider(this); |
| 23 } | 25 } |
| 24 | 26 |
| 25 void GamepadSharedMemoryReader::SendStartMessage() { | 27 void GamepadSharedMemoryReader::StartPollingIfNecessary() { |
| 28 if (is_polling_) |
| 29 return; |
| 30 |
| 26 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( | 31 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( |
| 27 &renderer_shared_memory_handle_))); | 32 &renderer_shared_memory_handle_))); |
| 28 } | |
| 29 | |
| 30 void GamepadSharedMemoryReader::SendStopMessage() { | |
| 31 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); | |
| 32 } | |
| 33 | |
| 34 void GamepadSharedMemoryReader::Start( | |
| 35 blink::WebPlatformEventListener* listener) { | |
| 36 PlatformEventObserver::Start(listener); | |
| 37 | 33 |
| 38 // 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 |
| 39 // probably out of memory or file handles). | 35 // probably out of memory or file handles). |
| 40 bool valid_handle = base::SharedMemory::IsHandleValid( | 36 bool valid_handle = base::SharedMemory::IsHandleValid( |
| 41 renderer_shared_memory_handle_); | 37 renderer_shared_memory_handle_); |
| 42 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); | 38 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); |
| 43 if (!valid_handle) | 39 if (!valid_handle) |
| 44 return; | 40 return; |
| 45 | 41 |
| 46 renderer_shared_memory_.reset( | 42 renderer_shared_memory_.reset( |
| 47 new base::SharedMemory(renderer_shared_memory_handle_, true)); | 43 new base::SharedMemory(renderer_shared_memory_handle_, true)); |
| 48 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); | 44 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); |
| 49 void *memory = renderer_shared_memory_->memory(); | 45 void *memory = renderer_shared_memory_->memory(); |
| 50 CHECK(memory); | 46 CHECK(memory); |
| 51 gamepad_hardware_buffer_ = | 47 gamepad_hardware_buffer_ = |
| 52 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 } |
| 53 } | 58 } |
| 54 | 59 |
| 55 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { | 60 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { |
| 56 // Blink should have started observing at that point. | 61 // Blink should set the listener before start sampling. |
| 57 CHECK(is_observing()); | 62 CHECK(gamepad_listener_); |
| 63 |
| 64 StartPollingIfNecessary(); |
| 65 if (!is_polling_) |
| 66 return; |
| 58 | 67 |
| 59 // ========== | 68 // ========== |
| 60 // DANGER | 69 // DANGER |
| 61 // ========== | 70 // ========== |
| 62 // | 71 // |
| 63 // 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 |
| 64 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. | 73 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. |
| 65 blink::WebGamepads read_into; | 74 blink::WebGamepads read_into; |
| 66 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); | 75 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); |
| 67 | 76 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 97 if (!ever_interacted_with_) { | 106 if (!ever_interacted_with_) { |
| 98 // Clear the connected flag if the user hasn't interacted with any of the | 107 // Clear the connected flag if the user hasn't interacted with any of the |
| 99 // gamepads to prevent fingerprinting. The actual data is not cleared. | 108 // gamepads to prevent fingerprinting. The actual data is not cleared. |
| 100 // WebKit will only copy out data into the JS buffers for connected | 109 // WebKit will only copy out data into the JS buffers for connected |
| 101 // gamepads so this is sufficient. | 110 // gamepads so this is sufficient. |
| 102 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) | 111 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) |
| 103 gamepads.items[i].connected = false; | 112 gamepads.items[i].connected = false; |
| 104 } | 113 } |
| 105 } | 114 } |
| 106 | 115 |
| 116 void GamepadSharedMemoryReader::SetGamepadListener( |
| 117 blink::WebGamepadListener* listener) { |
| 118 gamepad_listener_ = listener; |
| 119 if (gamepad_listener_) { |
| 120 // Polling has to be started rigth now and not just on the first sampling |
| 121 // because want to get connection events from now. |
| 122 StartPollingIfNecessary(); |
| 123 } else { |
| 124 StopPollingIfNecessary(); |
| 125 } |
| 126 } |
| 127 |
| 107 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | 128 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
| 129 StopPollingIfNecessary(); |
| 108 } | 130 } |
| 109 | 131 |
| 110 bool GamepadSharedMemoryReader::OnControlMessageReceived( | 132 bool GamepadSharedMemoryReader::OnControlMessageReceived( |
| 111 const IPC::Message& message) { | 133 const IPC::Message& message) { |
| 112 bool handled = true; | 134 bool handled = true; |
| 113 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message) | 135 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message) |
| 114 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected) | 136 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected) |
| 115 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected) | 137 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected) |
| 116 IPC_MESSAGE_UNHANDLED(handled = false) | 138 IPC_MESSAGE_UNHANDLED(handled = false) |
| 117 IPC_END_MESSAGE_MAP() | 139 IPC_END_MESSAGE_MAP() |
| 118 return handled; | 140 return handled; |
| 119 } | 141 } |
| 120 | 142 |
| 121 void GamepadSharedMemoryReader::OnGamepadConnected( | 143 void GamepadSharedMemoryReader::OnGamepadConnected( |
| 122 int index, | 144 int index, |
| 123 const blink::WebGamepad& gamepad) { | 145 const blink::WebGamepad& gamepad) { |
| 124 // The browser already checks if the user actually interacted with a device. | 146 // The browser already checks if the user actually interacted with a device. |
| 125 ever_interacted_with_ = true; | 147 ever_interacted_with_ = true; |
| 126 | 148 |
| 127 if (listener()) | 149 if (gamepad_listener_) |
| 128 listener()->didConnectGamepad(index, gamepad); | 150 gamepad_listener_->didConnectGamepad(index, gamepad); |
| 129 } | 151 } |
| 130 | 152 |
| 131 void GamepadSharedMemoryReader::OnGamepadDisconnected( | 153 void GamepadSharedMemoryReader::OnGamepadDisconnected( |
| 132 int index, | 154 int index, |
| 133 const blink::WebGamepad& gamepad) { | 155 const blink::WebGamepad& gamepad) { |
| 134 if (listener()) | 156 if (gamepad_listener_) |
| 135 listener()->didDisconnectGamepad(index, gamepad); | 157 gamepad_listener_->didDisconnectGamepad(index, gamepad); |
| 136 } | 158 } |
| 137 | 159 |
| 138 } // namespace content | 160 } // namespace content |
| OLD | NEW |