summaryrefslogtreecommitdiffstats
path: root/chrome/browser/usb/usb_service.h
blob: 3f11a7d9ec105722e66bd05988400e1de832a9c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_USB_USB_SERVICE_H_
#define CHROME_BROWSER_USB_USB_SERVICE_H_
#pragma once

#include <map>
#include <utility>

#include "base/basictypes.h"
#include "base/threading/thread.h"
#include "chrome/browser/profiles/profile_keyed_service.h"
#include "chrome/browser/usb/usb_device.h"
#include "third_party/libusb/libusb/libusb.h"

typedef libusb_context* PlatformUsbContext;

// The USB service handles creating and managing an event handler thread that is
// used to manage and dispatch USB events. It is also responsbile for device
// discovery on the system, which allows it to re-use device handles to prevent
// competition for the same USB device.
class UsbService : public ProfileKeyedService {
 public:
  UsbService();
  virtual ~UsbService();

  // Cleanup must be invoked before the service is destroyed. It interrupts the
  // event handling thread and disposes of open devices.
  void Cleanup();

  // Find the (topologically) first USB device identified by vendor_id and
  // product_id. The created device is associated with this service, so that
  // it can be used to close the device later.
  UsbDevice* FindDevice(const uint16 vendor_id, const uint16 product_id);

  // This function should not be called by normal code. It is invoked by a
  // UsbDevice's Close function and disposes of the associated platform handle.
  void CloseDevice(scoped_refptr<UsbDevice> device);

 private:
  // Posts a HandleEvent task to the event handling thread.
  void PostHandleEventTask();

  // Handles a single USB event. If the service is still running after the event
  // is handled, posts another HandleEvent callback to the thread.
  void HandleEvent();

  // PlatformShutdown is invoked after event handling has been suspended and is
  // used to free the platform resources associated with the service.
  void PlatformShutdown();

  bool running_;
  PlatformUsbContext context_;
  base::Thread thread_;

  // The devices_ map contains scoped_refptrs to all open devices, indicated by
  // their vendor and product id. This allows for reusing an open device without
  // creating another platform handle for it.
  typedef std::map<std::pair<uint16, uint16>, scoped_refptr<UsbDevice> >
      DeviceMap;
  DeviceMap devices_;

  DISALLOW_EVIL_CONSTRUCTORS(UsbService);
};

#endif  // CHROME_BROWSER_USB_USB_SERVICE_H_