Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit 52e4849

Browse files
author
pixiuPL
authored
Code refactoring (#15264)
* Refactoring /deploy * Refactoring /src #1 * Refactoring /src #2 * Code refactoring #3
1 parent 811593a commit 52e4849

6 files changed

+9
-65
lines changed

src/main.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@
3131
#include <QtPlugin>
3232
#include <QSslSocket>
3333
#include <QWebSettings>
34-
3534
#include "consts.h"
3635
#include "crashdump.h"
3736
#include "phantom.h"
3837

39-
#if defined(Q_OS_LINUX) && defined(QT_STATIC)
38+
if defined(Q_OS_LINUX) && defined(QT_STATIC)
4039
Q_IMPORT_PLUGIN(PhantomIntegrationPlugin);
41-
#endif
40+
endif
4241

4342
static bool g_printDebugMessages;
4443
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
@@ -76,9 +75,7 @@ static int inner_main(int argc, char** argv)
7675

7776
// Registering an alternative Message Handler
7877
qInstallMessageHandler(messageHandler);
79-
8078
QApplication app(argc, argv);
81-
8279
app.setWindowIcon(QIcon(":/phantomjs-icon.png"));
8380
app.setApplicationName("PhantomJS");
8481
app.setOrganizationName("Ofi Labs");
@@ -159,5 +156,4 @@ int main(int argc, char** argv)
159156
print_crash_message();
160157
return 1;
161158
}
162-
#endif
163-
}
159+
#endif}

src/networkaccessmanager.cpp

+1-14
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ NetworkAccessManager::NetworkAccessManager(QObject* parent, const Config* config
180180

181181

182182
connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), SLOT(provideAuthentication(QNetworkReply*, QAuthenticator*)));
183-
184183
connect(&m_replyTracker, SIGNAL(started(QNetworkReply*, int)), this, SLOT(handleStarted(QNetworkReply*, int)));
185184
connect(&m_replyTracker, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)), this, SLOT(handleSslErrors(QNetworkReply*, const QList<QSslError>&)));
186185
connect(&m_replyTracker, SIGNAL(error(QNetworkReply*, int, QNetworkReply::NetworkError)), this, SLOT(handleNetworkError(QNetworkReply*, int)));
@@ -205,7 +204,7 @@ void NetworkAccessManager::prepareSslConfiguration(const Config* config)
205204
break;
206205
}
207206
}
208-
// FIXME: actually object to an invalid setting.
207+
209208
if (!setProtocol) {
210209
m_sslConfiguration.setProtocol(QSsl::SecureProtocols);
211210
}
@@ -318,11 +317,6 @@ void NetworkAccessManager::compileCaptureContentPatterns()
318317
void NetworkAccessManager::setCookieJar(QNetworkCookieJar* cookieJar)
319318
{
320319
QNetworkAccessManager::setCookieJar(cookieJar);
321-
// Remove NetworkAccessManager's ownership of this CookieJar and
322-
// pass it to the PhantomJS Singleton object.
323-
// CookieJar is shared between multiple instances of NetworkAccessManager.
324-
// It shouldn't be deleted when the NetworkAccessManager is deleted, but
325-
// only when close is called on the cookie jar.
326320
cookieJar->setParent(Phantom::instance());
327321
}
328322

@@ -478,15 +472,8 @@ void NetworkAccessManager::handleStarted(QNetworkReply* reply, int requestId)
478472
data["headers"] = headers;
479473
data["requestHeaders"] = requestHeaders;
480474
data["time"] = QDateTime::currentDateTime();
481-
<<<<<<< HEAD
482-
data["body"] = "";
483-
=======
484-
data["fromCache"] = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool();
485-
>>>>>>> f1f3cd7b5aa0815028f4a18a65a1a7b76c41ade0
486-
487475
QWebFrame *frame = qobject_cast<QWebFrame *>(reply->request().originatingObject());
488476
data["frameName"] = frame->frameName();
489-
490477
emit resourceReceived(data);
491478
}
492479

src/networkaccessmanager.h

-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
#ifndef NETWORKACCESSMANAGER_H
3232
#define NETWORKACCESSMANAGER_H
33-
3433
#include <QAuthenticator>
3534
#include <QNetworkAccessManager>
3635
#include <QNetworkReply>
@@ -42,7 +41,6 @@
4241
class Config;
4342
class QNetworkDiskCache;
4443
class QSslConfiguration;
45-
4644
class TimeoutTimer : public QTimer
4745
{
4846
Q_OBJECT
@@ -123,15 +121,13 @@ private slots:
123121
bool shouldCaptureResponse(const QString& url);
124122
void compileCaptureContentPatterns();
125123
void prepareSslConfiguration(const Config* config);
126-
127124
int m_idCounter;
128125
QVariant m_lastHttpStatus;
129126
QNetworkDiskCache* m_networkDiskCache;
130127
QVariantMap m_customHeaders;
131128
QStringList m_captureContentPatterns;
132129
QList<QRegExp> m_compiledCaptureContentPatterns;
133130
QSslConfiguration m_sslConfiguration;
134-
135131
NetworkReplyTracker m_replyTracker;
136132
};
137133

