summaryrefslogtreecommitdiffstats
path: root/tools/android/forwarder2/daemon.h
blob: 4b05ea423c088721289874e959199afa64d77a48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 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.

#ifndef TOOLS_ANDROID_FORWARDER2_DAEMON_H_
#define TOOLS_ANDROID_FORWARDER2_DAEMON_H_

#include <string>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"

namespace forwarder2 {

class Socket;

// Provides a way to spawn a daemon and communicate with it.
class Daemon {
 public:
  // Callback used by the daemon to shutdown properly. See pipe_notifier.h for
  // more details.
  typedef int (*GetExitNotifierFDCallback)();

  class ClientDelegate {
   public:
    virtual ~ClientDelegate() {}

    // Called after the daemon is ready to receive commands.
    virtual void OnDaemonReady(Socket* daemon_socket) = 0;
  };

  class ServerDelegate {
   public:
    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(scoped_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);

  ~Daemon();

  // 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();

 private:
  const std::string log_file_path_;
  const std::string identifier_;
  ClientDelegate* const client_delegate_;
  ServerDelegate* const server_delegate_;
  const GetExitNotifierFDCallback get_exit_fd_callback_;

  DISALLOW_COPY_AND_ASSIGN(Daemon);
};

}  // namespace forwarder2

#endif  // TOOLS_ANDROID_FORWARDER2_DAEMON_H_