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

Side by Side Diff: chrome/browser/usb/usb_service.cc

Issue 10161035: Adding UsbService and UsbDevice constructs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adding DEPS Created 8 years, 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/usb/usb_service.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/stl_util.h"
10 #include "chrome/browser/usb/usb_device.h"
11 #include "third_party/libusb/libusb/libusb.h"
12
13 UsbService::UsbService() : running_(true), thread_("UsbThread") {
14 libusb_init(&context_);
15 thread_.Start();
16 PostHandleEventTask();
17 }
18
19 UsbService::~UsbService() {}
20
21 // TODO(gdk): There is currently no clean way to indicate to the event handler
22 // thread that it must break out of the handling loop before the event timeout,
23 // therefore we currently are at the whim of the event handler timeout before
24 // the message handling thread can be joined.
25 void UsbService::Cleanup() {
26 running_ = false;
27
28 if (!devices_.empty()) {
29 libusb_close(devices_.begin()->second->handle());
30 } else {
31 LOG(WARNING) << "UsbService cannot force the USB event-handler thread to "
32 << "exit because there are no open devices with which to "
33 << "manipulate it. It maybe take up to 60 (!) seconds for the "
34 << "thread to join from this point.";
35 }
36
37 thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
38 &UsbService::PlatformShutdown, base::Unretained(this)));
39 }
40
41 UsbDevice* UsbService::FindDevice(const uint16 vendor_id,
42 const uint16 product_id) {
43 const std::pair<uint16, uint16> key = std::make_pair(vendor_id, product_id);
44 if (ContainsKey(devices_, key)) {
45 return devices_[key];
46 }
47
48 libusb_device_handle* const handle = libusb_open_device_with_vid_pid(
49 context_, vendor_id, product_id);
50 if (!handle) {
51 return NULL;
52 }
53
54 UsbDevice* const device = new UsbDevice(this, handle);
55 devices_[key] = device;
56
57 return device;
58 }
59
60 void UsbService::CloseDevice(scoped_refptr<UsbDevice> device) {
61 DCHECK(running_) << "Cannot close device after service has stopped running.";
62
63 for (DeviceMap::iterator i = devices_.begin(); i != devices_.end(); ++i) {
64 if (i->second.get() == device.get()) {
65 devices_.erase(i);
66 libusb_close(device->handle());
67 return;
68 }
69 }
70 }
71
72 void UsbService::PostHandleEventTask() {
73 thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
74 &UsbService::HandleEvent, base::Unretained(this)));
75 }
76
77 void UsbService::HandleEvent() {
78 libusb_handle_events_completed(context_, NULL);
79 if (running_) {
80 PostHandleEventTask();
81 }
82 }
83
84 void UsbService::PlatformShutdown() {
85 libusb_exit(context_);
86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698