Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_nsDumpUtils_h |
michael@0 | 8 | #define mozilla_nsDumpUtils_h |
michael@0 | 9 | |
michael@0 | 10 | #include "nsIObserver.h" |
michael@0 | 11 | #include "base/message_loop.h" |
michael@0 | 12 | #include "nsXULAppAPI.h" |
michael@0 | 13 | #include "nsThreadUtils.h" |
michael@0 | 14 | #include "mozilla/Mutex.h" |
michael@0 | 15 | #include "mozilla/StaticPtr.h" |
michael@0 | 16 | #include "nsTArray.h" |
michael@0 | 17 | |
michael@0 | 18 | #ifdef LOG |
michael@0 | 19 | #undef LOG |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | #ifdef ANDROID |
michael@0 | 23 | #include "android/log.h" |
michael@0 | 24 | #define LOG(...) __android_log_print(ANDROID_LOG_INFO, "Gecko:DumpUtils", ## __VA_ARGS__) |
michael@0 | 25 | #else |
michael@0 | 26 | #define LOG(...) |
michael@0 | 27 | #endif |
michael@0 | 28 | |
michael@0 | 29 | using namespace mozilla; |
michael@0 | 30 | |
michael@0 | 31 | #ifdef XP_UNIX // { |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * Abstract base class for something which watches an fd and takes action when |
michael@0 | 35 | * we can read from it without blocking. |
michael@0 | 36 | */ |
michael@0 | 37 | class FdWatcher : public MessageLoopForIO::Watcher |
michael@0 | 38 | , public nsIObserver |
michael@0 | 39 | { |
michael@0 | 40 | protected: |
michael@0 | 41 | MessageLoopForIO::FileDescriptorWatcher mReadWatcher; |
michael@0 | 42 | int mFd; |
michael@0 | 43 | |
michael@0 | 44 | public: |
michael@0 | 45 | FdWatcher() |
michael@0 | 46 | : mFd(-1) |
michael@0 | 47 | { |
michael@0 | 48 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | virtual ~FdWatcher() |
michael@0 | 52 | { |
michael@0 | 53 | // StopWatching should have run. |
michael@0 | 54 | MOZ_ASSERT(mFd == -1); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | /** |
michael@0 | 58 | * Open the fd to watch. If we encounter an error, return -1. |
michael@0 | 59 | */ |
michael@0 | 60 | virtual int OpenFd() = 0; |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Called when you can read() from the fd without blocking. Note that this |
michael@0 | 64 | * function is also called when you're at eof (read() returns 0 in this case). |
michael@0 | 65 | */ |
michael@0 | 66 | virtual void OnFileCanReadWithoutBlocking(int aFd) = 0; |
michael@0 | 67 | virtual void OnFileCanWriteWithoutBlocking(int aFd) {}; |
michael@0 | 68 | |
michael@0 | 69 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * Initialize this object. This should be called right after the object is |
michael@0 | 73 | * constructed. (This would go in the constructor, except we interact with |
michael@0 | 74 | * XPCOM, which we can't do from a constructor because our refcount is 0 at |
michael@0 | 75 | * that point.) |
michael@0 | 76 | */ |
michael@0 | 77 | void Init(); |
michael@0 | 78 | |
michael@0 | 79 | // Implementations may call this function multiple times if they ensure that |
michael@0 | 80 | |
michael@0 | 81 | virtual void StartWatching(); |
michael@0 | 82 | |
michael@0 | 83 | // Since implementations can call StartWatching() multiple times, they can of |
michael@0 | 84 | // course call StopWatching() multiple times. |
michael@0 | 85 | virtual void StopWatching(); |
michael@0 | 86 | |
michael@0 | 87 | NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic, |
michael@0 | 88 | const char16_t* aData) |
michael@0 | 89 | { |
michael@0 | 90 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 91 | MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown")); |
michael@0 | 92 | |
michael@0 | 93 | XRE_GetIOMessageLoop()->PostTask( |
michael@0 | 94 | FROM_HERE, |
michael@0 | 95 | NewRunnableMethod(this, &FdWatcher::StopWatching)); |
michael@0 | 96 | |
michael@0 | 97 | return NS_OK; |
michael@0 | 98 | } |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | typedef void (* FifoCallback)(const nsCString& inputStr); |
michael@0 | 102 | struct FifoInfo { |
michael@0 | 103 | nsCString mCommand; |
michael@0 | 104 | FifoCallback mCallback; |
michael@0 | 105 | }; |
michael@0 | 106 | typedef nsTArray<FifoInfo> FifoInfoArray; |
michael@0 | 107 | |
michael@0 | 108 | class FifoWatcher : public FdWatcher |
michael@0 | 109 | { |
michael@0 | 110 | public: |
michael@0 | 111 | /** |
michael@0 | 112 | * The name of the preference used to enable/disable the FifoWatcher. |
michael@0 | 113 | */ |
michael@0 | 114 | static const char* const kPrefName; |
michael@0 | 115 | |
michael@0 | 116 | static FifoWatcher* GetSingleton(); |
michael@0 | 117 | |
michael@0 | 118 | static bool MaybeCreate(); |
michael@0 | 119 | |
michael@0 | 120 | void RegisterCallback(const nsCString& aCommand, FifoCallback aCallback); |
michael@0 | 121 | |
michael@0 | 122 | virtual ~FifoWatcher(); |
michael@0 | 123 | |
michael@0 | 124 | virtual int OpenFd(); |
michael@0 | 125 | |
michael@0 | 126 | virtual void OnFileCanReadWithoutBlocking(int aFd); |
michael@0 | 127 | |
michael@0 | 128 | private: |
michael@0 | 129 | nsAutoCString mDirPath; |
michael@0 | 130 | |
michael@0 | 131 | static StaticRefPtr<FifoWatcher> sSingleton; |
michael@0 | 132 | |
michael@0 | 133 | FifoWatcher(nsCString aPath) |
michael@0 | 134 | : mDirPath(aPath) |
michael@0 | 135 | , mFifoInfoLock("FifoWatcher.mFifoInfoLock") |
michael@0 | 136 | {} |
michael@0 | 137 | |
michael@0 | 138 | mozilla::Mutex mFifoInfoLock; // protects mFifoInfo |
michael@0 | 139 | FifoInfoArray mFifoInfo; |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | typedef void (* PipeCallback)(const uint8_t recvSig); |
michael@0 | 143 | struct SignalInfo { |
michael@0 | 144 | uint8_t mSignal; |
michael@0 | 145 | PipeCallback mCallback; |
michael@0 | 146 | }; |
michael@0 | 147 | typedef nsTArray<SignalInfo> SignalInfoArray; |
michael@0 | 148 | |
michael@0 | 149 | class SignalPipeWatcher : public FdWatcher |
michael@0 | 150 | { |
michael@0 | 151 | public: |
michael@0 | 152 | static SignalPipeWatcher* GetSingleton(); |
michael@0 | 153 | |
michael@0 | 154 | void RegisterCallback(uint8_t aSignal, PipeCallback aCallback); |
michael@0 | 155 | |
michael@0 | 156 | void RegisterSignalHandler(uint8_t aSignal = 0); |
michael@0 | 157 | |
michael@0 | 158 | virtual ~SignalPipeWatcher(); |
michael@0 | 159 | |
michael@0 | 160 | virtual int OpenFd(); |
michael@0 | 161 | |
michael@0 | 162 | virtual void StopWatching(); |
michael@0 | 163 | |
michael@0 | 164 | virtual void OnFileCanReadWithoutBlocking(int aFd); |
michael@0 | 165 | |
michael@0 | 166 | private: |
michael@0 | 167 | static StaticRefPtr<SignalPipeWatcher> sSingleton; |
michael@0 | 168 | |
michael@0 | 169 | SignalPipeWatcher() |
michael@0 | 170 | : mSignalInfoLock("SignalPipeWatcher.mSignalInfoLock") |
michael@0 | 171 | { |
michael@0 | 172 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | mozilla::Mutex mSignalInfoLock; // protects mSignalInfo |
michael@0 | 176 | SignalInfoArray mSignalInfo; |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | #endif // XP_UNIX } |
michael@0 | 180 | |
michael@0 | 181 | |
michael@0 | 182 | class nsDumpUtils |
michael@0 | 183 | { |
michael@0 | 184 | public: |
michael@0 | 185 | /** |
michael@0 | 186 | * This function creates a new unique file based on |aFilename| in a |
michael@0 | 187 | * world-readable temp directory. This is the system temp directory |
michael@0 | 188 | * or, in the case of Android, the downloads directory. If |aFile| is |
michael@0 | 189 | * non-null, it is assumed to point to a folder, and that folder is used |
michael@0 | 190 | * instead. |
michael@0 | 191 | */ |
michael@0 | 192 | static nsresult OpenTempFile(const nsACString& aFilename, |
michael@0 | 193 | nsIFile** aFile, |
michael@0 | 194 | const nsACString& aFoldername = EmptyCString()); |
michael@0 | 195 | }; |
michael@0 | 196 | |
michael@0 | 197 | #endif |