Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame^] | 1 | // 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 | |
| 11 | #include <base/files/scoped_file.h> |
| 12 | #include <base/macros.h> |
| 13 | #include <base/memory/weak_ptr.h> |
| 14 | #include <base/message_loop/message_loop.h> |
| 15 | |
| 16 | #include "arc/network/ipc.pb.h" |
| 17 | |
| 18 | namespace arc_networkd { |
| 19 | |
| 20 | // Helper message processor |
| 21 | class MessageDispatcher : public base::MessageLoopForIO::Watcher { |
| 22 | 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_|. |
| 35 | void OnFileCanReadWithoutBlocking(int fd) override; |
| 36 | void OnFileCanWriteWithoutBlocking(int fd) override {} |
| 37 | |
| 38 | private: |
| 39 | base::ScopedFD fd_; |
| 40 | base::MessageLoopForIO::FileDescriptorWatcher watcher_; |
| 41 | base::Callback<void()> failure_handler_; |
| 42 | base::Callback<void(const GuestMessage&)> guest_handler_; |
| 43 | base::Callback<void(const DeviceMessage&)> device_handler_; |
| 44 | IpHelperMessage msg_; |
| 45 | |
| 46 | base::WeakPtrFactory<MessageDispatcher> weak_factory_{this}; |
| 47 | DISALLOW_COPY_AND_ASSIGN(MessageDispatcher); |
| 48 | }; |
| 49 | |
| 50 | } // namespace arc_networkd |
| 51 | |
| 52 | #endif // ARC_NETWORK_MESSAGE_DISPATCHER_H_ |