blob: 126906fa514eb079cbd0b4a76c2d2a5682d260bb [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);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090024 MessageDispatcher(const MessageDispatcher&) = delete;
25 MessageDispatcher& operator=(const MessageDispatcher&) = delete;
Taoyu Liaf944c92019-10-01 12:22:31 +090026
27 void Start();
Garrick Evans96e03042019-05-28 14:30:52 +090028
29 void RegisterFailureHandler(const base::Callback<void()>& handler);
30
Taoyu Lia0727dc2020-09-24 19:54:59 +090031 void RegisterNDProxyMessageHandler(
32 const base::Callback<void(const NDProxyMessage&)>& handler);
33
Garrick Evans96e03042019-05-28 14:30:52 +090034 void RegisterGuestMessageHandler(
35 const base::Callback<void(const GuestMessage&)>& handler);
36
37 void RegisterDeviceMessageHandler(
38 const base::Callback<void(const DeviceMessage&)>& handler);
39
Taoyu Liaf944c92019-10-01 12:22:31 +090040 void SendMessage(const google::protobuf::MessageLite& proto) const;
41
Garrick Evans96e03042019-05-28 14:30:52 +090042 private:
43 // Overrides MessageLoopForIO callbacks for new data on |control_fd_|.
Hidehiko Abede129222019-08-16 00:55:04 +090044 void OnFileCanReadWithoutBlocking();
Garrick Evans96e03042019-05-28 14:30:52 +090045
Garrick Evans96e03042019-05-28 14:30:52 +090046 base::ScopedFD fd_;
Hidehiko Abede129222019-08-16 00:55:04 +090047 std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher_;
Garrick Evans96e03042019-05-28 14:30:52 +090048 base::Callback<void()> failure_handler_;
Taoyu Lia0727dc2020-09-24 19:54:59 +090049 base::Callback<void(const NDProxyMessage&)> ndproxy_handler_;
Garrick Evans96e03042019-05-28 14:30:52 +090050 base::Callback<void(const GuestMessage&)> guest_handler_;
51 base::Callback<void(const DeviceMessage&)> device_handler_;
Hidehiko Abede129222019-08-16 00:55:04 +090052
Garrick Evans96e03042019-05-28 14:30:52 +090053 IpHelperMessage msg_;
54
55 base::WeakPtrFactory<MessageDispatcher> weak_factory_{this};
Garrick Evans96e03042019-05-28 14:30:52 +090056};
57
Garrick Evans3388a032020-03-24 11:25:55 +090058} // namespace patchpanel
Garrick Evans96e03042019-05-28 14:30:52 +090059
Garrick Evans3388a032020-03-24 11:25:55 +090060#endif // PATCHPANEL_MESSAGE_DISPATCHER_H_