1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/message_loop/io_watcher.h"
6
7 #include <memory>
8
9 #include "base/check.h"
10 #include "base/no_destructor.h"
11 #include "base/task/current_thread.h"
12 #include "build/build_config.h"
13
14 namespace base {
15
16 IOWatcher::IOWatcher() = default;
17
Get()18 IOWatcher* IOWatcher::Get() {
19 if (!CurrentThread::IsSet()) {
20 return nullptr;
21 }
22 return CurrentThread::Get()->GetIOWatcher();
23 }
24
25 #if !BUILDFLAG(IS_NACL)
26 #if BUILDFLAG(IS_WIN)
RegisterIOHandler(HANDLE file,MessagePumpForIO::IOHandler * handler)27 bool IOWatcher::RegisterIOHandler(HANDLE file,
28 MessagePumpForIO::IOHandler* handler) {
29 return RegisterIOHandlerImpl(file, handler);
30 }
31
RegisterJobObject(HANDLE job,MessagePumpForIO::IOHandler * handler)32 bool IOWatcher::RegisterJobObject(HANDLE job,
33 MessagePumpForIO::IOHandler* handler) {
34 return RegisterJobObjectImpl(job, handler);
35 }
36 #elif BUILDFLAG(IS_POSIX)
WatchFileDescriptor(int fd,FdWatchDuration duration,FdWatchMode mode,FdWatcher & fd_watcher,const Location & location)37 std::unique_ptr<IOWatcher::FdWatch> IOWatcher::WatchFileDescriptor(
38 int fd,
39 FdWatchDuration duration,
40 FdWatchMode mode,
41 FdWatcher& fd_watcher,
42 const Location& location) {
43 return WatchFileDescriptorImpl(fd, duration, mode, fd_watcher, location);
44 }
45 #endif
46
47 #if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && !BUILDFLAG(CRONET_BUILD))
WatchMachReceivePort(mach_port_t port,MessagePumpForIO::MachPortWatchController * controller,MessagePumpForIO::MachPortWatcher * delegate)48 bool IOWatcher::WatchMachReceivePort(
49 mach_port_t port,
50 MessagePumpForIO::MachPortWatchController* controller,
51 MessagePumpForIO::MachPortWatcher* delegate) {
52 return WatchMachReceivePortImpl(port, controller, delegate);
53 }
54 #elif BUILDFLAG(IS_FUCHSIA)
WatchZxHandle(zx_handle_t handle,bool persistent,zx_signals_t signals,MessagePumpForIO::ZxHandleWatchController * controller,MessagePumpForIO::ZxHandleWatcher * delegate)55 bool IOWatcher::WatchZxHandle(
56 zx_handle_t handle,
57 bool persistent,
58 zx_signals_t signals,
59 MessagePumpForIO::ZxHandleWatchController* controller,
60 MessagePumpForIO::ZxHandleWatcher* delegate) {
61 return WatchZxHandleImpl(handle, persistent, signals, controller, delegate);
62 }
63 #endif
64 #endif // !BUILDFLAG(IS_NACL)
65
66 } // namespace base
67