blob: 1c91de15f61cf8dd701d0bc429011d393e383bad [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 Evansbdf1f982019-06-07 09:46:49 +090039 void Reset();
40
Garrick Evans3cbac7c2019-04-18 15:31:31 +090041 // Attempts to establish a connection to ADB at well-known destinations.
42 std::unique_ptr<Socket> Connect() const;
43
Garrick Evans96e03042019-05-28 14:30:52 +090044 MessageDispatcher msg_dispatcher_;
Garrick Evans3cbac7c2019-04-18 15:31:31 +090045 std::unique_ptr<Socket> src_;
46 std::deque<std::unique_ptr<SocketForwarder>> fwd_;
47 base::MessageLoopForIO::FileDescriptorWatcher src_watcher_;
48
Garrick Evansbdf1f982019-06-07 09:46:49 +090049 GuestMessage::GuestType arc_type_;
50
Garrick Evans3cbac7c2019-04-18 15:31:31 +090051 base::WeakPtrFactory<AdbProxy> weak_factory_{this};
52 DISALLOW_COPY_AND_ASSIGN(AdbProxy);
53};
54
55} // namespace arc_networkd
56
57#endif // ARC_NETWORK_ADB_PROXY_H_