summaryrefslogtreecommitdiff
path: root/chromium/device/usb/usb_service.h
blob: 79a1eff101f978b113389628a667afef37d65b7a (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2014 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 DEVICE_USB_USB_SERVICE_H_
#define DEVICE_USB_USB_SERVICE_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/task_scheduler/task_traits.h"
#include "base/threading/non_thread_safe.h"

namespace device {

class UsbDevice;

// The USB service handles creating and managing an event handler thread that is
// used to manage and dispatch USB events. It is also responsible 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 base::NonThreadSafe {
 public:
  using GetDevicesCallback =
      base::Callback<void(const std::vector<scoped_refptr<UsbDevice>>&)>;

  class Observer {
   public:
    virtual ~Observer();

    // These events are delivered from the thread on which the UsbService object
    // was created.
    virtual void OnDeviceAdded(scoped_refptr<UsbDevice> device);
    virtual void OnDeviceRemoved(scoped_refptr<UsbDevice> device);
    // For observers that need to process device removal after others have run.
    // Should not depend on any other service's knowledge of connected devices.
    virtual void OnDeviceRemovedCleanup(scoped_refptr<UsbDevice> device);

    // Notifies the observer that the UsbService it depends on is shutting down.
    virtual void WillDestroyUsbService();
  };

  // These task traits are to be used for posting blocking tasks to the task
  // scheduler.
  static constexpr base::TaskTraits kBlockingTaskTraits = {
      base::MayBlock(), base::TaskPriority::USER_VISIBLE,
      base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};

  // Returns nullptr when initialization fails.
  static std::unique_ptr<UsbService> Create();

  // Creates a SequencedTaskRunner with kBlockingTaskTraits.
  static scoped_refptr<base::SequencedTaskRunner> CreateBlockingTaskRunner();

  virtual ~UsbService();

  scoped_refptr<UsbDevice> GetDevice(const std::string& guid);

  // Enumerates available devices.
  virtual void GetDevices(const GetDevicesCallback& callback);

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Methods to add and remove devices for testing purposes. Only a device added
  // by this method can be removed by RemoveDeviceForTesting().
  void AddDeviceForTesting(scoped_refptr<UsbDevice> device);
  void RemoveDeviceForTesting(const std::string& device_guid);
  void GetTestDevices(std::vector<scoped_refptr<UsbDevice>>* devices);

 protected:
  UsbService(scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);

  void NotifyDeviceAdded(scoped_refptr<UsbDevice> device);
  void NotifyDeviceRemoved(scoped_refptr<UsbDevice> device);

  const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const {
    return task_runner_;
  }

  const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner() const {
    return blocking_task_runner_;
  }

  std::unordered_map<std::string, scoped_refptr<UsbDevice>>& devices() {
    return devices_;
  }

 private:
  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  std::unordered_map<std::string, scoped_refptr<UsbDevice>> devices_;
  std::unordered_set<std::string> testing_devices_;
  base::ObserverList<Observer, true> observer_list_;

  DISALLOW_COPY_AND_ASSIGN(UsbService);
};

}  // namespace device

#endif  // DEVICE_USB_USB_SERVICE_H_