| Index: device/usb/mojo/device_impl.cc
|
| diff --git a/device/usb/mojo/device_impl.cc b/device/usb/mojo/device_impl.cc
|
| index 6269b05bae419e7d01fbec5d3bc2a52c9f70fc61..5f7d1937a0a40a0b9c4e345dd37daca0964d52a0 100644
|
| --- a/device/usb/mojo/device_impl.cc
|
| +++ b/device/usb/mojo/device_impl.cc
|
| @@ -27,9 +27,30 @@
|
| namespace {
|
|
|
| using MojoTransferInCallback =
|
| - base::Callback<void(TransferStatus, mojo::Array<uint8_t>)>;
|
| -
|
| -using MojoTransferOutCallback = base::Callback<void(TransferStatus)>;
|
| + mojo::Callback<void(TransferStatus, mojo::Array<uint8_t>)>;
|
| +
|
| +using MojoTransferOutCallback = mojo::Callback<void(TransferStatus)>;
|
| +
|
| +template <typename... Args>
|
| +void CallMojoCallback(std::unique_ptr<mojo::Callback<void(Args...)>> callback,
|
| + Args... args) {
|
| + callback->Run(args...);
|
| +}
|
| +
|
| +// Generic wrapper to convert a Mojo callback to something we can rebind and
|
| +// pass around. This is only usable for callbacks with no move-only arguments.
|
| +template <typename... Args>
|
| +base::Callback<void(Args...)> WrapMojoCallback(
|
| + const mojo::Callback<void(Args...)>& callback) {
|
| + // mojo::Callback is not thread safe. By wrapping |callback| in a scoped_ptr
|
| + // we guarantee that it will be freed when CallMojoCallback is run and not
|
| + // retained until the base::Callback is destroyed, which could happen on any
|
| + // thread. This pattern is also used below in places where this generic
|
| + // wrapper is not used.
|
| + auto callback_ptr =
|
| + base::WrapUnique(new mojo::Callback<void(Args...)>(callback));
|
| + return base::Bind(&CallMojoCallback<Args...>, base::Passed(&callback_ptr));
|
| +}
|
|
|
| scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) {
|
| scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(
|
| @@ -236,7 +257,7 @@
|
|
|
| if (permission_provider_ &&
|
| permission_provider_->HasConfigurationPermission(value, device_)) {
|
| - device_handle_->SetConfiguration(value, callback);
|
| + device_handle_->SetConfiguration(value, WrapMojoCallback(callback));
|
| } else {
|
| callback.Run(false);
|
| }
|
| @@ -269,7 +290,8 @@
|
| permission_provider_->HasFunctionPermission(interface_it->first_interface,
|
| config->configuration_value,
|
| device_)) {
|
| - device_handle_->ClaimInterface(interface_number, callback);
|
| + device_handle_->ClaimInterface(interface_number,
|
| + WrapMojoCallback(callback));
|
| } else {
|
| callback.Run(false);
|
| }
|
| @@ -282,7 +304,8 @@
|
| return;
|
| }
|
|
|
| - device_handle_->ReleaseInterface(interface_number, callback);
|
| + device_handle_->ReleaseInterface(interface_number,
|
| + WrapMojoCallback(callback));
|
| }
|
|
|
| void DeviceImpl::SetInterfaceAlternateSetting(
|
| @@ -294,8 +317,8 @@
|
| return;
|
| }
|
|
|
| - device_handle_->SetInterfaceAlternateSetting(interface_number,
|
| - alternate_setting, callback);
|
| + device_handle_->SetInterfaceAlternateSetting(
|
| + interface_number, alternate_setting, WrapMojoCallback(callback));
|
| }
|
|
|
| void DeviceImpl::Reset(const ResetCallback& callback) {
|
| @@ -304,7 +327,7 @@
|
| return;
|
| }
|
|
|
| - device_handle_->ResetDevice(callback);
|
| + device_handle_->ResetDevice(WrapMojoCallback(callback));
|
| }
|
|
|
| void DeviceImpl::ClearHalt(uint8_t endpoint,
|
| @@ -314,7 +337,7 @@
|
| return;
|
| }
|
|
|
| - device_handle_->ClearHalt(endpoint, callback);
|
| + device_handle_->ClearHalt(endpoint, WrapMojoCallback(callback));
|
| }
|
|
|
| void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params,
|
|
|