1 // Copyright 2014 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.
5 #ifndef DEVICE_USB_USB_SERVICE_H_
6 #define DEVICE_USB_USB_SERVICE_H_
11 #include "base/bind_helpers.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/threading/non_thread_safe.h"
18 class SequencedTaskRunner
;
25 // The USB service handles creating and managing an event handler thread that is
26 // used to manage and dispatch USB events. It is also responsible for device
27 // discovery on the system, which allows it to re-use device handles to prevent
28 // competition for the same USB device.
29 class UsbService
: public base::NonThreadSafe
{
31 using GetDevicesCallback
=
32 base::Callback
<void(const std::vector
<scoped_refptr
<UsbDevice
>>&)>;
36 // These events are delivered from the thread on which the UsbService object
38 virtual void OnDeviceAdded(scoped_refptr
<UsbDevice
> device
);
39 virtual void OnDeviceRemoved(scoped_refptr
<UsbDevice
> device
);
40 // For observers that need to process device removal after others have run.
41 // Should not depend on any other service's knowledge of connected devices.
42 virtual void OnDeviceRemovedCleanup(scoped_refptr
<UsbDevice
> device
);
45 // The file task runner reference is used for blocking I/O operations.
46 // Returns NULL when initialization fails.
47 static UsbService
* GetInstance(
48 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
50 virtual scoped_refptr
<UsbDevice
> GetDevice(const std::string
& guid
) = 0;
52 // Enumerates available devices.
53 virtual void GetDevices(const GetDevicesCallback
& callback
) = 0;
55 void AddObserver(Observer
* observer
);
56 void RemoveObserver(Observer
* observer
);
60 virtual ~UsbService();
62 void NotifyDeviceAdded(scoped_refptr
<UsbDevice
> device
);
63 void NotifyDeviceRemoved(scoped_refptr
<UsbDevice
> device
);
65 base::ObserverList
<Observer
, true> observer_list_
;
68 friend void base::DeletePointer
<UsbService
>(UsbService
* service
);
70 DISALLOW_COPY_AND_ASSIGN(UsbService
);
75 #endif // DEVICE_USB_USB_SERVICE_H_