Move logic to wait for pending page loads into a decorator
This commit is contained in:
parent
312156ea6f
commit
4531f65dc3
|
@ -3,6 +3,7 @@
|
||||||
#include "UnsupportedContentHandler.h"
|
#include "UnsupportedContentHandler.h"
|
||||||
#include "CommandParser.h"
|
#include "CommandParser.h"
|
||||||
#include "CommandFactory.h"
|
#include "CommandFactory.h"
|
||||||
|
#include "PageLoadingCommand.h"
|
||||||
#include "Command.h"
|
#include "Command.h"
|
||||||
|
|
||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
|
@ -13,12 +14,8 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
|
||||||
m_page = page;
|
m_page = page;
|
||||||
m_commandFactory = new CommandFactory(page, this);
|
m_commandFactory = new CommandFactory(page, this);
|
||||||
m_commandParser = new CommandParser(socket, m_commandFactory, this);
|
m_commandParser = new CommandParser(socket, m_commandFactory, this);
|
||||||
m_runningCommand = NULL;
|
|
||||||
m_queuedCommand = NULL;
|
|
||||||
m_pageSuccess = true;
|
m_pageSuccess = true;
|
||||||
m_commandWaiting = false;
|
m_commandWaiting = false;
|
||||||
m_pageLoadingFromCommand = false;
|
|
||||||
m_pendingResponse = NULL;
|
|
||||||
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
|
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
|
||||||
connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
|
connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
|
||||||
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
||||||
|
@ -35,37 +32,18 @@ void Connection::commandReady(Command *command) {
|
||||||
void Connection::startCommand() {
|
void Connection::startCommand() {
|
||||||
m_commandWaiting = false;
|
m_commandWaiting = false;
|
||||||
if (m_pageSuccess) {
|
if (m_pageSuccess) {
|
||||||
m_runningCommand = m_queuedCommand;
|
m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_page, this);
|
||||||
m_queuedCommand = NULL;
|
connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
|
||||||
connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
|
||||||
connect(m_runningCommand,
|
|
||||||
SIGNAL(finished(Response *)),
|
|
||||||
this,
|
|
||||||
SLOT(finishCommand(Response *)));
|
|
||||||
m_runningCommand->start();
|
m_runningCommand->start();
|
||||||
} else {
|
} else {
|
||||||
writePageLoadFailure();
|
writePageLoadFailure();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::pageLoadingFromCommand() {
|
|
||||||
m_pageLoadingFromCommand = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::pendingLoadFinished(bool success) {
|
void Connection::pendingLoadFinished(bool success) {
|
||||||
m_pageSuccess = success;
|
m_pageSuccess = success;
|
||||||
if (m_commandWaiting)
|
if (m_commandWaiting)
|
||||||
startCommand();
|
startCommand();
|
||||||
if (m_pageLoadingFromCommand) {
|
|
||||||
m_pageLoadingFromCommand = false;
|
|
||||||
if (m_pendingResponse) {
|
|
||||||
if (m_pageSuccess) {
|
|
||||||
writeResponse(m_pendingResponse);
|
|
||||||
} else {
|
|
||||||
writePageLoadFailure();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::writePageLoadFailure() {
|
void Connection::writePageLoadFailure() {
|
||||||
|
@ -75,14 +53,7 @@ void Connection::writePageLoadFailure() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::finishCommand(Response *response) {
|
void Connection::finishCommand(Response *response) {
|
||||||
disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
|
||||||
m_runningCommand->deleteLater();
|
m_runningCommand->deleteLater();
|
||||||
m_runningCommand = NULL;
|
|
||||||
delete m_queuedCommand;
|
|
||||||
m_queuedCommand = NULL;
|
|
||||||
if (m_pageLoadingFromCommand)
|
|
||||||
m_pendingResponse = response;
|
|
||||||
else
|
|
||||||
writeResponse(response);
|
writeResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +68,5 @@ void Connection::writeResponse(Response *response) {
|
||||||
m_socket->write(messageLength.toAscii());
|
m_socket->write(messageLength.toAscii());
|
||||||
m_socket->write(messageUtf8);
|
m_socket->write(messageUtf8);
|
||||||
delete response;
|
delete response;
|
||||||
m_pendingResponse = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ class Command;
|
||||||
class Response;
|
class Response;
|
||||||
class CommandParser;
|
class CommandParser;
|
||||||
class CommandFactory;
|
class CommandFactory;
|
||||||
|
class PageLoadingCommand;
|
||||||
|
|
||||||
class Connection : public QObject {
|
class Connection : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -18,7 +19,6 @@ class Connection : public QObject {
|
||||||
void commandReady(Command *command);
|
void commandReady(Command *command);
|
||||||
void finishCommand(Response *response);
|
void finishCommand(Response *response);
|
||||||
void pendingLoadFinished(bool success);
|
void pendingLoadFinished(bool success);
|
||||||
void pageLoadingFromCommand();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void startCommand();
|
void startCommand();
|
||||||
|
@ -26,14 +26,12 @@ class Connection : public QObject {
|
||||||
void writePageLoadFailure();
|
void writePageLoadFailure();
|
||||||
|
|
||||||
QTcpSocket *m_socket;
|
QTcpSocket *m_socket;
|
||||||
Command *m_runningCommand;
|
|
||||||
Command *m_queuedCommand;
|
Command *m_queuedCommand;
|
||||||
WebPage *m_page;
|
WebPage *m_page;
|
||||||
CommandParser *m_commandParser;
|
CommandParser *m_commandParser;
|
||||||
CommandFactory *m_commandFactory;
|
CommandFactory *m_commandFactory;
|
||||||
|
PageLoadingCommand *m_runningCommand;
|
||||||
bool m_pageSuccess;
|
bool m_pageSuccess;
|
||||||
bool m_commandWaiting;
|
bool m_commandWaiting;
|
||||||
bool m_pageLoadingFromCommand;
|
|
||||||
Response *m_pendingResponse;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
#include "PageLoadingCommand.h"
|
||||||
|
#include "Command.h"
|
||||||
|
#include "WebPage.h"
|
||||||
|
|
||||||
|
PageLoadingCommand::PageLoadingCommand(Command *command, WebPage *page, QObject *parent) : QObject(parent) {
|
||||||
|
m_page = page;
|
||||||
|
m_command = command;
|
||||||
|
m_pageLoadingFromCommand = false;
|
||||||
|
m_pageSuccess = true;
|
||||||
|
m_pendingResponse = NULL;
|
||||||
|
connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
||||||
|
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageLoadingCommand::start() {
|
||||||
|
connect(m_command, SIGNAL(finished(Response *)), this, SLOT(commandFinished(Response *)));
|
||||||
|
m_command->start();
|
||||||
|
};
|
||||||
|
|
||||||
|
void PageLoadingCommand::pendingLoadFinished(bool success) {
|
||||||
|
m_pageSuccess = success;
|
||||||
|
if (m_pageLoadingFromCommand) {
|
||||||
|
m_pageLoadingFromCommand = false;
|
||||||
|
if (m_pendingResponse) {
|
||||||
|
if (m_pageSuccess) {
|
||||||
|
emit finished(m_pendingResponse);
|
||||||
|
} else {
|
||||||
|
QString message = m_page->failureString();
|
||||||
|
emit finished(new Response(false, message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageLoadingCommand::pageLoadingFromCommand() {
|
||||||
|
m_pageLoadingFromCommand = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PageLoadingCommand::commandFinished(Response *response) {
|
||||||
|
disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
||||||
|
m_command->deleteLater();
|
||||||
|
if (m_pageLoadingFromCommand)
|
||||||
|
m_pendingResponse = response;
|
||||||
|
else
|
||||||
|
emit finished(response);
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
#include <QObject>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
|
class Command;
|
||||||
|
class Response;
|
||||||
|
class WebPage;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Decorates a Command by deferring the finished() signal until any pending
|
||||||
|
* page loads are complete.
|
||||||
|
*
|
||||||
|
* If a Command starts a page load, no signal will be emitted until the page
|
||||||
|
* load is finished.
|
||||||
|
*
|
||||||
|
* If a pending page load fails, the command's response will be discarded and a
|
||||||
|
* failure response will be emitted instead.
|
||||||
|
*/
|
||||||
|
class PageLoadingCommand : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
PageLoadingCommand(Command *command, WebPage *page, QObject *parent = 0);
|
||||||
|
void start();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void pageLoadingFromCommand();
|
||||||
|
void pendingLoadFinished(bool success);
|
||||||
|
void commandFinished(Response *response);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void finished(Response *response);
|
||||||
|
|
||||||
|
private:
|
||||||
|
WebPage *m_page;
|
||||||
|
Command *m_command;
|
||||||
|
Response *m_pendingResponse;
|
||||||
|
bool m_pageSuccess;
|
||||||
|
bool m_pageLoadingFromCommand;
|
||||||
|
};
|
||||||
|
|
|
@ -35,6 +35,7 @@ HEADERS = \
|
||||||
CommandFactory.h \
|
CommandFactory.h \
|
||||||
SetProxy.h \
|
SetProxy.h \
|
||||||
NullCommand.h \
|
NullCommand.h \
|
||||||
|
PageLoadingCommand.h \
|
||||||
|
|
||||||
SOURCES = \
|
SOURCES = \
|
||||||
CurrentUrl.cpp \
|
CurrentUrl.cpp \
|
||||||
|
@ -71,6 +72,7 @@ SOURCES = \
|
||||||
CommandFactory.cpp \
|
CommandFactory.cpp \
|
||||||
SetProxy.cpp \
|
SetProxy.cpp \
|
||||||
NullCommand.cpp \
|
NullCommand.cpp \
|
||||||
|
PageLoadingCommand.cpp \
|
||||||
|
|
||||||
RESOURCES = webkit_server.qrc
|
RESOURCES = webkit_server.qrc
|
||||||
QT += network webkit
|
QT += network webkit
|
||||||
|
|
Loading…
Reference in New Issue