capybara-webkit/src/Connection.cpp

101 lines
2.9 KiB
C++
Raw Normal View History

2011-02-19 03:53:06 +00:00
#include "Connection.h"
#include "WebPage.h"
#include "UnsupportedContentHandler.h"
2011-10-14 15:22:24 +00:00
#include "CommandParser.h"
#include "CommandFactory.h"
2011-02-19 03:53:06 +00:00
#include "Command.h"
#include <QTcpSocket>
2011-02-26 18:02:43 +00:00
#include <iostream>
2011-02-19 03:53:06 +00:00
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
m_socket = socket;
m_page = page;
2011-10-14 15:22:24 +00:00
m_commandParser = new CommandParser(socket, this);
m_commandFactory = new CommandFactory(page, this);
2011-02-19 03:53:06 +00:00
m_command = NULL;
m_pageSuccess = true;
m_commandWaiting = false;
m_pageLoadingFromCommand = false;
m_pendingResponse = NULL;
2011-10-14 15:22:24 +00:00
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
connect(m_commandParser, SIGNAL(commandReady(QString, QStringList)), this, SLOT(commandReady(QString, QStringList)));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
2011-02-19 03:53:06 +00:00
}
2011-10-14 15:22:24 +00:00
void Connection::commandReady(QString commandName, QStringList arguments) {
m_commandName = commandName;
m_arguments = arguments;
2011-10-14 15:22:24 +00:00
if (m_page->isLoading())
m_commandWaiting = true;
else
startCommand();
}
void Connection::startCommand() {
m_commandWaiting = false;
if (m_pageSuccess) {
2011-10-14 15:22:24 +00:00
m_command = m_commandFactory->createCommand(m_commandName.toAscii().constData());
if (m_command) {
connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
connect(m_command,
SIGNAL(finished(Response *)),
this,
SLOT(finishCommand(Response *)));
m_command->start(m_arguments);
} else {
QString failure = QString("[Capybara WebKit] Unknown command: ") + m_commandName + "\n";
writeResponse(new Response(false, failure));
}
m_commandName = QString();
} else {
m_pageSuccess = true;
QString message = m_page->failureString();
writeResponse(new Response(false, message));
}
}
void Connection::pageLoadingFromCommand() {
m_pageLoadingFromCommand = true;
}
void Connection::pendingLoadFinished(bool success) {
m_pageSuccess = success;
if (m_commandWaiting)
startCommand();
if (m_pageLoadingFromCommand) {
m_pageLoadingFromCommand = false;
if (m_pendingResponse) {
writeResponse(m_pendingResponse);
m_pendingResponse = NULL;
}
}
}
void Connection::finishCommand(Response *response) {
disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
2011-02-19 03:53:06 +00:00
m_command->deleteLater();
m_command = NULL;
if (m_pageLoadingFromCommand)
m_pendingResponse = response;
else
writeResponse(response);
2011-02-26 19:03:30 +00:00
}
void Connection::writeResponse(Response *response) {
if (response->isSuccess())
2011-02-19 03:53:06 +00:00
m_socket->write("ok\n");
2011-02-26 19:03:30 +00:00
else
2011-02-19 03:53:06 +00:00
m_socket->write("failure\n");
2011-02-26 19:03:30 +00:00
QByteArray messageUtf8 = response->message().toUtf8();
QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;
2011-02-19 03:53:06 +00:00
}