blob: 84bbe4848f0c87db6c7c6bdf5e5cef0e3be62bd1 [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>
Prashant Malani73c1ea12017-09-06 13:28:53 -070014#include <mojo/public/cpp/bindings/binding.h>
Prashant Malanif07c9522017-04-11 14:54:29 -070015
Prashant Malanif9334732017-04-26 13:06:20 -070016#include "midis/device.h"
17#include "midis/device_tracker.h"
Prashant Malani73c1ea12017-09-06 13:28:53 -070018#include "mojo/midis.mojom.h"
Prashant Malanif9334732017-04-26 13:06:20 -070019
Prashant Malanif07c9522017-04-11 14:54:29 -070020namespace midis {
21
Prashant Malani73c1ea12017-09-06 13:28:53 -070022class Client : public DeviceTracker::Observer, public arc::mojom::MidisServer {
Prashant Malanif07c9522017-04-11 14:54:29 -070023 public:
Prashant Malani3e40a032017-05-07 21:42:56 -070024 using ClientDeletionCallback = base::Callback<void(uint32_t)>;
Prashant Malani9f15b9e2017-09-19 18:15:39 -070025 Client(DeviceTracker* device_tracker,
Prashant Malani73c1ea12017-09-06 13:28:53 -070026 uint32_t client_id,
27 ClientDeletionCallback del_cb,
28 arc::mojom::MidisServerRequest request,
29 arc::mojom::MidisClientPtr client_ptr);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090030 Client(const Client&) = delete;
31 Client& operator=(const Client&) = delete;
32
Prashant Malani9f15b9e2017-09-19 18:15:39 -070033 ~Client() override;
Prashant Malani73c1ea12017-09-06 13:28:53 -070034
Prashant Malani9f15b9e2017-09-19 18:15:39 -070035 void NotifyDeviceAddedOrRemoved(const Device& dev, bool added);
Prashant Malanif07c9522017-04-11 14:54:29 -070036
Prashant Malani9f15b9e2017-09-19 18:15:39 -070037 private:
Prashant Malani324a4e12017-05-01 17:44:17 -070038 // This function is a DeviceTracker::Observer override.
Prashant Malani270828a2017-09-13 21:30:42 -070039 void OnDeviceAddedOrRemoved(const Device& dev, bool added) override;
Prashant Malani324a4e12017-05-01 17:44:17 -070040
Prashant Malani3e40a032017-05-07 21:42:56 -070041 void TriggerClientDeletion();
42
Prashant Malanie2ca6612017-09-25 16:20:32 -070043 // arc::mojom:MidisServer overrides
Qijiang Fan0c22a7a2020-04-21 00:20:45 +090044 void ListDevices(ListDevicesCallback callback) override;
45 void RequestPortDeprecated(arc::mojom::MidisRequestPtr request,
46 RequestPortDeprecatedCallback callback) override;
Prashant Malanie2ca6612017-09-25 16:20:32 -070047 void RequestPort(arc::mojom::MidisRequestPtr request,
Qijiang Fan0c22a7a2020-04-21 00:20:45 +090048 RequestPortCallback callback) override;
Prashant Malanie2ca6612017-09-25 16:20:32 -070049 void CloseDevice(arc::mojom::MidisRequestPtr request) override;
Prashant Malani270828a2017-09-13 21:30:42 -070050
Prashant Malaniacf5cf02018-03-18 00:49:20 -070051 // Function which returns a scoped handle when a port is requested,
52 // and an empty handle on error. This can be used by both
53 // RequestPort and RequestPortDeprecated.
54 mojo::ScopedHandle CreateRequestPortFD(uint32_t card,
55 uint32_t device,
56 uint32_t subdevice);
57
Prashant Malanif9334732017-04-26 13:06:20 -070058 // The DeviceTracker can be guaranteed to exist for the lifetime of the
59 // service. As such, it is safe to maintain this pointer as a means to make
60 // updates and derive information regarding devices.
61 DeviceTracker* device_tracker_;
Prashant Malani3e40a032017-05-07 21:42:56 -070062 uint32_t client_id_;
63 base::Callback<void(uint32_t)> del_cb_;
Prashant Malani73c1ea12017-09-06 13:28:53 -070064
65 // Handle to the Mojo client interface. This is used to send necessary
66 // information to the clients when required.
67 arc::mojom::MidisClientPtr client_ptr_;
68 mojo::Binding<arc::mojom::MidisServer> binding_;
69
Prashant Malanif07c9522017-04-11 14:54:29 -070070 base::WeakPtrFactory<Client> weak_factory_;
Prashant Malanif07c9522017-04-11 14:54:29 -070071};
72
73} // namespace midis
74
75#endif // MIDIS_CLIENT_H_