1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "UnixFdWatcher.h"
14 #if defined(MOZ_WIDGET_GONK)
15 #include <android/log.h>
16 #define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "I/O", args);
20 #define CHROMIUM_LOG(args...) if (IODEBUG) printf(args);
26 UnixFdWatcher::~UnixFdWatcher()
28 NS_WARN_IF(IsOpen()); /* mFd should have been closed already */
32 UnixFdWatcher::Close()
34 MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop
);
36 if (NS_WARN_IF(!IsOpen())) {
37 /* mFd should have been open */
41 RemoveWatchers(READ_WATCHER
|WRITE_WATCHER
);
46 UnixFdWatcher::AddWatchers(unsigned long aWatchers
, bool aPersistent
)
48 MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop
);
51 // Before we add a watcher, we need to remove it! Removing is always
52 // safe, but adding the same watcher twice can lead to endless loops
54 RemoveWatchers(aWatchers
);
56 if (aWatchers
& READ_WATCHER
) {
57 MessageLoopForIO::current()->WatchFileDescriptor(
60 MessageLoopForIO::WATCH_READ
,
64 if (aWatchers
& WRITE_WATCHER
) {
65 MessageLoopForIO::current()->WatchFileDescriptor(
68 MessageLoopForIO::WATCH_WRITE
,
75 UnixFdWatcher::RemoveWatchers(unsigned long aWatchers
)
77 MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop
);
80 if (aWatchers
& READ_WATCHER
) {
81 mReadWatcher
.StopWatchingFileDescriptor();
83 if (aWatchers
& WRITE_WATCHER
) {
84 mWriteWatcher
.StopWatchingFileDescriptor();
89 UnixFdWatcher::OnError(const char* aFunction
, int aErrno
)
91 MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop
);
93 CHROMIUM_LOG("%s failed with error %d (%s)",
94 aFunction
, aErrno
, strerror(aErrno
));
97 UnixFdWatcher::UnixFdWatcher(MessageLoop
* aIOLoop
)
103 UnixFdWatcher::UnixFdWatcher(MessageLoop
* aIOLoop
, int aFd
)
111 UnixFdWatcher::SetFd(int aFd
)
113 MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop
);
114 MOZ_ASSERT(!IsOpen());
115 MOZ_ASSERT(FdIsNonBlocking(aFd
));
121 UnixFdWatcher::FdIsNonBlocking(int aFd
)
123 int flags
= TEMP_FAILURE_RETRY(fcntl(aFd
, F_GETFL
));
124 return (flags
> 0) && (flags
& O_NONBLOCK
);