summaryrefslogtreecommitdiff
path: root/chromium/device/usb/usb_device_android.h
blob: 70782635e79442129f3a5de79b442b2d21fcc7c9 (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
// 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_USB_DEVICE_ANDROID_H_
#define DEVICE_USB_USB_DEVICE_ANDROID_H_

#include "base/android/scoped_java_ref.h"
#include "base/memory/weak_ptr.h"
#include "device/usb/usb_device.h"

namespace device {

class UsbServiceAndroid;

class UsbDeviceAndroid : public UsbDevice {
 public:
  static scoped_refptr<UsbDeviceAndroid> Create(
      JNIEnv* env,
      base::WeakPtr<UsbServiceAndroid> service,
      const base::android::JavaRef<jobject>& usb_device);

  // UsbDevice:
  void RequestPermission(const ResultCallback& callback) override;
  bool permission_granted() const override;
  void Open(const OpenCallback& callback) override;

  jint device_id() const { return device_id_; }
  void PermissionGranted(bool granted);

 private:
  UsbDeviceAndroid(
      JNIEnv* env,
      base::WeakPtr<UsbServiceAndroid> service,
      uint16_t usb_version,
      uint8_t device_class,
      uint8_t device_subclass,
      uint8_t device_protocol,
      uint16_t vendor_id,
      uint16_t product_id,
      uint16_t device_version,
      const base::string16& manufacturer_string,
      const base::string16& product_string,
      const base::string16& serial_number,
      const base::android::JavaRef<jobject>& wrapper);
  ~UsbDeviceAndroid() override;

  void CallRequestPermissionCallbacks(bool granted);
  void OnDeviceOpenedToReadDescriptors(
      scoped_refptr<UsbDeviceHandle> device_handle);
  void OnReadDescriptors(scoped_refptr<UsbDeviceHandle> device_handle,
                         std::unique_ptr<UsbDeviceDescriptor> descriptor);
  void OnReadWebUsbDescriptors(
      scoped_refptr<UsbDeviceHandle> device_handle,
      const GURL& landing_page);

  const jint device_id_;
  bool permission_granted_ = false;
  std::list<ResultCallback> request_permission_callbacks_;
  base::WeakPtr<UsbServiceAndroid> service_;

  // Java object org.chromium.device.usb.ChromeUsbDevice.
  base::android::ScopedJavaGlobalRef<jobject> j_object_;
};

}  // namespace device

#endif  // DEVICE_USB_USB_DEVICE_ANDROID_H_