summaryrefslogtreecommitdiff
path: root/chromium/content/browser/hid/hid_service.h
blob: 07abd4993abea82ed4c6a2efef1bb745a97389e0 (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
// Copyright 2019 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 CONTENT_BROWSER_HID_HID_SERVICE_H_
#define CONTENT_BROWSER_HID_HID_SERVICE_H_

#include <memory>
#include <string>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "content/public/browser/frame_service_base.h"
#include "content/public/browser/hid_delegate.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "services/device/public/mojom/hid.mojom.h"
#include "third_party/blink/public/mojom/hid/hid.mojom.h"

namespace content {

class HidChooser;
class RenderFrameHost;

// HidService provides an implementation of the HidService mojom interface. This
// interface is used by Blink to implement the WebHID API.
class HidService : public content::FrameServiceBase<blink::mojom::HidService>,
                   public device::mojom::HidConnectionWatcher,
                   public HidDelegate::Observer {
 public:
  HidService(HidService&) = delete;
  HidService& operator=(HidService&) = delete;

  static void Create(RenderFrameHost*,
                     mojo::PendingReceiver<blink::mojom::HidService>);

  // blink::mojom::HidService:
  void RegisterClient(
      device::mojom::HidManagerClientAssociatedPtrInfo client) override;
  void GetDevices(GetDevicesCallback callback) override;
  void RequestDevice(std::vector<blink::mojom::HidDeviceFilterPtr> filters,
                     RequestDeviceCallback callback) override;
  void Connect(const std::string& device_guid,
               mojo::PendingRemote<device::mojom::HidConnectionClient> client,
               ConnectCallback callback) override;

  // HidDelegate::Observer:
  void OnDeviceAdded(const device::mojom::HidDeviceInfo& device_info) override;
  void OnDeviceRemoved(
      const device::mojom::HidDeviceInfo& device_info) override;
  void OnHidManagerConnectionError() override;
  void OnPermissionRevoked(const url::Origin& requesting_origin,
                           const url::Origin& embedding_origin) override;

 private:
  HidService(RenderFrameHost*, mojo::PendingReceiver<blink::mojom::HidService>);
  ~HidService() override;

  void OnWatcherConnectionError();
  void DecrementActiveFrameCount();

  void FinishGetDevices(GetDevicesCallback callback,
                        std::vector<device::mojom::HidDeviceInfoPtr> devices);
  void FinishRequestDevice(
      RequestDeviceCallback callback,
      std::vector<device::mojom::HidDeviceInfoPtr> devices);
  void FinishConnect(
      ConnectCallback callback,
      mojo::PendingRemote<device::mojom::HidConnection> connection);

  // The last shown HID chooser UI.
  std::unique_ptr<HidChooser> chooser_;
  url::Origin requesting_origin_;
  url::Origin embedding_origin_;

  // Used to bind with Blink.
  mojo::AssociatedRemoteSet<device::mojom::HidManagerClient> clients_;

  // Each pipe here watches a connection created by Connect() in order to notify
  // the WebContentsImpl when an active connection indicator should be shown.
  mojo::ReceiverSet<device::mojom::HidConnectionWatcher> watchers_;

  base::WeakPtrFactory<HidService> weak_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_HID_HID_SERVICE_H_