2014-03-20 11:29:51 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_nsDumpUtils_h
|
|
|
|
#define mozilla_nsDumpUtils_h
|
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2014-04-18 03:57:30 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2014-03-20 11:29:51 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#ifdef LOG
|
|
|
|
# undef LOG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
# include "android/log.h"
|
|
|
|
# define LOG(...) \
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko:DumpUtils", ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define LOG(...)
|
|
|
|
#endif
|
|
|
|
|
2014-05-29 07:24:00 +04:00
|
|
|
#ifdef XP_UNIX // {
|
2014-03-20 11:29:51 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract base class for something which watches an fd and takes action when
|
|
|
|
* we can read from it without blocking.
|
|
|
|
*/
|
2014-05-13 21:41:38 +04:00
|
|
|
class FdWatcher : public MessageLoopForIO::Watcher, public nsIObserver {
|
2014-03-20 11:29:51 +04:00
|
|
|
protected:
|
|
|
|
MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
|
|
|
|
int mFd;
|
|
|
|
|
2014-07-01 02:11:53 +04:00
|
|
|
virtual ~FdWatcher() {
|
|
|
|
// StopWatching should have run.
|
|
|
|
MOZ_ASSERT(mFd == -1);
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:29:51 +04:00
|
|
|
public:
|
|
|
|
FdWatcher() : mFd(-1) { MOZ_ASSERT(NS_IsMainThread()); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the fd to watch. If we encounter an error, return -1.
|
|
|
|
*/
|
|
|
|
virtual int OpenFd() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when you can read() from the fd without blocking. Note that this
|
|
|
|
* function is also called when you're at eof (read() returns 0 in this case).
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void OnFileCanReadWithoutBlocking(int aFd) override = 0;
|
|
|
|
virtual void OnFileCanWriteWithoutBlocking(int aFd) override{};
|
2014-03-20 11:29:51 +04:00
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize this object. This should be called right after the object is
|
|
|
|
* constructed. (This would go in the constructor, except we interact with
|
|
|
|
* XPCOM, which we can't do from a constructor because our refcount is 0 at
|
|
|
|
* that point.)
|
|
|
|
*/
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
// Implementations may call this function multiple times if they ensure that
|
|
|
|
|
|
|
|
virtual void StartWatching();
|
|
|
|
|
|
|
|
// Since implementations can call StartWatching() multiple times, they can of
|
|
|
|
// course call StopWatching() multiple times.
|
|
|
|
virtual void StopWatching();
|
|
|
|
|
|
|
|
NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
|
2015-03-21 19:28:04 +03:00
|
|
|
const char16_t* aData) override {
|
2014-03-20 11:29:51 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "xpcom-shutdown"));
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(mozilla::NewRunnableMethod(
|
|
|
|
"FdWatcher::StopWatching", this, &FdWatcher::StopWatching));
|
2014-03-20 11:29:51 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-15 00:14:58 +04:00
|
|
|
typedef void (*FifoCallback)(const nsCString& aInputStr);
|
2014-05-13 21:41:38 +04:00
|
|
|
struct FifoInfo {
|
2014-03-20 11:29:51 +04:00
|
|
|
nsCString mCommand;
|
|
|
|
FifoCallback mCallback;
|
|
|
|
};
|
|
|
|
typedef nsTArray<FifoInfo> FifoInfoArray;
|
|
|
|
|
|
|
|
class FifoWatcher : public FdWatcher {
|
|
|
|
public:
|
2014-04-25 02:58:57 +04:00
|
|
|
/**
|
|
|
|
* The name of the preference used to enable/disable the FifoWatcher.
|
|
|
|
*/
|
2018-07-01 09:06:17 +03:00
|
|
|
// The length of this array must match the size of the string constant in
|
|
|
|
// the definition in nsDumpUtils.cpp. A mismatch will result in a compile-time
|
|
|
|
// error.
|
|
|
|
static const char kPrefName[38];
|
2014-04-25 02:58:57 +04:00
|
|
|
|
2014-03-20 11:29:51 +04:00
|
|
|
static FifoWatcher* GetSingleton();
|
|
|
|
|
|
|
|
static bool MaybeCreate();
|
|
|
|
|
|
|
|
void RegisterCallback(const nsCString& aCommand, FifoCallback aCallback);
|
|
|
|
|
|
|
|
virtual ~FifoWatcher();
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual int OpenFd() override;
|
2014-03-20 11:29:51 +04:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual void OnFileCanReadWithoutBlocking(int aFd) override;
|
2014-03-20 11:29:51 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoCString mDirPath;
|
|
|
|
|
2014-05-19 03:34:00 +04:00
|
|
|
static mozilla::StaticRefPtr<FifoWatcher> sSingleton;
|
2014-03-20 11:29:51 +04:00
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit FifoWatcher(nsCString aPath)
|
2014-04-18 03:57:30 +04:00
|
|
|
: mDirPath(aPath), mFifoInfoLock("FifoWatcher.mFifoInfoLock") {}
|
2014-03-20 11:29:51 +04:00
|
|
|
|
2022-03-24 01:22:51 +03:00
|
|
|
mozilla::Mutex mFifoInfoLock; // protects mFifoInfo
|
|
|
|
FifoInfoArray mFifoInfo GUARDED_BY(mFifoInfoLock);
|
2014-03-20 11:29:51 +04:00
|
|
|
};
|
|
|
|
|
2014-05-15 00:14:58 +04:00
|
|
|
typedef void (*PipeCallback)(const uint8_t aRecvSig);
|
2014-05-13 21:41:38 +04:00
|
|
|
struct SignalInfo {
|
2014-03-20 11:29:51 +04:00
|
|
|
uint8_t mSignal;
|
|
|
|
PipeCallback mCallback;
|
|
|
|
};
|
|
|
|
typedef nsTArray<SignalInfo> SignalInfoArray;
|
|
|
|
|
|
|
|
class SignalPipeWatcher : public FdWatcher {
|
|
|
|
public:
|
|
|
|
static SignalPipeWatcher* GetSingleton();
|
|
|
|
|
2014-04-18 03:57:30 +04:00
|
|
|
void RegisterCallback(uint8_t aSignal, PipeCallback aCallback);
|
2014-03-20 11:29:51 +04:00
|
|
|
|
2014-04-18 03:57:30 +04:00
|
|
|
void RegisterSignalHandler(uint8_t aSignal = 0);
|
2014-03-20 11:29:51 +04:00
|
|
|
|
|
|
|
virtual ~SignalPipeWatcher();
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual int OpenFd() override;
|
2014-03-20 11:29:51 +04:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual void StopWatching() override;
|
2014-03-20 11:29:51 +04:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual void OnFileCanReadWithoutBlocking(int aFd) override;
|
2014-03-20 11:29:51 +04:00
|
|
|
|
|
|
|
private:
|
2014-05-19 03:34:00 +04:00
|
|
|
static mozilla::StaticRefPtr<SignalPipeWatcher> sSingleton;
|
2014-03-20 11:29:51 +04:00
|
|
|
|
2014-04-18 03:57:30 +04:00
|
|
|
SignalPipeWatcher() : mSignalInfoLock("SignalPipeWatcher.mSignalInfoLock") {
|
2014-03-20 11:29:51 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2022-03-24 01:22:51 +03:00
|
|
|
mozilla::Mutex mSignalInfoLock; // protects mSignalInfo
|
|
|
|
SignalInfoArray mSignalInfo GUARDED_BY(mSignalInfoLock);
|
2014-03-20 11:29:51 +04:00
|
|
|
};
|
|
|
|
|
2014-05-29 07:24:00 +04:00
|
|
|
#endif // XP_UNIX }
|
2015-06-04 09:47:09 +03:00
|
|
|
|
|
|
|
class nsDumpUtils {
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2015-06-04 09:47:09 +03:00
|
|
|
enum Mode { CREATE, CREATE_UNIQUE };
|
|
|
|
|
2014-03-20 11:29:51 +04:00
|
|
|
/**
|
|
|
|
* This function creates a new unique file based on |aFilename| in a
|
|
|
|
* world-readable temp directory. This is the system temp directory
|
|
|
|
* or, in the case of Android, the downloads directory. If |aFile| is
|
|
|
|
* non-null, it is assumed to point to a folder, and that folder is used
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
static nsresult OpenTempFile(const nsACString& aFilename, nsIFile** aFile,
|
2020-09-23 18:17:15 +03:00
|
|
|
const nsACString& aFoldername = ""_ns,
|
2015-06-04 09:47:09 +03:00
|
|
|
Mode aMode = CREATE_UNIQUE);
|
2014-03-20 11:29:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|