blob: 54285df43dafc9efcb8d8c519bae12bcd755a715 [file] [log] [blame]
// Copyright (c) 2012 The Chromium 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 <string>
#include "base/macros.h"
namespace forwarder2 {
class Socket;
// Provides a way to spawn a daemon and communicate with it.
class Daemon {
// Callback used by the daemon to shutdown properly. See pipe_notifier.h for
// more details.
typedef int (*GetExitNotifierFDCallback)();
class ClientDelegate {
virtual ~ClientDelegate() {}
// Called after the daemon is ready to receive commands.
virtual void OnDaemonReady(Socket* daemon_socket) = 0;
class ServerDelegate {
virtual ~ServerDelegate() {}
// Called after the daemon bound its Unix Domain Socket. This can be used to
// setup signal handlers or perform global initialization.
virtual void Init() = 0;
virtual void OnClientConnected(std::unique_ptr<Socket> client_socket) = 0;
// |identifier| should be a unique string identifier. It is used to
// bind/connect the underlying Unix Domain Socket.
// Note that this class does not take ownership of |client_delegate| and
// |server_delegate|.
Daemon(const std::string& log_file_path,
const std::string& identifier,
ClientDelegate* client_delegate,
ServerDelegate* server_delegate,
GetExitNotifierFDCallback get_exit_fd_callback);
// Returns whether the daemon was successfully spawned. Note that this does
// not necessarily mean that the current process was forked in case the daemon
// is already running.
bool SpawnIfNeeded();
// Kills the daemon and blocks until it exited. Returns whether it succeeded.
bool Kill();
const std::string log_file_path_;
const std::string identifier_;
ClientDelegate* const client_delegate_;
ServerDelegate* const server_delegate_;
const GetExitNotifierFDCallback get_exit_fd_callback_;
} // namespace forwarder2