summaryrefslogtreecommitdiff
path: root/chromium/device/usb/usb_service_android.h
blob: ae1ec5a3f2269ec9435feb283ddf94f4c269744e (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
// 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_SERVICE_ANDROID_H_
#define DEVICE_USB_USB_SERVICE_ANDROID_H_

#include <jni.h>

#include <unordered_map>

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

namespace device {

class UsbDeviceAndroid;

// USB service implementation for Android. This is a stub implementation that
// does not return any devices.
class UsbServiceAndroid : public UsbService {
 public:
  // Register C++ methods exposed to Java using JNI.
  static bool RegisterJNI(JNIEnv* env);

  UsbServiceAndroid();
  ~UsbServiceAndroid() override;

  // Methods called by Java.
  void DeviceAttached(JNIEnv* env,
                      const base::android::JavaRef<jobject>& caller,
                      const base::android::JavaRef<jobject>& usb_device);
  void DeviceDetached(JNIEnv* env,
                      const base::android::JavaRef<jobject>& caller,
                      jint device_id);
  void DevicePermissionRequestComplete(
      JNIEnv* env,
      const base::android::JavaRef<jobject>& caller,
      jint device_id,
      jboolean granted);

  // Called by UsbDeviceAndroid.
  base::android::ScopedJavaLocalRef<jobject> OpenDevice(
      JNIEnv* env,
      const base::android::JavaRef<jobject>& wrapper);
  void RequestDevicePermission(const base::android::JavaRef<jobject>& wrapper,
                               jint device_id);

 private:
  void AddDevice(scoped_refptr<UsbDeviceAndroid> device);

  std::unordered_map<jint, scoped_refptr<UsbDeviceAndroid>> devices_by_id_;

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

  base::WeakPtrFactory<UsbServiceAndroid> weak_factory_;
};

}  // namespace device

#endif  // DEVICE_USB_USB_SERVICE_ANDROID_H_