blob: f1db148197eadbb2768eedf8b46b673aa03e0d1d [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 Malanie2ca6612017-09-25 16:20:32 -070042 void RequestPort(arc::mojom::MidisRequestPtr request,
43 const RequestPortCallback& callback) override;
44 void CloseDevice(arc::mojom::MidisRequestPtr request) override;
Prashant Malani270828a2017-09-13 21:30:42 -070045
Prashant Malanif9334732017-04-26 13:06:20 -070046 // 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 Malani3e40a032017-05-07 21:42:56 -070050 uint32_t client_id_;
51 base::Callback<void(uint32_t)> del_cb_;
Prashant Malani73c1ea12017-09-06 13:28:53 -070052
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 Malanif07c9522017-04-11 14:54:29 -070058 base::WeakPtrFactory<Client> weak_factory_;
59
60 DISALLOW_COPY_AND_ASSIGN(Client);
61};
62
63} // namespace midis
64
65#endif // MIDIS_CLIENT_H_