Permalink
Browse files

WebPageManager is no longer a singleton

  • Loading branch information...
1 parent 33bc195 commit 82f84fbde1f5316c60b1bccdae6c0c414e56508f @mhoran mhoran committed Mar 29, 2012
View
11 src/CommandFactory.cpp
@@ -28,18 +28,19 @@
#include "WindowFocus.h"
#include "GetWindowHandles.h"
#include "GetWindowHandle.h"
+#include "WebPageManager.h"
-CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
- m_page = page;
+CommandFactory::CommandFactory(WebPageManager *manager, QObject *parent) : QObject(parent) {
+ m_manager = manager;
}
Command *CommandFactory::createCommand(const char *name, QStringList &arguments) {
#include "find_command.h"
arguments.clear();
arguments.append(QString(name));
- return new NullCommand(m_page, arguments);
+ return new NullCommand(currentPage(), arguments);
}
-void CommandFactory::changeWindow(WebPage *newPage) {
- m_page = newPage;
+WebPage *CommandFactory::currentPage() {
+ return m_manager->currentPage();
}
View
9 src/CommandFactory.h
@@ -2,18 +2,17 @@
class Command;
class WebPage;
+class WebPageManager;
class CommandFactory : public QObject {
Q_OBJECT
public:
- CommandFactory(WebPage *page, QObject *parent = 0);
+ CommandFactory(WebPageManager *page, QObject *parent = 0);
Command *createCommand(const char *name, QStringList &arguments);
-
- public slots:
- void changeWindow(WebPage *);
+ WebPageManager *m_manager;
private:
- WebPage *m_page;
+ WebPage *currentPage();
};
View
28 src/Connection.cpp
@@ -1,28 +1,30 @@
#include "Connection.h"
#include "WebPage.h"
+#include "WebPageManager.h"
#include "CommandParser.h"
#include "CommandFactory.h"
#include "PageLoadingCommand.h"
#include "Command.h"
#include <QTcpSocket>
-Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
+Connection::Connection(QTcpSocket *socket, WebPageManager *manager, QObject *parent) :
QObject(parent) {
m_socket = socket;
- m_page = page;
- m_commandFactory = new CommandFactory(page, this);
+ m_manager = manager;
+ m_manager->setCurrentPage(m_manager->createPage(this));
+ m_commandFactory = new CommandFactory(m_manager, this);
m_commandParser = new CommandParser(socket, m_commandFactory, this);
m_pageSuccess = true;
m_commandWaiting = false;
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)));
+ connect(currentPage(), SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
}
void Connection::commandReady(Command *command) {
m_queuedCommand = command;
- if (m_page->isLoading())
+ if (currentPage()->isLoading())
m_commandWaiting = true;
else
startCommand();
@@ -31,10 +33,9 @@ void Connection::commandReady(Command *command) {
void Connection::startCommand() {
m_commandWaiting = false;
if (m_pageSuccess) {
- m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_page, this);
+ m_runningCommand = new PageLoadingCommand(m_queuedCommand, currentPage(), this);
connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
connect(m_queuedCommand, SIGNAL(windowChanged(WebPage *)), this, SLOT(changeWindow(WebPage *)));
- connect(m_queuedCommand, SIGNAL(windowChanged(WebPage *)), m_commandFactory, SLOT(changeWindow(WebPage *)));
m_runningCommand->start();
} else {
writePageLoadFailure();
@@ -49,7 +50,7 @@ void Connection::pendingLoadFinished(bool success) {
void Connection::writePageLoadFailure() {
m_pageSuccess = true;
- QString message = m_page->failureString();
+ QString message = currentPage()->failureString();
writeResponse(new Response(false, message));
}
@@ -71,9 +72,12 @@ void Connection::writeResponse(Response *response) {
delete response;
}
-void Connection::changeWindow(WebPage *newPage) {
- disconnect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
- m_page = newPage;
- connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
+void Connection::changeWindow(WebPage *page) {
+ disconnect(currentPage(), SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
+ m_manager->setCurrentPage(page);
+ connect(currentPage(), SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
}
+WebPage *Connection::currentPage() {
+ return m_manager->currentPage();
+}
View
6 src/Connection.h
@@ -8,12 +8,13 @@ class Response;
class CommandParser;
class CommandFactory;
class PageLoadingCommand;
+class WebPageManager;
class Connection : public QObject {
Q_OBJECT
public:
- Connection(QTcpSocket *socket, WebPage *page, QObject *parent = 0);
+ Connection(QTcpSocket *socket, WebPageManager *manager, QObject *parent = 0);
public slots:
void commandReady(Command *command);
@@ -28,11 +29,12 @@ class Connection : public QObject {
QTcpSocket *m_socket;
Command *m_queuedCommand;
- WebPage *m_page;
+ WebPageManager *m_manager;
CommandParser *m_commandParser;
CommandFactory *m_commandFactory;
PageLoadingCommand *m_runningCommand;
bool m_pageSuccess;
bool m_commandWaiting;
+ WebPage *currentPage();
};
View
1 src/GetWindowHandle.cpp
@@ -1,4 +1,5 @@
#include "GetWindowHandle.h"
+#include "WebPage.h"
#include <QStringList>
GetWindowHandle::GetWindowHandle(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
View
3 src/GetWindowHandle.h
@@ -1,5 +1,6 @@
#include "Command.h"
-#include "WebPage.h"
+
+class WebPage;
class GetWindowHandle : public Command {
Q_OBJECT
View
4 src/GetWindowHandles.cpp
@@ -1,13 +1,15 @@
#include "GetWindowHandles.h"
#include "WebPageManager.h"
+#include "CommandFactory.h"
+#include "WebPage.h"
#include <QStringList>
GetWindowHandles::GetWindowHandles(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
}
void GetWindowHandles::start() {
QListIterator<WebPage *> pageIterator =
- WebPageManager::getInstance()->iterator();
+ ((CommandFactory *) parent())->m_manager->iterator();
QString handles = "[";
QStringList stringList;
View
5 src/Server.cpp
@@ -1,12 +1,11 @@
#include "Server.h"
-#include "WebPage.h"
#include "Connection.h"
+#include "WebPageManager.h"
#include <QTcpServer>
Server::Server(QObject *parent) : QObject(parent) {
m_tcp_server = new QTcpServer(this);
- m_page = new WebPage(this);
}
bool Server::start() {
@@ -20,5 +19,5 @@ quint16 Server::server_port() const {
void Server::handleConnection() {
QTcpSocket *socket = m_tcp_server->nextPendingConnection();
- new Connection(socket, m_page, this);
+ new Connection(socket, new WebPageManager(), this);
}
View
2 src/Server.h
@@ -1,7 +1,6 @@
#include <QObject>
class QTcpServer;
-class WebPage;
class Server : public QObject {
Q_OBJECT
@@ -16,6 +15,5 @@ class Server : public QObject {
private:
QTcpServer *m_tcp_server;
- WebPage *m_page;
};
View
7 src/WebPage.cpp
@@ -9,7 +9,7 @@
#include <QWebSettings>
#include <QUuid>
-WebPage::WebPage(QObject *parent) : QWebPage(parent) {
+WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) {
setForwardUnsupportedContent(true);
loadJavascript();
setUserStylesheet();
@@ -27,9 +27,10 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
resetWindowSize();
settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
- WebPageManager::getInstance()->append(this);
m_uuid = QUuid::createUuid().toString();
+
+ m_manager = manager;
}
void WebPage::resetWindowSize() {
@@ -252,7 +253,7 @@ void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
QWebPage *WebPage::createWindow(WebWindowType type) {
Q_UNUSED(type);
- return new WebPage(this);
+ return m_manager->createPage(this);
}
QString WebPage::uuid() {
View
5 src/WebPage.h
@@ -2,11 +2,13 @@
#define _WEBPAGE_H
#include <QtWebKit>
+class WebPageManager;
+
class WebPage : public QWebPage {
Q_OBJECT
public:
- WebPage(QObject *parent = 0);
+ WebPage(WebPageManager *, QObject *parent = 0);
QVariant invokeCapybaraFunction(const char *name, QStringList &arguments);
QVariant invokeCapybaraFunction(QString &name, QStringList &arguments);
QString failureString();
@@ -61,6 +63,7 @@ class WebPage : public QWebPage {
bool m_ignoreSslErrors;
QStringList m_consoleMessages;
QString m_uuid;
+ WebPageManager *m_manager;
};
#endif //_WEBPAGE_H
View
24 src/WebPageManager.cpp
@@ -1,17 +1,10 @@
#include "WebPageManager.h"
-
-WebPageManager *WebPageManager::m_instance = NULL;
+#include "WebPage.h"
+#include <stdio.h>
WebPageManager::WebPageManager() {
}
-WebPageManager *WebPageManager::getInstance() {
- if(!m_instance)
- m_instance = new WebPageManager();
-
- return m_instance;
-}
-
void WebPageManager::append(WebPage *value) {
m_pages.append(value);
}
@@ -20,3 +13,16 @@ QListIterator<WebPage *> WebPageManager::iterator() {
return QListIterator<WebPage *>(m_pages);
}
+void WebPageManager::setCurrentPage(WebPage *page) {
+ m_currentPage = page;
+}
+
+WebPage *WebPageManager::currentPage() {
+ return m_currentPage;
+}
+
+WebPage *WebPageManager::createPage(QObject *parent) {
+ WebPage *page = new WebPage(this, parent);
+ append(page);
+ return page;
+}
View
12 src/WebPageManager.h
@@ -1,18 +1,22 @@
#ifndef _WEBPAGEMANAGER_H
#define _WEBPAGEMANAGER_H
-#include "WebPage.h"
#include <QList>
+#include <QObject>
+
+class WebPage;
class WebPageManager {
public:
- static WebPageManager *getInstance();
+ WebPageManager();
void append(WebPage *value);
QListIterator<WebPage *> iterator();
+ void setCurrentPage(WebPage *);
+ WebPage *currentPage();
+ WebPage *createPage(QObject *);
private:
- WebPageManager();
QList<WebPage *> m_pages;
- static WebPageManager *m_instance;
+ WebPage *m_currentPage;
};
#endif // _WEBPAGEMANAGER_H
View
3 src/WindowFocus.cpp
@@ -1,6 +1,7 @@
#include "WindowFocus.h"
#include "Command.h"
#include "WebPage.h"
+#include "CommandFactory.h"
#include "WebPageManager.h"
WindowFocus::WindowFocus(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
@@ -21,7 +22,7 @@ void WindowFocus::success(WebPage *page) {
void WindowFocus::focusWindow(QString selector) {
QListIterator<WebPage *> pageIterator =
- WebPageManager::getInstance()->iterator();
+ ((CommandFactory *) parent())->m_manager->iterator();
while (pageIterator.hasNext()) {
WebPage *page = pageIterator.next();
View
1 src/WindowFocus.h
@@ -1,5 +1,4 @@
#include "Command.h"
-#include "WebPageManager.h"
class WebPage;
View
2 src/find_command.h
@@ -1,6 +1,6 @@
#define CHECK_COMMAND(expectedName) \
if (strcmp(#expectedName, name) == 0) { \
- return new expectedName(m_page, arguments, this); \
+ return new expectedName(currentPage(), arguments, this); \
}
CHECK_COMMAND(Visit)

0 comments on commit 82f84fb

Please sign in to comment.