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