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

Issue 18593002: Usb backend refactor step 2: Separate Usb Device Handle and Usb Device (deprecate) (Closed)

Created:
7 years, 5 months ago by Bei Zhang
Modified:
7 years, 4 months ago
Reviewers:
pfeldman
CC:
chromium-reviews, chromium-apps-reviews_chromium.org, extensions-reviews_chromium.org
Visibility:
Public.

Description

Usb backend refactor step 2: Separate Usb Device Handle and Usb Device. This change will allow UsbService to provide unique device ids. It will fix all the threading problems as well. BUG=223817

Patch Set 1 : Separate Usb Device Handle and Usb Device #

Total comments: 6

Patch Set 2 : #

Total comments: 26
Unified diffs Side-by-side diffs Delta from patch set Stats (+764 lines, -300 lines) Patch
M chrome/browser/devtools/adb/android_usb_device.cc View 1 4 chunks +13 lines, -15 lines 0 comments Download
M chrome/browser/extensions/api/usb/usb_api.h View 3 chunks +5 lines, -0 lines 0 comments Download
M chrome/browser/extensions/api/usb/usb_api.cc View 16 chunks +68 lines, -41 lines 2 comments Download
M chrome/browser/extensions/api/usb/usb_device_resource.h View 2 chunks +48 lines, -3 lines 0 comments Download
M chrome/browser/extensions/api/usb/usb_device_resource.cc View 2 chunks +174 lines, -1 line 1 comment Download
M chrome/browser/usb/usb_device.h View 1 5 chunks +32 lines, -37 lines 2 comments Download
M chrome/browser/usb/usb_device.cc View 1 11 chunks +180 lines, -63 lines 10 comments Download
M chrome/browser/usb/usb_service.h View 1 2 chunks +35 lines, -40 lines 4 comments Download
M chrome/browser/usb/usb_service.cc View 1 6 chunks +209 lines, -100 lines 7 comments Download

Messages

Total messages: 5 (0 generated)
pfeldman
https://codereview.chromium.org/18593002/diff/3007/chrome/browser/usb/usb_device.h File chrome/browser/usb/usb_device.h (right): https://codereview.chromium.org/18593002/diff/3007/chrome/browser/usb/usb_device.h#newcode23 chrome/browser/usb/usb_device.h:23: typedef libusb_device* PlatformUsbDevice; All these typedefs were here to ...
7 years, 5 months ago (2013-07-21 08:48:23 UTC) #1
Bei Zhang
Thanks for the comment. I'll start experiment on introducing UsbTransfer now. https://codereview.chromium.org/18593002/diff/3007/chrome/browser/usb/usb_device.h File chrome/browser/usb/usb_device.h (right): ...
7 years, 5 months ago (2013-07-21 21:31:52 UTC) #2
pfeldman
I know I'm throwing a lot of suggestions below, but I think they eventually make ...
7 years, 5 months ago (2013-07-22 08:39:45 UTC) #3
pfeldman
https://codereview.chromium.org/18593002/diff/69002/chrome/browser/extensions/api/usb/usb_api.cc File chrome/browser/extensions/api/usb/usb_api.cc (right): https://codereview.chromium.org/18593002/diff/69002/chrome/browser/extensions/api/usb/usb_api.cc#newcode455 chrome/browser/extensions/api/usb/usb_api.cc:455: base::Unretained(service_), service_ might have been deleted by now. You ...
7 years, 5 months ago (2013-07-22 18:23:13 UTC) #4
Bei Zhang
7 years, 5 months ago (2013-07-23 17:58:44 UTC) #5
https://codereview.chromium.org/18593002/diff/69002/chrome/browser/extensions...
File chrome/browser/extensions/api/usb/usb_api.cc (right):

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/extensions...
chrome/browser/extensions/api/usb/usb_api.cc:455: base::Unretained(service_),
I will make it Singleton and it will never shows up in the PostTask. Also,
libusb_context is designed to have only one instance as well.

On 2013/07/22 18:23:13, pfeldman wrote:
> service_ might have been deleted by now. You should make it
RefCountedThreadSafe

> (and wrap with another object for browsercontextgetter).

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_de...
File chrome/browser/usb/usb_device.cc (right):

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_de...
chrome/browser/usb/usb_device.cc:103: TransferInfo();
On 2013/07/22 08:39:45, pfeldman wrote:
> Constructor should go first

