Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: content/renderer/gamepad_shared_memory_reader.cc

Issue 200873002: Gamepad API: add support for connection events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/renderer/gamepad_shared_memory_reader.h ('k') | content/renderer/render_thread_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
14 13
15 namespace content { 14 namespace content {
16 15
17 GamepadSharedMemoryReader::GamepadSharedMemoryReader() 16 GamepadSharedMemoryReader::GamepadSharedMemoryReader(
18 : gamepad_hardware_buffer_(NULL), 17 const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
18 : io_message_loop_(io_message_loop),
19 main_message_loop_(base::MessageLoopProxy::current()),
20 gamepad_hardware_buffer_(NULL),
21 gamepad_listener_(NULL),
22 is_polling_(false),
19 ever_interacted_with_(false) { 23 ever_interacted_with_(false) {
24 }
25
26 bool GamepadSharedMemoryReader::EnsurePollingStarted() {
27 if (is_polling_)
28 return true;
29
20 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( 30 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling(
21 &renderer_shared_memory_handle_))); 31 &renderer_shared_memory_handle_)));
32
22 // If we don't get a valid handle from the browser, don't try to Map (we're 33 // 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). 34 // probably out of memory or file handles).
24 bool valid_handle = base::SharedMemory::IsHandleValid( 35 bool valid_handle = base::SharedMemory::IsHandleValid(
25 renderer_shared_memory_handle_); 36 renderer_shared_memory_handle_);
26 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); 37 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle);
27 if (!valid_handle) 38 if (!valid_handle)
28 return; 39 return false;
40
29 renderer_shared_memory_.reset( 41 renderer_shared_memory_.reset(
30 new base::SharedMemory(renderer_shared_memory_handle_, true)); 42 new base::SharedMemory(renderer_shared_memory_handle_, true));
31 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); 43 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer)));
32 void *memory = renderer_shared_memory_->memory(); 44 void *memory = renderer_shared_memory_->memory();
33 CHECK(memory); 45 CHECK(memory);
34 gamepad_hardware_buffer_ = 46 gamepad_hardware_buffer_ =
35 static_cast<GamepadHardwareBuffer*>(memory); 47 static_cast<GamepadHardwareBuffer*>(memory);
48
49 is_polling_ = true;
50 return true;
36 } 51 }
37 52
38 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { 53 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) {
54 if (!EnsurePollingStarted())
55 return;
56
39 // ========== 57 // ==========
40 // DANGER 58 // DANGER
41 // ========== 59 // ==========
42 // 60 //
43 // This logic is duplicated in Pepper as well. If you change it, that also 61 // 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. 62 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc.
45 blink::WebGamepads read_into; 63 blink::WebGamepads read_into;
46 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); 64 TRACE_EVENT0("GAMEPAD", "SampleGamepads");
47 65
48 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) 66 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 // Clear the connected flag if the user hasn't interacted with any of the 99 // 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. 100 // gamepads to prevent fingerprinting. The actual data is not cleared.
83 // WebKit will only copy out data into the JS buffers for connected 101 // WebKit will only copy out data into the JS buffers for connected
84 // gamepads so this is sufficient. 102 // gamepads so this is sufficient.
85 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) 103 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++)
86 gamepads.items[i].connected = false; 104 gamepads.items[i].connected = false;
87 } 105 }
88 } 106 }
89 } 107 }
90 108
109 void GamepadSharedMemoryReader::SetGamepadListener(
110 blink::WebGamepadListener* listener) {
111 gamepad_listener_ = listener;
112 }
113
91 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { 114 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
92 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); 115 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
93 } 116 }
94 117
118 bool GamepadSharedMemoryReader::OnMessageReceived(const IPC::Message& message) {
119 DCHECK(io_message_loop_->BelongsToCurrentThread());
120 bool handled = true;
121 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message)
122 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected)
123 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected)
124 IPC_MESSAGE_UNHANDLED(handled = false)
125 IPC_END_MESSAGE_MAP()
126 return handled;
127 }
128
129 void GamepadSharedMemoryReader::OnGamepadConnected(
130 const GamepadConnectionEventMessageParams& params) {
131 int index = params.index;
132 blink::WebGamepad gamepad;
133 params.GetWebGamepad(&gamepad);
134
135 // Dispatch on the main JS thread.
136 main_message_loop_->PostTask(
137 FROM_HERE,
138 base::Bind(&GamepadSharedMemoryReader::DispatchGamepadConnected, this,
139 index, gamepad));
140 }
141
142 void GamepadSharedMemoryReader::OnGamepadDisconnected(
143 const GamepadConnectionEventMessageParams& params) {
144 int index = params.index;
145 blink::WebGamepad gamepad;
146 params.GetWebGamepad(&gamepad);
147
148 // Dispatch on the main JS thread.
149 main_message_loop_->PostTask(
150 FROM_HERE,
151 base::Bind(&GamepadSharedMemoryReader::DispatchGamepadDisconnected, this,
152 index, gamepad));
153 }
154
155 void GamepadSharedMemoryReader::DispatchGamepadConnected(
156 int index,
157 const blink::WebGamepad& gamepad) {
158 if (gamepad_listener_)
159 gamepad_listener_->onGamepadConnected(index, gamepad);
160 }
161
162 void GamepadSharedMemoryReader::DispatchGamepadDisconnected(
163 int index,
164 const blink::WebGamepad& gamepad) {
165 if (gamepad_listener_)
166 gamepad_listener_->onGamepadDisconnected(index, gamepad);
167 }
168
95 } // namespace content 169 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/gamepad_shared_memory_reader.h ('k') | content/renderer/render_thread_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698