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 <QApplication>
#include <QSslSocket> #include <QSslSocket>
#include <QIcon>
int main(int argc, char** argv) int main(int argc, char** argv)
{ {

View File

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

View File

@ -28,91 +28,17 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "utils.h"
#include "consts.h"
#include "terminal.h"
#include <QFile> #include <QFile>
#include <QDebug> #include <QDebug>
#include <QDateTime> #include <QDateTime>
#include <QDir> #include <QDir>
#include <QTemporaryFile> #include <QtWebKitWidgets/QWebFrame>
#include "consts.h" static QString findScript(const QString& jsFilePath, const QString &libraryPath)
#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)
{ {
QString filePath = jsFilePath; QString filePath = jsFilePath;
if (!jsFilePath.isEmpty()) { if (!jsFilePath.isEmpty()) {
@ -130,7 +56,7 @@ QString Utils::findScript(const QString& jsFilePath, const QString &libraryPath)
return QString(); 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); QFile jsFile(scriptPath);
if (jsFile.exists() && jsFile.open(QFile::ReadOnly)) { if (jsFile.exists() && jsFile.open(QFile::ReadOnly)) {
@ -158,31 +84,85 @@ QString Utils::jsFromScriptFile(const QString& scriptPath, const QString& script
} }
} }
namespace Utils {
void bool printDebugMessages = false;
Utils::cleanupFromDebug()
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{ {
if (m_tempHarness) { Q_UNUSED(context);
// Will erase the temp file on disk QDateTime now = QDateTime::currentDateTime();
delete m_tempHarness;
m_tempHarness = 0; switch (type) {
} case QtDebugMsg:
if (m_tempWrapper) { if (printDebugMessages) {
delete m_tempWrapper; fprintf(stderr, "%s [DEBUG] %s\n", qPrintable(now.toString(Qt::ISODate)), qPrintable(msg));
m_tempWrapper = 0; }
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); QFile f(resourceFilePath);
f.open(QFile::ReadOnly); //< It's OK to assume this succeed. If it doesn't, we have a bigger problem. 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()); return QString::fromUtf8(f.readAll());
} }
// private: }; // namespace Utils
Utils::Utils()
{
// Nothing to do here
}

View File

@ -32,39 +32,47 @@
#define UTILS_H #define UTILS_H
#include <QtGlobal> #include <QtGlobal>
#include <QtWebKitWidgets/QWebFrame>
#include <QFile>
#include "encoding.h" #include "encoding.h"
class QTemporaryFile; class QWebFrame;
/** /**
* Aggregate common utility functions. * 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); namespace Utils {
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);
static bool loadJSForDebug(const QString &jsFilePath, const QString &jsFileLanguage, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool autorun = false); void messageHandler(QtMsgType type,
static bool loadJSForDebug(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool autorun = false); const QMessageLogContext &context,
static void cleanupFromDebug(); const QString &msg);
extern bool printDebugMessages;
static bool printDebugMessages; bool injectJsInFrame(const QString &jsFilePath,
const QString &libraryPath,
QWebFrame *targetFrame,
const bool startingScript = false);
private: bool injectJsInFrame(const QString &jsFilePath,
static QString findScript(const QString &jsFilePath, const QString& libraryPath); const QString &jsFileLanguage,
static QString jsFromScriptFile(const QString& scriptPath, const QString& lang, const Encoding& enc); const Encoding &jsFileEnc,
Utils(); //< This class shouldn't be instantiated 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 #endif // UTILS_H