blob: c3e29f7da9d9e7ef3d194466d94f68766b0bcca7 [file] [log] [blame]
Garrick Evans96e03042019-05-28 14:30:52 +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
Garrick Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_MESSAGE_DISPATCHER_H_
6#define PATCHPANEL_MESSAGE_DISPATCHER_H_
Garrick Evans96e03042019-05-28 14:30:52 +09007
8#include <memory>
9#include <string>
10
Hidehiko Abede129222019-08-16 00:55:04 +090011#include <base/files/file_descriptor_watcher_posix.h>
Garrick Evans96e03042019-05-28 14:30:52 +090012#include <base/files/scoped_file.h>
13#include <base/macros.h>
14#include <base/memory/weak_ptr.h>
Garrick Evans96e03042019-05-28 14:30:52 +090015
Garrick Evans3388a032020-03-24 11:25:55 +090016#include "patchpanel/ipc.pb.h"
Garrick Evans96e03042019-05-28 14:30:52 +090017
Garrick Evans3388a032020-03-24 11:25:55 +090018namespace patchpanel {
Garrick Evans96e03042019-05-28 14:30:52 +090019
20// Helper message processor
Hidehiko Abede129222019-08-16 00:55:04 +090021class MessageDispatcher {
Garrick Evans96e03042019-05-28 14:30:52 +090022 public:
Taoyu Liaf944c92019-10-01 12:22:31 +090023 explicit MessageDispatcher(base::ScopedFD fd, bool start = true);
24
25 void Start();
Garrick Evans96e03042019-05-28 14:30:52 +090026
27 void RegisterFailureHandler(const base::Callback<void()>& handler);
28
Taoyu Lia0727dc2020-09-24 19:54:59 +090029 void RegisterNDProxyMessageHandler(
30 const base::Callback<void(const NDProxyMessage&)>& handler);
31
Garrick Evans96e03042019-05-28 14:30:52 +090032 void RegisterGuestMessageHandler(
33 const base::Callback<void(const GuestMessage&)>& handler);
34
35 void RegisterDeviceMessageHandler(
36 const base::Callback<void(const DeviceMessage&)>& handler);
37
Taoyu Liaf944c92019-10-01 12:22:31 +090038 void SendMessage(const google::protobuf::MessageLite& proto) const;
39
Garrick Evans96e03042019-05-28 14:30:52 +090040 private:
41 // Overrides MessageLoopForIO callbacks for new data on |control_fd_|.
Hidehiko Abede129222019-08-16 00:55:04 +090042 void OnFileCanReadWithoutBlocking();
Garrick Evans96e03042019-05-28 14:30:52 +090043
Garrick Evans96e03042019-05-28 14:30:52 +090044 base::ScopedFD fd_;
Hidehiko Abede129222019-08-16 00:55:04 +090045 std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher_;
Garrick Evans96e03042019-05-28 14:30:52 +090046 base::Callback<void()> failure_handler_;
Taoyu Lia0727dc2020-09-24 19:54:59 +090047 base::Callback<void(const NDProxyMessage&)> ndproxy_handler_;
Garrick Evans96e03042019-05-28 14:30:52 +090048 base::Callback<void(const GuestMessage&)> guest_handler_;
49 base::Callback<void(const DeviceMessage&)> device_handler_;
Hidehiko Abede129222019-08-16 00:55:04 +090050
Garrick Evans96e03042019-05-28 14:30:52 +090051 IpHelperMessage msg_;
52
53 base::WeakPtrFactory<MessageDispatcher> weak_factory_{this};
54 DISALLOW_COPY_AND_ASSIGN(MessageDispatcher);
55};
56
Garrick Evans3388a032020-03-24 11:25:55 +090057} // namespace patchpanel
Garrick Evans96e03042019-05-28 14:30:52 +090058
Garrick Evans3388a032020-03-24 11:25:55 +090059#endif // PATCHPANEL_MESSAGE_DISPATCHER_H_