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
);
46 // Notifies the observer that the UsbService it depends on is shutting down.
47 virtual void WillDestroyUsbService();
50 // The file task runner reference is used for blocking I/O operations.
51 // Returns nullptr when initialization fails.
52 static scoped_ptr
<UsbService
> Create(
53 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
55 virtual ~UsbService();
57 virtual scoped_refptr
<UsbDevice
> GetDevice(const std::string
& guid
) = 0;
59 // Enumerates available devices.
60 virtual void GetDevices(const GetDevicesCallback
& callback
) = 0;
62 void AddObserver(Observer
* observer
);
63 void RemoveObserver(Observer
* observer
);
68 void NotifyDeviceAdded(scoped_refptr
<UsbDevice
> device
);
69 void NotifyDeviceRemoved(scoped_refptr
<UsbDevice
> device
);
71 base::ObserverList
<Observer
, true> observer_list_
;
74 friend void base::DeletePointer
<UsbService
>(UsbService
* service
);
76 DISALLOW_COPY_AND_ASSIGN(UsbService
);
81 #endif // DEVICE_USB_USB_SERVICE_H_