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 | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 18 | #include "mojo/midis.mojom.h" |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 19 | |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 20 | namespace midis { |
| 21 | |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 22 | class Client : public DeviceTracker::Observer, public arc::mojom::MidisServer { |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 23 | public: |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 24 | using ClientDeletionCallback = base::Callback<void(uint32_t)>; |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 25 | Client(DeviceTracker* device_tracker, |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 26 | uint32_t client_id, |
| 27 | ClientDeletionCallback del_cb, |
| 28 | arc::mojom::MidisServerRequest request, |
| 29 | arc::mojom::MidisClientPtr client_ptr); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 30 | Client(const Client&) = delete; |
| 31 | Client& operator=(const Client&) = delete; |
| 32 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 33 | ~Client() override; |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 34 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 35 | void NotifyDeviceAddedOrRemoved(const Device& dev, bool added); |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 36 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 37 | private: |
Prashant Malani | 324a4e1 | 2017-05-01 17:44:17 -0700 | [diff] [blame] | 38 | // This function is a DeviceTracker::Observer override. |
Prashant Malani | 270828a | 2017-09-13 21:30:42 -0700 | [diff] [blame] | 39 | void OnDeviceAddedOrRemoved(const Device& dev, bool added) override; |
Prashant Malani | 324a4e1 | 2017-05-01 17:44:17 -0700 | [diff] [blame] | 40 | |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 41 | void TriggerClientDeletion(); |
| 42 | |
Prashant Malani | e2ca661 | 2017-09-25 16:20:32 -0700 | [diff] [blame] | 43 | // arc::mojom:MidisServer overrides |
Qijiang Fan | 0c22a7a | 2020-04-21 00:20:45 +0900 | [diff] [blame] | 44 | void ListDevices(ListDevicesCallback callback) override; |
| 45 | void RequestPortDeprecated(arc::mojom::MidisRequestPtr request, |
| 46 | RequestPortDeprecatedCallback callback) override; |
Prashant Malani | e2ca661 | 2017-09-25 16:20:32 -0700 | [diff] [blame] | 47 | void RequestPort(arc::mojom::MidisRequestPtr request, |
Qijiang Fan | 0c22a7a | 2020-04-21 00:20:45 +0900 | [diff] [blame] | 48 | RequestPortCallback callback) override; |
Prashant Malani | e2ca661 | 2017-09-25 16:20:32 -0700 | [diff] [blame] | 49 | void CloseDevice(arc::mojom::MidisRequestPtr request) override; |
Prashant Malani | 270828a | 2017-09-13 21:30:42 -0700 | [diff] [blame] | 50 | |
Prashant Malani | acf5cf0 | 2018-03-18 00:49:20 -0700 | [diff] [blame] | 51 | // 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 Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 58 | // 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 Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 62 | uint32_t client_id_; |
| 63 | base::Callback<void(uint32_t)> del_cb_; |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 64 | |
| 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 Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 70 | base::WeakPtrFactory<Client> weak_factory_; |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // namespace midis |
| 74 | |
| 75 | #endif // MIDIS_CLIENT_H_ |