1 /************************************************************************** 2 * Otter Browser: Web browser controlled by the user, not vice-versa. 3 * Copyright (C) 2013 - 2019 Michal Dutkiewicz aka Emdek <michal@emdek.pl> 4 * Copyright (C) 2014 Piotr Wójcik <chocimier@tlen.pl> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * 19 **************************************************************************/ 20 21 #ifndef OTTER_NETWORKMANAGERFACTORY_H 22 #define OTTER_NETWORKMANAGERFACTORY_H 23 24 #include "ItemModel.h" 25 26 #include <QtCore/QCoreApplication> 27 #include <QtNetwork/QAuthenticator> 28 #include <QtNetwork/QNetworkReply> 29 #include <QtNetwork/QSslCipher> 30 31 namespace Otter 32 { 33 34 struct ProxyDefinition final 35 { 36 enum ProxyType 37 { 38 NoProxy = 0, 39 SystemProxy, 40 ManualProxy, 41 AutomaticProxy 42 }; 43 44 enum ProtocolType 45 { 46 AnyProtocol = 0, 47 HttpProtocol, 48 HttpsProtocol, 49 FtpProtocol, 50 SocksProtocol 51 }; 52 53 struct ProxyServer final 54 { 55 QString hostName; 56 quint16 port = 8080; 57 ProxyServerfinal::final58 explicit ProxyServer(const QString &hostNameValue, quint16 portValue) 59 { 60 hostName = hostNameValue; 61 port = portValue; 62 } 63 ProxyServerfinal::final64 ProxyServer() 65 { 66 } 67 }; 68 69 QString identifier; 70 QString title; 71 QString path; 72 QStringList children; 73 QStringList exceptions; 74 QHash<ProtocolType, ProxyServer> servers; 75 ProxyType type = SystemProxy; 76 bool isFolder = false; 77 bool usesSystemAuthentication = false; 78 getTitlefinal79 QString getTitle() const 80 { 81 if (title.isEmpty()) 82 { 83 if (identifier == QLatin1String("noProxy")) 84 { 85 return QCoreApplication::translate("proxies", "No Proxy"); 86 } 87 88 if (identifier == QLatin1String("system")) 89 { 90 return QCoreApplication::translate("proxies", "System Configuration"); 91 } 92 } 93 94 return (title.isEmpty() ? QCoreApplication::translate("proxies", "(Untitled)") : title); 95 } 96 isValidfinal97 bool isValid() const 98 { 99 return !identifier.isEmpty(); 100 } 101 }; 102 103 struct UserAgentDefinition final 104 { 105 QString identifier; 106 QString name; 107 QString title; 108 QString value; 109 QStringList children; 110 bool isFolder = false; 111 getTitlefinal112 QString getTitle() const 113 { 114 if (title.isEmpty()) 115 { 116 if (identifier == QLatin1String("default")) 117 { 118 return QCoreApplication::translate("userAgents", "Default User Agent"); 119 } 120 121 if (!name.isEmpty()) 122 { 123 return QCoreApplication::translate("userAgents", "Mask as {name}").replace(QLatin1String("{name}"), QString(name).replace(QLatin1Char('&'), QLatin1String("&&"))); 124 } 125 } 126 127 return (title.isEmpty() ? QCoreApplication::translate("userAgents", "(Untitled)") : title); 128 } 129 isValidfinal130 bool isValid() const 131 { 132 return !identifier.isEmpty(); 133 } 134 }; 135 136 class CookieJar; 137 class NetworkCache; 138 class NetworkManager; 139 class NetworkManagerFactory; 140 class NetworkProxyFactory; 141 142 class ProxiesModel final : public ItemModel 143 { 144 public: 145 enum ItemRole 146 { 147 TitleRole = ItemModel::TitleRole, 148 IdentifierRole = ItemModel::UserRole 149 }; 150 151 explicit ProxiesModel(const QString &selectedProxy, bool isEditor, QObject *parent = nullptr); 152 153 protected: 154 void populateProxies(const QStringList &proxies, QStandardItem *parent, const QString &selectedProxy); 155 156 private: 157 bool m_isEditor; 158 }; 159 160 class UserAgentsModel final : public ItemModel 161 { 162 public: 163 enum ItemRole 164 { 165 TitleRole = ItemModel::TitleRole, 166 IdentifierRole = ItemModel::UserRole, 167 UserAgentRole 168 }; 169 170 explicit UserAgentsModel(const QString &selectedUserAgent, bool isEditor, QObject *parent = nullptr); 171 172 protected: 173 void populateUserAgents(const QStringList &userAgents, QStandardItem *parent, const QString &selectedUserAgent); 174 175 private: 176 bool m_isEditor; 177 }; 178 179 class NetworkManagerFactory final : public QObject 180 { 181 Q_OBJECT 182 Q_ENUMS(DoNotTrackPolicy) 183 184 public: 185 enum DoNotTrackPolicy 186 { 187 SkipTrackPolicy = 0, 188 AllowToTrackPolicy, 189 DoNotAllowToTrackPolicy 190 }; 191 192 static void createInstance(); 193 static void initialize(); 194 static void clearCookies(int period = 0); 195 static void clearCache(int period = 0); 196 static void loadProxies(); 197 static void loadUserAgents(); 198 static void notifyAuthenticated(QAuthenticator *authenticator, bool wasAccepted); 199 static NetworkManagerFactory* getInstance(); 200 static NetworkManager* getNetworkManager(bool isPrivate = false); 201 static NetworkCache* getCache(); 202 static CookieJar* getCookieJar(); 203 static QNetworkReply* createRequest(const QUrl &url, QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, bool isPrivate = false, QIODevice *outgoingData = nullptr); 204 static QString getAcceptLanguage(); 205 static QString getUserAgent(); 206 static QStringList getProxies(); 207 static QStringList getUserAgents(); 208 static QList<QSslCipher> getDefaultCiphers(); 209 static ProxyDefinition getProxy(const QString &identifier); 210 static UserAgentDefinition getUserAgent(const QString &identifier); 211 static DoNotTrackPolicy getDoNotTrackPolicy(); 212 static bool canSendReferrer(); 213 static bool isWorkingOffline(); 214 static bool usesSystemProxyAuthentication(); 215 bool event(QEvent *event) override; 216 217 protected: 218 explicit NetworkManagerFactory(QObject *parent = nullptr); 219 220 static void readProxy(const QJsonValue &value, ProxyDefinition *parent); 221 static void readUserAgent(const QJsonValue &value, UserAgentDefinition *parent); 222 static void updateProxiesOption(); 223 static void updateUserAgentsOption(); 224 225 protected slots: 226 void handleOptionChanged(int identifier, const QVariant &value); 227 228 private: 229 static NetworkManagerFactory *m_instance; 230 static NetworkManager *m_privateNetworkManager; 231 static NetworkManager *m_standardNetworkManager; 232 static NetworkProxyFactory *m_proxyFactory; 233 static NetworkCache *m_cache; 234 static CookieJar *m_cookieJar; 235 static QString m_acceptLanguage; 236 static QMap<QString, ProxyDefinition> m_proxies; 237 static QMap<QString, UserAgentDefinition> m_userAgents; 238 static QList<QSslCipher> m_defaultCiphers; 239 static DoNotTrackPolicy m_doNotTrackPolicy; 240 static bool m_canSendReferrer; 241 static bool m_isInitialized; 242 static bool m_isWorkingOffline; 243 244 signals: 245 void authenticated(QAuthenticator *authenticator, bool wasAccepted); 246 void onlineStateChanged(bool isOnline); 247 248 friend class NetworkManager; 249 }; 250 251 } 252 253 #endif 254