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 3 // found in the LICENSE file. 4 5 #include "content/browser/renderer_host/gamepad_browser_message_filter.h" 6 7 #include "content/browser/gamepad/gamepad_service.h" 8 #include "content/common/gamepad_messages.h" 9 10 namespace content { 11 GamepadBrowserMessageFilter()12GamepadBrowserMessageFilter::GamepadBrowserMessageFilter() 13 : is_started_(false) { 14 } 15 ~GamepadBrowserMessageFilter()16GamepadBrowserMessageFilter::~GamepadBrowserMessageFilter() { 17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 18 if (is_started_) 19 GamepadService::GetInstance()->RemoveConsumer(); 20 } 21 OnMessageReceived(const IPC::Message & message,bool * message_was_ok)22bool GamepadBrowserMessageFilter::OnMessageReceived( 23 const IPC::Message& message, 24 bool* message_was_ok) { 25 bool handled = true; 26 IPC_BEGIN_MESSAGE_MAP_EX(GamepadBrowserMessageFilter, 27 message, 28 *message_was_ok) 29 IPC_MESSAGE_HANDLER(GamepadHostMsg_StartPolling, OnGamepadStartPolling) 30 IPC_MESSAGE_HANDLER(GamepadHostMsg_StopPolling, OnGamepadStopPolling) 31 IPC_MESSAGE_UNHANDLED(handled = false) 32 IPC_END_MESSAGE_MAP_EX() 33 return handled; 34 } 35 OnGamepadStartPolling(base::SharedMemoryHandle * renderer_handle)36void GamepadBrowserMessageFilter::OnGamepadStartPolling( 37 base::SharedMemoryHandle* renderer_handle) { 38 GamepadService* service = GamepadService::GetInstance(); 39 if (!is_started_) { 40 is_started_ = true; 41 service->AddConsumer(); 42 *renderer_handle = service->GetSharedMemoryHandleForProcess(PeerHandle()); 43 } else { 44 // Currently we only expect the renderer to tell us once to start. 45 NOTREACHED(); 46 } 47 } 48 OnGamepadStopPolling()49void GamepadBrowserMessageFilter::OnGamepadStopPolling() { 50 // TODO(scottmg): Probably get rid of this message. We can't trust it will 51 // arrive anyway if the renderer crashes, etc. 52 if (is_started_) { 53 is_started_ = false; 54 GamepadService::GetInstance()->RemoveConsumer(); 55 } else { 56 NOTREACHED(); 57 } 58 } 59 60 } // namespace content 61