capybara-webkit/src/Connection.cpp

103 lines
2.8 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>
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_commandFactory = new CommandFactory(page, this);
m_commandParser = new CommandParser(socket, m_commandFactory, this);
m_runningCommand = NULL;
m_queuedCommand = 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(Command *)), this, SLOT(commandReady(Command *)));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
2011-02-19 03:53:06 +00:00
}
void Connection::commandReady(Command *command) {
m_queuedCommand = command;
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) {
m_runningCommand = m_queuedCommand;
m_queuedCommand = NULL;
connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
connect(m_runningCommand,
SIGNAL(finished(Response *)),
this,
SLOT(finishCommand(Response *)));
m_runningCommand->start();
} else {
2012-03-21 23:11:50 +00:00
writePageLoadFailure();
}
}
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) {
2012-03-16 21:53:36 +00:00
if (m_pageSuccess) {
writeResponse(m_pendingResponse);
} else {
2012-03-21 23:11:50 +00:00
writePageLoadFailure();
2012-03-16 21:53:36 +00:00
}
}
}
}
2012-03-21 23:11:50 +00:00
void Connection::writePageLoadFailure() {
2012-03-16 21:53:36 +00:00
m_pageSuccess = true;
QString message = m_page->failureString();
writeResponse(new Response(false, message));
}
void Connection::finishCommand(Response *response) {
disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
m_runningCommand->deleteLater();
m_runningCommand = NULL;
delete m_queuedCommand;
m_queuedCommand = 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;
2012-03-16 21:53:36 +00:00
m_pendingResponse = NULL;
2011-02-19 03:53:06 +00:00
}