blob: 956168424a4bf221bdc232999c18f0c208e49d55 [file] [log] [blame]
// Copyright 2018 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.
#ifndef VM_TOOLS_CONCIERGE_STARTUP_LISTENER_IMPL_H_
#define VM_TOOLS_CONCIERGE_STARTUP_LISTENER_IMPL_H_
#include <stdint.h>
#include <map>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
#include <base/sequenced_task_runner.h>
#include <base/synchronization/lock.h>
#include <base/synchronization/waitable_event.h>
#include <grpc++/grpc++.h>
#include "vm_host.grpc.pb.h" // NOLINT(build/include)
namespace vm_tools {
namespace concierge {
class Service;
// Listens for VMs to announce that they are ready before signaling the
// WaitableEvent associated with that VM.
class StartupListenerImpl final : public vm_tools::StartupListener::Service {
public:
explicit StartupListenerImpl(
base::WeakPtr<vm_tools::concierge::Service> service);
~StartupListenerImpl() override = default;
// StartupListener overrides.
grpc::Status VmReady(grpc::ServerContext* ctx,
const vm_tools::EmptyMessage* request,
vm_tools::EmptyMessage* response) override;
grpc::Status ContainerStartupFailed(
grpc::ServerContext* ctx,
const vm_tools::ContainerName* request,
vm_tools::EmptyMessage* response) override;
// Add the VM with the vsock context id |cid| to the set of VMs that have
// been started but have not checked in as ready yet.
void AddPendingVm(uint32_t cid, base::WaitableEvent* event);
// Remove the WaitableEvent associated with |cid|.
void RemovePendingVm(uint32_t cid);
private:
base::WeakPtr<vm_tools::concierge::Service> service_; // not owned
scoped_refptr<base::SequencedTaskRunner> task_runner_;
// VMs that have been started but have not checked in as being ready yet.
std::map<uint32_t, base::WaitableEvent*> pending_vms_;
// Lock to protect |pending_vms_|.
base::Lock vm_lock_;
DISALLOW_COPY_AND_ASSIGN(StartupListenerImpl);
};
} // namespace concierge
} // namespace vm_tools
#endif // VM_TOOLS_CONCIERGE_STARTUP_LISTENER_IMPL_H_