xpcom/base/nsDumpUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/base/nsDumpUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,197 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=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 +#ifndef mozilla_nsDumpUtils_h
    1.11 +#define mozilla_nsDumpUtils_h
    1.12 +
    1.13 +#include "nsIObserver.h"
    1.14 +#include "base/message_loop.h"
    1.15 +#include "nsXULAppAPI.h"
    1.16 +#include "nsThreadUtils.h"
    1.17 +#include "mozilla/Mutex.h"
    1.18 +#include "mozilla/StaticPtr.h"
    1.19 +#include "nsTArray.h"
    1.20 +
    1.21 +#ifdef LOG
    1.22 +#undef LOG
    1.23 +#endif
    1.24 +
    1.25 +#ifdef ANDROID
    1.26 +#include "android/log.h"
    1.27 +#define LOG(...) __android_log_print(ANDROID_LOG_INFO, "Gecko:DumpUtils", ## __VA_ARGS__)
    1.28 +#else
    1.29 +#define LOG(...)
    1.30 +#endif
    1.31 +
    1.32 +using namespace mozilla;
    1.33 +
    1.34 +#ifdef XP_UNIX // {
    1.35 +
    1.36 +/**
    1.37 + * Abstract base class for something which watches an fd and takes action when
    1.38 + * we can read from it without blocking.
    1.39 + */
    1.40 +class FdWatcher : public MessageLoopForIO::Watcher
    1.41 +                , public nsIObserver
    1.42 +{
    1.43 +protected:
    1.44 +  MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
    1.45 +  int mFd;
    1.46 +
    1.47 +public:
    1.48 +  FdWatcher()
    1.49 +    : mFd(-1)
    1.50 +  {
    1.51 +    MOZ_ASSERT(NS_IsMainThread());
    1.52 +  }
    1.53 +
    1.54 +  virtual ~FdWatcher()
    1.55 +  {
    1.56 +    // StopWatching should have run.
    1.57 +    MOZ_ASSERT(mFd == -1);
    1.58 +  }
    1.59 +
    1.60 +  /**
    1.61 +   * Open the fd to watch.  If we encounter an error, return -1.
    1.62 +   */
    1.63 +  virtual int OpenFd() = 0;
    1.64 +
    1.65 +  /**
    1.66 +   * Called when you can read() from the fd without blocking.  Note that this
    1.67 +   * function is also called when you're at eof (read() returns 0 in this case).
    1.68 +   */
    1.69 +  virtual void OnFileCanReadWithoutBlocking(int aFd) = 0;
    1.70 +  virtual void OnFileCanWriteWithoutBlocking(int aFd) {};
    1.71 +
    1.72 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.73 +
    1.74 +  /**
    1.75 +   * Initialize this object.  This should be called right after the object is
    1.76 +   * constructed.  (This would go in the constructor, except we interact with
    1.77 +   * XPCOM, which we can't do from a constructor because our refcount is 0 at
    1.78 +   * that point.)
    1.79 +   */
    1.80 +  void Init();
    1.81 +
    1.82 +  // Implementations may call this function multiple times if they ensure that
    1.83 +
    1.84 +  virtual void StartWatching();
    1.85 +
    1.86 +  // Since implementations can call StartWatching() multiple times, they can of
    1.87 +  // course call StopWatching() multiple times.
    1.88 +  virtual void StopWatching();
    1.89 +
    1.90 +  NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
    1.91 +                     const char16_t* aData)
    1.92 +  {
    1.93 +    MOZ_ASSERT(NS_IsMainThread());
    1.94 +    MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown"));
    1.95 +
    1.96 +    XRE_GetIOMessageLoop()->PostTask(
    1.97 +        FROM_HERE,
    1.98 +        NewRunnableMethod(this, &FdWatcher::StopWatching));
    1.99 +
   1.100 +    return NS_OK;
   1.101 +  }
   1.102 +};
   1.103 +
   1.104 +typedef void (* FifoCallback)(const nsCString& inputStr);
   1.105 +struct FifoInfo {
   1.106 +  nsCString mCommand;
   1.107 +  FifoCallback mCallback;
   1.108 +};
   1.109 +typedef nsTArray<FifoInfo> FifoInfoArray;
   1.110 +
   1.111 +class FifoWatcher : public FdWatcher
   1.112 +{
   1.113 +public:
   1.114 +  /**
   1.115 +   * The name of the preference used to enable/disable the FifoWatcher.
   1.116 +   */
   1.117 +  static const char* const kPrefName;
   1.118 +
   1.119 +  static FifoWatcher* GetSingleton();
   1.120 +
   1.121 +  static bool MaybeCreate();
   1.122 +
   1.123 +  void RegisterCallback(const nsCString& aCommand, FifoCallback aCallback);
   1.124 +
   1.125 +  virtual ~FifoWatcher();
   1.126 +
   1.127 +  virtual int OpenFd();
   1.128 +
   1.129 +  virtual void OnFileCanReadWithoutBlocking(int aFd);
   1.130 +
   1.131 +private:
   1.132 +  nsAutoCString mDirPath;
   1.133 +
   1.134 +  static StaticRefPtr<FifoWatcher> sSingleton;
   1.135 +
   1.136 +  FifoWatcher(nsCString aPath)
   1.137 +    : mDirPath(aPath)
   1.138 +    , mFifoInfoLock("FifoWatcher.mFifoInfoLock")
   1.139 +  {}
   1.140 +
   1.141 +  mozilla::Mutex mFifoInfoLock; // protects mFifoInfo
   1.142 +  FifoInfoArray mFifoInfo;
   1.143 +};
   1.144 +
   1.145 +typedef void (* PipeCallback)(const uint8_t recvSig);
   1.146 +struct SignalInfo {
   1.147 +  uint8_t mSignal;
   1.148 +  PipeCallback mCallback;
   1.149 +};
   1.150 +typedef nsTArray<SignalInfo> SignalInfoArray;
   1.151 +
   1.152 +class SignalPipeWatcher : public FdWatcher
   1.153 +{
   1.154 +public:
   1.155 +  static SignalPipeWatcher* GetSingleton();
   1.156 +
   1.157 +  void RegisterCallback(uint8_t aSignal, PipeCallback aCallback);
   1.158 +
   1.159 +  void RegisterSignalHandler(uint8_t aSignal = 0);
   1.160 +
   1.161 +  virtual ~SignalPipeWatcher();
   1.162 +
   1.163 +  virtual int OpenFd();
   1.164 +
   1.165 +  virtual void StopWatching();
   1.166 +
   1.167 +  virtual void OnFileCanReadWithoutBlocking(int aFd);
   1.168 +
   1.169 +private:
   1.170 +  static StaticRefPtr<SignalPipeWatcher> sSingleton;
   1.171 +
   1.172 +  SignalPipeWatcher()
   1.173 +    : mSignalInfoLock("SignalPipeWatcher.mSignalInfoLock")
   1.174 +  {
   1.175 +    MOZ_ASSERT(NS_IsMainThread());
   1.176 +  }
   1.177 +
   1.178 +  mozilla::Mutex mSignalInfoLock; // protects mSignalInfo
   1.179 +  SignalInfoArray mSignalInfo;
   1.180 +};
   1.181 +
   1.182 +#endif // XP_UNIX }
   1.183 +
   1.184 +
   1.185 +class nsDumpUtils
   1.186 +{
   1.187 +public:
   1.188 +  /**
   1.189 +   * This function creates a new unique file based on |aFilename| in a
   1.190 +   * world-readable temp directory. This is the system temp directory
   1.191 +   * or, in the case of Android, the downloads directory. If |aFile| is
   1.192 +   * non-null, it is assumed to point to a folder, and that folder is used
   1.193 +   * instead.
   1.194 +   */
   1.195 +  static nsresult OpenTempFile(const nsACString& aFilename,
   1.196 +                        nsIFile** aFile,
   1.197 +                        const nsACString& aFoldername = EmptyCString());
   1.198 +};
   1.199 +
   1.200 +#endif

mercurial