OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/time/time.h" | |
scottmg
2014/02/06 21:17:07
This should go below the include of gamepad_browse
| |
5 #include "content/browser/renderer_host/gamepad_browser_message_filter.h" | 6 #include "content/browser/renderer_host/gamepad_browser_message_filter.h" |
6 | |
7 #include "content/browser/gamepad/gamepad_service.h" | 7 #include "content/browser/gamepad/gamepad_service.h" |
8 #include "content/common/gamepad_messages.h" | 8 #include "content/common/gamepad_messages.h" |
9 | 9 |
10 namespace content { | 10 namespace content { |
11 | 11 |
12 GamepadBrowserMessageFilter::GamepadBrowserMessageFilter() | 12 GamepadBrowserMessageFilter::GamepadBrowserMessageFilter() |
13 : is_started_(false) { | 13 : is_started_(false) { |
14 } | 14 } |
15 | 15 |
16 GamepadBrowserMessageFilter::~GamepadBrowserMessageFilter() { | 16 GamepadBrowserMessageFilter::~GamepadBrowserMessageFilter() { |
17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
18 if (is_started_) | 18 if (is_started_) |
19 GamepadService::GetInstance()->RemoveConsumer(); | 19 GamepadService::GetInstance()->RemoveConsumer(); |
20 } | 20 } |
21 | 21 |
22 bool GamepadBrowserMessageFilter::OnMessageReceived( | 22 bool GamepadBrowserMessageFilter::OnMessageReceived( |
23 const IPC::Message& message, | 23 const IPC::Message& message, |
24 bool* message_was_ok) { | 24 bool* message_was_ok) { |
25 bool handled = true; | 25 bool handled = true; |
26 IPC_BEGIN_MESSAGE_MAP_EX(GamepadBrowserMessageFilter, | 26 IPC_BEGIN_MESSAGE_MAP_EX(GamepadBrowserMessageFilter, |
27 message, | 27 message, |
28 *message_was_ok) | 28 *message_was_ok) |
29 IPC_MESSAGE_HANDLER(GamepadHostMsg_StartPolling, OnGamepadStartPolling) | 29 IPC_MESSAGE_HANDLER(GamepadHostMsg_StartPolling, OnGamepadStartPolling) |
30 IPC_MESSAGE_HANDLER(GamepadHostMsg_StopPolling, OnGamepadStopPolling) | 30 IPC_MESSAGE_HANDLER(GamepadHostMsg_StopPolling, OnGamepadStopPolling) |
31 IPC_MESSAGE_HANDLER(GamepadHostMsg_ResumePolling, OnGamepadResumePolling) | |
32 IPC_MESSAGE_HANDLER(GamepadHostMsg_PausePolling, OnGamepadPausePolling) | |
33 IPC_MESSAGE_HANDLER(GamepadHostMsg_UpdateTimestamp, | |
34 OnGamepadUpdateTimestamp) | |
scottmg
2014/02/06 21:17:07
nit; align at (.
| |
31 IPC_MESSAGE_UNHANDLED(handled = false) | 35 IPC_MESSAGE_UNHANDLED(handled = false) |
32 IPC_END_MESSAGE_MAP_EX() | 36 IPC_END_MESSAGE_MAP_EX() |
33 return handled; | 37 return handled; |
34 } | 38 } |
35 | 39 |
36 void GamepadBrowserMessageFilter::OnGamepadStartPolling( | 40 void GamepadBrowserMessageFilter::OnGamepadStartPolling( |
37 base::SharedMemoryHandle* renderer_handle) { | 41 base::SharedMemoryHandle* renderer_handle) { |
38 GamepadService* service = GamepadService::GetInstance(); | 42 GamepadService* service = GamepadService::GetInstance(); |
39 if (!is_started_) { | 43 if (!is_started_) { |
40 is_started_ = true; | 44 is_started_ = true; |
41 service->AddConsumer(); | 45 service->AddConsumer(); |
42 *renderer_handle = service->GetSharedMemoryHandleForProcess(PeerHandle()); | 46 *renderer_handle = service->GetSharedMemoryHandleForProcess(PeerHandle()); |
43 } else { | 47 } else { |
44 // Currently we only expect the renderer to tell us once to start. | 48 // Currently we only expect the renderer to tell us once to start. |
45 NOTREACHED(); | 49 NOTREACHED(); |
46 } | 50 } |
47 } | 51 } |
48 | 52 |
49 void GamepadBrowserMessageFilter::OnGamepadStopPolling() { | 53 void GamepadBrowserMessageFilter::OnGamepadStopPolling() { |
50 // TODO(scottmg): Probably get rid of this message. We can't trust it will | 54 // TODO(scottmg): Probably get rid of this message. We can't trust it will |
51 // arrive anyway if the renderer crashes, etc. | 55 // arrive anyway if the renderer crashes, etc. |
52 if (is_started_) { | 56 if (is_started_) { |
53 is_started_ = false; | 57 is_started_ = false; |
54 GamepadService::GetInstance()->RemoveConsumer(); | 58 GamepadService::GetInstance()->RemoveConsumer(); |
55 } else { | 59 } else { |
56 NOTREACHED(); | 60 NOTREACHED(); |
57 } | 61 } |
58 } | 62 } |
59 | 63 |
64 void GamepadBrowserMessageFilter::OnGamepadResumePolling() { | |
65 // The renderer will put the background polling thread to resume state. | |
66 if (is_started_ && GamepadService::GetInstance()->GetPollState()) { | |
67 GamepadService::GetInstance()->Resume(); | |
68 } else { | |
69 NOTREACHED(); | |
70 } | |
71 } | |
72 | |
73 void GamepadBrowserMessageFilter::OnGamepadPausePolling() { | |
74 // The renderer will put the background polling thread to pause state. | |
75 if (is_started_ && !GamepadService::GetInstance()->GetPollState()) { | |
76 GamepadService::GetInstance()->Pause(); | |
77 } else { | |
78 NOTREACHED(); | |
79 } | |
80 } | |
81 | |
82 void GamepadBrowserMessageFilter::OnGamepadUpdateTimestamp() { | |
83 // The renderer will set the timestamp for last gamepad data access. This way | |
84 // the polling thread can be put to pause state when webpage stops accessing | |
85 // data i.e browser minimize or tab change. | |
86 // TODO(SaurabhK): Currently this approach is expensive. We need to get rid | |
87 // of this message and set the polling thread to pause state everytime | |
88 // gamepad data access stops. One possible way is tracking the focus change | |
89 // on the webpage. | |
90 GamepadService::GetInstance()->SetGamepadAccessTimestamp( | |
91 base::Time::NowFromSystemTime()); | |
scottmg
2014/02/06 21:17:07
Ah, I see. Why can't we detect the situations abov
SaurabhK
2014/02/10 12:50:47
On 2014/02/06 21:17:07, scottmg wrote:
Regarding
| |
92 OnGamepadResumePolling(); | |
93 } | |
94 | |
60 } // namespace content | 95 } // namespace content |
OLD | NEW |