blob: e9b5e9713cec1351f30d1f6f58d627d8c60bc507 [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
5#ifndef ARC_NETWORK_MESSAGE_DISPATCHER_H_
6#define ARC_NETWORK_MESSAGE_DISPATCHER_H_
7
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
16#include "arc/network/ipc.pb.h"
17
18namespace arc_networkd {
19
20// Helper message processor
Hidehiko Abede129222019-08-16 00:55:04 +090021class MessageDispatcher {
Garrick Evans96e03042019-05-28 14:30:52 +090022 public:
23 explicit MessageDispatcher(base::ScopedFD fd);
24
25 void RegisterFailureHandler(const base::Callback<void()>& handler);
26
27 void RegisterGuestMessageHandler(
28 const base::Callback<void(const GuestMessage&)>& handler);
29
30 void RegisterDeviceMessageHandler(
31 const base::Callback<void(const DeviceMessage&)>& handler);
32
33 private:
34 // Overrides MessageLoopForIO callbacks for new data on |control_fd_|.
Hidehiko Abede129222019-08-16 00:55:04 +090035 void OnFileCanReadWithoutBlocking();
Garrick Evans96e03042019-05-28 14:30:52 +090036
Garrick Evans96e03042019-05-28 14:30:52 +090037 base::ScopedFD fd_;
Hidehiko Abede129222019-08-16 00:55:04 +090038 std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher_;
Garrick Evans96e03042019-05-28 14:30:52 +090039 base::Callback<void()> failure_handler_;
40 base::Callback<void(const GuestMessage&)> guest_handler_;
41 base::Callback<void(const DeviceMessage&)> device_handler_;
Hidehiko Abede129222019-08-16 00:55:04 +090042
Garrick Evans96e03042019-05-28 14:30:52 +090043 IpHelperMessage msg_;
44
45 base::WeakPtrFactory<MessageDispatcher> weak_factory_{this};
46 DISALLOW_COPY_AND_ASSIGN(MessageDispatcher);
47};
48
49} // namespace arc_networkd
50
51#endif // ARC_NETWORK_MESSAGE_DISPATCHER_H_