| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/proxy/device_enumeration_resource_helper.h" | 5 #include "ppapi/proxy/device_enumeration_resource_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "ipc/ipc_message.h" | 10 #include "ipc/ipc_message.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "ppapi/shared_impl/tracked_callback.h" | 23 #include "ppapi/shared_impl/tracked_callback.h" |
| 24 | 24 |
| 25 namespace ppapi { | 25 namespace ppapi { |
| 26 namespace proxy { | 26 namespace proxy { |
| 27 | 27 |
| 28 DeviceEnumerationResourceHelper::DeviceEnumerationResourceHelper( | 28 DeviceEnumerationResourceHelper::DeviceEnumerationResourceHelper( |
| 29 PluginResource* owner) | 29 PluginResource* owner) |
| 30 : owner_(owner), | 30 : owner_(owner), |
| 31 pending_enumerate_devices_(false), | 31 pending_enumerate_devices_(false), |
| 32 monitor_callback_id_(0), | 32 monitor_callback_id_(0), |
| 33 monitor_callback_(NULL), | |
| 34 monitor_user_data_(NULL) { | 33 monitor_user_data_(NULL) { |
| 35 } | 34 } |
| 36 | 35 |
| 37 DeviceEnumerationResourceHelper::~DeviceEnumerationResourceHelper() { | 36 DeviceEnumerationResourceHelper::~DeviceEnumerationResourceHelper() { |
| 38 } | 37 } |
| 39 | 38 |
| 40 int32_t DeviceEnumerationResourceHelper::EnumerateDevices0_2( | 39 int32_t DeviceEnumerationResourceHelper::EnumerateDevices0_2( |
| 41 PP_Resource* devices, | 40 PP_Resource* devices, |
| 42 scoped_refptr<TrackedCallback> callback) { | 41 scoped_refptr<TrackedCallback> callback) { |
| 43 if (pending_enumerate_devices_) | 42 if (pending_enumerate_devices_) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 if (result == PP_OK) | 82 if (result == PP_OK) |
| 84 result = WriteToArrayOutput(devices, output); | 83 result = WriteToArrayOutput(devices, output); |
| 85 | 84 |
| 86 return result; | 85 return result; |
| 87 } | 86 } |
| 88 | 87 |
| 89 int32_t DeviceEnumerationResourceHelper::MonitorDeviceChange( | 88 int32_t DeviceEnumerationResourceHelper::MonitorDeviceChange( |
| 90 PP_MonitorDeviceChangeCallback callback, | 89 PP_MonitorDeviceChangeCallback callback, |
| 91 void* user_data) { | 90 void* user_data) { |
| 92 monitor_callback_id_++; | 91 monitor_callback_id_++; |
| 93 monitor_callback_ = callback; | 92 monitor_callback_.reset( |
| 93 new ThreadAwareCallback<PP_MonitorDeviceChangeCallback>(callback)); |
| 94 monitor_user_data_ = user_data; | 94 monitor_user_data_ = user_data; |
| 95 | 95 |
| 96 if (callback) { | 96 if (callback) { |
| 97 owner_->Post(PluginResource::RENDERER, | 97 owner_->Post(PluginResource::RENDERER, |
| 98 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange( | 98 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange( |
| 99 monitor_callback_id_)); | 99 monitor_callback_id_)); |
| 100 } else { | 100 } else { |
| 101 owner_->Post(PluginResource::RENDERER, | 101 owner_->Post(PluginResource::RENDERER, |
| 102 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange()); | 102 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange()); |
| 103 } | 103 } |
| 104 return PP_OK; | 104 return PP_OK; |
| 105 } | 105 } |
| 106 | 106 |
| 107 bool DeviceEnumerationResourceHelper::HandleReply( | 107 bool DeviceEnumerationResourceHelper::HandleReply( |
| 108 const ResourceMessageReplyParams& params, | 108 const ResourceMessageReplyParams& params, |
| 109 const IPC::Message& msg) { | 109 const IPC::Message& msg) { |
| 110 IPC_BEGIN_MESSAGE_MAP(DeviceEnumerationResourceHelper, msg) | 110 IPC_BEGIN_MESSAGE_MAP(DeviceEnumerationResourceHelper, msg) |
| 111 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | 111 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
| 112 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange, | 112 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange, |
| 113 OnPluginMsgNotifyDeviceChange) | 113 OnPluginMsgNotifyDeviceChange) |
| 114 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(return false) | 114 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(return false) |
| 115 IPC_END_MESSAGE_MAP() | 115 IPC_END_MESSAGE_MAP() |
| 116 | 116 |
| 117 return true; | 117 return true; |
| 118 } | 118 } |
| 119 | 119 |
| 120 void DeviceEnumerationResourceHelper::LastPluginRefWasDeleted() { | 120 void DeviceEnumerationResourceHelper::LastPluginRefWasDeleted() { |
| 121 // Make sure that no further notifications are sent to the plugin. | 121 // Make sure that no further notifications are sent to the plugin. |
| 122 monitor_callback_id_++; | 122 monitor_callback_id_++; |
| 123 monitor_callback_ = NULL; | 123 monitor_callback_.reset(); |
| 124 monitor_user_data_ = NULL; | 124 monitor_user_data_ = NULL; |
| 125 | 125 |
| 126 // There is no need to do anything with pending callback of | 126 // There is no need to do anything with pending callback of |
| 127 // EnumerateDevices(), because OnPluginMsgEnumerateDevicesReply*() will handle | 127 // EnumerateDevices(), because OnPluginMsgEnumerateDevicesReply*() will handle |
| 128 // that properly. | 128 // that properly. |
| 129 } | 129 } |
| 130 | 130 |
| 131 void DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply0_2( | 131 void DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply0_2( |
| 132 PP_Resource* devices_resource, | 132 PP_Resource* devices_resource, |
| 133 scoped_refptr<TrackedCallback> callback, | 133 scoped_refptr<TrackedCallback> callback, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 | 171 |
| 172 void DeviceEnumerationResourceHelper::OnPluginMsgNotifyDeviceChange( | 172 void DeviceEnumerationResourceHelper::OnPluginMsgNotifyDeviceChange( |
| 173 const ResourceMessageReplyParams& /* params */, | 173 const ResourceMessageReplyParams& /* params */, |
| 174 uint32_t callback_id, | 174 uint32_t callback_id, |
| 175 const std::vector<DeviceRefData>& devices) { | 175 const std::vector<DeviceRefData>& devices) { |
| 176 if (monitor_callback_id_ != callback_id) { | 176 if (monitor_callback_id_ != callback_id) { |
| 177 // A new callback or NULL has been set. | 177 // A new callback or NULL has been set. |
| 178 return; | 178 return; |
| 179 } | 179 } |
| 180 | 180 |
| 181 CHECK(monitor_callback_); | 181 CHECK(monitor_callback_.get()); |
| 182 | 182 |
| 183 scoped_array<PP_Resource> elements; | 183 scoped_array<PP_Resource> elements; |
| 184 uint32_t size = devices.size(); | 184 uint32_t size = devices.size(); |
| 185 if (size > 0) { | 185 if (size > 0) { |
| 186 elements.reset(new PP_Resource[size]); | 186 elements.reset(new PP_Resource[size]); |
| 187 for (size_t index = 0; index < size; ++index) { | 187 for (size_t index = 0; index < size; ++index) { |
| 188 PPB_DeviceRef_Shared* device_object = new PPB_DeviceRef_Shared( | 188 PPB_DeviceRef_Shared* device_object = new PPB_DeviceRef_Shared( |
| 189 OBJECT_IS_PROXY, owner_->pp_instance(), devices[index]); | 189 OBJECT_IS_PROXY, owner_->pp_instance(), devices[index]); |
| 190 elements[index] = device_object->GetReference(); | 190 elements[index] = device_object->GetReference(); |
| 191 } | 191 } |
| 192 } | 192 } |
| 193 | 193 |
| 194 // TODO(yzshen): make sure |monitor_callback_| is called on the same thread as | 194 monitor_callback_->RunOnTargetThread(monitor_user_data_, size, |
| 195 // the one on which MonitorDeviceChange() is called. | 195 elements.get()); |
| 196 CallWhileUnlocked(base::Bind(monitor_callback_, monitor_user_data_, size, | |
| 197 elements.get())); | |
| 198 for (size_t index = 0; index < size; ++index) | 196 for (size_t index = 0; index < size; ++index) |
| 199 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(elements[index]); | 197 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(elements[index]); |
| 200 } | 198 } |
| 201 | 199 |
| 202 int32_t DeviceEnumerationResourceHelper::WriteToArrayOutput( | 200 int32_t DeviceEnumerationResourceHelper::WriteToArrayOutput( |
| 203 const std::vector<DeviceRefData>& devices, | 201 const std::vector<DeviceRefData>& devices, |
| 204 const PP_ArrayOutput& output) { | 202 const PP_ArrayOutput& output) { |
| 205 ArrayWriter writer(output); | 203 ArrayWriter writer(output); |
| 206 if (!writer.is_valid()) | 204 if (!writer.is_valid()) |
| 207 return PP_ERROR_BADARGUMENT; | 205 return PP_ERROR_BADARGUMENT; |
| 208 | 206 |
| 209 std::vector<scoped_refptr<Resource> > device_resources; | 207 std::vector<scoped_refptr<Resource> > device_resources; |
| 210 for (size_t i = 0; i < devices.size(); ++i) { | 208 for (size_t i = 0; i < devices.size(); ++i) { |
| 211 device_resources.push_back(new PPB_DeviceRef_Shared( | 209 device_resources.push_back(new PPB_DeviceRef_Shared( |
| 212 OBJECT_IS_PROXY, owner_->pp_instance(), devices[i])); | 210 OBJECT_IS_PROXY, owner_->pp_instance(), devices[i])); |
| 213 } | 211 } |
| 214 if (!writer.StoreResourceVector(device_resources)) | 212 if (!writer.StoreResourceVector(device_resources)) |
| 215 return PP_ERROR_FAILED; | 213 return PP_ERROR_FAILED; |
| 216 | 214 |
| 217 return PP_OK; | 215 return PP_OK; |
| 218 } | 216 } |
| 219 | 217 |
| 220 } // namespace proxy | 218 } // namespace proxy |
| 221 } // namespace ppapi | 219 } // namespace ppapi |
| OLD | NEW |