1 // Copyright (c) 2012 The Chromium 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.
5 #ifndef TOOLS_ANDROID_FORWARDER2_DAEMON_H_
6 #define TOOLS_ANDROID_FORWARDER2_DAEMON_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
13 namespace forwarder2 {
17 // Provides a way to spawn a daemon and communicate with it.
20 // Callback used by the daemon to shutdown properly. See pipe_notifier.h for
22 typedef int (*GetExitNotifierFDCallback)();
24 class ClientDelegate {
26 virtual ~ClientDelegate() {}
28 // Called after the daemon is ready to receive commands.
29 virtual void OnDaemonReady(Socket* daemon_socket) = 0;
32 class ServerDelegate {
34 virtual ~ServerDelegate() {}
36 // Called after the daemon bound its Unix Domain Socket. This can be used to
37 // setup signal handlers or perform global initialization.
38 virtual void Init() = 0;
40 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) = 0;
43 // |identifier| should be a unique string identifier. It is used to
44 // bind/connect the underlying Unix Domain Socket.
45 // Note that this class does not take ownership of |client_delegate| and
47 Daemon(const std::string& log_file_path,
48 const std::string& identifier,
49 ClientDelegate* client_delegate,
50 ServerDelegate* server_delegate,
51 GetExitNotifierFDCallback get_exit_fd_callback);
55 // Returns whether the daemon was successfully spawned. Note that this does
56 // not necessarily mean that the current process was forked in case the daemon
57 // is already running.
60 // Kills the daemon and blocks until it exited. Returns whether it succeeded.
64 const std::string log_file_path_;
65 const std::string identifier_;
66 ClientDelegate* const client_delegate_;
67 ServerDelegate* const server_delegate_;
68 const GetExitNotifierFDCallback get_exit_fd_callback_;
70 DISALLOW_COPY_AND_ASSIGN(Daemon);
73 } // namespace forwarder2
75 #endif // TOOLS_ANDROID_FORWARDER2_DAEMON_H_