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/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
9 #include "content/common/gamepad_hardware_buffer.h" | 9 #include "content/common/gamepad_hardware_buffer.h" |
10 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
11 #include "content/renderer/renderer_blink_platform_impl.h" | 11 #include "content/renderer/renderer_blink_platform_impl.h" |
12 #include "ipc/ipc_sync_message_filter.h" | 12 #include "ipc/ipc_sync_message_filter.h" |
| 13 #include "services/service_manager/public/cpp/interface_provider.h" |
13 #include "third_party/WebKit/public/platform/WebGamepadListener.h" | 14 #include "third_party/WebKit/public/platform/WebGamepadListener.h" |
14 #include "third_party/WebKit/public/platform/WebPlatformEventListener.h" | 15 #include "third_party/WebKit/public/platform/WebPlatformEventListener.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 GamepadSharedMemoryReader::GamepadSharedMemoryReader(RenderThread* thread) | 19 GamepadSharedMemoryReader::GamepadSharedMemoryReader(RenderThread* thread) |
19 : RendererGamepadProvider(thread), | 20 : RendererGamepadProvider(thread), |
20 gamepad_hardware_buffer_(NULL), | 21 gamepad_hardware_buffer_(NULL), |
21 ever_interacted_with_(false) { | 22 ever_interacted_with_(false), |
| 23 binding_(this) { |
| 24 if (thread) { |
| 25 thread->GetRemoteInterfaces()->GetInterface( |
| 26 mojo::GetProxy(&gamepad_monitor_)); |
| 27 gamepad_monitor_->SetObserver(binding_.CreateInterfacePtrAndBind()); |
| 28 } |
22 } | 29 } |
23 | 30 |
24 void GamepadSharedMemoryReader::SendStartMessage() { | 31 void GamepadSharedMemoryReader::SendStartMessage() { |
25 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( | 32 if (gamepad_monitor_) { |
26 &renderer_shared_memory_handle_))); | 33 mojo::ScopedSharedBufferHandle buffer_handle; |
| 34 gamepad_monitor_->GamepadStartPolling(&buffer_handle); |
| 35 // TODO(heke): Use mojo::SharedBuffer rather than base::SharedMemory. See |
| 36 // crbug.com/670655. |
| 37 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 38 std::move(buffer_handle), &renderer_shared_memory_handle_, nullptr, |
| 39 nullptr); |
| 40 CHECK_EQ(MOJO_RESULT_OK, result); |
| 41 } |
27 } | 42 } |
28 | 43 |
29 void GamepadSharedMemoryReader::SendStopMessage() { | 44 void GamepadSharedMemoryReader::SendStopMessage() { |
30 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); | 45 if (gamepad_monitor_) { |
| 46 gamepad_monitor_->GamepadStopPolling(); |
| 47 } |
31 } | 48 } |
32 | 49 |
33 void GamepadSharedMemoryReader::Start( | 50 void GamepadSharedMemoryReader::Start( |
34 blink::WebPlatformEventListener* listener) { | 51 blink::WebPlatformEventListener* listener) { |
35 PlatformEventObserver::Start(listener); | 52 PlatformEventObserver::Start(listener); |
36 | 53 |
37 // If we don't get a valid handle from the browser, don't try to Map (we're | 54 // If we don't get a valid handle from the browser, don't try to Map (we're |
38 // probably out of memory or file handles). | 55 // probably out of memory or file handles). |
39 bool valid_handle = base::SharedMemory::IsHandleValid( | 56 bool valid_handle = base::SharedMemory::IsHandleValid( |
40 renderer_shared_memory_handle_); | 57 renderer_shared_memory_handle_); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // gamepads so this is sufficient. | 117 // gamepads so this is sufficient. |
101 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) | 118 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) |
102 gamepads.items[i].connected = false; | 119 gamepads.items[i].connected = false; |
103 } | 120 } |
104 } | 121 } |
105 | 122 |
106 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | 123 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
107 StopIfObserving(); | 124 StopIfObserving(); |
108 } | 125 } |
109 | 126 |
110 bool GamepadSharedMemoryReader::OnControlMessageReceived( | 127 void GamepadSharedMemoryReader::GamepadConnected( |
111 const IPC::Message& message) { | |
112 bool handled = true; | |
113 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message) | |
114 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected) | |
115 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected) | |
116 IPC_MESSAGE_UNHANDLED(handled = false) | |
117 IPC_END_MESSAGE_MAP() | |
118 return handled; | |
119 } | |
120 | |
121 void GamepadSharedMemoryReader::OnGamepadConnected( | |
122 int index, | 128 int index, |
123 const blink::WebGamepad& gamepad) { | 129 const blink::WebGamepad& gamepad) { |
124 // The browser already checks if the user actually interacted with a device. | 130 // The browser already checks if the user actually interacted with a device. |
125 ever_interacted_with_ = true; | 131 ever_interacted_with_ = true; |
126 | 132 |
127 if (listener()) | 133 if (listener()) |
128 listener()->didConnectGamepad(index, gamepad); | 134 listener()->didConnectGamepad(index, gamepad); |
129 } | 135 } |
130 | 136 |
131 void GamepadSharedMemoryReader::OnGamepadDisconnected( | 137 void GamepadSharedMemoryReader::GamepadDisconnected( |
132 int index, | 138 int index, |
133 const blink::WebGamepad& gamepad) { | 139 const blink::WebGamepad& gamepad) { |
134 if (listener()) | 140 if (listener()) |
135 listener()->didDisconnectGamepad(index, gamepad); | 141 listener()->didDisconnectGamepad(index, gamepad); |
136 } | 142 } |
137 | 143 |
138 } // namespace content | 144 } // namespace content |
OLD | NEW |