blob: 4fc30255de472dd8acaa1623248995e48635dac9 [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);
Prashant Malani9f15b9e2017-09-19 18:15:39 -070030 ~Client() override;
Prashant Malani73c1ea12017-09-06 13:28:53 -070031
Prashant Malani9f15b9e2017-09-19 18:15:39 -070032 void NotifyDeviceAddedOrRemoved(const Device& dev, bool added);
Prashant Malanif07c9522017-04-11 14:54:29 -070033
Prashant Malani9f15b9e2017-09-19 18:15:39 -070034 private:
Prashant Malani324a4e12017-05-01 17:44:17 -070035 // This function is a DeviceTracker::Observer override.
Prashant Malani270828a2017-09-13 21:30:42 -070036 void OnDeviceAddedOrRemoved(const Device& dev, bool added) override;
Prashant Malani324a4e12017-05-01 17:44:17 -070037
Prashant Malani3e40a032017-05-07 21:42:56 -070038 void TriggerClientDeletion();
39
Prashant Malanie2ca6612017-09-25 16:20:32 -070040 // arc::mojom:MidisServer overrides
Prashant Malani270828a2017-09-13 21:30:42 -070041 void ListDevices(const ListDevicesCallback& callback) override;
Prashant Malaniacf5cf02018-03-18 00:49:20 -070042 void RequestPortDeprecated(
43 arc::mojom::MidisRequestPtr request,
44 const RequestPortDeprecatedCallback& callback) override;
Prashant Malanie2ca6612017-09-25 16:20:32 -070045 void RequestPort(arc::mojom::MidisRequestPtr request,
46 const RequestPortCallback& callback) override;
47 void CloseDevice(arc::mojom::MidisRequestPtr request) override;
Prashant Malani270828a2017-09-13 21:30:42 -070048
Prashant Malaniacf5cf02018-03-18 00:49:20 -070049 // Function which returns a scoped handle when a port is requested,
50 // and an empty handle on error. This can be used by both
51 // RequestPort and RequestPortDeprecated.
52 mojo::ScopedHandle CreateRequestPortFD(uint32_t card,
53 uint32_t device,
54 uint32_t subdevice);
55
Prashant Malanif9334732017-04-26 13:06:20 -070056 // The DeviceTracker can be guaranteed to exist for the lifetime of the
57 // service. As such, it is safe to maintain this pointer as a means to make
58 // updates and derive information regarding devices.
59 DeviceTracker* device_tracker_;
Prashant Malani3e40a032017-05-07 21:42:56 -070060 uint32_t client_id_;
61 base::Callback<void(uint32_t)> del_cb_;
Prashant Malani73c1ea12017-09-06 13:28:53 -070062
63 // Handle to the Mojo client interface. This is used to send necessary
64 // information to the clients when required.
65 arc::mojom::MidisClientPtr client_ptr_;
66 mojo::Binding<arc::mojom::MidisServer> binding_;
67
Prashant Malanif07c9522017-04-11 14:54:29 -070068 base::WeakPtrFactory<Client> weak_factory_;
69
70 DISALLOW_COPY_AND_ASSIGN(Client);
71};
72
73} // namespace midis
74
75#endif // MIDIS_CLIENT_H_