netwerk/system/qt/nsQtNetworkManager.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "nsQtNetworkManager.h"
michael@0 6
michael@0 7 #include "nsCOMPtr.h"
michael@0 8 #include "nsThreadUtils.h"
michael@0 9 #include "nsINetworkLinkService.h"
michael@0 10 #include "nsIOService.h"
michael@0 11 #include "nsIObserverService.h"
michael@0 12 #include "nsIOService.h"
michael@0 13
michael@0 14 #include <QHostInfo>
michael@0 15 #include <QHostAddress>
michael@0 16 #include <QTime>
michael@0 17
michael@0 18 nsQtNetworkManager* nsQtNetworkManager::gQtNetworkManager = nullptr;
michael@0 19
michael@0 20 void nsQtNetworkManager::create()
michael@0 21 {
michael@0 22 if (!gQtNetworkManager) {
michael@0 23 gQtNetworkManager = new nsQtNetworkManager();
michael@0 24 connect(gQtNetworkManager, SIGNAL(openConnectionSignal()),
michael@0 25 gQtNetworkManager, SLOT(openSession()),
michael@0 26 Qt::BlockingQueuedConnection);
michael@0 27 connect(&gQtNetworkManager->networkConfigurationManager,
michael@0 28 SIGNAL(onlineStateChanged(bool)), gQtNetworkManager,
michael@0 29 SLOT(onlineStateChanged(bool)));
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 void nsQtNetworkManager::destroy()
michael@0 34 {
michael@0 35 delete gQtNetworkManager;
michael@0 36 gQtNetworkManager = nullptr;
michael@0 37 }
michael@0 38
michael@0 39 nsQtNetworkManager::nsQtNetworkManager(QObject* parent)
michael@0 40 : QObject(parent), networkSession(0)
michael@0 41 {
michael@0 42 mOnline = networkConfigurationManager.isOnline();
michael@0 43 NS_ASSERTION(NS_IsMainThread(), "nsQtNetworkManager can only initiated in Main Thread");
michael@0 44 }
michael@0 45
michael@0 46 nsQtNetworkManager::~nsQtNetworkManager()
michael@0 47 {
michael@0 48 closeSession();
michael@0 49 networkSession->deleteLater();
michael@0 50 }
michael@0 51
michael@0 52 bool
michael@0 53 nsQtNetworkManager::isOnline()
michael@0 54 {
michael@0 55 static bool sForceOnlineUSB = getenv("MOZ_MEEGO_NET_ONLINE") != 0;
michael@0 56 return sForceOnlineUSB || mOnline;
michael@0 57 }
michael@0 58
michael@0 59 void
michael@0 60 nsQtNetworkManager::onlineStateChanged(bool online)
michael@0 61 {
michael@0 62 mOnline = online;
michael@0 63 }
michael@0 64
michael@0 65 /*
michael@0 66 This function is called from different threads, we need to make sure that
michael@0 67 the attempt to create a network connection is done in the mainthread
michael@0 68
michael@0 69 In case this function is called by another thread than the mainthread
michael@0 70 we emit a signal which is connected through "BlockingQueue"-Connection Type.
michael@0 71
michael@0 72 This cause that the current thread is blocked and waiting for the result.
michael@0 73
michael@0 74 Of course, in case this gets called from the mainthread we must not emit the signal,
michael@0 75 but call the slot directly.
michael@0 76 */
michael@0 77
michael@0 78 bool
michael@0 79 nsQtNetworkManager::openConnection(const QString& host)
michael@0 80 {
michael@0 81 // we are already online -> return true.
michael@0 82 if (isOnline()) {
michael@0 83 return true;
michael@0 84 }
michael@0 85
michael@0 86 if (NS_IsMainThread()) {
michael@0 87 openSession();
michael@0 88 } else {
michael@0 89 // jump to mainthread and do the work there
michael@0 90 Q_EMIT openConnectionSignal();
michael@0 91 }
michael@0 92
michael@0 93 // if its claiming its online -> send one resolve request ahead.
michael@0 94 // this is important because on mobile the first request can take up to 10 seconds
michael@0 95 // sending here one will help to avoid trouble and timeouts later
michael@0 96 if (isOnline()) {
michael@0 97 QHostInfo::fromName(host);
michael@0 98 }
michael@0 99
michael@0 100 return isOnline();
michael@0 101 }
michael@0 102
michael@0 103 void
michael@0 104 nsQtNetworkManager::openSession()
michael@0 105 {
michael@0 106 if (mBlockTimer.isActive()) {
michael@0 107 // if openSession is called within 200 ms again, we forget this attempt since
michael@0 108 // its mostlike an automatic connection attempt which was not successful or canceled 200ms ago.
michael@0 109 // we reset the timer and start it again.
michael@0 110
michael@0 111 // As example: Go in firefox mobile into AwesomeView, see that the icons are not always cached and
michael@0 112 // get loaded on the fly. Not having the 200 ms rule here would mean that instantly
michael@0 113 // after the user dismissed the one connection dialog once another
michael@0 114 // would get opened. The user will never be able to close / leave the view until each such attempt is gone through.
michael@0 115
michael@0 116 // Basically 200 ms are working fine, its huge enough for automatic attempts to get covered and small enough to
michael@0 117 // still be able to react on direct user request.
michael@0 118
michael@0 119 mBlockTimer.stop();
michael@0 120 mBlockTimer.setSingleShot(true);
michael@0 121 mBlockTimer.start(200);
michael@0 122 return;
michael@0 123 }
michael@0 124
michael@0 125 if (isOnline()) {
michael@0 126 return;
michael@0 127 }
michael@0 128
michael@0 129 // this only means we did not shutdown before...
michael@0 130 // renew Session every time
michael@0 131 // fix/workaround for prestart bug
michael@0 132 if (networkSession) {
michael@0 133 networkSession->close();
michael@0 134 networkSession->deleteLater();
michael@0 135 }
michael@0 136
michael@0 137 // renew always to react on changed Configurations
michael@0 138 networkConfigurationManager.updateConfigurations();
michael@0 139 // go always with default configuration
michael@0 140 networkConfiguration = networkConfigurationManager.defaultConfiguration();
michael@0 141 networkSession = new QNetworkSession(networkConfiguration);
michael@0 142
michael@0 143 networkSession->open();
michael@0 144 QTime current;
michael@0 145 current.start();
michael@0 146 networkSession->waitForOpened(-1);
michael@0 147
michael@0 148 if (current.elapsed() < 1000) {
michael@0 149 NS_WARNING("Connection Creation was to fast, something is not right.");
michael@0 150 }
michael@0 151
michael@0 152 mBlockTimer.setSingleShot(true);
michael@0 153 mBlockTimer.start(200);
michael@0 154 }
michael@0 155
michael@0 156 void
michael@0 157 nsQtNetworkManager::closeSession()
michael@0 158 {
michael@0 159 if (networkSession) {
michael@0 160 networkSession->close();
michael@0 161 }
michael@0 162 }

mercurial