blob: 1a2b2402a8dcc20853ea08b2e066b6e42c297e21 [file] [log] [blame]
Garrick Evans3cbac7c2019-04-18 15:31:31 +09001// Copyright 2019 The Chromium OS 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.
4
5#ifndef ARC_NETWORK_ADB_PROXY_H_
6#define ARC_NETWORK_ADB_PROXY_H_
7
8#include <deque>
9#include <memory>
10
Garrick Evans96e03042019-05-28 14:30:52 +090011#include <base/files/scoped_file.h>
Garrick Evans3cbac7c2019-04-18 15:31:31 +090012#include <base/memory/weak_ptr.h>
13#include <brillo/daemons/daemon.h>
14#include <base/message_loop/message_loop.h>
15
Garrick Evans96e03042019-05-28 14:30:52 +090016#include "arc/network/message_dispatcher.h"
Garrick Evans3cbac7c2019-04-18 15:31:31 +090017#include "arc/network/socket.h"
18#include "arc/network/socket_forwarder.h"
19
20namespace arc_networkd {
21
22// Subprocess for proxying ADB traffic.
23class AdbProxy : public brillo::Daemon, public base::MessageLoopForIO::Watcher {
24 public:
Garrick Evans96e03042019-05-28 14:30:52 +090025 explicit AdbProxy(base::ScopedFD control_fd);
Garrick Evans3cbac7c2019-04-18 15:31:31 +090026 virtual ~AdbProxy();
27
28 protected:
Garrick Evans3cbac7c2019-04-18 15:31:31 +090029 int OnInit() override;
30
31 // Watcher callback for accepting new connections.
32 void OnFileCanReadWithoutBlocking(int fd) override;
33 void OnFileCanWriteWithoutBlocking(int fd) override {}
34
Garrick Evans96e03042019-05-28 14:30:52 +090035 void OnParentProcessExit();
36 void OnGuestMessage(const GuestMessage& msg);
Garrick Evans3cbac7c2019-04-18 15:31:31 +090037
Garrick Evans96e03042019-05-28 14:30:52 +090038 private:
Garrick Evans3cbac7c2019-04-18 15:31:31 +090039 // Attempts to establish a connection to ADB at well-known destinations.
40 std::unique_ptr<Socket> Connect() const;
41
Garrick Evans96e03042019-05-28 14:30:52 +090042 MessageDispatcher msg_dispatcher_;
Garrick Evans3cbac7c2019-04-18 15:31:31 +090043 std::unique_ptr<Socket> src_;
44 std::deque<std::unique_ptr<SocketForwarder>> fwd_;
45 base::MessageLoopForIO::FileDescriptorWatcher src_watcher_;
46
47 base::WeakPtrFactory<AdbProxy> weak_factory_{this};
48 DISALLOW_COPY_AND_ASSIGN(AdbProxy);
49};
50
51} // namespace arc_networkd
52
53#endif // ARC_NETWORK_ADB_PROXY_H_