summaryrefslogtreecommitdiffstats
path: root/device/devices_app/usb/device_manager_impl.h
blob: 4e9578d0e095337623bc559cddd057ea711c4bec (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 2015 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_DEVICE_MANAGER_IMPL_H_
#define DEVICE_USB_DEVICE_MANAGER_IMPL_H_

#include <queue>
#include <set>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "device/devices_app/usb/public/interfaces/device_manager.mojom.h"
#include "device/devices_app/usb/public/interfaces/permission_provider.mojom.h"
#include "device/usb/usb_service.h"
#include "mojo/public/cpp/bindings/array.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/interface_request.h"

namespace base {
class SequencedTaskRunner;
}

namespace device {

class UsbDevice;
class UsbDeviceFilter;
class UsbDeviceHandle;

namespace usb {

class DeviceManagerDelegate;

// Implementation of the public DeviceManager interface. This interface can be
// requested from the devices app located at "mojo:devices", if available.
class DeviceManagerImpl : public DeviceManager,
                          public device::UsbService::Observer {
 public:
  using DeviceList = std::vector<scoped_refptr<UsbDevice>>;
  using DeviceMap = std::map<std::string, scoped_refptr<device::UsbDevice>>;

  static void Create(PermissionProviderPtr permission_provider,
                     mojo::InterfaceRequest<DeviceManager> request);

  DeviceManagerImpl(PermissionProviderPtr permission_provider,
                    mojo::InterfaceRequest<DeviceManager> request);
  ~DeviceManagerImpl() override;

  void set_connection_error_handler(const mojo::Closure& error_handler) {
    connection_error_handler_ = error_handler;
  }

 private:
  // DeviceManager implementation:
  void GetDevices(EnumerationOptionsPtr options,
                  const GetDevicesCallback& callback) override;
  void GetDeviceChanges(const GetDeviceChangesCallback& callback) override;
  void GetDevice(const mojo::String& guid,
                 mojo::InterfaceRequest<Device> device_request) override;

  // Callbacks to handle the async responses from the underlying UsbService.
  void OnGetDevicePermissionCheckComplete(
      scoped_refptr<device::UsbDevice> device,
      mojo::InterfaceRequest<Device> device_request,
      mojo::Array<mojo::String> allowed_guids);
  void OnGetDevices(EnumerationOptionsPtr options,
                    const GetDevicesCallback& callback,
                    const DeviceList& devices);

  // UsbService::Observer implementation:
  void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override;
  void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override;
  void WillDestroyUsbService() override;

  void MaybeRunDeviceChangesCallback();
  void OnEnumerationPermissionCheckComplete(
      const DeviceMap& devices_added,
      const DeviceMap& devices_removed,
      mojo::Array<mojo::String> allowed_guids);

  PermissionProviderPtr permission_provider_;

  // If there are unfinished calls to GetDeviceChanges their callbacks
  // are stored in |device_change_callbacks_|. Otherwise device changes
  // are collected in |devices_added_| and |devices_removed_| until the
  // next call to GetDeviceChanges.
  std::queue<GetDeviceChangesCallback> device_change_callbacks_;
  DeviceMap devices_added_;
  DeviceMap devices_removed_;
  // To ensure that GetDeviceChangesCallbacks are called in the correct order
  // only perform a single request to |permission_provider_| at a time.
  bool permission_request_pending_ = false;

  UsbService* usb_service_;
  ScopedObserver<device::UsbService, device::UsbService::Observer> observer_;

  mojo::Closure connection_error_handler_;

  mojo::Binding<DeviceManager> binding_;
  base::WeakPtrFactory<DeviceManagerImpl> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(DeviceManagerImpl);
};

}  // namespace usb
}  // namespace device

#endif  // DEVICE_USB_DEVICE_MANAGER_IMPL_H_