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/renderer/pepper/pepper_device_enumeration_host_helper.h"
8 #include "base/logging.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "ipc/ipc_message.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/host/dispatch_host_message.h"
14 #include "ppapi/host/host_message_context.h"
15 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/host/resource_host.h"
17 #include "ppapi/proxy/ppapi_messages.h"
18 #include "ppapi/shared_impl/ppb_device_ref_shared.h"
20 using ppapi::host::HostMessageContext;
24 // Makes sure that StopEnumerateDevices() is called for each EnumerateDevices().
25 class PepperDeviceEnumerationHostHelper::ScopedRequest
26 : public base::SupportsWeakPtr<ScopedRequest> {
28 // |owner| must outlive this object.
29 ScopedRequest(PepperDeviceEnumerationHostHelper* owner,
30 const Delegate::EnumerateDevicesCallback& callback)
36 if (!owner_->document_url_.is_valid())
41 // Note that the callback passed into
42 // PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevices() may be
43 // called synchronously. In that case, |request_id_| hasn't been updated
44 // when the callback is called. Moreover, |callback| may destroy this
45 // object. So we don't pass in |callback| directly. Instead, we use
46 // EnumerateDevicesCallbackBody() to ensure that we always call |callback|
49 request_id_ = owner_->delegate_->EnumerateDevices(
51 owner_->document_url_,
52 base::Bind(&ScopedRequest::EnumerateDevicesCallbackBody, AsWeakPtr()));
58 owner_->delegate_->StopEnumerateDevices(request_id_);
62 bool requested() const { return requested_; }
65 void EnumerateDevicesCallbackBody(
67 const std::vector<ppapi::DeviceRefData>& devices) {
69 base::MessageLoop::current()->PostTask(
71 base::Bind(&ScopedRequest::EnumerateDevicesCallbackBody,
76 DCHECK_EQ(request_id_, request_id);
77 callback_.Run(request_id, devices);
78 // This object may have been destroyed at this point.
82 PepperDeviceEnumerationHostHelper* owner_;
83 PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevicesCallback
89 DISALLOW_COPY_AND_ASSIGN(ScopedRequest);
92 PepperDeviceEnumerationHostHelper::PepperDeviceEnumerationHostHelper(
93 ppapi::host::ResourceHost* resource_host,
95 PP_DeviceType_Dev device_type,
96 const GURL& document_url)
97 : resource_host_(resource_host),
99 device_type_(device_type),
100 document_url_(document_url) {}
102 PepperDeviceEnumerationHostHelper::~PepperDeviceEnumerationHostHelper() {}
104 bool PepperDeviceEnumerationHostHelper::HandleResourceMessage(
105 const IPC::Message& msg,
106 HostMessageContext* context,
108 bool return_value = false;
109 *result = InternalHandleResourceMessage(msg, context, &return_value);
113 int32_t PepperDeviceEnumerationHostHelper::InternalHandleResourceMessage(
114 const IPC::Message& msg,
115 HostMessageContext* context,
118 IPC_BEGIN_MESSAGE_MAP(PepperDeviceEnumerationHostHelper, msg)
119 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
120 PpapiHostMsg_DeviceEnumeration_EnumerateDevices, OnEnumerateDevices)
121 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
122 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange, OnMonitorDeviceChange)
123 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
124 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange,
125 OnStopMonitoringDeviceChange)
126 IPC_END_MESSAGE_MAP()
129 return PP_ERROR_FAILED;
132 int32_t PepperDeviceEnumerationHostHelper::OnEnumerateDevices(
133 HostMessageContext* context) {
134 if (enumerate_devices_context_.is_valid())
135 return PP_ERROR_INPROGRESS;
137 enumerate_.reset(new ScopedRequest(
139 base::Bind(&PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete,
140 base::Unretained(this))));
141 if (!enumerate_->requested())
142 return PP_ERROR_FAILED;
144 enumerate_devices_context_ = context->MakeReplyMessageContext();
145 return PP_OK_COMPLETIONPENDING;
148 int32_t PepperDeviceEnumerationHostHelper::OnMonitorDeviceChange(
149 HostMessageContext* /* context */,
150 uint32_t callback_id) {
151 monitor_.reset(new ScopedRequest(
153 base::Bind(&PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange,
154 base::Unretained(this),
157 return monitor_->requested() ? PP_OK : PP_ERROR_FAILED;
160 int32_t PepperDeviceEnumerationHostHelper::OnStopMonitoringDeviceChange(
161 HostMessageContext* /* context */) {
162 monitor_.reset(NULL);
166 void PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete(
167 int /* request_id */,
168 const std::vector<ppapi::DeviceRefData>& devices) {
169 DCHECK(enumerate_devices_context_.is_valid());
171 enumerate_.reset(NULL);
173 enumerate_devices_context_.params.set_result(PP_OK);
174 resource_host_->host()->SendReply(
175 enumerate_devices_context_,
176 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply(devices));
177 enumerate_devices_context_ = ppapi::host::ReplyMessageContext();
180 void PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange(
181 uint32_t callback_id,
182 int /* request_id */,
183 const std::vector<ppapi::DeviceRefData>& devices) {
184 resource_host_->host()->SendUnsolicitedReply(
185 resource_host_->pp_resource(),
186 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(callback_id,
190 } // namespace content