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_
10 #include "base/bind_helpers.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/threading/non_thread_safe.h"
17 class SequencedTaskRunner
;
24 // The USB service handles creating and managing an event handler thread that is
25 // used to manage and dispatch USB events. It is also responsible for device
26 // discovery on the system, which allows it to re-use device handles to prevent
27 // competition for the same USB device.
28 class UsbService
: public base::NonThreadSafe
{
30 using GetDevicesCallback
=
31 base::Callback
<void(const std::vector
<scoped_refptr
<UsbDevice
>>&)>;
35 // These events are delivered from the thread on which the UsbService object
37 virtual void OnDeviceAdded(scoped_refptr
<UsbDevice
> device
);
38 virtual void OnDeviceRemoved(scoped_refptr
<UsbDevice
> device
);
39 // For observers that need to process device removal after others have run.
40 // Should not depend on any other service's knowledge of connected devices.
41 virtual void OnDeviceRemovedCleanup(scoped_refptr
<UsbDevice
> device
);
44 // The file task runner reference is used for blocking I/O operations.
45 // Returns NULL when initialization fails.
46 static UsbService
* GetInstance(
47 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
49 virtual scoped_refptr
<UsbDevice
> GetDeviceById(uint32 unique_id
) = 0;
51 // Enumerates available devices.
52 virtual void GetDevices(const GetDevicesCallback
& callback
) = 0;
54 void AddObserver(Observer
* observer
);
55 void RemoveObserver(Observer
* observer
);
59 virtual ~UsbService();
61 void NotifyDeviceAdded(scoped_refptr
<UsbDevice
> device
);
62 void NotifyDeviceRemoved(scoped_refptr
<UsbDevice
> device
);
64 ObserverList
<Observer
, true> observer_list_
;
67 friend void base::DeletePointer
<UsbService
>(UsbService
* service
);
69 DISALLOW_COPY_AND_ASSIGN(UsbService
);
74 #endif // DEVICE_USB_USB_SERVICE_H_