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

143 lines
3.4 KiB
C++
Raw Normal View History

2011-02-18 22:53:06 -05:00
#include "Connection.h"
#include "WebPage.h"
2011-02-18 22:53:06 -05:00
#include "Visit.h"
#include "Find.h"
#include "Command.h"
#include "Reset.h"
#include "Node.h"
2011-02-25 17:53:36 -05:00
#include "Url.h"
2011-02-25 18:04:23 -05:00
#include "Source.h"
2011-02-26 13:02:43 -05:00
#include "Evaluate.h"
2011-02-26 14:03:30 -05:00
#include "Execute.h"
#include "FrameFocus.h"
2011-02-18 22:53:06 -05:00
#include <QTcpSocket>
2011-02-26 13:02:43 -05:00
#include <iostream>
2011-02-18 22:53:06 -05:00
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
m_socket = socket;
m_page = page;
m_command = NULL;
m_expectingDataSize = -1;
m_pageSuccess = true;
m_commandWaiting = false;
2011-02-18 22:53:06 -05:00
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
2011-02-18 22:53:06 -05:00
}
void Connection::checkNext() {
if (m_expectingDataSize == -1) {
if (m_socket->canReadLine()) {
readLine();
checkNext();
}
} else {
if (m_socket->bytesAvailable() >= m_expectingDataSize) {
readDataBlock();
checkNext();
}
2011-02-18 22:53:06 -05:00
}
}
void Connection::readLine() {
char buffer[128];
qint64 lineLength = m_socket->readLine(buffer, 128);
2011-02-18 22:53:06 -05:00
if (lineLength != -1) {
buffer[lineLength - 1] = 0;
processNext(buffer);
}
}
void Connection::readDataBlock() {
char *buffer = new char[m_expectingDataSize + 1];
m_socket->read(buffer, m_expectingDataSize);
buffer[m_expectingDataSize] = 0;
processNext(buffer);
m_expectingDataSize = -1;
delete buffer;
}
void Connection::processNext(const char *data) {
if (m_commandName.isNull()) {
m_commandName = data;
m_argumentsExpected = -1;
} else {
processArgument(data);
2011-02-18 22:53:06 -05:00
}
}
void Connection::processArgument(const char *data) {
if (m_argumentsExpected == -1) {
m_argumentsExpected = QString(data).toInt();
} else if (m_expectingDataSize == -1) {
m_expectingDataSize = QString(data).toInt();
} else {
m_arguments.append(QString::fromUtf8(data));
}
if (m_arguments.length() == m_argumentsExpected) {
if (m_page->isLoading())
m_commandWaiting = true;
else
startCommand();
}
}
void Connection::startCommand() {
m_commandWaiting = false;
if (m_pageSuccess) {
m_command = createCommand(m_commandName.toAscii().constData());
if (m_command) {
connect(m_command,
SIGNAL(finished(Response *)),
this,
SLOT(finishCommand(Response *)));
m_command->start(m_arguments);
} else {
QString failure = QString("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));
}
}
Command *Connection::createCommand(const char *name) {
#include "find_command.h"
return NULL;
}
void Connection::pendingLoadFinished(bool success) {
m_pageSuccess = success;
if (m_commandWaiting)
startCommand();
}
void Connection::finishCommand(Response *response) {
2011-02-18 22:53:06 -05:00
m_command->deleteLater();
m_command = NULL;
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
QByteArray messageUtf8 = response->message().toUtf8();
QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;
m_arguments.clear();
m_commandName = QString();
m_argumentsExpected = -1;
2011-02-18 22:53:06 -05:00
}