Convert Utils into a namespace and remove dead code.

Post-cleanup for #12236.
2.0
Zack Weinberg 2014-08-20 16:06:53 -04:00
parent 2ab57b875e
commit 858972e7ef
4 changed files with 109 additions and 124 deletions

View File

@ -38,6 +38,7 @@
#include <QApplication>
#include <QSslSocket>
#include <QIcon>
int main(int argc, char** argv)
{

View File

@ -493,10 +493,6 @@ void Phantom::clearCookies()
// private:
void Phantom::doExit(int code)
{
if (m_config.debug()) {
Utils::cleanupFromDebug();
}
emit aboutToExit(code);
m_terminated = true;
m_returnValue = code;

View File

@ -28,91 +28,17 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "utils.h"
#include "consts.h"
#include "terminal.h"
#include <QFile>
#include <QDebug>
#include <QDateTime>
#include <QDir>
#include <QTemporaryFile>
#include <QtWebKitWidgets/QWebFrame>
#include "consts.h"
#include "terminal.h"
#include "utils.h"
QTemporaryFile* Utils::m_tempHarness = 0;
QTemporaryFile* Utils::m_tempWrapper = 0;
bool Utils::printDebugMessages = false;
void Utils::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(context);
QDateTime now = QDateTime::currentDateTime();
switch (type) {
case QtDebugMsg:
if (printDebugMessages) {
fprintf(stderr, "%s [DEBUG] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
}
break;
case QtWarningMsg:
if (printDebugMessages) {
fprintf(stderr, "%s [WARNING] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
}
break;
case QtCriticalMsg:
fprintf(stderr, "%s [CRITICAL] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
break;
case QtFatalMsg:
fprintf(stderr, "%s [FATAL] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
abort();
}
}
bool Utils::injectJsInFrame(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript)
{
return injectJsInFrame(jsFilePath, QString(), Encoding::UTF8, libraryPath, targetFrame, startingScript);
}
bool Utils::injectJsInFrame(const QString &jsFilePath, const QString &jsFileLanguage, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript)
{
// Don't do anything if an empty string is passed
QString scriptPath = findScript(jsFilePath, libraryPath);
QString scriptBody = jsFromScriptFile(scriptPath, jsFileLanguage, jsFileEnc);
if (scriptBody.isEmpty())
{
if (startingScript) {
Terminal::instance()->cerr(QString("Can't open '%1'").arg(jsFilePath));
} else {
qWarning("Can't open '%s'", qPrintable(jsFilePath));
}
return false;
}
// Execute JS code in the context of the document
targetFrame->evaluateJavaScript(scriptBody, jsFilePath);
return true;
}
bool Utils::loadJSForDebug(const QString& jsFilePath, const QString& libraryPath, QWebFrame* targetFrame, const bool autorun)
{
return loadJSForDebug(jsFilePath, QString(), Encoding::UTF8, libraryPath, targetFrame, autorun);
}
bool Utils::loadJSForDebug(const QString& jsFilePath, const QString &jsFileLanguage, const Encoding& jsFileEnc, const QString& libraryPath, QWebFrame* targetFrame, const bool autorun)
{
QString scriptPath = findScript(jsFilePath, libraryPath);
QString scriptBody = jsFromScriptFile(scriptPath, jsFileLanguage, jsFileEnc);
QString remoteDebuggerHarnessSrc = Utils::readResourceFileUtf8(":/remote_debugger_harness.html");
remoteDebuggerHarnessSrc = remoteDebuggerHarnessSrc.arg(scriptBody);
targetFrame->setHtml(remoteDebuggerHarnessSrc);
if (autorun) {
targetFrame->evaluateJavaScript("__run()", QString());
}
return true;
}
QString Utils::findScript(const QString& jsFilePath, const QString &libraryPath)
static QString findScript(const QString& jsFilePath, const QString &libraryPath)
{
QString filePath = jsFilePath;
if (!jsFilePath.isEmpty()) {
@ -130,7 +56,7 @@ QString Utils::findScript(const QString& jsFilePath, const QString &libraryPath)
return QString();
}
QString Utils::jsFromScriptFile(const QString& scriptPath, const QString& scriptLanguage, const Encoding& enc)
static QString jsFromScriptFile(const QString& scriptPath, const QString& scriptLanguage, const Encoding& enc)
{
QFile jsFile(scriptPath);
if (jsFile.exists() && jsFile.open(QFile::ReadOnly)) {
@ -158,31 +84,85 @@ QString Utils::jsFromScriptFile(const QString& scriptPath, const QString& script
}
}
namespace Utils {
void
Utils::cleanupFromDebug()
bool printDebugMessages = false;
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
if (m_tempHarness) {
// Will erase the temp file on disk
delete m_tempHarness;
m_tempHarness = 0;
}
if (m_tempWrapper) {
delete m_tempWrapper;
m_tempWrapper = 0;
Q_UNUSED(context);
QDateTime now = QDateTime::currentDateTime();
switch (type) {
case QtDebugMsg:
if (printDebugMessages) {
fprintf(stderr, "%s [DEBUG] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
}
break;
case QtWarningMsg:
if (printDebugMessages) {
fprintf(stderr, "%s [WARNING] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
}
break;
case QtCriticalMsg:
fprintf(stderr, "%s [CRITICAL] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
break;
case QtFatalMsg:
fprintf(stderr, "%s [FATAL] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
abort();
}
}
bool injectJsInFrame(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript)
{
return injectJsInFrame(jsFilePath, QString(), Encoding::UTF8, libraryPath, targetFrame, startingScript);
}
QString Utils::readResourceFileUtf8(const QString &resourceFilePath)
bool injectJsInFrame(const QString &jsFilePath, const QString &jsFileLanguage, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript)
{
// Don't do anything if an empty string is passed
QString scriptPath = findScript(jsFilePath, libraryPath);
QString scriptBody = jsFromScriptFile(scriptPath, jsFileLanguage, jsFileEnc);
if (scriptBody.isEmpty())
{
if (startingScript) {
Terminal::instance()->cerr(QString("Can't open '%1'").arg(jsFilePath));
} else {
qWarning("Can't open '%s'", qPrintable(jsFilePath));
}
return false;
}
// Execute JS code in the context of the document
targetFrame->evaluateJavaScript(scriptBody, jsFilePath);
return true;
}
bool loadJSForDebug(const QString& jsFilePath, const QString& libraryPath, QWebFrame* targetFrame, const bool autorun)
{
return loadJSForDebug(jsFilePath, QString(), Encoding::UTF8, libraryPath, targetFrame, autorun);
}
bool loadJSForDebug(const QString& jsFilePath, const QString &jsFileLanguage, const Encoding& jsFileEnc, const QString& libraryPath, QWebFrame* targetFrame, const bool autorun)
{
QString scriptPath = findScript(jsFilePath, libraryPath);
QString scriptBody = jsFromScriptFile(scriptPath, jsFileLanguage, jsFileEnc);
QString remoteDebuggerHarnessSrc = readResourceFileUtf8(":/remote_debugger_harness.html");
remoteDebuggerHarnessSrc = remoteDebuggerHarnessSrc.arg(scriptBody);
targetFrame->setHtml(remoteDebuggerHarnessSrc);
if (autorun) {
targetFrame->evaluateJavaScript("__run()", QString());
}
return true;
}
QString readResourceFileUtf8(const QString &resourceFilePath)
{
QFile f(resourceFilePath);
f.open(QFile::ReadOnly); //< It's OK to assume this succeed. If it doesn't, we have a bigger problem.
return QString::fromUtf8(f.readAll());
}
// private:
Utils::Utils()
{
// Nothing to do here
}
}; // namespace Utils

View File

@ -32,39 +32,47 @@
#define UTILS_H
#include <QtGlobal>
#include <QtWebKitWidgets/QWebFrame>
#include <QFile>
#include "encoding.h"
class QTemporaryFile;
class QWebFrame;
/**
* Aggregate common utility functions.
* Functions are static methods.
* It's important to notice that, at the moment, this class can't be instantiated by design.
*/
class Utils
{
public:
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
static bool injectJsInFrame(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript = false);
static bool injectJsInFrame(const QString &jsFilePath, const QString &jsFileLanguage, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript = false);
static QString readResourceFileUtf8(const QString &resourceFilePath);
namespace Utils {
static bool loadJSForDebug(const QString &jsFilePath, const QString &jsFileLanguage, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool autorun = false);
static bool loadJSForDebug(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool autorun = false);
static void cleanupFromDebug();
void messageHandler(QtMsgType type,
const QMessageLogContext &context,
const QString &msg);
extern bool printDebugMessages;
static bool printDebugMessages;
bool injectJsInFrame(const QString &jsFilePath,
const QString &libraryPath,
QWebFrame *targetFrame,
const bool startingScript = false);
private:
static QString findScript(const QString &jsFilePath, const QString& libraryPath);
static QString jsFromScriptFile(const QString& scriptPath, const QString& lang, const Encoding& enc);
Utils(); //< This class shouldn't be instantiated
bool injectJsInFrame(const QString &jsFilePath,
const QString &jsFileLanguage,
const Encoding &jsFileEnc,
const QString &libraryPath,
QWebFrame *targetFrame,
const bool startingScript = false);
bool loadJSForDebug(const QString &jsFilePath,
const QString &libraryPath,
QWebFrame *targetFrame,
const bool autorun = false);
bool loadJSForDebug(const QString &jsFilePath,
const QString &jsFileLanguage,
const Encoding &jsFileEnc,
const QString &libraryPath,
QWebFrame *targetFrame,
const bool autorun = false);
QString readResourceFileUtf8(const QString &resourceFilePath);
static QTemporaryFile* m_tempHarness; //< We want to make sure to clean up after ourselves
static QTemporaryFile* m_tempWrapper;
};
#endif // UTILS_H