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

Unified Diff: chrome/browser/usb/usb_service.cc

Issue 22914023: Introducing chrome.usb.getDevices/openDevice API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-interface
Patch Set: Adding chrome.usb.requestAccess Created 7 years, 4 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
Index: chrome/browser/usb/usb_service.cc
diff --git a/chrome/browser/usb/usb_service.cc b/chrome/browser/usb/usb_service.cc
index 4425d9b4b728bef2a60a47d31e0f7cc1d00b9252..0d575fbf3733dc644d12713f69b6645c6b24034a 100644
--- a/chrome/browser/usb/usb_service.cc
+++ b/chrome/browser/usb/usb_service.cc
@@ -7,12 +7,15 @@
#include <set>
#include <vector>
+#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
+#include "base/memory/ref_counted.h"
#include "base/stl_util.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/usb/usb_context.h"
+#include "chrome/browser/usb/usb_device.h"
#include "chrome/browser/usb/usb_device_handle.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_observer.h"
@@ -20,12 +23,6 @@
#include "content/public/browser/notification_service.h"
#include "third_party/libusb/src/libusb/libusb.h"
-#if defined(OS_CHROMEOS)
-#include "base/chromeos/chromeos_version.h"
-#include "chromeos/dbus/dbus_thread_manager.h"
-#include "chromeos/dbus/permission_broker_client.h"
-#endif // defined(OS_CHROMEOS)
-
namespace content {
class NotificationDetails;
@@ -64,14 +61,14 @@ class ExitObserver : public content::NotificationObserver {
using content::BrowserThread;
UsbService::UsbService()
- : context_(new UsbContext()) {
+ : context_(new UsbContext()),
+ next_unique_id_(0) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
}
UsbService::~UsbService() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- for (DeviceMap::iterator it = devices_.begin();
- it != devices_.end(); ++it) {
+ for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
it->second->OnDisconnect();
}
}
@@ -82,97 +79,24 @@ UsbService* UsbService::GetInstance() {
return Singleton<UsbService, LeakySingletonTraits<UsbService> >::get();
}
-void UsbService::FindDevices(
- const uint16 vendor_id,
- const uint16 product_id,
- int interface_id,
- const base::Callback<void(ScopedDeviceVector vector)>& callback) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
-#if defined(OS_CHROMEOS)
- // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to
- // use permission broker.
- if (base::chromeos::IsRunningOnChromeOS()) {
- chromeos::PermissionBrokerClient* client =
- chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
- DCHECK(client) << "Could not get permission broker client.";
- if (!client) {
- callback.Run(ScopedDeviceVector());
- return;
- }
-
- BrowserThread::PostTask(
- BrowserThread::UI,
- FROM_HERE,
- base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess,
- base::Unretained(client),
- vendor_id,
- product_id,
- interface_id,
- base::Bind(&UsbService::OnRequestUsbAccessReplied,
- base::Unretained(this),
- vendor_id,
- product_id,
- callback)));
- } else {
- FindDevicesImpl(vendor_id, product_id, callback, true);
- }
-#else
- FindDevicesImpl(vendor_id, product_id, callback, true);
-#endif // defined(OS_CHROMEOS)
-}
-
void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
STLClearObject(devices);
RefreshDevices();
- for (DeviceMap::iterator it = devices_.begin();
- it != devices_.end(); ++it) {
+ for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
devices->push_back(it->second);
}
}
-void UsbService::OnRequestUsbAccessReplied(
- const uint16 vendor_id,
- const uint16 product_id,
- const base::Callback<void(ScopedDeviceVector vectors)>& callback,
- bool success) {
- BrowserThread::PostTask(
- BrowserThread::FILE,
- FROM_HERE,
- base::Bind(&UsbService::FindDevicesImpl,
- base::Unretained(this),
- vendor_id,
- product_id,
- callback,
- success));
-}
-
-void UsbService::FindDevicesImpl(
- const uint16 vendor_id,
- const uint16 product_id,
- const base::Callback<void(ScopedDeviceVector vectors)>& callback,
- bool success) {
+scoped_refptr<UsbDevice> UsbService::GetDeviceById(uint32 unique_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- ScopedDeviceVector devices(new vector<scoped_refptr<UsbDevice> >());
-
- // If the permission broker was unable to obtain permission for the specified
- // devices then there is no point in attempting to enumerate the devices. On
- // platforms without a permission broker, we assume permission is granted.
- if (!success) {
- callback.Run(devices.Pass());
- return;
- }
-
RefreshDevices();
- for (DeviceMap::iterator it = devices_.begin();
- it != devices_.end(); ++it) {
- if (DeviceMatches(it->second, vendor_id, product_id))
- devices->push_back(it->second);
+ for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
+ if (it->second->unique_id() == unique_id) return it->second;
}
-
- callback.Run(devices.Pass());
+ return NULL;
}
void UsbService::RefreshDevices() {
@@ -193,9 +117,10 @@ void UsbService::RefreshDevices() {
if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor))
continue;
UsbDevice* new_device = new UsbDevice(context_,
- platform_devices[i],
- descriptor.idVendor,
- descriptor.idProduct);
+ platform_devices[i],
+ descriptor.idVendor,
+ descriptor.idProduct,
+ ++next_unique_id_);
devices_[platform_devices[i]] = new_device;
connected_devices.insert(new_device);
} else {
@@ -219,9 +144,3 @@ void UsbService::RefreshDevices() {
libusb_free_device_list(platform_devices, true);
}
-
-bool UsbService::DeviceMatches(scoped_refptr<UsbDevice> device,
- const uint16 vendor_id,
- const uint16 product_id) {
- return device->vendor_id() == vendor_id && device->product_id() == product_id;
-}

Powered by Google App Engine
This is Rietveld 408576698