src/networkreplyproxy.cpp

+2-11
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,11 @@ NetworkReplyProxy::NetworkReplyProxy(QObject* parent, QNetworkReply* reply,
6060

6161
QString NetworkReplyProxy::body()
6262
{
63-
//TODO converting data to QString in a special way(without charset encoding),
64-
//similar to File::read method in 'filesystem' module.
65-
QString ret(m_data.size(), ' ');
63+
QString ret(m_data.size(), ' ');
6664
for (int i = 0; i < m_data.size(); ++i) {
6765
ret[i] = m_data.at(i);
6866
}
69-
70-
return ret;
67+
return ret;
7168
}
7269

7370
int NetworkReplyProxy::bodySize()
@@ -120,12 +117,6 @@ void NetworkReplyProxy::ignoreSslErrors()
120117

121118
void NetworkReplyProxy::applyMetaData()
122119
{
123-
/*
124-
We have to store headers and attributes, otherwise
125-
QNetworkReply non-virtual methods (attribute, header, etc.)
126-
would not have data to return.
127-
*/
128-
129120
QList<QByteArray> headers = m_reply->rawHeaderList();
130121
foreach (QByteArray header, headers) {
131122
setRawHeader(header, m_reply->rawHeader(header));

src/networkreplytracker.cpp

+2-13
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,22 @@
3232
#include "networkreplyproxy.h"
3333

3434

35-
NetworkReplyTracker::NetworkReplyTracker(QObject* parent)
36-
: QObject(parent)
35+
NetworkReplyTracker::NetworkReplyTracker(QObject* parent): QObject(parent)
3736
{
38-
// TODO body
37+
// TODO body. Once done, remove TODO marker and test.
3938
}
4039

4140

4241
QNetworkReply* NetworkReplyTracker::trackReply(QNetworkReply* reply, int requestId,
4342
bool shouldCaptureResponseBody)
4443
{
4544
NetworkReplyProxy* proxy = new NetworkReplyProxy(this, reply, shouldCaptureResponseBody);
46-
47-
/*
48-
Tracking link between proxy and proxied reply.
49-
Its possible to get nested reply as a slot argument instead of proxy.
50-
QNetworkAccessManager::createRequest stores original reply internally
51-
and passes it in certain conditions as a signal argument -
52-
authenticationRequire slot, for instance, will receive original reply not the proxy.
53-
*/
5445
m_replies[reply] = proxy;
5546
m_ids[proxy] = requestId;
56-
5747
connect(proxy, SIGNAL(readyRead()), this, SLOT(handleIncomingData()));
5848
connect(proxy, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(handleNetworkError(QNetworkReply::NetworkError)));
5949
connect(proxy, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(handleSslErrors(const QList<QSslError>&)));
6050
connect(proxy, SIGNAL(finished()), SLOT(handleReplyFinished()));
61-
6251
return proxy;
6352
}
6453

src/networkreplytracker.h

+1-16
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,13 @@
3535
#include <QSet>
3636
#include <QNetworkReply>
3737

38-
3938
class QSslError;
4039
class NetworkReplyProxy;
41-
42-
/*
43-
This class tracks replies and converts reply signals
44-
to phantomjs specific ones. It uses NetworkReplyProxy to collect
45-
body of the response and passes body as a signal argument.
46-
*/
4740
class NetworkReplyTracker: public QObject
4841
{
4942
Q_OBJECT
50-
public:
51-
52-
NetworkReplyTracker(QObject* parent = 0);
53-
5443

44+
public: NetworkReplyTracker(QObject* parent = 0);
5545
/*
5646
reply - reply to track
5747
requestId - unique request id, used to distinguis replies internally
@@ -60,25 +50,20 @@ class NetworkReplyTracker: public QObject
6050
QNetworkReply* trackReply(QNetworkReply* reply, int requestId,
6151
bool shouldCaptureResponseBody);
6252

63-
6453
/*
6554
Abort request
6655
status - set custom HTTP status code
6756
statusString - text description of status code
6857
*/
6958
void abort(QNetworkReply* reply, int status, const QString& statusString);
7059

71-
7260
signals:
73-
7461
void started(QNetworkReply* reply, int requestId);
7562
void finished(QNetworkReply* reply, int requestId, int status, const QString& statusText, const QString& body, int bodySize);
7663
void sslErrors(QNetworkReply*, const QList<QSslError>&);
7764
void error(QNetworkReply*, int requestId, QNetworkReply::NetworkError);
7865

79-
8066
private slots:
81-
8267
void handleIncomingData();
8368
void handleReplyFinished();
8469
void handleSslErrors(const QList<QSslError>& errors);

0 commit comments

Comments
 (0)