blob: a626ef2edfb642af3621d8b4469b53a1d1d25c01 [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 Malani3e40a032017-05-07 21:42:56 -070039 void TriggerClientDeletion();
40
Prashant Malanie2ca6612017-09-25 16:20:32 -070041 // arc::mojom:MidisServer overrides
Prashant Malani270828a2017-09-13 21:30:42 -070042 void ListDevices(const ListDevicesCallback& callback) override;
Prashant Malanie2ca6612017-09-25 16:20:32 -070043 void RequestPort(arc::mojom::MidisRequestPtr request,
44 const RequestPortCallback& callback) override;
45 void CloseDevice(arc::mojom::MidisRequestPtr request) override;
Prashant Malani270828a2017-09-13 21:30:42 -070046
Prashant Malanif9334732017-04-26 13:06:20 -070047 // The DeviceTracker can be guaranteed to exist for the lifetime of the
48 // service. As such, it is safe to maintain this pointer as a means to make
49 // updates and derive information regarding devices.
50 DeviceTracker* device_tracker_;
Prashant Malani3e40a032017-05-07 21:42:56 -070051 uint32_t client_id_;
52 base::Callback<void(uint32_t)> del_cb_;
Prashant Malani73c1ea12017-09-06 13:28:53 -070053
54 // Handle to the Mojo client interface. This is used to send necessary
55 // information to the clients when required.
56 arc::mojom::MidisClientPtr client_ptr_;
57 mojo::Binding<arc::mojom::MidisServer> binding_;
58
Prashant Malanif07c9522017-04-11 14:54:29 -070059 base::WeakPtrFactory<Client> weak_factory_;
60
61 DISALLOW_COPY_AND_ASSIGN(Client);
62};
63
64} // namespace midis
65
66#endif // MIDIS_CLIENT_H_