1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 //
4 // Copyright 2010-2020 wkhtmltopdf authors
5 //
6 // This file is part of wkhtmltopdf.
7 //
8 // wkhtmltopdf is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // wkhtmltopdf is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
20 
21 #ifndef __MULTIPAGELOADER_P_HH__
22 #define __MULTIPAGELOADER_P_HH__
23 
24 #include "multipageloader.hh"
25 #include "tempfile.hh"
26 #include <QAtomicInt>
27 #include <QAuthenticator>
28 #include <QFile>
29 #include <QFileInfo>
30 #include <QNetworkAccessManager>
31 #include <QNetworkCookieJar>
32 #include <QNetworkReply>
33 #include <QWebFrame>
34 
35 #include "dllbegin.inc"
36 namespace wkhtmltopdf {
37 
38 class DLL_LOCAL MyNetworkProxyFactory: public QObject, public QNetworkProxyFactory {
39 	Q_OBJECT
40 private:
41 	QList<QString> bypassHosts;
42 	QList<QNetworkProxy> originalProxy, noProxy;
43 public:
44 	MyNetworkProxyFactory(QNetworkProxy defaultProxy, QList<QString> bypassHosts);
45 	QList<QNetworkProxy> queryProxy (const QNetworkProxyQuery & query);
46 };
47 
48 class DLL_LOCAL MyNetworkAccessManager: public QNetworkAccessManager {
49 	Q_OBJECT
50 private:
51 	bool disposed;
52 	QSet<QString> allowed;
53 	const settings::LoadPage & settings;
54 public:
55 	void dispose();
56 	void allow(QString path);
57 	MyNetworkAccessManager(const settings::LoadPage & s);
58 	QNetworkReply * createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0);
59 signals:
60 	void warning(const QString & text);
61 	void error(const QString & text);
62 };
63 
64 class DLL_LOCAL MultiPageLoaderPrivate;
65 class DLL_LOCAL ResourceObject;
66 
67 class DLL_LOCAL MyQWebPage: public QWebPage {
68 	Q_OBJECT ;
69 private:
70 	ResourceObject & resource;
71 public:
72 	MyQWebPage(ResourceObject & res);
73 	virtual void javaScriptAlert(QWebFrame * frame, const QString & msg);
74 	virtual bool javaScriptConfirm(QWebFrame * frame, const QString & msg);
75 	virtual bool javaScriptPrompt(QWebFrame * frame, const QString & msg, const QString & defaultValue, QString * result);
76 	virtual void javaScriptConsoleMessage(const QString & message, int lineNumber, const QString & sourceID);
77 public slots:
78 	bool shouldInterruptJavaScript();
79 };
80 
81 class DLL_LOCAL ResourceObject: public QObject {
82 	Q_OBJECT
83 private:
84 	MyNetworkAccessManager networkAccessManager;
85 	QUrl url;
86 	int loginTry;
87 	int progress;
88 	bool finished;
89 	bool signalPrint;
90 	MultiPageLoaderPrivate & multiPageLoader;
91 public:
92 	ResourceObject(MultiPageLoaderPrivate & mpl, const QUrl & u, const settings::LoadPage & s);
93 	MyQWebPage webPage;
94 	LoaderObject lo;
95 	int httpErrorCode;
96 	const settings::LoadPage settings;
97 public slots:
98 	void load();
99 	void loadStarted();
100 	void loadProgress(int progress);
101 	void loadFinished(bool ok);
102 	void waitWindowStatus();
103 	void printRequested(QWebFrame * frame);
104 	void loadDone();
105 	void handleAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator);
106 	void warning(const QString & str);
107 	void error(const QString & str);
108 	void sslErrors(QNetworkReply *reply, const QList<QSslError> &);
109 	void amfinished(QNetworkReply * reply);
110 };
111 
112 class DLL_LOCAL MyCookieJar: public QNetworkCookieJar {
113 private:
114 	QList<QNetworkCookie> extraCookies;
115 public:
116 	void clearExtraCookies();
117 	void useCookie(const QUrl & url, const QString & name, const QString & value);
118 	QList<QNetworkCookie> cookiesForUrl(const QUrl & url) const;
119 	void loadFromFile(const QString & path);
120 	void saveToFile(const QString & path);
121 };
122 
123 class DLL_LOCAL MultiPageLoaderPrivate: public QObject {
124 	Q_OBJECT
125 public:
126 	MyCookieJar * cookieJar;
127 
128 	MultiPageLoader & outer;
129 	const settings::LoadGlobal settings;
130 
131 	QList<ResourceObject *> resources;
132 
133 	int loading;
134 	int progressSum;
135 	bool isMainLoader;
136 	bool loadStartedEmitted;
137 	bool hasError;
138 	bool finishedEmitted;
139 	TempFile tempIn;
140 	int dpi;
141 
142 	MultiPageLoaderPrivate(const settings::LoadGlobal & settings, int dpi, MultiPageLoader & o);
143 	~MultiPageLoaderPrivate();
144 	LoaderObject * addResource(const QUrl & url, const settings::LoadPage & settings);
145 	void load();
146 	void clearResources();
147 	void cancel();
148 public slots:
149 	void fail();
150 	void loadDone();
151 };
152 
153 }
154 #include "dllend.inc"
155 #endif //__MULTIPAGELOADER_P_HH__
156