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 "ppapi/proxy/device_enumeration_resource_helper.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "ipc/ipc_message.h"
11 #include "ipc/ipc_message_macros.h"
12 #include "ppapi/c/pp_array_output.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/proxy/dispatch_reply_message.h"
15 #include "ppapi/proxy/plugin_resource.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/proxy/resource_message_params.h"
18 #include "ppapi/shared_impl/array_writer.h"
19 #include "ppapi/shared_impl/ppapi_globals.h"
20 #include "ppapi/shared_impl/ppb_device_ref_shared.h"
21 #include "ppapi/shared_impl/proxy_lock.h"
22 #include "ppapi/shared_impl/resource_tracker.h"
23 #include "ppapi/shared_impl/tracked_callback.h"
28 DeviceEnumerationResourceHelper::DeviceEnumerationResourceHelper(
29 PluginResource
* owner
)
31 pending_enumerate_devices_(false),
32 monitor_callback_id_(0),
33 monitor_callback_(NULL
),
34 monitor_user_data_(NULL
) {
37 DeviceEnumerationResourceHelper::~DeviceEnumerationResourceHelper() {
40 int32_t DeviceEnumerationResourceHelper::EnumerateDevices0_2(
42 scoped_refptr
<TrackedCallback
> callback
) {
43 if (pending_enumerate_devices_
)
44 return PP_ERROR_INPROGRESS
;
46 return PP_ERROR_BADARGUMENT
;
48 pending_enumerate_devices_
= true;
49 PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg
;
50 owner_
->Call
<PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply
>(
51 PluginResource::RENDERER
, msg
,
53 &DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply0_2
,
54 AsWeakPtr(), devices
, callback
));
55 return PP_OK_COMPLETIONPENDING
;
58 int32_t DeviceEnumerationResourceHelper::EnumerateDevices(
59 const PP_ArrayOutput
& output
,
60 scoped_refptr
<TrackedCallback
> callback
) {
61 if (pending_enumerate_devices_
)
62 return PP_ERROR_INPROGRESS
;
64 pending_enumerate_devices_
= true;
65 PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg
;
66 owner_
->Call
<PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply
>(
67 PluginResource::RENDERER
, msg
,
69 &DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply
,
70 AsWeakPtr(), output
, callback
));
71 return PP_OK_COMPLETIONPENDING
;
74 int32_t DeviceEnumerationResourceHelper::EnumerateDevicesSync(
75 const PP_ArrayOutput
& output
) {
76 std::vector
<DeviceRefData
> devices
;
78 owner_
->SyncCall
<PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply
>(
79 PluginResource::RENDERER
,
80 PpapiHostMsg_DeviceEnumeration_EnumerateDevices(),
84 result
= WriteToArrayOutput(devices
, output
);
89 int32_t DeviceEnumerationResourceHelper::MonitorDeviceChange(
90 PP_MonitorDeviceChangeCallback callback
,
92 monitor_callback_id_
++;
93 monitor_callback_
= callback
;
94 monitor_user_data_
= user_data
;
97 owner_
->Post(PluginResource::RENDERER
,
98 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange(
99 monitor_callback_id_
));
101 owner_
->Post(PluginResource::RENDERER
,
102 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange());
107 bool DeviceEnumerationResourceHelper::HandleReply(
108 const ResourceMessageReplyParams
& params
,
109 const IPC::Message
& msg
) {
110 IPC_BEGIN_MESSAGE_MAP(DeviceEnumerationResourceHelper
, msg
)
111 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
112 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange
,
113 OnPluginMsgNotifyDeviceChange
)
114 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(return false)
115 IPC_END_MESSAGE_MAP()
120 void DeviceEnumerationResourceHelper::LastPluginRefWasDeleted() {
121 // Make sure that no further notifications are sent to the plugin.
122 monitor_callback_id_
++;
123 monitor_callback_
= NULL
;
124 monitor_user_data_
= NULL
;
126 // There is no need to do anything with pending callback of
127 // EnumerateDevices(), because OnPluginMsgEnumerateDevicesReply*() will handle
131 void DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply0_2(
132 PP_Resource
* devices_resource
,
133 scoped_refptr
<TrackedCallback
> callback
,
134 const ResourceMessageReplyParams
& params
,
135 const std::vector
<DeviceRefData
>& devices
) {
136 pending_enumerate_devices_
= false;
138 // We shouldn't access |devices_resource| if the callback has been called,
139 // which is possible if the last plugin reference to the corresponding
140 // resource has gone away, and the callback has been aborted.
141 if (!TrackedCallback::IsPending(callback
))
144 if (params
.result() == PP_OK
) {
145 *devices_resource
= PPB_DeviceRef_Shared::CreateResourceArray(
146 OBJECT_IS_PROXY
, owner_
->pp_instance(), devices
);
149 callback
->Run(params
.result());
152 void DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply(
153 const PP_ArrayOutput
& output
,
154 scoped_refptr
<TrackedCallback
> callback
,
155 const ResourceMessageReplyParams
& params
,
156 const std::vector
<DeviceRefData
>& devices
) {
157 pending_enumerate_devices_
= false;
159 // We shouldn't access |output| if the callback has been called, which is
160 // possible if the last plugin reference to the corresponding resource has
161 // gone away, and the callback has been aborted.
162 if (!TrackedCallback::IsPending(callback
))
165 int32_t result
= params
.result();
167 result
= WriteToArrayOutput(devices
, output
);
169 callback
->Run(result
);
172 void DeviceEnumerationResourceHelper::OnPluginMsgNotifyDeviceChange(
173 const ResourceMessageReplyParams
& /* params */,
174 uint32_t callback_id
,
175 const std::vector
<DeviceRefData
>& devices
) {
176 if (monitor_callback_id_
!= callback_id
) {
177 // A new callback or NULL has been set.
181 CHECK(monitor_callback_
);
183 scoped_array
<PP_Resource
> elements
;
184 uint32_t size
= devices
.size();
186 elements
.reset(new PP_Resource
[size
]);
187 for (size_t index
= 0; index
< size
; ++index
) {
188 PPB_DeviceRef_Shared
* device_object
= new PPB_DeviceRef_Shared(
189 OBJECT_IS_PROXY
, owner_
->pp_instance(), devices
[index
]);
190 elements
[index
] = device_object
->GetReference();
194 // TODO(yzshen): make sure |monitor_callback_| is called on the same thread as
195 // the one on which MonitorDeviceChange() is called.
196 CallWhileUnlocked(base::Bind(monitor_callback_
, monitor_user_data_
, size
,
198 for (size_t index
= 0; index
< size
; ++index
)
199 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(elements
[index
]);
202 int32_t DeviceEnumerationResourceHelper::WriteToArrayOutput(
203 const std::vector
<DeviceRefData
>& devices
,
204 const PP_ArrayOutput
& output
) {
205 ArrayWriter
writer(output
);
206 if (!writer
.is_valid())
207 return PP_ERROR_BADARGUMENT
;
209 std::vector
<scoped_refptr
<Resource
> > device_resources
;
210 for (size_t i
= 0; i
< devices
.size(); ++i
) {
211 device_resources
.push_back(new PPB_DeviceRef_Shared(
212 OBJECT_IS_PROXY
, owner_
->pp_instance(), devices
[i
]));
214 if (!writer
.StoreResourceVector(device_resources
))
215 return PP_ERROR_FAILED
;