| 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 "content/renderer/pepper/pepper_device_enumeration_host_helper.h" | 5 #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
| 15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/host/dispatch_host_message.h" | 16 #include "ppapi/host/dispatch_host_message.h" |
| 17 #include "ppapi/host/host_message_context.h" | 17 #include "ppapi/host/host_message_context.h" |
| 18 #include "ppapi/host/ppapi_host.h" | 18 #include "ppapi/host/ppapi_host.h" |
| 19 #include "ppapi/host/resource_host.h" | 19 #include "ppapi/host/resource_host.h" |
| 20 #include "ppapi/proxy/ppapi_messages.h" | 20 #include "ppapi/proxy/ppapi_messages.h" |
| 21 #include "ppapi/shared_impl/ppb_device_ref_shared.h" | 21 #include "ppapi/shared_impl/ppb_device_ref_shared.h" |
| 22 | 22 |
| 23 using ppapi::host::HostMessageContext; | 23 using ppapi::host::HostMessageContext; |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 | 26 |
| 27 // Makes sure that StopEnumerateDevices() is called for each EnumerateDevices(). | 27 // Makes sure that StopEnumerateDevices() is called for each EnumerateDevices(). |
| 28 class PepperDeviceEnumerationHostHelper::ScopedRequest | 28 class PepperDeviceEnumerationHostHelper::ScopedEnumerationRequest |
| 29 : public base::SupportsWeakPtr<ScopedRequest> { | 29 : public base::SupportsWeakPtr<ScopedEnumerationRequest> { |
| 30 public: | 30 public: |
| 31 // |owner| must outlive this object. | 31 // |owner| must outlive this object. |
| 32 ScopedRequest(PepperDeviceEnumerationHostHelper* owner, | 32 ScopedEnumerationRequest(PepperDeviceEnumerationHostHelper* owner, |
| 33 const Delegate::EnumerateDevicesCallback& callback) | 33 const Delegate::DevicesCallback& callback) |
| 34 : owner_(owner), | 34 : callback_(callback), requested_(false), sync_call_(false) { |
| 35 callback_(callback), | 35 if (!owner->document_url_.is_valid()) |
| 36 requested_(false), | |
| 37 request_id_(0), | |
| 38 sync_call_(false) { | |
| 39 if (!owner_->document_url_.is_valid()) | |
| 40 return; | 36 return; |
| 41 | 37 |
| 42 requested_ = true; | 38 requested_ = true; |
| 43 | 39 |
| 44 // Note that the callback passed into | 40 // Note that the callback passed into |
| 45 // PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevices() may be | 41 // PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevices() may be |
| 46 // called synchronously. In that case, |request_id_| hasn't been updated | 42 // called synchronously. In that case, |callback| may destroy this |
| 47 // when the callback is called. Moreover, |callback| may destroy this | |
| 48 // object. So we don't pass in |callback| directly. Instead, we use | 43 // object. So we don't pass in |callback| directly. Instead, we use |
| 49 // EnumerateDevicesCallbackBody() to ensure that we always call |callback| | 44 // EnumerateDevicesCallbackBody() to ensure that we always call |callback| |
| 50 // asynchronously. | 45 // asynchronously. |
| 51 sync_call_ = true; | 46 sync_call_ = true; |
| 52 DCHECK(owner_->delegate_); | 47 DCHECK(owner->delegate_); |
| 53 request_id_ = owner_->delegate_->EnumerateDevices( | 48 owner->delegate_->EnumerateDevices( |
| 54 owner_->device_type_, | 49 owner->device_type_, owner->document_url_, |
| 55 owner_->document_url_, | 50 base::Bind(&ScopedEnumerationRequest::EnumerateDevicesCallbackBody, |
| 56 base::Bind(&ScopedRequest::EnumerateDevicesCallbackBody, AsWeakPtr())); | 51 AsWeakPtr())); |
| 57 sync_call_ = false; | 52 sync_call_ = false; |
| 58 } | 53 } |
| 59 | 54 |
| 60 ~ScopedRequest() { | 55 bool requested() const { return requested_; } |
| 56 |
| 57 private: |
| 58 void EnumerateDevicesCallbackBody( |
| 59 const std::vector<ppapi::DeviceRefData>& devices) { |
| 60 if (sync_call_) { |
| 61 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 62 FROM_HERE, |
| 63 base::Bind(&ScopedEnumerationRequest::EnumerateDevicesCallbackBody, |
| 64 AsWeakPtr(), devices)); |
| 65 } else { |
| 66 callback_.Run(devices); |
| 67 // This object may have been destroyed at this point. |
| 68 } |
| 69 } |
| 70 |
| 71 PepperDeviceEnumerationHostHelper::Delegate::DevicesCallback callback_; |
| 72 bool requested_; |
| 73 bool sync_call_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(ScopedEnumerationRequest); |
| 76 }; |
| 77 |
| 78 // Makes sure that StopMonitoringDevices() is called for each |
| 79 // StartMonitoringDevices(). |
| 80 class PepperDeviceEnumerationHostHelper::ScopedMonitoringRequest |
| 81 : public base::SupportsWeakPtr<ScopedMonitoringRequest> { |
| 82 public: |
| 83 // |owner| must outlive this object. |
| 84 ScopedMonitoringRequest(PepperDeviceEnumerationHostHelper* owner, |
| 85 const Delegate::DevicesCallback& callback) |
| 86 : owner_(owner), |
| 87 callback_(callback), |
| 88 requested_(false), |
| 89 subscription_id_(0) { |
| 90 DCHECK(owner_); |
| 91 if (!owner_->document_url_.is_valid()) |
| 92 return; |
| 93 |
| 94 requested_ = true; |
| 95 |
| 96 DCHECK(owner_->delegate_); |
| 97 // |callback| is never called synchronously by StartMonitoringDevices(), |
| 98 // so it is OK to pass it directly, even if |callback| destroys |this|. |
| 99 subscription_id_ = owner_->delegate_->StartMonitoringDevices( |
| 100 owner_->device_type_, owner_->document_url_, callback); |
| 101 } |
| 102 |
| 103 ~ScopedMonitoringRequest() { |
| 61 if (requested_ && owner_->delegate_) { | 104 if (requested_ && owner_->delegate_) { |
| 62 owner_->delegate_->StopEnumerateDevices(request_id_); | 105 owner_->delegate_->StopMonitoringDevices(owner_->device_type_, |
| 106 subscription_id_); |
| 63 } | 107 } |
| 64 } | 108 } |
| 65 | 109 |
| 66 bool requested() const { return requested_; } | 110 bool requested() const { return requested_; } |
| 67 | 111 |
| 68 private: | 112 private: |
| 69 void EnumerateDevicesCallbackBody( | 113 PepperDeviceEnumerationHostHelper* const owner_; |
| 70 int request_id, | 114 PepperDeviceEnumerationHostHelper::Delegate::DevicesCallback callback_; |
| 71 const std::vector<ppapi::DeviceRefData>& devices) { | 115 bool requested_; |
| 72 if (sync_call_) { | 116 int subscription_id_; |
| 73 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 74 FROM_HERE, base::Bind(&ScopedRequest::EnumerateDevicesCallbackBody, | |
| 75 AsWeakPtr(), request_id, devices)); | |
| 76 } else { | |
| 77 DCHECK_EQ(request_id_, request_id); | |
| 78 callback_.Run(request_id, devices); | |
| 79 // This object may have been destroyed at this point. | |
| 80 } | |
| 81 } | |
| 82 | 117 |
| 83 PepperDeviceEnumerationHostHelper* owner_; | 118 DISALLOW_COPY_AND_ASSIGN(ScopedMonitoringRequest); |
| 84 PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevicesCallback | |
| 85 callback_; | |
| 86 bool requested_; | |
| 87 int request_id_; | |
| 88 bool sync_call_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(ScopedRequest); | |
| 91 }; | 119 }; |
| 92 | 120 |
| 93 PepperDeviceEnumerationHostHelper::PepperDeviceEnumerationHostHelper( | 121 PepperDeviceEnumerationHostHelper::PepperDeviceEnumerationHostHelper( |
| 94 ppapi::host::ResourceHost* resource_host, | 122 ppapi::host::ResourceHost* resource_host, |
| 95 base::WeakPtr<Delegate> delegate, | 123 base::WeakPtr<Delegate> delegate, |
| 96 PP_DeviceType_Dev device_type, | 124 PP_DeviceType_Dev device_type, |
| 97 const GURL& document_url) | 125 const GURL& document_url) |
| 98 : resource_host_(resource_host), | 126 : resource_host_(resource_host), |
| 99 delegate_(delegate), | 127 delegate_(delegate), |
| 100 device_type_(device_type), | 128 device_type_(device_type), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 129 | 157 |
| 130 *handled = false; | 158 *handled = false; |
| 131 return PP_ERROR_FAILED; | 159 return PP_ERROR_FAILED; |
| 132 } | 160 } |
| 133 | 161 |
| 134 int32_t PepperDeviceEnumerationHostHelper::OnEnumerateDevices( | 162 int32_t PepperDeviceEnumerationHostHelper::OnEnumerateDevices( |
| 135 HostMessageContext* context) { | 163 HostMessageContext* context) { |
| 136 if (enumerate_devices_context_.is_valid()) | 164 if (enumerate_devices_context_.is_valid()) |
| 137 return PP_ERROR_INPROGRESS; | 165 return PP_ERROR_INPROGRESS; |
| 138 | 166 |
| 139 enumerate_.reset(new ScopedRequest( | 167 enumerate_.reset(new ScopedEnumerationRequest( |
| 140 this, | 168 this, |
| 141 base::Bind(&PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete, | 169 base::Bind(&PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete, |
| 142 base::Unretained(this)))); | 170 base::Unretained(this)))); |
| 143 if (!enumerate_->requested()) | 171 if (!enumerate_->requested()) |
| 144 return PP_ERROR_FAILED; | 172 return PP_ERROR_FAILED; |
| 145 | 173 |
| 146 enumerate_devices_context_ = context->MakeReplyMessageContext(); | 174 enumerate_devices_context_ = context->MakeReplyMessageContext(); |
| 147 return PP_OK_COMPLETIONPENDING; | 175 return PP_OK_COMPLETIONPENDING; |
| 148 } | 176 } |
| 149 | 177 |
| 150 int32_t PepperDeviceEnumerationHostHelper::OnMonitorDeviceChange( | 178 int32_t PepperDeviceEnumerationHostHelper::OnMonitorDeviceChange( |
| 151 HostMessageContext* /* context */, | 179 HostMessageContext* /* context */, |
| 152 uint32_t callback_id) { | 180 uint32_t callback_id) { |
| 153 monitor_.reset(new ScopedRequest( | 181 monitor_.reset(new ScopedMonitoringRequest( |
| 154 this, | 182 this, base::Bind(&PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange, |
| 155 base::Bind(&PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange, | 183 base::Unretained(this), callback_id))); |
| 156 base::Unretained(this), | |
| 157 callback_id))); | |
| 158 | 184 |
| 159 return monitor_->requested() ? PP_OK : PP_ERROR_FAILED; | 185 return monitor_->requested() ? PP_OK : PP_ERROR_FAILED; |
| 160 } | 186 } |
| 161 | 187 |
| 162 int32_t PepperDeviceEnumerationHostHelper::OnStopMonitoringDeviceChange( | 188 int32_t PepperDeviceEnumerationHostHelper::OnStopMonitoringDeviceChange( |
| 163 HostMessageContext* /* context */) { | 189 HostMessageContext* /* context */) { |
| 164 monitor_.reset(NULL); | 190 monitor_.reset(NULL); |
| 165 return PP_OK; | 191 return PP_OK; |
| 166 } | 192 } |
| 167 | 193 |
| 168 void PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete( | 194 void PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete( |
| 169 int /* request_id */, | |
| 170 const std::vector<ppapi::DeviceRefData>& devices) { | 195 const std::vector<ppapi::DeviceRefData>& devices) { |
| 171 DCHECK(enumerate_devices_context_.is_valid()); | 196 DCHECK(enumerate_devices_context_.is_valid()); |
| 172 | 197 |
| 173 enumerate_.reset(NULL); | 198 enumerate_.reset(NULL); |
| 174 | 199 |
| 175 enumerate_devices_context_.params.set_result(PP_OK); | 200 enumerate_devices_context_.params.set_result(PP_OK); |
| 176 resource_host_->host()->SendReply( | 201 resource_host_->host()->SendReply( |
| 177 enumerate_devices_context_, | 202 enumerate_devices_context_, |
| 178 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply(devices)); | 203 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply(devices)); |
| 179 enumerate_devices_context_ = ppapi::host::ReplyMessageContext(); | 204 enumerate_devices_context_ = ppapi::host::ReplyMessageContext(); |
| 180 } | 205 } |
| 181 | 206 |
| 182 void PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange( | 207 void PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange( |
| 183 uint32_t callback_id, | 208 uint32_t callback_id, |
| 184 int /* request_id */, | |
| 185 const std::vector<ppapi::DeviceRefData>& devices) { | 209 const std::vector<ppapi::DeviceRefData>& devices) { |
| 186 resource_host_->host()->SendUnsolicitedReply( | 210 resource_host_->host()->SendUnsolicitedReply( |
| 187 resource_host_->pp_resource(), | 211 resource_host_->pp_resource(), |
| 188 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(callback_id, | 212 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(callback_id, |
| 189 devices)); | 213 devices)); |
| 190 } | 214 } |
| 191 | 215 |
| 192 } // namespace content | 216 } // namespace content |
| OLD | NEW |