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

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

Issue 446603002: Refactor code listening to platform events in content/renderer/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webkitplatform_impl_start_stop
Patch Set: cleaner Created 6 years, 4 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
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_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"
15 16
16 namespace content { 17 namespace content {
17 18
18 GamepadSharedMemoryReader::GamepadSharedMemoryReader( 19 GamepadSharedMemoryReader::GamepadSharedMemoryReader(RenderThread* thread)
19 RendererWebKitPlatformSupportImpl* webkit_platform_support) 20 : RendererGamepadProvider(thread),
20 : gamepad_hardware_buffer_(NULL), 21 gamepad_hardware_buffer_(NULL),
21 gamepad_listener_(NULL),
22 is_polling_(false),
23 ever_interacted_with_(false) { 22 ever_interacted_with_(false) {
24 webkit_platform_support->set_gamepad_provider(this);
25 } 23 }
26 24
27 void GamepadSharedMemoryReader::StartPollingIfNecessary() { 25 void GamepadSharedMemoryReader::SendStartMessage() {
28 if (is_polling_)
29 return;
30
31 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling( 26 CHECK(RenderThread::Get()->Send(new GamepadHostMsg_StartPolling(
32 &renderer_shared_memory_handle_))); 27 &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);
33 37
34 // If we don't get a valid handle from the browser, don't try to Map (we're 38 // If we don't get a valid handle from the browser, don't try to Map (we're
35 // probably out of memory or file handles). 39 // probably out of memory or file handles).
36 bool valid_handle = base::SharedMemory::IsHandleValid( 40 bool valid_handle = base::SharedMemory::IsHandleValid(
37 renderer_shared_memory_handle_); 41 renderer_shared_memory_handle_);
38 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); 42 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle);
39 if (!valid_handle) 43 if (!valid_handle)
40 return; 44 return;
41 45
42 renderer_shared_memory_.reset( 46 renderer_shared_memory_.reset(
43 new base::SharedMemory(renderer_shared_memory_handle_, true)); 47 new base::SharedMemory(renderer_shared_memory_handle_, true));
44 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); 48 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer)));
45 void *memory = renderer_shared_memory_->memory(); 49 void *memory = renderer_shared_memory_->memory();
46 CHECK(memory); 50 CHECK(memory);
47 gamepad_hardware_buffer_ = 51 gamepad_hardware_buffer_ =
48 static_cast<GamepadHardwareBuffer*>(memory); 52 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 }
58 } 53 }
59 54
60 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { 55 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) {
61 // Blink should set the listener before start sampling. 56 // Blink should have started observing at that point.
62 CHECK(gamepad_listener_); 57 CHECK(IsObserving());
63
64 StartPollingIfNecessary();
65 if (!is_polling_)
66 return;
67 58
68 // ========== 59 // ==========
69 // DANGER 60 // DANGER
70 // ========== 61 // ==========
71 // 62 //
72 // This logic is duplicated in Pepper as well. If you change it, that also 63 // This logic is duplicated in Pepper as well. If you change it, that also
73 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. 64 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc.
74 blink::WebGamepads read_into; 65 blink::WebGamepads read_into;
75 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); 66 TRACE_EVENT0("GAMEPAD", "SampleGamepads");
76 67
(...skipping 29 matching lines...) Expand all
106 if (!ever_interacted_with_) { 97 if (!ever_interacted_with_) {
107 // Clear the connected flag if the user hasn't interacted with any of the 98 // Clear the connected flag if the user hasn't interacted with any of the
108 // gamepads to prevent fingerprinting. The actual data is not cleared. 99 // gamepads to prevent fingerprinting. The actual data is not cleared.
109 // WebKit will only copy out data into the JS buffers for connected 100 // WebKit will only copy out data into the JS buffers for connected
110 // gamepads so this is sufficient. 101 // gamepads so this is sufficient.
111 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++) 102 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; i++)
112 gamepads.items[i].connected = false; 103 gamepads.items[i].connected = false;
113 } 104 }
114 } 105 }
115 106
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
128 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { 107 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
129 StopPollingIfNecessary();
130 } 108 }
131 109
132 bool GamepadSharedMemoryReader::OnControlMessageReceived( 110 bool GamepadSharedMemoryReader::OnControlMessageReceived(
133 const IPC::Message& message) { 111 const IPC::Message& message) {
134 bool handled = true; 112 bool handled = true;
135 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message) 113 IPC_BEGIN_MESSAGE_MAP(GamepadSharedMemoryReader, message)
136 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected) 114 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadConnected, OnGamepadConnected)
137 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected) 115 IPC_MESSAGE_HANDLER(GamepadMsg_GamepadDisconnected, OnGamepadDisconnected)
138 IPC_MESSAGE_UNHANDLED(handled = false) 116 IPC_MESSAGE_UNHANDLED(handled = false)
139 IPC_END_MESSAGE_MAP() 117 IPC_END_MESSAGE_MAP()
140 return handled; 118 return handled;
141 } 119 }
142 120
143 void GamepadSharedMemoryReader::OnGamepadConnected( 121 void GamepadSharedMemoryReader::OnGamepadConnected(
144 int index, 122 int index,
145 const blink::WebGamepad& gamepad) { 123 const blink::WebGamepad& gamepad) {
146 // The browser already checks if the user actually interacted with a device. 124 // The browser already checks if the user actually interacted with a device.
147 ever_interacted_with_ = true; 125 ever_interacted_with_ = true;
148 126
149 if (gamepad_listener_) 127 if (listener_)
scottmg 2014/08/05 21:09:19 I'm sorry, I don't see where listener_ is set.
mlamouri (slow - plz ping) 2014/08/05 21:32:37 I switched to |listener()|.
150 gamepad_listener_->didConnectGamepad(index, gamepad); 128 listener_->didConnectGamepad(index, gamepad);
151 } 129 }
152 130
153 void GamepadSharedMemoryReader::OnGamepadDisconnected( 131 void GamepadSharedMemoryReader::OnGamepadDisconnected(
154 int index, 132 int index,
155 const blink::WebGamepad& gamepad) { 133 const blink::WebGamepad& gamepad) {
156 if (gamepad_listener_) 134 if (listener_)
157 gamepad_listener_->didDisconnectGamepad(index, gamepad); 135 listener_->didDisconnectGamepad(index, gamepad);
158 } 136 }
159 137
160 } // namespace content 138 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698