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); |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 30 | ~Client() override; |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 31 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 32 | void NotifyDeviceAddedOrRemoved(const Device& dev, bool added); |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 33 | |
Prashant Malani | 9f15b9e | 2017-09-19 18:15:39 -0700 | [diff] [blame] | 34 | private: |
Prashant Malani | 324a4e1 | 2017-05-01 17:44:17 -0700 | [diff] [blame] | 35 | // This function is a DeviceTracker::Observer override. |
Prashant Malani | 270828a | 2017-09-13 21:30:42 -0700 | [diff] [blame] | 36 | void OnDeviceAddedOrRemoved(const Device& dev, bool added) override; |
Prashant Malani | 324a4e1 | 2017-05-01 17:44:17 -0700 | [diff] [blame] | 37 | |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 38 | void TriggerClientDeletion(); |
| 39 | |
Prashant Malani | e2ca661 | 2017-09-25 16:20:32 -0700 | [diff] [blame] | 40 | // arc::mojom:MidisServer overrides |
Prashant Malani | 270828a | 2017-09-13 21:30:42 -0700 | [diff] [blame] | 41 | void ListDevices(const ListDevicesCallback& callback) override; |
Prashant Malani | e2ca661 | 2017-09-25 16:20:32 -0700 | [diff] [blame] | 42 | void RequestPort(arc::mojom::MidisRequestPtr request, |
| 43 | const RequestPortCallback& callback) override; |
| 44 | void CloseDevice(arc::mojom::MidisRequestPtr request) override; |
Prashant Malani | 270828a | 2017-09-13 21:30:42 -0700 | [diff] [blame] | 45 | |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 46 | // The DeviceTracker can be guaranteed to exist for the lifetime of the |
| 47 | // service. As such, it is safe to maintain this pointer as a means to make |
| 48 | // updates and derive information regarding devices. |
| 49 | DeviceTracker* device_tracker_; |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 50 | uint32_t client_id_; |
| 51 | base::Callback<void(uint32_t)> del_cb_; |
Prashant Malani | 73c1ea1 | 2017-09-06 13:28:53 -0700 | [diff] [blame] | 52 | |
| 53 | // Handle to the Mojo client interface. This is used to send necessary |
| 54 | // information to the clients when required. |
| 55 | arc::mojom::MidisClientPtr client_ptr_; |
| 56 | mojo::Binding<arc::mojom::MidisServer> binding_; |
| 57 | |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 58 | base::WeakPtrFactory<Client> weak_factory_; |
| 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(Client); |
| 61 | }; |
| 62 | |
| 63 | } // namespace midis |
| 64 | |
| 65 | #endif // MIDIS_CLIENT_H_ |