Done.

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_de...
chrome/browser/usb/usb_device.cc:231: void
UsbDevice::ListInterfaces(UsbConfigDescriptor* config,
I see... That'll do.

On 2013/07/22 18:23:13, pfeldman wrote:
> Why is this using callback? It returns synchronously. If you require it to be
> running on FILE, you can drop the callback.

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_de...
chrome/browser/usb/usb_device.cc:316: base::Bind(&UsbDevice::SubmitTransfer,
There is a map to the transfers which is not thread safe. I'm trying to avoid
the lock.

On 2013/07/22 18:23:13, pfeldman wrote:
> Why scheduling submit? libusb_submit_transfer claims to be returning
immediately
> as a part of async io.

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_de...
File chrome/browser/usb/usb_device.h (right):

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_de...
chrome/browser/usb/usb_device.h:130: static void API_CALL
HandleTransferCompletion(
This callback needs __stdcall on windows.

On 2013/07/22 18:23:13, pfeldman wrote:
> Why API_CALL?

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_se...
File chrome/browser/usb/usb_service.cc (right):

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_se...
chrome/browser/usb/usb_service.cc:72: class UsbContext : public
base::RefCountedThreadSafe<UsbContext> {
On 2013/07/22 08:39:45, pfeldman wrote:
> I think this tiny bit is worth landing separately.

Done.

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_se...
chrome/browser/usb/usb_service.cc:199: client->RequestUsbAccess(vendor_id,
Nope, this is a bug.

On 2013/07/22 18:23:13, pfeldman wrote:
> Can you call it on FILE?

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_se...
chrome/browser/usb/usb_service.cc:202: base::Bind(&UsbService::FindDevicesImpl,
If it was working, which I doubt, then it shall be called on IO thread.

On 2013/07/22 18:23:13, pfeldman wrote:
> On what thread will it be called?

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_se...
File chrome/browser/usb/usb_service.h (right):

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_se...
chrome/browser/usb/usb_service.h:34: // The following methods must be called on
FILE thread:
This is the summary of our discussion about this:
1. We determined that the enumeration of the usb devices will require blocking
IOs that is not allowed on UI thread or IO thread.
2. We shall not have more than one UsbService because libusb does not actually
support that.

In conclusion, we will make UsbService a Singleton that works mostly on FILE
thread but construct/destructs on UI thread.


On 2013/07/22 08:39:45, pfeldman wrote:
> UsbService is created and deleted on UI thread. You can't call its methods on
> FILE thread since you are introducing a race condition.
> 
> You either need to make it ref-counted + wrap it for use with
> BrowserContextKeyedService (as I do in
>
https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/dev...)
> or you need to make it only work UI thread only.
> 
> I actually think that latter is preferred. You would essentially need to
> marshall its public methods to FILE thread first in usb_service.cc, but it
> should not be a big deal given that your API has callbacks for return values.
As
> a benefit, all clients become simpler - they no longer need to do marshalling
as
> you do in usb_api.cc.

https://codereview.chromium.org/18593002/diff/69002/chrome/browser/usb/usb_se...
chrome/browser/usb/usb_service.h:47: void GetDevices(std::vector<int>* devices);
Yes, that will be exactly what the step 3 does.

On 2013/07/22 08:39:45, pfeldman wrote:
> I don't think this integer handle makes sense. There is a number of operations
> available on the device (on libusb_device) without opening it.
> libusb_get_device_descriptor and libusb_get_active_config_descriptor would be
> good examples. Your UsbDeviceStub captures (not yet opened) device well and I
> think you should expose it to the API. I.e. I think your API could reflect
> libusb API better:
> 
> UsbService -> context, singleton
> UsbDevice -> libusb_device
> UsbDeviceHandle -> libusb_device_handle
> UsbInterface -> libusb_device_handle with claimed interface
> 
> those types would use RAII well.
> 
> Another idea is that although UsbDevice is not thread-safe, it could be
operated
> on any IO thread. Like WeakPtr - client picks thread with its first call and
> sticks to it. libusb_device is thread-safe, so it should be doable.

Powered by Google App Engine
This is Rietveld 408576698