summaryrefslogtreecommitdiffstats
path: root/ipc/unixfd/UnixFdWatcher.h
blob: 676b1dbf0bcaf1d126da7490676496ff1010821e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_ipc_UnixFdWatcher_h
#define mozilla_ipc_UnixFdWatcher_h

#include "base/message_loop.h"
#include "mozilla/FileUtils.h"

namespace mozilla {
namespace ipc {

class UnixFdWatcher : public MessageLoopForIO::Watcher
{
public:
  enum {
    READ_WATCHER = 1<<0,
    WRITE_WATCHER = 1<<1
  };

  virtual ~UnixFdWatcher();

  MessageLoop* GetIOLoop() const
  {
    return mIOLoop;
  }

  int GetFd() const
  {
    return mFd;
  }

  bool IsOpen() const
  {
    return GetFd() >= 0;
  }

  virtual void Close();

  void AddWatchers(unsigned long aWatchers, bool aPersistent);
  void RemoveWatchers(unsigned long aWatchers);

  // Callback method that's run before closing the file descriptor
  virtual void OnClose() {};

  // Callback method that's run on POSIX errors
  virtual void OnError(const char* aFunction, int aErrno);

protected:
  UnixFdWatcher(MessageLoop* aIOLoop);
  UnixFdWatcher(MessageLoop* aIOLoop, int aFd);
  void SetFd(int aFd);

private:
  static bool FdIsNonBlocking(int aFd);

  MessageLoop* mIOLoop;
  ScopedClose mFd;
  MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
  MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
};

}
}

#endif