blob: dd3eabbce3a9d4b0f21457bb7a1578e0d71d0ffa [file] [log] [blame]
Prashant Malanif07c9522017-04-11 14:54:29 -07001// Copyright 2017 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MIDIS_CLIENT_H_
6#define MIDIS_CLIENT_H_
7
8#include <memory>
Prashant Malanif9334732017-04-26 13:06:20 -07009#include <vector>
Prashant Malanif07c9522017-04-11 14:54:29 -070010
11#include <base/files/scoped_file.h>
12#include <base/memory/weak_ptr.h>
13#include <brillo/message_loops/message_loop.h>
hscham84b55932021-02-24 17:43:00 +090014#include <mojo/public/cpp/bindings/pending_receiver.h>
15#include <mojo/public/cpp/bindings/pending_remote.h>
16#include <mojo/public/cpp/bindings/receiver.h>
17#include <mojo/public/cpp/bindings/remote.h>
Prashant Malanif07c9522017-04-11 14:54:29 -070018
Prashant Malanif9334732017-04-26 13:06:20 -070019#include "midis/device.h"
20#include "midis/device_tracker.h"
Prashant Malani73c1ea12017-09-06 13:28:53 -070021#include "mojo/midis.mojom.h"
Prashant Malanif9334732017-04-26 13:06:20 -070022
Prashant Malanif07c9522017-04-11 14:54:29 -070023namespace midis {
24
Prashant Malani73c1ea12017-09-06 13:28:53 -070025class Client : public DeviceTracker::Observer, public arc::mojom::MidisServer {
Prashant Malanif07c9522017-04-11 14:54:29 -070026 public:
Prashant Malani3e40a032017-05-07 21:42:56 -070027 using ClientDeletionCallback = base::Callback<void(uint32_t)>;
Prashant Malani9f15b9e2017-09-19 18:15:39 -070028 Client(DeviceTracker* device_tracker,
Prashant Malani73c1ea12017-09-06 13:28:53 -070029 uint32_t client_id,
30 ClientDeletionCallback del_cb,
hscham84b55932021-02-24 17:43:00 +090031 mojo::PendingReceiver<arc::mojom::MidisServer> receiver,
32 mojo::PendingRemote<arc::mojom::MidisClient> client);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090033 Client(const Client&) = delete;
34 Client& operator=(const Client&) = delete;
35
Prashant Malani9f15b9e2017-09-19 18:15:39 -070036 ~Client() override;
Prashant Malani73c1ea12017-09-06 13:28:53 -070037
Prashant Malani9f15b9e2017-09-19 18:15:39 -070038 void NotifyDeviceAddedOrRemoved(const Device& dev, bool added);
Prashant Malanif07c9522017-04-11 14:54:29 -070039
Prashant Malani9f15b9e2017-09-19 18:15:39 -070040 private:
Prashant Malani324a4e12017-05-01 17:44:17 -070041 // This function is a DeviceTracker::Observer override.
Prashant Malani270828a2017-09-13 21:30:42 -070042 void OnDeviceAddedOrRemoved(const Device& dev, bool added) override;
Prashant Malani324a4e12017-05-01 17:44:17 -070043
Prashant Malani3e40a032017-05-07 21:42:56 -070044 void TriggerClientDeletion();
45
Prashant Malanie2ca6612017-09-25 16:20:32 -070046 // arc::mojom:MidisServer overrides
Qijiang Fan0c22a7a2020-04-21 00:20:45 +090047 void ListDevices(ListDevicesCallback callback) override;
48 void RequestPortDeprecated(arc::mojom::MidisRequestPtr request,
49 RequestPortDeprecatedCallback callback) override;
Prashant Malanie2ca6612017-09-25 16:20:32 -070050 void RequestPort(arc::mojom::MidisRequestPtr request,
Qijiang Fan0c22a7a2020-04-21 00:20:45 +090051 RequestPortCallback callback) override;
Prashant Malanie2ca6612017-09-25 16:20:32 -070052 void CloseDevice(arc::mojom::MidisRequestPtr request) override;
Prashant Malani270828a2017-09-13 21:30:42 -070053
Prashant Malaniacf5cf02018-03-18 00:49:20 -070054 // Function which returns a scoped handle when a port is requested,
55 // and an empty handle on error. This can be used by both
56 // RequestPort and RequestPortDeprecated.
57 mojo::ScopedHandle CreateRequestPortFD(uint32_t card,
58 uint32_t device,
59 uint32_t subdevice);
60
Prashant Malanif9334732017-04-26 13:06:20 -070061 // The DeviceTracker can be guaranteed to exist for the lifetime of the
62 // service. As such, it is safe to maintain this pointer as a means to make
63 // updates and derive information regarding devices.
64 DeviceTracker* device_tracker_;
Prashant Malani3e40a032017-05-07 21:42:56 -070065 uint32_t client_id_;
66 base::Callback<void(uint32_t)> del_cb_;
Prashant Malani73c1ea12017-09-06 13:28:53 -070067
68 // Handle to the Mojo client interface. This is used to send necessary
69 // information to the clients when required.
hscham84b55932021-02-24 17:43:00 +090070 mojo::Remote<arc::mojom::MidisClient> client_;
71 mojo::Receiver<arc::mojom::MidisServer> receiver_;
Prashant Malani73c1ea12017-09-06 13:28:53 -070072
Prashant Malanif07c9522017-04-11 14:54:29 -070073 base::WeakPtrFactory<Client> weak_factory_;
Prashant Malanif07c9522017-04-11 14:54:29 -070074};
75
76} // namespace midis
77
78#endif // MIDIS_CLIENT_H_