michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_ipc_UnixFdWatcher_h michael@0: #define mozilla_ipc_UnixFdWatcher_h michael@0: michael@0: #include "base/message_loop.h" michael@0: #include "mozilla/FileUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: michael@0: class UnixFdWatcher : public MessageLoopForIO::Watcher michael@0: { michael@0: public: michael@0: enum { michael@0: READ_WATCHER = 1<<0, michael@0: WRITE_WATCHER = 1<<1 michael@0: }; michael@0: michael@0: virtual ~UnixFdWatcher(); michael@0: michael@0: MessageLoop* GetIOLoop() const michael@0: { michael@0: return mIOLoop; michael@0: } michael@0: michael@0: int GetFd() const michael@0: { michael@0: return mFd; michael@0: } michael@0: michael@0: bool IsOpen() const michael@0: { michael@0: return GetFd() >= 0; michael@0: } michael@0: michael@0: virtual void Close(); michael@0: michael@0: void AddWatchers(unsigned long aWatchers, bool aPersistent); michael@0: void RemoveWatchers(unsigned long aWatchers); michael@0: michael@0: // Callback method that's run before closing the file descriptor michael@0: virtual void OnClose() {}; michael@0: michael@0: // Callback method that's run on POSIX errors michael@0: virtual void OnError(const char* aFunction, int aErrno); michael@0: michael@0: protected: michael@0: UnixFdWatcher(MessageLoop* aIOLoop); michael@0: UnixFdWatcher(MessageLoop* aIOLoop, int aFd); michael@0: void SetFd(int aFd); michael@0: michael@0: private: michael@0: static bool FdIsNonBlocking(int aFd); michael@0: michael@0: MessageLoop* mIOLoop; michael@0: ScopedClose mFd; michael@0: MessageLoopForIO::FileDescriptorWatcher mReadWatcher; michael@0: MessageLoopForIO::FileDescriptorWatcher mWriteWatcher; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif