[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "ppapi/proxy/device_enumeration_resource_helper.h" |
| 6 | |
| 7 | #include "base/bind.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" |
| 24 | |
| 25 | namespace ppapi { |
| 26 | namespace proxy { |
| 27 | |
| 28 | DeviceEnumerationResourceHelper::DeviceEnumerationResourceHelper( |
| 29 | PluginResource* owner) |
| 30 | : owner_(owner), |
| 31 | pending_enumerate_devices_(false), |
| 32 | monitor_callback_id_(0), |
| 33 | monitor_callback_(NULL), |
| 34 | monitor_user_data_(NULL) { |
| 35 | } |
| 36 | |
| 37 | DeviceEnumerationResourceHelper::~DeviceEnumerationResourceHelper() { |
| 38 | } |
| 39 | |
| 40 | int32_t DeviceEnumerationResourceHelper::EnumerateDevices0_2( |
| 41 | PP_Resource* devices, |
| 42 | scoped_refptr<TrackedCallback> callback) { |
| 43 | if (pending_enumerate_devices_) |
| 44 | return PP_ERROR_INPROGRESS; |
| 45 | if (!devices) |
| 46 | return PP_ERROR_BADARGUMENT; |
| 47 | |
| 48 | pending_enumerate_devices_ = true; |
| 49 | PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg; |
| 50 | owner_->Call<PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>( |
| 51 | PluginResource::RENDERER, msg, |
| 52 | base::Bind( |
| 53 | &DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply0_2, |
| 54 | AsWeakPtr(), devices, callback)); |
| 55 | return PP_OK_COMPLETIONPENDING; |
| 56 | } |
| 57 | |
| 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; |
| 63 | |
| 64 | pending_enumerate_devices_ = true; |
| 65 | PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg; |
| 66 | owner_->Call<PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>( |
| 67 | PluginResource::RENDERER, msg, |
| 68 | base::Bind( |
| 69 | &DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply, |
| 70 | AsWeakPtr(), output, callback)); |
| 71 | return PP_OK_COMPLETIONPENDING; |
| 72 | } |
| 73 | |
[email protected] | 33eccce | 2012-12-10 22:15:10 | [diff] [blame^] | 74 | int32_t DeviceEnumerationResourceHelper::EnumerateDevicesSync( |
| 75 | const PP_ArrayOutput& output) { |
| 76 | std::vector<DeviceRefData> devices; |
| 77 | int32_t result = |
| 78 | owner_->SyncCall<PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>( |
| 79 | PluginResource::RENDERER, |
| 80 | PpapiHostMsg_DeviceEnumeration_EnumerateDevices(), |
| 81 | &devices); |
| 82 | |
| 83 | if (result == PP_OK) |
| 84 | result = WriteToArrayOutput(devices, output); |
| 85 | |
| 86 | return result; |
| 87 | } |
| 88 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 89 | int32_t DeviceEnumerationResourceHelper::MonitorDeviceChange( |
| 90 | PP_MonitorDeviceChangeCallback callback, |
| 91 | void* user_data) { |
| 92 | monitor_callback_id_++; |
| 93 | monitor_callback_ = callback; |
| 94 | monitor_user_data_ = user_data; |
| 95 | |
| 96 | if (callback) { |
| 97 | owner_->Post(PluginResource::RENDERER, |
| 98 | PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange( |
| 99 | monitor_callback_id_)); |
| 100 | } else { |
| 101 | owner_->Post(PluginResource::RENDERER, |
| 102 | PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange()); |
| 103 | } |
| 104 | return PP_OK; |
| 105 | } |
| 106 | |
| 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() |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
| 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; |
| 125 | |
| 126 | // There is no need to do anything with pending callback of |
| 127 | // EnumerateDevices(), because OnPluginMsgEnumerateDevicesReply*() will handle |
| 128 | // that properly. |
| 129 | } |
| 130 | |
| 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; |
| 137 | |
| 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)) |
| 142 | return; |
| 143 | |
| 144 | if (params.result() == PP_OK) { |
| 145 | *devices_resource = PPB_DeviceRef_Shared::CreateResourceArray( |
| 146 | OBJECT_IS_PROXY, owner_->pp_instance(), devices); |
| 147 | } |
| 148 | |
| 149 | callback->Run(params.result()); |
| 150 | } |
| 151 | |
| 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; |
| 158 | |
| 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)) |
| 163 | return; |
| 164 | |
| 165 | int32_t result = params.result(); |
[email protected] | 33eccce | 2012-12-10 22:15:10 | [diff] [blame^] | 166 | if (result == PP_OK) |
| 167 | result = WriteToArrayOutput(devices, output); |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 168 | |
[email protected] | 33eccce | 2012-12-10 22:15:10 | [diff] [blame^] | 169 | callback->Run(result); |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 170 | } |
| 171 | |
| 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. |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | CHECK(monitor_callback_); |
| 182 | |
| 183 | scoped_array<PP_Resource> elements; |
| 184 | uint32_t size = devices.size(); |
| 185 | if (size > 0) { |
| 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(); |
| 191 | } |
| 192 | } |
| 193 | |
| 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, |
| 197 | elements.get())); |
| 198 | for (size_t index = 0; index < size; ++index) |
| 199 | PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(elements[index]); |
| 200 | } |
| 201 | |
[email protected] | 33eccce | 2012-12-10 22:15:10 | [diff] [blame^] | 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; |
| 208 | |
| 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])); |
| 213 | } |
| 214 | if (!writer.StoreResourceVector(device_resources)) |
| 215 | return PP_ERROR_FAILED; |
| 216 | |
| 217 | return PP_OK; |
| 218 | } |
| 219 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 220 | } // namespace proxy |
| 221 | } // namespace ppapi |