Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 1 | // 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 Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 9 | #include <vector> |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 10 | |
| 11 | #include <base/files/scoped_file.h> |
| 12 | #include <base/memory/weak_ptr.h> |
| 13 | #include <brillo/message_loops/message_loop.h> |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 14 | #include <mojo/public/cpp/bindings/binding.h> |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 15 | |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 16 | #include "midis/device.h" |
| 17 | #include "midis/device_tracker.h" |
Prashant Malani | d0c0f1a | 2017-07-20 15:50:08 -0700 | [diff] [blame] | 18 | #include "midis/libmidis/clientlib.h" |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 19 | #include "mojo/midis.mojom.h" |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 20 | |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 21 | namespace midis { |
| 22 | |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 23 | class Client : public DeviceTracker::Observer, public arc::mojom::MidisServer { |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 24 | public: |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 25 | using ClientDeletionCallback = base::Callback<void(uint32_t)>; |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 26 | Client(DeviceTracker* device_tracker, |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 27 | uint32_t client_id, |
| 28 | ClientDeletionCallback del_cb, |
| 29 | arc::mojom::MidisServerRequest request, |
| 30 | arc::mojom::MidisClientPtr client_ptr); |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 31 | ~Client() override; |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 32 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 33 | void NotifyDeviceAddedOrRemoved(const Device& dev, bool added); |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 34 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 35 | private: |
Prashant Malani | 324a4e1 | 2017-05-01 17:44:17 -0700 | [diff] [blame] | 36 | // This function is a DeviceTracker::Observer override. |
Prashant Malani | 270828a | 2017-09-13 21:30:42 -0700 | [diff] [blame] | 37 | void OnDeviceAddedOrRemoved(const Device& dev, bool added) override; |
Prashant Malani | 324a4e1 | 2017-05-01 17:44:17 -0700 | [diff] [blame] | 38 | |
Prashant Malani | 4acb19f | 2017-08-09 11:35:16 -0700 | [diff] [blame] | 39 | void HandleCloseDeviceMessage(); |
| 40 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 41 | // 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 Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 49 | void AddClientToPort(); |
| 50 | |
| 51 | void TriggerClientDeletion(); |
| 52 | |
Prashant Malani | 270828a | 2017-09-13 21:30:42 -0700 | [diff] [blame] | 53 | // arc::mojom::MidisServer |
| 54 | void ListDevices(const ListDevicesCallback& callback) override; |
| 55 | |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 56 | // 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 Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 60 | uint32_t client_id_; |
| 61 | base::Callback<void(uint32_t)> del_cb_; |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 62 | |
| 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 Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 68 | base::WeakPtrFactory<Client> weak_factory_; |
| 69 | |
| 70 | DISALLOW_COPY_AND_ASSIGN(Client); |
| 71 | }; |
| 72 | |
| 73 | } // namespace midis |
| 74 | |
| 75 | #endif // MIDIS_CLIENT_H_ |