Skip to content
This repository
Browse code

Move logic to wait for pending page loads into a decorator

  • Loading branch information...
commit 4531f65dc3b47979b6bba9b459b75d0302f30b7e 1 parent 312156e
Joe Ferris authored March 23, 2012
38  src/Connection.cpp
@@ -3,6 +3,7 @@
3 3
 #include "UnsupportedContentHandler.h"
4 4
 #include "CommandParser.h"
5 5
 #include "CommandFactory.h"
  6
+#include "PageLoadingCommand.h"
6 7
 #include "Command.h"
7 8
 
8 9
 #include <QTcpSocket>
@@ -13,12 +14,8 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
13 14
   m_page = page;
14 15
   m_commandFactory = new CommandFactory(page, this);
15 16
   m_commandParser = new CommandParser(socket, m_commandFactory, this);
16  
-  m_runningCommand = NULL;
17  
-  m_queuedCommand = NULL;
18 17
   m_pageSuccess = true;
19 18
   m_commandWaiting = false;
20  
-  m_pageLoadingFromCommand = false;
21  
-  m_pendingResponse = NULL;
22 19
   connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
23 20
   connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
24 21
   connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
@@ -35,37 +32,18 @@ void Connection::commandReady(Command *command) {
35 32
 void Connection::startCommand() {
36 33
   m_commandWaiting = false;
37 34
   if (m_pageSuccess) {
38  
-    m_runningCommand = m_queuedCommand;
39  
-    m_queuedCommand = NULL;
40  
-    connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
41  
-    connect(m_runningCommand,
42  
-            SIGNAL(finished(Response *)),
43  
-            this,
44  
-            SLOT(finishCommand(Response *)));
  35
+    m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_page, this);
  36
+    connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
45 37
     m_runningCommand->start();
46 38
   } else {
47 39
     writePageLoadFailure();
48 40
   }
49 41
 }
50 42
 
51  
-void Connection::pageLoadingFromCommand() {
52  
-  m_pageLoadingFromCommand = true;
53  
-}
54  
-
55 43
 void Connection::pendingLoadFinished(bool success) {
56 44
   m_pageSuccess = success;
57 45
   if (m_commandWaiting)
58 46
     startCommand();
59  
-  if (m_pageLoadingFromCommand) {
60  
-    m_pageLoadingFromCommand = false;
61  
-    if (m_pendingResponse) {
62  
-      if (m_pageSuccess) {
63  
-        writeResponse(m_pendingResponse);
64  
-      } else {
65  
-        writePageLoadFailure();
66  
-      }
67  
-    }
68  
-  }
69 47
 }
70 48
 
