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