blob: 995528a3382f52d4136f809e2fa8c7d6128685de [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 Malanid0c0f1a2017-07-20 15:50:08 -070018#include "midis/libmidis/clientlib.h"
Prashant Malani73c1ea12017-09-06 13:28:53 -070019#include "mojo/midis.mojom.h"
Prashant Malanif9334732017-04-26 13:06:20 -070020
Prashant Malanif07c9522017-04-11 14:54:29 -070021namespace midis {
22
Prashant Malani73c1ea12017-09-06 13:28:53 -070023class Client : public DeviceTracker::Observer, public arc::mojom::MidisServer {
Prashant Malanif07c9522017-04-11 14:54:29 -070024 public:
Prashant Malani3e40a032017-05-07 21:42:56 -070025 using ClientDeletionCallback = base::Callback<void(uint32_t)>;
Prashant Malani9f15b9e2017-09-19 18:15:39 -070026 Client(DeviceTracker* device_tracker,
Prashant Malani73c1ea12017-09-06 13:28:53 -070027 uint32_t client_id,
28 ClientDeletionCallback del_cb,
29 arc::mojom::MidisServerRequest request,
30 arc::mojom::MidisClientPtr client_ptr);
Prashant Malani9f15b9e2017-09-19 18:15:39 -070031 ~Client() override;
Prashant Malani73c1ea12017-09-06 13:28:53 -070032
Prashant Malani9f15b9e2017-09-19 18:15:39 -070033 void NotifyDeviceAddedOrRemoved(const Device& dev, bool added);
Prashant Malanif07c9522017-04-11 14:54:29 -070034
Prashant Malani9f15b9e2017-09-19 18:15:39 -070035 private:
Prashant Malani324a4e12017-05-01 17:44:17 -070036 // This function is a DeviceTracker::Observer override.
Prashant Malani270828a2017-09-13 21:30:42 -070037 void OnDeviceAddedOrRemoved(const Device& dev, bool added) override;
Prashant Malani324a4e12017-05-01 17:44:17 -070038
Prashant Malani4acb19f2017-08-09 11:35:16 -070039 void HandleCloseDeviceMessage();
40
Prashant Malani9f15b9e2017-09-19 18:15:39 -070041 // TODO(pmalani): This function will eventually be an implementation of an
42 // interface routine in the Mojo interface MidisServer, and will return
43 // a FD using which the client can read from / write to the requested
44 // port.
45 // Leaving this in here, since this function was also present when the
46 // IPC mechanism for midis clients was Unix Domain Sockets, and we'd like
47 // to re-use that name (also as a reminder that this function needs
48 // to be implemented!).
Prashant Malani3e40a032017-05-07 21:42:56 -070049 void AddClientToPort();
50
51 void TriggerClientDeletion();
52
Prashant Malani270828a2017-09-13 21:30:42 -070053 // arc::mojom::MidisServer
54 void ListDevices(const ListDevicesCallback& callback) override;
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_