71 49
 void Connection::writePageLoadFailure() {
@@ -75,15 +53,8 @@ void Connection::writePageLoadFailure() {
75 53
 }
76 54
 
77 55
 void Connection::finishCommand(Response *response) {
78  
-  disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
79 56
   m_runningCommand->deleteLater();
80  
-  m_runningCommand = NULL;
81  
-  delete m_queuedCommand;
82  
-  m_queuedCommand = NULL;
83  
-  if (m_pageLoadingFromCommand)
84  
-    m_pendingResponse = response;
85  
-  else
86  
-    writeResponse(response);
  57
+  writeResponse(response);
87 58
 }
88 59
 
89 60
 void Connection::writeResponse(Response *response) {
@@ -97,6 +68,5 @@ void Connection::writeResponse(Response *response) {
97 68
   m_socket->write(messageLength.toAscii());
98 69
   m_socket->write(messageUtf8);
99 70
   delete response;
100  
-  m_pendingResponse = NULL;
101 71
 }
102 72
 
6  src/Connection.h
@@ -7,6 +7,7 @@ class Command;
7 7
 class Response;
8 8
 class CommandParser;
9 9
 class CommandFactory;
  10
+class PageLoadingCommand;
10 11
 
11 12
 class Connection : public QObject {
12 13
   Q_OBJECT
@@ -18,7 +19,6 @@ class Connection : public QObject {
18 19
     void commandReady(Command *command);
19 20
     void finishCommand(Response *response);
20 21
     void pendingLoadFinished(bool success);
21  
-    void pageLoadingFromCommand();
22 22
 
23 23
   private:
24 24
     void startCommand();
@@ -26,14 +26,12 @@ class Connection : public QObject {
26 26
     void writePageLoadFailure();
27 27
 
28 28
     QTcpSocket *m_socket;
29  
-    Command *m_runningCommand;
30 29
     Command *m_queuedCommand;
31 30
     WebPage *m_page;
32 31
     CommandParser *m_commandParser;
33 32
     CommandFactory *m_commandFactory;
  33
+    PageLoadingCommand *m_runningCommand;
34 34
     bool m_pageSuccess;
35 35
     bool m_commandWaiting;
36  
-    bool m_pageLoadingFromCommand;
37  
-    Response *m_pendingResponse;
38 36
 };
39 37
 
46  src/PageLoadingCommand.cpp
... ...
@@ -0,0 +1,46 @@
  1
+#include "PageLoadingCommand.h"
  2
+#include "Command.h"
  3
+#include "WebPage.h"
  4
+
  5
+PageLoadingCommand::PageLoadingCommand(Command *command, WebPage *page, QObject *parent) : QObject(parent) {
  6
+  m_page = page;
  7
+  m_command = command;
  8
+  m_pageLoadingFromCommand = false;
  9
+  m_pageSuccess = true;
  10
+  m_pendingResponse = NULL;
  11
+  connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
  12
+  connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
  13
+}
  14
+
  15
+void PageLoadingCommand::start() {
  16
+  connect(m_command, SIGNAL(finished(Response *)), this, SLOT(commandFinished(Response *)));
  17
+  m_command->start();
  18
+};
  19
+
  20
+void PageLoadingCommand::pendingLoadFinished(bool success) {
  21
+  m_pageSuccess = success;
  22
+  if (m_pageLoadingFromCommand) {
  23
+    m_pageLoadingFromCommand = false;
  24
+    if (m_pendingResponse) {
  25
+      if (m_pageSuccess) {
  26
+        emit finished(m_pendingResponse);
  27
+      } else {
  28
+        QString message = m_page->failureString();
  29
+        emit finished(new Response(false, message));
  30
+      }
  31
+    }
  32
+  }
  33
+}
  34
+
  35
+void PageLoadingCommand::pageLoadingFromCommand() {
  36
+  m_pageLoadingFromCommand = true;
  37
+}
  38
+
  39
+void PageLoadingCommand::commandFinished(Response *response) {
  40
+  disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
  41
+  m_command->deleteLater();
  42
+  if (m_pageLoadingFromCommand)
  43
+    m_pendingResponse = response;
  44
+  else
  45
+    emit finished(response);
  46
+}
40  src/PageLoadingCommand.h
... ...
@@ -0,0 +1,40 @@
  1
+#include <QObject>
  2
+#include <QStringList>
  3
+
  4
+class Command;
  5
+class Response;
  6
+class WebPage;
  7
+
  8
+/*
  9
+ * Decorates a Command by deferring the finished() signal until any pending
  10
+ * page loads are complete.
  11
+ *
  12
+ * If a Command starts a page load, no signal will be emitted until the page
  13
+ * load is finished.
  14
+ *
  15
+ * If a pending page load fails, the command's response will be discarded and a
  16
+ * failure response will be emitted instead.
  17
+ */
  18
+class PageLoadingCommand : public QObject {
  19
+  Q_OBJECT
  20
+
  21
+  public:
  22
+    PageLoadingCommand(Command *command, WebPage *page, QObject *parent = 0);
  23
+    void start();
  24
+
  25
+  public slots:
  26
+    void pageLoadingFromCommand();
  27
+    void pendingLoadFinished(bool success);
  28
+    void commandFinished(Response *response);
  29
+
  30
+  signals:
  31
+    void finished(Response *response);
  32
+
  33
+  private:
  34
+    WebPage *m_page;
  35
+    Command *m_command;
  36
+    Response *m_pendingResponse;
  37
+    bool m_pageSuccess;
  38
+    bool m_pageLoadingFromCommand;
  39
+};
  40
+
2  src/webkit_server.pro
@@ -35,6 +35,7 @@ HEADERS = \
35 35
   CommandFactory.h \
36 36
   SetProxy.h \
37 37
   NullCommand.h \
  38
+  PageLoadingCommand.h \
38 39
 
39 40
 SOURCES = \
40 41
   CurrentUrl.cpp \
@@ -71,6 +72,7 @@ SOURCES = \
71 72
   CommandFactory.cpp \
72 73
   SetProxy.cpp \
73 74
   NullCommand.cpp \
  75
+  PageLoadingCommand.cpp \
74 76
 
75 77
 RESOURCES = webkit_server.qrc
76 78
 QT += network webkit

0 notes on commit 4531f65

Please sign in to comment.
Something went wrong with that request. Please try again.