Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 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 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
Hans Wennborg | 708fa82 | 2020-04-27 17:23:15 | [diff] [blame] | 11 | #include "base/check.h" |
Avi Drissman | 821ca309 | 2023-01-11 22:42:15 | [diff] [blame] | 12 | #include "base/functional/bind.h" |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 13 | #include "ipc/ipc_message.h" |
| 14 | #include "ipc/ipc_message_macros.h" |
| 15 | #include "ppapi/c/pp_array_output.h" |
| 16 | #include "ppapi/c/pp_errors.h" |
| 17 | #include "ppapi/proxy/dispatch_reply_message.h" |
| 18 | #include "ppapi/proxy/plugin_resource.h" |
| 19 | #include "ppapi/proxy/ppapi_messages.h" |
| 20 | #include "ppapi/proxy/resource_message_params.h" |
| 21 | #include "ppapi/shared_impl/array_writer.h" |
| 22 | #include "ppapi/shared_impl/ppapi_globals.h" |
| 23 | #include "ppapi/shared_impl/ppb_device_ref_shared.h" |
| 24 | #include "ppapi/shared_impl/proxy_lock.h" |
| 25 | #include "ppapi/shared_impl/resource_tracker.h" |
| 26 | #include "ppapi/shared_impl/tracked_callback.h" |
| 27 | |
| 28 | namespace ppapi { |
| 29 | namespace proxy { |
| 30 | |
| 31 | DeviceEnumerationResourceHelper::DeviceEnumerationResourceHelper( |
| 32 | PluginResource* owner) |
| 33 | : owner_(owner), |
| 34 | pending_enumerate_devices_(false), |
| 35 | monitor_callback_id_(0), |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 36 | monitor_user_data_(NULL) { |
| 37 | } |
| 38 | |
| 39 | DeviceEnumerationResourceHelper::~DeviceEnumerationResourceHelper() { |
| 40 | } |
| 41 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 42 | int32_t DeviceEnumerationResourceHelper::EnumerateDevices( |
| 43 | const PP_ArrayOutput& output, |
| 44 | scoped_refptr<TrackedCallback> callback) { |
| 45 | if (pending_enumerate_devices_) |
| 46 | return PP_ERROR_INPROGRESS; |
| 47 | |
| 48 | pending_enumerate_devices_ = true; |
| 49 | PpapiHostMsg_DeviceEnumeration_EnumerateDevices msg; |
| 50 | owner_->Call<PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>( |
| 51 | PluginResource::RENDERER, msg, |
Anand K Mistry | 9182c2a7 | 2021-03-17 04:40:08 | [diff] [blame] | 52 | base::BindOnce( |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 53 | &DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply, |
| 54 | AsWeakPtr(), output, callback)); |
| 55 | return PP_OK_COMPLETIONPENDING; |
| 56 | } |
| 57 | |
[email protected] | 33eccce | 2012-12-10 22:15:10 | [diff] [blame] | 58 | int32_t DeviceEnumerationResourceHelper::EnumerateDevicesSync( |
| 59 | const PP_ArrayOutput& output) { |
| 60 | std::vector<DeviceRefData> devices; |
| 61 | int32_t result = |
| 62 | owner_->SyncCall<PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply>( |
| 63 | PluginResource::RENDERER, |
| 64 | PpapiHostMsg_DeviceEnumeration_EnumerateDevices(), |
| 65 | &devices); |
| 66 | |
| 67 | if (result == PP_OK) |
| 68 | result = WriteToArrayOutput(devices, output); |
| 69 | |
| 70 | return result; |
| 71 | } |
| 72 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 73 | int32_t DeviceEnumerationResourceHelper::MonitorDeviceChange( |
| 74 | PP_MonitorDeviceChangeCallback callback, |
| 75 | void* user_data) { |
| 76 | monitor_callback_id_++; |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 77 | monitor_user_data_ = user_data; |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 78 | if (callback) { |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 79 | monitor_callback_.reset( |
| 80 | ThreadAwareCallback<PP_MonitorDeviceChangeCallback>::Create(callback)); |
| 81 | if (!monitor_callback_.get()) |
| 82 | return PP_ERROR_NO_MESSAGE_LOOP; |
| 83 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 84 | owner_->Post(PluginResource::RENDERER, |
| 85 | PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange( |
| 86 | monitor_callback_id_)); |
| 87 | } else { |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 88 | monitor_callback_.reset(NULL); |
| 89 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 90 | owner_->Post(PluginResource::RENDERER, |
| 91 | PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange()); |
| 92 | } |
| 93 | return PP_OK; |
| 94 | } |
| 95 | |
| 96 | bool DeviceEnumerationResourceHelper::HandleReply( |
| 97 | const ResourceMessageReplyParams& params, |
| 98 | const IPC::Message& msg) { |
[email protected] | dade5f8 | 2014-05-13 21:59:21 | [diff] [blame] | 99 | PPAPI_BEGIN_MESSAGE_MAP(DeviceEnumerationResourceHelper, msg) |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 100 | PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
| 101 | PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange, |
| 102 | OnPluginMsgNotifyDeviceChange) |
| 103 | PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(return false) |
[email protected] | dade5f8 | 2014-05-13 21:59:21 | [diff] [blame] | 104 | PPAPI_END_MESSAGE_MAP() |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | void DeviceEnumerationResourceHelper::LastPluginRefWasDeleted() { |
| 110 | // Make sure that no further notifications are sent to the plugin. |
| 111 | monitor_callback_id_++; |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 112 | monitor_callback_.reset(NULL); |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 113 | monitor_user_data_ = NULL; |
| 114 | |
| 115 | // There is no need to do anything with pending callback of |
| 116 | // EnumerateDevices(), because OnPluginMsgEnumerateDevicesReply*() will handle |
| 117 | // that properly. |
| 118 | } |
| 119 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 120 | void DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply( |
| 121 | const PP_ArrayOutput& output, |
| 122 | scoped_refptr<TrackedCallback> callback, |
| 123 | const ResourceMessageReplyParams& params, |
| 124 | const std::vector<DeviceRefData>& devices) { |
| 125 | pending_enumerate_devices_ = false; |
| 126 | |
| 127 | // We shouldn't access |output| if the callback has been called, which is |
| 128 | // possible if the last plugin reference to the corresponding resource has |
| 129 | // gone away, and the callback has been aborted. |
| 130 | if (!TrackedCallback::IsPending(callback)) |
| 131 | return; |
| 132 | |
| 133 | int32_t result = params.result(); |
[email protected] | 33eccce | 2012-12-10 22:15:10 | [diff] [blame] | 134 | if (result == PP_OK) |
| 135 | result = WriteToArrayOutput(devices, output); |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 136 | |
[email protected] | 33eccce | 2012-12-10 22:15:10 | [diff] [blame] | 137 | callback->Run(result); |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void DeviceEnumerationResourceHelper::OnPluginMsgNotifyDeviceChange( |
| 141 | const ResourceMessageReplyParams& /* params */, |
| 142 | uint32_t callback_id, |
| 143 | const std::vector<DeviceRefData>& devices) { |
| 144 | if (monitor_callback_id_ != callback_id) { |
| 145 | // A new callback or NULL has been set. |
| 146 | return; |
| 147 | } |
| 148 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 149 | CHECK(monitor_callback_.get()); |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 150 | |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 151 | std::unique_ptr<PP_Resource[]> elements; |
brettw | 669d47b1 | 2015-02-13 21:17:38 | [diff] [blame] | 152 | uint32_t size = static_cast<uint32_t>(devices.size()); |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 153 | if (size > 0) { |
| 154 | elements.reset(new PP_Resource[size]); |
| 155 | for (size_t index = 0; index < size; ++index) { |
| 156 | PPB_DeviceRef_Shared* device_object = new PPB_DeviceRef_Shared( |
| 157 | OBJECT_IS_PROXY, owner_->pp_instance(), devices[index]); |
| 158 | elements[index] = device_object->GetReference(); |
| 159 | } |
| 160 | } |
| 161 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 162 | monitor_callback_->RunOnTargetThread(monitor_user_data_, size, |
| 163 | elements.get()); |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 164 | for (size_t index = 0; index < size; ++index) |
| 165 | PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(elements[index]); |
| 166 | } |
| 167 | |
[email protected] | 33eccce | 2012-12-10 22:15:10 | [diff] [blame] | 168 | int32_t DeviceEnumerationResourceHelper::WriteToArrayOutput( |
| 169 | const std::vector<DeviceRefData>& devices, |
| 170 | const PP_ArrayOutput& output) { |
| 171 | ArrayWriter writer(output); |
| 172 | if (!writer.is_valid()) |
| 173 | return PP_ERROR_BADARGUMENT; |
| 174 | |
| 175 | std::vector<scoped_refptr<Resource> > device_resources; |
| 176 | for (size_t i = 0; i < devices.size(); ++i) { |
| 177 | device_resources.push_back(new PPB_DeviceRef_Shared( |
| 178 | OBJECT_IS_PROXY, owner_->pp_instance(), devices[i])); |
| 179 | } |
| 180 | if (!writer.StoreResourceVector(device_resources)) |
| 181 | return PP_ERROR_FAILED; |
| 182 | |
| 183 | return PP_OK; |
| 184 | } |
| 185 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 186 | } // namespace proxy |
| 187 | } // namespace ppapi |