Ignore non-active fullscreen windows for shelf state.
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / pepper_gamepad_host.cc
blob818bf24a2745cbcf339f8347d1fbda727e56fc82
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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/pepper/pepper_gamepad_host.h"
7 #include "base/bind.h"
8 #include "content/browser/gamepad/gamepad_service.h"
9 #include "content/public/browser/browser_ppapi_host.h"
10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/host/dispatch_host_message.h"
12 #include "ppapi/host/host_message_context.h"
13 #include "ppapi/host/ppapi_host.h"
14 #include "ppapi/proxy/ppapi_messages.h"
15 #include "ppapi/shared_impl/ppb_gamepad_shared.h"
17 namespace content {
19 PepperGamepadHost::PepperGamepadHost(BrowserPpapiHost* host,
20 PP_Instance instance,
21 PP_Resource resource)
22 : ResourceHost(host->GetPpapiHost(), instance, resource),
23 browser_ppapi_host_(host),
24 gamepad_service_(GamepadService::GetInstance()),
25 is_started_(false),
26 weak_factory_(this) {
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_(this) {
40 PepperGamepadHost::~PepperGamepadHost() {
41 if (is_started_)
42 gamepad_service_->RemoveConsumer();
45 int32_t PepperGamepadHost::OnResourceMessageReceived(
46 const IPC::Message& msg,
47 ppapi::host::HostMessageContext* context) {
48 IPC_BEGIN_MESSAGE_MAP(PepperGamepadHost, msg)
49 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Gamepad_RequestMemory,
50 OnRequestMemory)
51 IPC_END_MESSAGE_MAP()
52 return PP_ERROR_FAILED;
55 int32_t PepperGamepadHost::OnRequestMemory(
56 ppapi::host::HostMessageContext* context) {
57 if (is_started_)
58 return PP_ERROR_FAILED;
60 gamepad_service_->AddConsumer();
61 is_started_ = true;
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->MakeReplyMessageContext()));
70 return PP_OK_COMPLETIONPENDING;
73 void PepperGamepadHost::GotUserGesture(
74 const ppapi::host::ReplyMessageContext& context) {
75 base::SharedMemoryHandle handle =
76 gamepad_service_->GetSharedMemoryHandleForProcess(
77 browser_ppapi_host_->GetPluginProcessHandle());
79 context.params.AppendHandle(ppapi::proxy::SerializedHandle(
80 handle, sizeof(ppapi::ContentGamepadHardwareBuffer)));
81 host()->SendReply(context, PpapiPluginMsg_Gamepad_SendMemory());
84 } // namespace content