#ifndef BASE_MESSAGE_LOOP_IO_WATCHER_H_
#define BASE_MESSAGE_LOOP_IO_WATCHER_H_
#include <memory>
#include "base/base_export.h"
#include "base/location.h"
#include "base/message_loop/message_pump_for_io.h"
#include "base/types/pass_key.h"
#include "build/build_config.h"
#if defined(IS_WINDOWS)
#include "base/win/windows_types.h"
#endif
#if BUILDFLAG(IS_MAC) || \
(BUILDFLAG(IS_IOS) && !BUILDFLAG(CRONET_BUILD) && !BUILDFLAG(IS_IOS_TVOS))
#include <mach/mach.h>
#endif
namespace base {
class BASE_EXPORT IOWatcher {
public:
virtual ~IOWatcher() = default;
static IOWatcher* Get();
#if BUILDFLAG(IS_WIN)
[[nodiscard]] bool RegisterIOHandler(HANDLE file,
MessagePumpForIO::IOHandler* handler);
bool RegisterJobObject(HANDLE job, MessagePumpForIO::IOHandler* handler);
#elif BUILDFLAG(IS_POSIX)
class FdWatcher {
public:
virtual void OnFdReadable(int fd) = 0;
virtual void OnFdWritable(int fd) = 0;
protected:
virtual ~FdWatcher() = default;
};
class FdWatch {
public:
virtual ~FdWatch() = default;
};
enum class FdWatchDuration {
kOneShot,
kPersistent,
};
enum class FdWatchMode {
kRead,
kWrite,
kReadWrite,
};
std::unique_ptr<FdWatch> WatchFileDescriptor(
int fd,
FdWatchDuration duration,
FdWatchMode mode,
FdWatcher& fd_watcher,
const Location& location = Location::Current());
#endif
#if BUILDFLAG(IS_MAC) || \
(BUILDFLAG(IS_IOS) && !BUILDFLAG(CRONET_BUILD) && !BUILDFLAG(IS_IOS_TVOS))
bool WatchMachReceivePort(
mach_port_t port,
MessagePumpForIO::MachPortWatchController* controller,
MessagePumpForIO::MachPortWatcher* delegate);
#elif BUILDFLAG(IS_FUCHSIA)
bool WatchZxHandle(zx_handle_t handle,
bool persistent,
zx_signals_t signals,
MessagePumpForIO::ZxHandleWatchController* controller,
MessagePumpForIO::ZxHandleWatcher* delegate);
#endif
protected:
IOWatcher();
#if BUILDFLAG(IS_WIN)
virtual bool RegisterIOHandlerImpl(HANDLE file,
MessagePumpForIO::IOHandler* handler) = 0;
virtual bool RegisterJobObjectImpl(HANDLE job,
MessagePumpForIO::IOHandler* handler) = 0;
#elif BUILDFLAG(IS_POSIX)
virtual std::unique_ptr<FdWatch> WatchFileDescriptorImpl(
int fd,
FdWatchDuration duration,
FdWatchMode mode,
FdWatcher& fd_watcher,
const Location& location) = 0;
#endif
#if BUILDFLAG(IS_MAC) || \
(BUILDFLAG(IS_IOS) && !BUILDFLAG(CRONET_BUILD) && !BUILDFLAG(IS_IOS_TVOS))
virtual bool WatchMachReceivePortImpl(
mach_port_t port,
MessagePumpForIO::MachPortWatchController* controller,
MessagePumpForIO::MachPortWatcher* delegate) = 0;
#elif BUILDFLAG(IS_FUCHSIA)
virtual bool WatchZxHandleImpl(
zx_handle_t handle,
bool persistent,
zx_signals_t signals,
MessagePumpForIO::ZxHandleWatchController* controller,
MessagePumpForIO::ZxHandleWatcher* delegate) = 0;
#endif
};
}
#endif