1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00
capybara-webkit/src/Connection.cpp

83 lines
2.4 KiB
C++
Raw Normal View History

2011-02-18 22:53:06 -05:00
#include "Connection.h"
#include "WebPage.h"
#include "WebPageManager.h"
2011-10-14 11:22:24 -04:00
#include "CommandParser.h"
#include "CommandFactory.h"
#include "PageLoadingCommand.h"
#include "SocketCommand.h"
2011-02-18 22:53:06 -05:00
#include <QTcpSocket>
Connection::Connection(QTcpSocket *socket, WebPageManager *manager, QObject *parent) :
2011-02-18 22:53:06 -05:00
QObject(parent) {
m_socket = socket;
m_manager = manager;
m_commandFactory = new CommandFactory(m_manager, this);
m_commandParser = new CommandParser(socket, m_commandFactory, this);
m_pageSuccess = true;
m_commandWaiting = false;
2011-10-14 11:22:24 -04:00
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
connect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
2011-02-18 22:53:06 -05:00
}
void Connection::commandReady(Command *command) {
m_queuedCommand = command;
m_manager->logger() << "Received" << command->toString();
if (m_manager->isLoading()) {
m_manager->logger() << command->toString() << "waiting for load to finish";
2011-10-14 11:22:24 -04:00
m_commandWaiting = true;
} else {
2011-10-14 11:22:24 -04:00
startCommand();
}
}
void Connection::startCommand() {
m_commandWaiting = false;
if (m_pageSuccess) {
2012-05-14 21:59:28 -04:00
m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_manager, this);
connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
m_runningCommand->start();
} else {
2012-03-21 19:11:50 -04:00
writePageLoadFailure();
}
}
void Connection::pendingLoadFinished(bool success) {
m_pageSuccess = m_pageSuccess && success;
if (m_commandWaiting) {
startCommand();
}
}
2012-03-21 19:11:50 -04:00
void Connection::writePageLoadFailure() {
2012-03-16 17:53:36 -04:00
m_pageSuccess = true;
QString message = currentPage()->failureString();
2012-03-16 17:53:36 -04:00
writeResponse(new Response(false, message));
}
void Connection::finishCommand(Response *response) {
m_pageSuccess = true;
m_runningCommand->deleteLater();
writeResponse(response);
2011-02-26 14:03:30 -05:00
}
void Connection::writeResponse(Response *response) {
if (response->isSuccess())
2011-02-18 22:53:06 -05:00
m_socket->write("ok\n");
2011-02-26 14:03:30 -05:00
else
2011-02-18 22:53:06 -05:00
m_socket->write("failure\n");
2011-02-26 14:03:30 -05:00
m_manager->logger() << "Wrote response" << response->isSuccess() << response->message();
QByteArray messageUtf8 = response->message();
QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;
2011-02-18 22:53:06 -05:00
}
WebPage *Connection::currentPage() {
return m_manager->currentPage();
}