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

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

Issue 195873019: Gamepad API: add support for connection events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: and a missing override Created 6 years, 7 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"
13 #include "third_party/WebKit/public/platform/WebGamepadListener.h"
14 14
15 namespace content { 15 namespace content {
16 16
17 GamepadSharedMemoryReader::GamepadSharedMemoryReader() 17 GamepadSharedMemoryReader::GamepadSharedMemoryReader()
18 : gamepad_hardware_buffer_(NULL), 18 : gamepad_hardware_buffer_(NULL),
19 gamepad_listener_(NULL),
20 is_polling_(false),
19 ever_interacted_with_(false) { 21 ever_interacted_with_(false) {
22 }
23
24 void GamepadSharedMemoryReader::StartPollingIfNecessary() {
25 if (is_polling_)
26 return;
27
20 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( 28 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling(
21 &renderer_shared_memory_handle_))); 29 &renderer_shared_memory_handle_)));
30
22 // If we don't get a valid handle from the browser, don't try to Map (we're 31 // 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). 32 // probably out of memory or file handles).
24 bool valid_handle = base::SharedMemory::IsHandleValid( 33 bool valid_handle = base::SharedMemory::IsHandleValid(
25 renderer_shared_memory_handle_); 34 renderer_shared_memory_handle_);
26 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); 35 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle);
27 if (!valid_handle) 36 if (!valid_handle)
28 return; 37 return;
38
29 renderer_shared_memory_.reset( 39 renderer_shared_memory_.reset(
30 new base::SharedMemory(renderer_shared_memory_handle_, true)); 40 new base::SharedMemory(renderer_shared_memory_handle_, true));
31 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); 41 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer)));
32 void *memory = renderer_shared_memory_->memory(); 42 void *memory = renderer_shared_memory_->memory();
33 CHECK(memory); 43 CHECK(memory);
34 gamepad_hardware_buffer_ = 44 gamepad_hardware_buffer_ =
35 static_cast<GamepadHardwareBuffer*>(memory); 45 static_cast<GamepadHardwareBuffer*>(memory);
46
47 is_polling_ = true;
48 }
49
50 void GamepadSharedMemoryReader::StopPollingIfNecessary() {
51 if (is_polling_) {
52 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
53 is_polling_ = false;
54 }
36 } 55 }
37 56
38 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { 57 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) {
58 // Blink should set the listener before start sampling.
59 CHECK(gamepad_listener_);
60
61 StartPollingIfNecessary();
62 if (!is_polling_)
63 return;
64
39 // ========== 65 // ==========
40 // DANGER 66 // DANGER
41 // ========== 67 // ==========
42 // 68 //
43 // This logic is duplicated in Pepper as well. If you change it, that also 69 // 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. 70 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc.
45 blink::WebGamepads read_into; 71 blink::WebGamepads read_into;
46 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); 72 TRACE_EVENT0("GAMEPAD", "SampleGamepads");
47 73
48 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) 74 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 107 // 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. 108 // gamepads to prevent fingerprinting. The actual data is not cleared.
83 // 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
84 // gamepads so this is sufficient. 110 // gamepads so this is sufficient.
85 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) 111 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++)
86 gamepads.items[i].connected = false; 112 gamepads.items[i].connected = false;
87 } 113 }
88 } 114 }
89 } 115 }
90 116
117 void GamepadSharedMemoryReader::SetGamepadListener(
118 blink::WebGamepadListener* listener) {
119 gamepad_listener_ = listener;
120 if (gamepad_listener_) {
121 // Polling has to be started rigth now and not just on the first sampling
122 // because want to get connection events from now.
123 StartPollingIfNecessary();
124 } else {
125 StopPollingIfNecessary();
126 }
127 }
128
91 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { 129 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
92 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); 130 StopPollingIfNecessary();
131 }
132
133 bool GamepadSharedMemoryReader::OnControlMessageReceived(
134 const IPC::Message& message) {
135 bool handled = true;
136 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message)
137 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected)
138 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected)
139 IPC_MESSAGE_UNHANDLED(handled = false)
140 IPC_END_MESSAGE_MAP()
141 return handled;
142 }
143
144 void GamepadSharedMemoryReader::OnGamepadConnected(
145 int index,
146 const blink::WebGamepad& gamepad) {
147 if (gamepad_listener_)
148 gamepad_listener_->didConnectGamepad(index, gamepad);
149 }
150
151 void GamepadSharedMemoryReader::OnGamepadDisconnected(
152 int index,
153 const blink::WebGamepad& gamepad) {
154 if (gamepad_listener_)
155 gamepad_listener_->didDisconnectGamepad(index, gamepad);
93 } 156 }
94 157
95 } // namespace content 158 } // 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