| OLD | NEW |
| 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/browser/renderer_host/pepper/pepper_gamepad_host.h" | 5 #include "content/browser/renderer_host/pepper/pepper_gamepad_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "content/browser/gamepad/gamepad_service.h" |
| 7 #include "content/public/browser/browser_ppapi_host.h" | 9 #include "content/public/browser/browser_ppapi_host.h" |
| 8 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/host/dispatch_host_message.h" | 11 #include "ppapi/host/dispatch_host_message.h" |
| 12 #include "ppapi/host/host_message_context.h" |
| 13 #include "ppapi/host/ppapi_host.h" |
| 10 #include "ppapi/proxy/ppapi_messages.h" | 14 #include "ppapi/proxy/ppapi_messages.h" |
| 11 | 15 |
| 12 namespace content { | 16 namespace content { |
| 13 | 17 |
| 14 PepperGamepadHost::PepperGamepadHost(BrowserPpapiHost* host, | 18 PepperGamepadHost::PepperGamepadHost(BrowserPpapiHost* host, |
| 15 PP_Instance instance, | 19 PP_Instance instance, |
| 16 PP_Resource resource) | 20 PP_Resource resource) |
| 17 : ResourceHost(host->GetPpapiHost(), instance, resource), | 21 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 18 browser_ppapi_host_(host) { | 22 browser_ppapi_host_(host), |
| 23 gamepad_service_(GamepadService::GetInstance()), |
| 24 is_started_(false), |
| 25 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 26 } |
| 27 |
| 28 PepperGamepadHost::PepperGamepadHost(GamepadService* gamepad_service, |
| 29 BrowserPpapiHost* host, |
| 30 PP_Instance instance, |
| 31 PP_Resource resource) |
| 32 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 33 browser_ppapi_host_(host), |
| 34 gamepad_service_(gamepad_service), |
| 35 is_started_(false), |
| 36 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 19 } | 37 } |
| 20 | 38 |
| 21 PepperGamepadHost::~PepperGamepadHost() { | 39 PepperGamepadHost::~PepperGamepadHost() { |
| 40 if (is_started_) |
| 41 gamepad_service_->RemoveConsumer(); |
| 22 } | 42 } |
| 23 | 43 |
| 24 int32_t PepperGamepadHost::OnResourceMessageReceived( | 44 int32_t PepperGamepadHost::OnResourceMessageReceived( |
| 25 const IPC::Message& msg, | 45 const IPC::Message& msg, |
| 26 ppapi::host::HostMessageContext* context) { | 46 ppapi::host::HostMessageContext* context) { |
| 27 IPC_BEGIN_MESSAGE_MAP(PepperGamepadHost, msg) | 47 IPC_BEGIN_MESSAGE_MAP(PepperGamepadHost, msg) |
| 28 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Gamepad_RequestMemory, | 48 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Gamepad_RequestMemory, |
| 29 OnMsgRequestMemory) | 49 OnMsgRequestMemory) |
| 30 IPC_END_MESSAGE_MAP() | 50 IPC_END_MESSAGE_MAP() |
| 31 return PP_ERROR_FAILED; | 51 return PP_ERROR_FAILED; |
| 32 } | 52 } |
| 33 | 53 |
| 34 int32_t PepperGamepadHost::OnMsgRequestMemory( | 54 int32_t PepperGamepadHost::OnMsgRequestMemory( |
| 35 ppapi::host::HostMessageContext* context) { | 55 ppapi::host::HostMessageContext* context) { |
| 36 return PP_ERROR_FAILED; | 56 if (is_started_) |
| 57 return PP_ERROR_FAILED; |
| 58 |
| 59 gamepad_service_->AddConsumer(); |
| 60 is_started_ = true; |
| 61 |
| 62 // Don't send the shared memory back until the user has interacted with the |
| 63 // gamepad. This is to prevent fingerprinting and matches what the web |
| 64 // platform does. |
| 65 gamepad_service_->RegisterForUserGesture( |
| 66 base::Bind(&PepperGamepadHost::GotUserGesture, |
| 67 weak_factory_.GetWeakPtr(), |
| 68 context->MakeReplyParams())); |
| 69 return PP_OK_COMPLETIONPENDING; |
| 70 } |
| 71 |
| 72 void PepperGamepadHost::GotUserGesture( |
| 73 const ppapi::proxy::ResourceMessageReplyParams& params) { |
| 74 base::SharedMemoryHandle handle = |
| 75 gamepad_service_->GetSharedMemoryHandleForProcess( |
| 76 browser_ppapi_host_->GetPluginProcessHandle()); |
| 77 host()->SendReply(params, PpapiPluginMsg_Gamepad_SendMemory(handle)); |
| 37 } | 78 } |
| 38 | 79 |
| 39 } // namespace content | 80 } // namespace content |
| OLD | NEW |