blob: 84bbe4848f0c87db6c7c6bdf5e5cef0e3be62bd1 [file] [log] [blame]
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <vector>
#include <base/files/scoped_file.h>
#include <base/memory/weak_ptr.h>
#include <brillo/message_loops/message_loop.h>
#include <mojo/public/cpp/bindings/binding.h>
#include "midis/device.h"
#include "midis/device_tracker.h"
#include "mojo/midis.mojom.h"
namespace midis {
class Client : public DeviceTracker::Observer, public arc::mojom::MidisServer {
using ClientDeletionCallback = base::Callback<void(uint32_t)>;
Client(DeviceTracker* device_tracker,
uint32_t client_id,
ClientDeletionCallback del_cb,
arc::mojom::MidisServerRequest request,
arc::mojom::MidisClientPtr client_ptr);
Client(const Client&) = delete;
Client& operator=(const Client&) = delete;
~Client() override;
void NotifyDeviceAddedOrRemoved(const Device& dev, bool added);
// This function is a DeviceTracker::Observer override.
void OnDeviceAddedOrRemoved(const Device& dev, bool added) override;
void TriggerClientDeletion();
// arc::mojom:MidisServer overrides
void ListDevices(ListDevicesCallback callback) override;
void RequestPortDeprecated(arc::mojom::MidisRequestPtr request,
RequestPortDeprecatedCallback callback) override;
void RequestPort(arc::mojom::MidisRequestPtr request,
RequestPortCallback callback) override;
void CloseDevice(arc::mojom::MidisRequestPtr request) override;
// Function which returns a scoped handle when a port is requested,
// and an empty handle on error. This can be used by both
// RequestPort and RequestPortDeprecated.
mojo::ScopedHandle CreateRequestPortFD(uint32_t card,
uint32_t device,
uint32_t subdevice);
// The DeviceTracker can be guaranteed to exist for the lifetime of the
// service. As such, it is safe to maintain this pointer as a means to make
// updates and derive information regarding devices.
DeviceTracker* device_tracker_;
uint32_t client_id_;
base::Callback<void(uint32_t)> del_cb_;
// Handle to the Mojo client interface. This is used to send necessary
// information to the clients when required.
arc::mojom::MidisClientPtr client_ptr_;
mojo::Binding<arc::mojom::MidisServer> binding_;
base::WeakPtrFactory<Client> weak_factory_;
} // namespace midis
#endif // MIDIS_CLIENT_H_