Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2055)

Unified Diff: device/usb/mojo/device_impl.cc

Issue 2080083002: Revert of Deletes mojo::Callback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/usb/mojo/device_impl.h ('k') | device/usb/mojo/device_manager_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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,
« no previous file with comments | « device/usb/mojo/device_impl.h ('k') | device/usb/mojo/device_manager_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698