/src/mozilla-central/xpcom/base/nsDumpUtils.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef mozilla_nsDumpUtils_h |
8 | | #define mozilla_nsDumpUtils_h |
9 | | |
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" |
17 | | |
18 | | #ifdef LOG |
19 | | #undef LOG |
20 | | #endif |
21 | | |
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 |
28 | | |
29 | | #ifdef XP_UNIX // { |
30 | | |
31 | | /** |
32 | | * Abstract base class for something which watches an fd and takes action when |
33 | | * we can read from it without blocking. |
34 | | */ |
35 | | class FdWatcher |
36 | | : public MessageLoopForIO::Watcher |
37 | | , public nsIObserver |
38 | | { |
39 | | protected: |
40 | | MessageLoopForIO::FileDescriptorWatcher mReadWatcher; |
41 | | int mFd; |
42 | | |
43 | | virtual ~FdWatcher() |
44 | 0 | { |
45 | 0 | // StopWatching should have run. |
46 | 0 | MOZ_ASSERT(mFd == -1); |
47 | 0 | } |
48 | | |
49 | | public: |
50 | | FdWatcher() |
51 | | : mFd(-1) |
52 | 3 | { |
53 | 3 | MOZ_ASSERT(NS_IsMainThread()); |
54 | 3 | } |
55 | | |
56 | | /** |
57 | | * Open the fd to watch. If we encounter an error, return -1. |
58 | | */ |
59 | | virtual int OpenFd() = 0; |
60 | | |
61 | | /** |
62 | | * Called when you can read() from the fd without blocking. Note that this |
63 | | * function is also called when you're at eof (read() returns 0 in this case). |
64 | | */ |
65 | | virtual void OnFileCanReadWithoutBlocking(int aFd) override = 0; |
66 | 0 | virtual void OnFileCanWriteWithoutBlocking(int aFd) override {}; |
67 | | |
68 | | NS_DECL_THREADSAFE_ISUPPORTS |
69 | | |
70 | | /** |
71 | | * Initialize this object. This should be called right after the object is |
72 | | * constructed. (This would go in the constructor, except we interact with |
73 | | * XPCOM, which we can't do from a constructor because our refcount is 0 at |
74 | | * that point.) |
75 | | */ |
76 | | void Init(); |
77 | | |
78 | | // Implementations may call this function multiple times if they ensure that |
79 | | |
80 | | virtual void StartWatching(); |
81 | | |
82 | | // Since implementations can call StartWatching() multiple times, they can of |
83 | | // course call StopWatching() multiple times. |
84 | | virtual void StopWatching(); |
85 | | |
86 | | NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic, |
87 | | const char16_t* aData) override |
88 | 0 | { |
89 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
90 | 0 | MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown")); |
91 | 0 |
|
92 | 0 | XRE_GetIOMessageLoop()->PostTask(mozilla::NewRunnableMethod( |
93 | 0 | "FdWatcher::StopWatching", this, &FdWatcher::StopWatching)); |
94 | 0 |
|
95 | 0 | return NS_OK; |
96 | 0 | } |
97 | | }; |
98 | | |
99 | | typedef void (*FifoCallback)(const nsCString& aInputStr); |
100 | | struct FifoInfo |
101 | | { |
102 | | nsCString mCommand; |
103 | | FifoCallback mCallback; |
104 | | }; |
105 | | typedef nsTArray<FifoInfo> FifoInfoArray; |
106 | | |
107 | | class FifoWatcher : public FdWatcher |
108 | | { |
109 | | public: |
110 | | /** |
111 | | * The name of the preference used to enable/disable the FifoWatcher. |
112 | | */ |
113 | | // The length of this array must match the size of the string constant in |
114 | | // the definition in nsDumpUtils.cpp. A mismatch will result in a compile-time |
115 | | // error. |
116 | | static const char kPrefName[38]; |
117 | | |
118 | | static FifoWatcher* GetSingleton(); |
119 | | |
120 | | static bool MaybeCreate(); |
121 | | |
122 | | void RegisterCallback(const nsCString& aCommand, FifoCallback aCallback); |
123 | | |
124 | | virtual ~FifoWatcher(); |
125 | | |
126 | | virtual int OpenFd() override; |
127 | | |
128 | | virtual void OnFileCanReadWithoutBlocking(int aFd) override; |
129 | | |
130 | | private: |
131 | | nsAutoCString mDirPath; |
132 | | |
133 | | static mozilla::StaticRefPtr<FifoWatcher> sSingleton; |
134 | | |
135 | | explicit FifoWatcher(nsCString aPath) |
136 | | : mDirPath(aPath) |
137 | | , mFifoInfoLock("FifoWatcher.mFifoInfoLock") |
138 | 0 | { |
139 | 0 | } |
140 | | |
141 | | mozilla::Mutex mFifoInfoLock; // protects mFifoInfo |
142 | | FifoInfoArray mFifoInfo; |
143 | | }; |
144 | | |
145 | | typedef void (*PipeCallback)(const uint8_t aRecvSig); |
146 | | struct SignalInfo |
147 | | { |
148 | | uint8_t mSignal; |
149 | | PipeCallback mCallback; |
150 | | }; |
151 | | typedef nsTArray<SignalInfo> SignalInfoArray; |
152 | | |
153 | | class SignalPipeWatcher : public FdWatcher |
154 | | { |
155 | | public: |
156 | | static SignalPipeWatcher* GetSingleton(); |
157 | | |
158 | | void RegisterCallback(uint8_t aSignal, PipeCallback aCallback); |
159 | | |
160 | | void RegisterSignalHandler(uint8_t aSignal = 0); |
161 | | |
162 | | virtual ~SignalPipeWatcher(); |
163 | | |
164 | | virtual int OpenFd() override; |
165 | | |
166 | | virtual void StopWatching() override; |
167 | | |
168 | | virtual void OnFileCanReadWithoutBlocking(int aFd) override; |
169 | | |
170 | | private: |
171 | | static mozilla::StaticRefPtr<SignalPipeWatcher> sSingleton; |
172 | | |
173 | | SignalPipeWatcher() |
174 | | : mSignalInfoLock("SignalPipeWatcher.mSignalInfoLock") |
175 | 3 | { |
176 | 3 | MOZ_ASSERT(NS_IsMainThread()); |
177 | 3 | } |
178 | | |
179 | | mozilla::Mutex mSignalInfoLock; // protects mSignalInfo |
180 | | SignalInfoArray mSignalInfo; |
181 | | }; |
182 | | |
183 | | #endif // XP_UNIX } |
184 | | |
185 | | class nsDumpUtils |
186 | | { |
187 | | public: |
188 | | |
189 | | enum Mode { |
190 | | CREATE, |
191 | | CREATE_UNIQUE |
192 | | }; |
193 | | |
194 | | /** |
195 | | * This function creates a new unique file based on |aFilename| in a |
196 | | * world-readable temp directory. This is the system temp directory |
197 | | * or, in the case of Android, the downloads directory. If |aFile| is |
198 | | * non-null, it is assumed to point to a folder, and that folder is used |
199 | | * instead. |
200 | | */ |
201 | | static nsresult OpenTempFile(const nsACString& aFilename, |
202 | | nsIFile** aFile, |
203 | | const nsACString& aFoldername = EmptyCString(), |
204 | | Mode aMode = CREATE_UNIQUE); |
205 | | }; |
206 | | |
207 | | #endif |