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

Side by Side Diff: content/browser/renderer_host/gamepad_browser_message_filter.cc

Issue 133943002: Gamepad API support for chrome on android (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 6 years, 11 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 3 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 4 // found in the LICENSE file.
4 5
6 #include "base/time/time.h"
5 #include "content/browser/renderer_host/gamepad_browser_message_filter.h" 7 #include "content/browser/renderer_host/gamepad_browser_message_filter.h"
6
7 #include "content/browser/gamepad/gamepad_service.h" 8 #include "content/browser/gamepad/gamepad_service.h"
8 #include "content/common/gamepad_messages.h" 9 #include "content/common/gamepad_messages.h"
9 10
10 namespace content { 11 namespace content {
11 12
12 GamepadBrowserMessageFilter::GamepadBrowserMessageFilter() 13 GamepadBrowserMessageFilter::GamepadBrowserMessageFilter()
13 : is_started_(false) { 14 : is_started_(false) {
14 } 15 }
15 16
16 GamepadBrowserMessageFilter::~GamepadBrowserMessageFilter() { 17 GamepadBrowserMessageFilter::~GamepadBrowserMessageFilter() {
17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 18 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
18 if (is_started_) 19 if (is_started_)
19 GamepadService::GetInstance()->RemoveConsumer(); 20 GamepadService::GetInstance()->RemoveConsumer();
20 } 21 }
21 22
22 bool GamepadBrowserMessageFilter::OnMessageReceived( 23 bool GamepadBrowserMessageFilter::OnMessageReceived(
23 const IPC::Message& message, 24 const IPC::Message& message,
24 bool* message_was_ok) { 25 bool* message_was_ok) {
25 bool handled = true; 26 bool handled = true;
26 IPC_BEGIN_MESSAGE_MAP_EX(GamepadBrowserMessageFilter, 27 IPC_BEGIN_MESSAGE_MAP_EX(GamepadBrowserMessageFilter,
27 message, 28 message,
28 *message_was_ok) 29 *message_was_ok)
29 IPC_MESSAGE_HANDLER(GamepadHostMsg_StartPolling, OnGamepadStartPolling) 30 IPC_MESSAGE_HANDLER(GamepadHostMsg_StartPolling, OnGamepadStartPolling)
30 IPC_MESSAGE_HANDLER(GamepadHostMsg_StopPolling, OnGamepadStopPolling) 31 IPC_MESSAGE_HANDLER(GamepadHostMsg_StopPolling, OnGamepadStopPolling)
32 #if defined(OS_ANDROID)
33 IPC_MESSAGE_HANDLER(GamepadHostMsg_ResumePolling, OnGamepadResumePolling)
34 IPC_MESSAGE_HANDLER(GamepadHostMsg_PausePolling, OnGamepadPausePolling)
35 IPC_MESSAGE_HANDLER(GamepadHostMsg_UpdateTimestamp,
36 OnGamepadUpdateTimestamp)
37 #endif
31 IPC_MESSAGE_UNHANDLED(handled = false) 38 IPC_MESSAGE_UNHANDLED(handled = false)
32 IPC_END_MESSAGE_MAP_EX() 39 IPC_END_MESSAGE_MAP_EX()
33 return handled; 40 return handled;
34 } 41 }
35 42
36 void GamepadBrowserMessageFilter::OnGamepadStartPolling( 43 void GamepadBrowserMessageFilter::OnGamepadStartPolling(
37 base::SharedMemoryHandle* renderer_handle) { 44 base::SharedMemoryHandle* renderer_handle) {
38 GamepadService* service = GamepadService::GetInstance(); 45 GamepadService* service = GamepadService::GetInstance();
39 if (!is_started_) { 46 if (!is_started_) {
40 is_started_ = true; 47 is_started_ = true;
41 service->AddConsumer(); 48 service->AddConsumer();
42 *renderer_handle = service->GetSharedMemoryHandleForProcess(PeerHandle()); 49 *renderer_handle = service->GetSharedMemoryHandleForProcess(PeerHandle());
43 } else { 50 } else {
44 // Currently we only expect the renderer to tell us once to start. 51 // Currently we only expect the renderer to tell us once to start.
45 NOTREACHED(); 52 NOTREACHED();
46 } 53 }
47 } 54 }
48 55
49 void GamepadBrowserMessageFilter::OnGamepadStopPolling() { 56 void GamepadBrowserMessageFilter::OnGamepadStopPolling() {
50 // TODO(scottmg): Probably get rid of this message. We can't trust it will 57 // TODO(scottmg): Probably get rid of this message. We can't trust it will
51 // arrive anyway if the renderer crashes, etc. 58 // arrive anyway if the renderer crashes, etc.
52 if (is_started_) { 59 if (is_started_) {
53 is_started_ = false; 60 is_started_ = false;
54 GamepadService::GetInstance()->RemoveConsumer(); 61 GamepadService::GetInstance()->RemoveConsumer();
55 } else { 62 } else {
56 NOTREACHED(); 63 NOTREACHED();
57 } 64 }
58 } 65 }
59 66
67 #if defined(OS_ANDROID)
68 void GamepadBrowserMessageFilter::OnGamepadResumePolling() {
69 // The renderer will put the background polling thread to resume state.
70 if (is_started_ && GamepadService::GetInstance()->GetPollState()) {
71 GamepadService::GetInstance()->Resume();
72 } else {
73 NOTREACHED();
74 }
75 }
76
77 void GamepadBrowserMessageFilter::OnGamepadPausePolling() {
78 // The renderer will put the background polling thread to pause state.
79 if (is_started_ && !GamepadService::GetInstance()->GetPollState()) {
80 GamepadService::GetInstance()->Pause();
81 } else {
82 NOTREACHED();
83 }
84 }
85
86 void GamepadBrowserMessageFilter::OnGamepadUpdateTimestamp() {
87 // The renderer will set the timestamp for last gamepad data access. This way
88 // the polling thread can be put to pause state when webpage stops accessing
89 // data i.e browser minimize or tab change.
90 // TODO: Currently this approach is expensive. We need to get rid of this
91 // message and set the polling thread to pause state everytime gamepad data
92 // access stops. One possible way is tracking the focus change on the
93 // webpage.
94 GamepadService::GetInstance()->SetGamepadAccessTimestamp(
95 base::Time::NowFromSystemTime());
96 OnGamepadResumePolling();
97 }
98 #endif
99
60 } // namespace content 100 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698