ipc/unixfd/UnixFdWatcher.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/unixfd/UnixFdWatcher.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include <fcntl.h>
    1.11 +#include "UnixFdWatcher.h"
    1.12 +
    1.13 +#ifdef CHROMIUM_LOG
    1.14 +#undef CHROMIUM_LOG
    1.15 +#endif
    1.16 +
    1.17 +#if defined(MOZ_WIDGET_GONK)
    1.18 +#include <android/log.h>
    1.19 +#define CHROMIUM_LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "I/O", args);
    1.20 +#else
    1.21 +#include <stdio.h>
    1.22 +#define IODEBUG true
    1.23 +#define CHROMIUM_LOG(args...) if (IODEBUG) printf(args);
    1.24 +#endif
    1.25 +
    1.26 +namespace mozilla {
    1.27 +namespace ipc {
    1.28 +
    1.29 +UnixFdWatcher::~UnixFdWatcher()
    1.30 +{
    1.31 +  NS_WARN_IF(IsOpen()); /* mFd should have been closed already */
    1.32 +}
    1.33 +
    1.34 +void
    1.35 +UnixFdWatcher::Close()
    1.36 +{
    1.37 +  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);
    1.38 +
    1.39 +  if (NS_WARN_IF(!IsOpen())) {
    1.40 +    /* mFd should have been open */
    1.41 +    return;
    1.42 +  }
    1.43 +  OnClose();
    1.44 +  RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
    1.45 +  mFd.dispose();
    1.46 +}
    1.47 +
    1.48 +void
    1.49 +UnixFdWatcher::AddWatchers(unsigned long aWatchers, bool aPersistent)
    1.50 +{
    1.51 +  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);
    1.52 +  MOZ_ASSERT(IsOpen());
    1.53 +
    1.54 +  // Before we add a watcher, we need to remove it! Removing is always
    1.55 +  // safe, but adding the same watcher twice can lead to endless loops
    1.56 +  // inside libevent.
    1.57 +  RemoveWatchers(aWatchers);
    1.58 +
    1.59 +  if (aWatchers & READ_WATCHER) {
    1.60 +    MessageLoopForIO::current()->WatchFileDescriptor(
    1.61 +      mFd,
    1.62 +      aPersistent,
    1.63 +      MessageLoopForIO::WATCH_READ,
    1.64 +      &mReadWatcher,
    1.65 +      this);
    1.66 +  }
    1.67 +  if (aWatchers & WRITE_WATCHER) {
    1.68 +    MessageLoopForIO::current()->WatchFileDescriptor(
    1.69 +      mFd,
    1.70 +      aPersistent,
    1.71 +      MessageLoopForIO::WATCH_WRITE,
    1.72 +      &mWriteWatcher,
    1.73 +      this);
    1.74 +  }
    1.75 +}
    1.76 +
    1.77 +void
    1.78 +UnixFdWatcher::RemoveWatchers(unsigned long aWatchers)
    1.79 +{
    1.80 +  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);
    1.81 +  MOZ_ASSERT(IsOpen());
    1.82 +
    1.83 +  if (aWatchers & READ_WATCHER) {
    1.84 +    mReadWatcher.StopWatchingFileDescriptor();
    1.85 +  }
    1.86 +  if (aWatchers & WRITE_WATCHER) {
    1.87 +    mWriteWatcher.StopWatchingFileDescriptor();
    1.88 +  }
    1.89 +}
    1.90 +
    1.91 +void
    1.92 +UnixFdWatcher::OnError(const char* aFunction, int aErrno)
    1.93 +{
    1.94 +  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);
    1.95 +
    1.96 +  CHROMIUM_LOG("%s failed with error %d (%s)",
    1.97 +               aFunction, aErrno, strerror(aErrno));
    1.98 +}
    1.99 +
   1.100 +UnixFdWatcher::UnixFdWatcher(MessageLoop* aIOLoop)
   1.101 +: mIOLoop(aIOLoop)
   1.102 +{
   1.103 +  MOZ_ASSERT(mIOLoop);
   1.104 +}
   1.105 +
   1.106 +UnixFdWatcher::UnixFdWatcher(MessageLoop* aIOLoop, int aFd)
   1.107 +: mIOLoop(aIOLoop)
   1.108 +, mFd(aFd)
   1.109 +{
   1.110 +  MOZ_ASSERT(mIOLoop);
   1.111 +}
   1.112 +
   1.113 +void
   1.114 +UnixFdWatcher::SetFd(int aFd)
   1.115 +{
   1.116 +  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);
   1.117 +  MOZ_ASSERT(!IsOpen());
   1.118 +  MOZ_ASSERT(FdIsNonBlocking(aFd));
   1.119 +
   1.120 +  mFd = aFd;
   1.121 +}
   1.122 +
   1.123 +bool
   1.124 +UnixFdWatcher::FdIsNonBlocking(int aFd)
   1.125 +{
   1.126 +  int flags = TEMP_FAILURE_RETRY(fcntl(aFd, F_GETFL));
   1.127 +  return (flags > 0) && (flags & O_NONBLOCK);
   1.128 +}
   1.129 +
   1.130 +}
   1.131 +}

mercurial