|
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/. */ |
|
6 |
|
7 #ifndef mozilla_ipc_UnixFdWatcher_h |
|
8 #define mozilla_ipc_UnixFdWatcher_h |
|
9 |
|
10 #include "base/message_loop.h" |
|
11 #include "mozilla/FileUtils.h" |
|
12 |
|
13 namespace mozilla { |
|
14 namespace ipc { |
|
15 |
|
16 class UnixFdWatcher : public MessageLoopForIO::Watcher |
|
17 { |
|
18 public: |
|
19 enum { |
|
20 READ_WATCHER = 1<<0, |
|
21 WRITE_WATCHER = 1<<1 |
|
22 }; |
|
23 |
|
24 virtual ~UnixFdWatcher(); |
|
25 |
|
26 MessageLoop* GetIOLoop() const |
|
27 { |
|
28 return mIOLoop; |
|
29 } |
|
30 |
|
31 int GetFd() const |
|
32 { |
|
33 return mFd; |
|
34 } |
|
35 |
|
36 bool IsOpen() const |
|
37 { |
|
38 return GetFd() >= 0; |
|
39 } |
|
40 |
|
41 virtual void Close(); |
|
42 |
|
43 void AddWatchers(unsigned long aWatchers, bool aPersistent); |
|
44 void RemoveWatchers(unsigned long aWatchers); |
|
45 |
|
46 // Callback method that's run before closing the file descriptor |
|
47 virtual void OnClose() {}; |
|
48 |
|
49 // Callback method that's run on POSIX errors |
|
50 virtual void OnError(const char* aFunction, int aErrno); |
|
51 |
|
52 protected: |
|
53 UnixFdWatcher(MessageLoop* aIOLoop); |
|
54 UnixFdWatcher(MessageLoop* aIOLoop, int aFd); |
|
55 void SetFd(int aFd); |
|
56 |
|
57 private: |
|
58 static bool FdIsNonBlocking(int aFd); |
|
59 |
|
60 MessageLoop* mIOLoop; |
|
61 ScopedClose mFd; |
|
62 MessageLoopForIO::FileDescriptorWatcher mReadWatcher; |
|
63 MessageLoopForIO::FileDescriptorWatcher mWriteWatcher; |
|
64 }; |
|
65 |
|
66 } |
|
67 } |
|
68 |
|
69 #endif |