phantomjs/src/phantom.cpp

299 lines
9.0 KiB
C++
Raw Normal View History

2011-04-09 21:34:04 +04:00
/*
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
2011-04-10 09:37:06 +04:00
Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
2011-04-09 21:34:04 +04:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "phantom.h"
#include <QtGui>
#include <QtWebKit>
#include <QDir>
#include <QFileInfo>
#include <QFile>
2010-12-27 07:04:53 +03:00
#include "consts.h"
2011-08-21 11:47:34 +04:00
#include "terminal.h"
#include "utils.h"
#include "webpage.h"
// public:
2010-12-27 07:04:53 +03:00
Phantom::Phantom(QObject *parent)
: QObject(parent)
, m_terminated(false)
2010-12-27 07:04:53 +03:00
, m_returnValue(0)
, m_netAccessMan(0)
2010-12-27 07:04:53 +03:00
{
// Load default configuration
m_config.load();
m_page = new WebPage(this);
m_pages.append(m_page);
2010-12-27 07:04:53 +03:00
bool autoLoadImages = true;
bool pluginsEnabled = false;
bool localAccessRemote = false;
2010-12-27 07:04:53 +03:00
// second argument: script name
QStringList args = QApplication::arguments();
2011-02-13 22:38:09 +03:00
// Skip the first argument, i.e. the application executable (phantomjs).
args.removeFirst();
// Handle all command-line options.
QStringListIterator argIterator(args);
while (argIterator.hasNext()) {
const QString &arg = argIterator.next();
2011-06-06 11:36:31 +04:00
if (arg == "--version") {
m_terminated = true;
2011-08-21 11:47:34 +04:00
Terminal::instance()->cout(QString("%1 (development)").arg(PHANTOMJS_VERSION_STRING));
2011-06-06 11:36:31 +04:00
return;
}
if (arg == "--load-images=yes") {
autoLoadImages = true;
continue;
}
if (arg == "--load-images=no") {
autoLoadImages = false;
continue;
}
if (arg == "--load-plugins=yes") {
pluginsEnabled = true;
continue;
}
if (arg == "--load-plugins=no") {
pluginsEnabled = false;
continue;
}
2011-04-12 18:21:09 +04:00
if (arg == "--disk-cache=yes") {
m_config.setDiskCacheEnabled(true);
2011-04-12 18:21:09 +04:00
continue;
}
if (arg == "--disk-cache=no") {
m_config.setDiskCacheEnabled(false);
2011-04-12 18:21:09 +04:00
continue;
}
if (arg == "--ignore-ssl-errors=yes") {
m_config.setIgnoreSslErrors(true);
continue;
}
if (arg == "--ignore-ssl-errors=no") {
m_config.setIgnoreSslErrors(false);
continue;
}
if (arg == "--local-access-remote=no") {
localAccessRemote = false;
continue;
}
if (arg == "--local-access-remote=yes") {
localAccessRemote = true;
continue;
}
if (arg.startsWith("--proxy=")) {
m_config.setProxy(arg.mid(8).trimmed());
continue;
}
2011-07-19 21:32:42 +04:00
if (arg.startsWith("--cookies=")) {
m_config.setCookieFile(arg.mid(10).trimmed());
2011-07-19 21:32:42 +04:00
continue;
}
if (arg.startsWith("--output-encoding=")) {
m_config.setOutputEncoding(arg.mid(18).trimmed());
continue;
}
if (arg.startsWith("--script-encoding=")) {
m_config.setScriptEncoding(arg.mid(18).trimmed());
continue;
}
if (arg.startsWith("--")) {
2011-08-21 11:47:34 +04:00
Terminal::instance()->cerr(QString("Unknown option '%1'").arg(arg));
m_terminated = true;
return;
} else {
m_scriptFile = arg;
break;
}
}
if (m_scriptFile.isEmpty()) {
Utils::showUsage();
return;
}
if (m_config.proxyHost().isEmpty()) {
QNetworkProxyFactory::setUseSystemConfiguration(true);
} else {
QNetworkProxy proxy(QNetworkProxy::HttpProxy, m_config.proxyHost(), m_config.proxyPort());
QNetworkProxy::setApplicationProxy(proxy);
}
// The remaining arguments are available for the script.
while (argIterator.hasNext()) {
const QString &arg = argIterator.next();
m_args += arg;
}
2010-12-27 07:04:53 +03:00
// Set output encoding
Terminal::instance()->setEncoding(m_config.outputEncoding());
// Set script file encoding
m_scriptFileEnc.setEncoding(m_config.scriptEncoding());
// Provide WebPage with a non-standard Network Access Manager
m_netAccessMan = new NetworkAccessManager(this, m_config.diskCacheEnabled(), m_config.cookieFile(), m_config.ignoreSslErrors());
m_page->setNetworkAccessManager(m_netAccessMan);
connect(m_page, SIGNAL(javaScriptConsoleMessageSent(QString, int, QString)),
SLOT(printConsoleMessage(QString, int, QString)));
m_defaultPageSettings[PAGE_SETTINGS_LOAD_IMAGES] = QVariant::fromValue(autoLoadImages);
m_defaultPageSettings[PAGE_SETTINGS_LOAD_PLUGINS] = QVariant::fromValue(pluginsEnabled);
m_defaultPageSettings[PAGE_SETTINGS_JS_ENABLED] = QVariant::fromValue(true);
m_defaultPageSettings[PAGE_SETTINGS_XSS_AUDITING] = QVariant::fromValue(false);
m_defaultPageSettings[PAGE_SETTINGS_USER_AGENT] = QVariant::fromValue(m_page->userAgent());
m_defaultPageSettings[PAGE_SETTINGS_LOCAL_ACCESS_REMOTE] = QVariant::fromValue(localAccessRemote);
2011-05-28 09:36:34 +04:00
m_page->applySettings(m_defaultPageSettings);
2011-06-18 07:17:58 +04:00
setLibraryPath(QFileInfo(m_scriptFile).dir().absolutePath());
m_page->mainFrame()->addToJavaScriptWindowObject("phantom", this);
m_page->mainFrame()->addToJavaScriptWindowObject("fs", &m_filesystem);
2011-05-26 13:18:09 +04:00
QFile file(":/bootstrap.js");
if (!file.open(QFile::ReadOnly)) {
2011-08-21 11:47:34 +04:00
Terminal::instance()->cerr("Can not bootstrap!");
2011-05-26 13:18:09 +04:00
exit(1);
}
QString bootstrapper = QString::fromUtf8(file.readAll());
file.close();
if (bootstrapper.isEmpty()) {
2011-08-21 11:47:34 +04:00
Terminal::instance()->cerr("Can not bootstrap!");
2011-05-26 13:18:09 +04:00
exit(1);
}
m_page->mainFrame()->evaluateJavaScript(bootstrapper);
2010-12-27 07:04:53 +03:00
}
QStringList Phantom::args() const
2010-12-27 07:04:53 +03:00
{
return m_args;
2010-12-27 07:04:53 +03:00
}
2011-05-28 09:36:34 +04:00
QVariantMap Phantom::defaultPageSettings() const
{
return m_defaultPageSettings;
}
QString Phantom::outputEncoding() const
{
2011-08-21 11:47:34 +04:00
return Terminal::instance()->getEncoding();
}
void Phantom::setOutputEncoding(const QString &encoding)
{
2011-08-21 11:47:34 +04:00
Terminal::instance()->setEncoding(encoding);
}
bool Phantom::execute()
2010-12-27 07:04:53 +03:00
{
if (m_terminated)
return false;
if (m_scriptFile.isEmpty())
return false;
if (!Utils::injectJsInFrame(m_scriptFile, m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), true)) {
m_returnValue = -1;
return false;
}
return !m_terminated;
2010-12-27 07:04:53 +03:00
}
int Phantom::returnValue() const
2010-12-27 07:04:53 +03:00
{
return m_returnValue;
2010-12-27 07:04:53 +03:00
}
QString Phantom::libraryPath() const
{
return m_page->libraryPath();
}
void Phantom::setLibraryPath(const QString &libraryPath)
{
m_page->setLibraryPath(libraryPath);
}
QString Phantom::scriptName() const
{
return QFileInfo(m_scriptFile).fileName();
}
QVariantMap Phantom::version() const
{
QVariantMap result;
result["major"] = PHANTOMJS_VERSION_MAJOR;
result["minor"] = PHANTOMJS_VERSION_MINOR;
result["patch"] = PHANTOMJS_VERSION_PATCH;
return result;
}
// public slots:
QObject *Phantom::createWebPage()
{
WebPage *page = new WebPage(this);
m_pages.append(page);
2011-05-28 09:36:34 +04:00
page->applySettings(m_defaultPageSettings);
page->setNetworkAccessManager(m_netAccessMan);
2011-06-18 07:17:58 +04:00
page->setLibraryPath(QFileInfo(m_scriptFile).dir().absolutePath());
return page;
}
bool Phantom::injectJs(const QString &jsFilePath) {
return Utils::injectJsInFrame(jsFilePath, libraryPath(), m_page->mainFrame());
}
void Phantom::exit(int code)
{
m_terminated = true;
m_returnValue = code;
qDeleteAll(m_pages);
m_pages.clear();
m_page = 0;
QApplication::instance()->exit(code);
2010-12-27 07:04:53 +03:00
}
// private slots:
void Phantom::printConsoleMessage(const QString &message, int lineNumber, const QString &source)
{
QString msg = message;
if (!source.isEmpty())
msg = source + ":" + QString::number(lineNumber) + " " + msg;
2011-08-21 11:47:34 +04:00
Terminal::instance()->cout(msg);
}