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
>>&)>;
38 // These events are delivered from the thread on which the UsbService object
40 virtual void OnDeviceAdded(scoped_refptr
<UsbDevice
> device
);
41 virtual void OnDeviceRemoved(scoped_refptr
<UsbDevice
> device
);
42 // For observers that need to process device removal after others have run.
43 // Should not depend on any other service's knowledge of connected devices.
44 virtual void OnDeviceRemovedCleanup(scoped_refptr
<UsbDevice
> device
);
47 // The file task runner reference is used for blocking I/O operations.
48 // Returns nullptr when initialization fails.
49 static scoped_ptr
<UsbService
> Create(
50 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
52 virtual ~UsbService();
54 virtual scoped_refptr
<UsbDevice
> GetDevice(const std::string
& guid
) = 0;
56 // Enumerates available devices.
57 virtual void GetDevices(const GetDevicesCallback
& callback
) = 0;
59 void AddObserver(Observer
* observer
);
60 void RemoveObserver(Observer
* observer
);
65 void NotifyDeviceAdded(scoped_refptr
<UsbDevice
> device
);
66 void NotifyDeviceRemoved(scoped_refptr
<UsbDevice
> device
);
68 base::ObserverList
<Observer
, true> observer_list_
;
71 friend void base::DeletePointer
<UsbService
>(UsbService
* service
);
73 DISALLOW_COPY_AND_ASSIGN(UsbService
);
78 #endif // DEVICE_USB_USB_SERVICE_H_