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; | |
94 monitor_user_data_ = user_data; | 92 monitor_user_data_ = user_data; |
| 93 if (callback) { |
| 94 monitor_callback_.reset( |
| 95 ThreadAwareCallback<PP_MonitorDeviceChangeCallback>::Create(callback)); |
| 96 if (!monitor_callback_.get()) |
| 97 return PP_ERROR_NO_MESSAGE_LOOP; |
95 | 98 |
96 if (callback) { | |
97 owner_->Post(PluginResource::RENDERER, | 99 owner_->Post(PluginResource::RENDERER, |
98 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange( | 100 PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange( |
99 monitor_callback_id_)); | 101 monitor_callback_id_)); |
100 } else { | 102 } else { |
| 103 monitor_callback_.reset(NULL); |
| 104 |
101 owner_->Post(PluginResource::RENDERER, | 105 owner_->Post(PluginResource::RENDERER, |
102 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange()); | 106 PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange()); |
103 } | 107 } |
104 return PP_OK; | 108 return PP_OK; |
105 } | 109 } |
106 | 110 |
107 bool DeviceEnumerationResourceHelper::HandleReply( | 111 bool DeviceEnumerationResourceHelper::HandleReply( |
108 const ResourceMessageReplyParams& params, | 112 const ResourceMessageReplyParams& params, |
109 const IPC::Message& msg) { | 113 const IPC::Message& msg) { |
110 IPC_BEGIN_MESSAGE_MAP(DeviceEnumerationResourceHelper, msg) | 114 IPC_BEGIN_MESSAGE_MAP(DeviceEnumerationResourceHelper, msg) |
111 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | 115 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
112 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange, | 116 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange, |
113 OnPluginMsgNotifyDeviceChange) | 117 OnPluginMsgNotifyDeviceChange) |
114 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(return false) | 118 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(return false) |
115 IPC_END_MESSAGE_MAP() | 119 IPC_END_MESSAGE_MAP() |
116 | 120 |
117 return true; | 121 return true; |
118 } | 122 } |
119 | 123 |
120 void DeviceEnumerationResourceHelper::LastPluginRefWasDeleted() { | 124 void DeviceEnumerationResourceHelper::LastPluginRefWasDeleted() { |
121 // Make sure that no further notifications are sent to the plugin. | 125 // Make sure that no further notifications are sent to the plugin. |
122 monitor_callback_id_++; | 126 monitor_callback_id_++; |
123 monitor_callback_ = NULL; | 127 monitor_callback_.reset(NULL); |
124 monitor_user_data_ = NULL; | 128 monitor_user_data_ = NULL; |
125 | 129 |
126 // There is no need to do anything with pending callback of | 130 // There is no need to do anything with pending callback of |
127 // EnumerateDevices(), because OnPluginMsgEnumerateDevicesReply*() will handle | 131 // EnumerateDevices(), because OnPluginMsgEnumerateDevicesReply*() will handle |
128 // that properly. | 132 // that properly. |
129 } | 133 } |
130 | 134 |
131 void DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply0_2( | 135 void DeviceEnumerationResourceHelper::OnPluginMsgEnumerateDevicesReply0_2( |
132 PP_Resource* devices_resource, | 136 PP_Resource* devices_resource, |
133 scoped_refptr<TrackedCallback> callback, | 137 scoped_refptr<TrackedCallback> callback, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 175 |
172 void DeviceEnumerationResourceHelper::OnPluginMsgNotifyDeviceChange( | 176 void DeviceEnumerationResourceHelper::OnPluginMsgNotifyDeviceChange( |
173 const ResourceMessageReplyParams& /* params */, | 177 const ResourceMessageReplyParams& /* params */, |
174 uint32_t callback_id, | 178 uint32_t callback_id, |
175 const std::vector<DeviceRefData>& devices) { | 179 const std::vector<DeviceRefData>& devices) { |
176 if (monitor_callback_id_ != callback_id) { | 180 if (monitor_callback_id_ != callback_id) { |
177 // A new callback or NULL has been set. | 181 // A new callback or NULL has been set. |
178 return; | 182 return; |
179 } | 183 } |
180 | 184 |
181 CHECK(monitor_callback_); | 185 CHECK(monitor_callback_.get()); |
182 | 186 |
183 scoped_array<PP_Resource> elements; | 187 scoped_array<PP_Resource> elements; |
184 uint32_t size = devices.size(); | 188 uint32_t size = devices.size(); |
185 if (size > 0) { | 189 if (size > 0) { |
186 elements.reset(new PP_Resource[size]); | 190 elements.reset(new PP_Resource[size]); |
187 for (size_t index = 0; index < size; ++index) { | 191 for (size_t index = 0; index < size; ++index) { |
188 PPB_DeviceRef_Shared* device_object = new PPB_DeviceRef_Shared( | 192 PPB_DeviceRef_Shared* device_object = new PPB_DeviceRef_Shared( |
189 OBJECT_IS_PROXY, owner_->pp_instance(), devices[index]); | 193 OBJECT_IS_PROXY, owner_->pp_instance(), devices[index]); |
190 elements[index] = device_object->GetReference(); | 194 elements[index] = device_object->GetReference(); |
191 } | 195 } |
192 } | 196 } |
193 | 197 |
194 // TODO(yzshen): make sure |monitor_callback_| is called on the same thread as | 198 monitor_callback_->RunOnTargetThread(monitor_user_data_, size, |
195 // the one on which MonitorDeviceChange() is called. | 199 elements.get()); |
196 CallWhileUnlocked(base::Bind(monitor_callback_, monitor_user_data_, size, | |
197 elements.get())); | |
198 for (size_t index = 0; index < size; ++index) | 200 for (size_t index = 0; index < size; ++index) |
199 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(elements[index]); | 201 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(elements[index]); |
200 } | 202 } |
201 | 203 |
202 int32_t DeviceEnumerationResourceHelper::WriteToArrayOutput( | 204 int32_t DeviceEnumerationResourceHelper::WriteToArrayOutput( |
203 const std::vector<DeviceRefData>& devices, | 205 const std::vector<DeviceRefData>& devices, |
204 const PP_ArrayOutput& output) { | 206 const PP_ArrayOutput& output) { |
205 ArrayWriter writer(output); | 207 ArrayWriter writer(output); |
206 if (!writer.is_valid()) | 208 if (!writer.is_valid()) |
207 return PP_ERROR_BADARGUMENT; | 209 return PP_ERROR_BADARGUMENT; |
208 | 210 |
209 std::vector<scoped_refptr<Resource> > device_resources; | 211 std::vector<scoped_refptr<Resource> > device_resources; |
210 for (size_t i = 0; i < devices.size(); ++i) { | 212 for (size_t i = 0; i < devices.size(); ++i) { |
211 device_resources.push_back(new PPB_DeviceRef_Shared( | 213 device_resources.push_back(new PPB_DeviceRef_Shared( |
212 OBJECT_IS_PROXY, owner_->pp_instance(), devices[i])); | 214 OBJECT_IS_PROXY, owner_->pp_instance(), devices[i])); |
213 } | 215 } |
214 if (!writer.StoreResourceVector(device_resources)) | 216 if (!writer.StoreResourceVector(device_resources)) |
215 return PP_ERROR_FAILED; | 217 return PP_ERROR_FAILED; |
216 | 218 |
217 return PP_OK; | 219 return PP_OK; |
218 } | 220 } |
219 | 221 |
220 } // namespace proxy | 222 } // namespace proxy |
221 } // namespace ppapi | 223 } // namespace ppapi |
OLD | NEW |