Move URL utilities from ProjectExplorer to Utils

This way we can access them from the qmldebug library.

Change-Id: I90ba80228f44a9d5ea825ad59f4bd1572969980e
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Ulf Hermann
2017-09-29 12:41:53 +02:00
parent 2f9d82791e
commit fb370f460d
12 changed files with 144 additions and 72 deletions

66
src/libs/utils/url.cpp Normal file
View File

@@ -0,0 +1,66 @@
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "url.h"
#include "temporaryfile.h"
#include <QHostAddress>
#include <QTcpServer>
namespace Utils {
QUrl urlFromLocalHostAndFreePort()
{
QUrl serverUrl;
QTcpServer server;
serverUrl.setScheme(urlTcpScheme());
if (server.listen(QHostAddress::LocalHost) || server.listen(QHostAddress::LocalHostIPv6)) {
serverUrl.setHost(server.serverAddress().toString());
serverUrl.setPort(server.serverPort());
}
return serverUrl;
}
QUrl urlFromLocalSocket()
{
QUrl serverUrl;
serverUrl.setScheme(urlSocketScheme());
Utils::TemporaryFile file("qtcreator-freesocket");
if (file.open())
serverUrl.setPath(file.fileName());
return serverUrl;
}
QString urlSocketScheme()
{
return QString("socket");
}
QString urlTcpScheme()
{
return QString("tcp");
}
}

38
src/libs/utils/url.h Normal file
View File

@@ -0,0 +1,38 @@
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "utils_global.h"
#include <QUrl>
namespace Utils {
QTCREATOR_UTILS_EXPORT QUrl urlFromLocalHostAndFreePort();
QTCREATOR_UTILS_EXPORT QUrl urlFromLocalSocket();
QTCREATOR_UTILS_EXPORT QString urlSocketScheme();
QTCREATOR_UTILS_EXPORT QString urlTcpScheme();
}

View File

@@ -118,7 +118,8 @@ SOURCES += \
$$PWD/guard.cpp \ $$PWD/guard.cpp \
$$PWD/highlightingitemdelegate.cpp \ $$PWD/highlightingitemdelegate.cpp \
$$PWD/camelhumpmatcher.cpp \ $$PWD/camelhumpmatcher.cpp \
$$PWD/textutils.cpp $$PWD/textutils.cpp \
$$PWD/url.cpp
win32:SOURCES += $$PWD/consoleprocess_win.cpp win32:SOURCES += $$PWD/consoleprocess_win.cpp
else:SOURCES += $$PWD/consoleprocess_unix.cpp else:SOURCES += $$PWD/consoleprocess_unix.cpp
@@ -249,7 +250,8 @@ HEADERS += \
$$PWD/qtcfallthrough.h \ $$PWD/qtcfallthrough.h \
$$PWD/highlightingitemdelegate.h \ $$PWD/highlightingitemdelegate.h \
$$PWD/camelhumpmatcher.h \ $$PWD/camelhumpmatcher.h \
$$PWD/textutils.h $$PWD/textutils.h \
$$PWD/url.h
FORMS += $$PWD/filewizardpage.ui \ FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \ $$PWD/projectintropage.ui \

View File

@@ -44,6 +44,7 @@
#include <utils/runextensions.h> #include <utils/runextensions.h>
#include <utils/synchronousprocess.h> #include <utils/synchronousprocess.h>
#include <utils/temporaryfile.h> #include <utils/temporaryfile.h>
#include <utils/url.h>
#include <chrono> #include <chrono>
#include <memory> #include <memory>
@@ -293,7 +294,7 @@ AndroidRunnerWorker::AndroidRunnerWorker(RunControl *runControl, const AndroidRu
QTC_ASSERT(server.listen(QHostAddress::LocalHost) QTC_ASSERT(server.listen(QHostAddress::LocalHost)
|| server.listen(QHostAddress::LocalHostIPv6), || server.listen(QHostAddress::LocalHostIPv6),
qDebug() << tr("No free ports available on host for QML debugging.")); qDebug() << tr("No free ports available on host for QML debugging."));
m_qmlServer.setScheme(urlTcpScheme()); m_qmlServer.setScheme(Utils::urlTcpScheme());
m_qmlServer.setHost(server.serverAddress().toString()); m_qmlServer.setHost(server.serverAddress().toString());
m_qmlServer.setPort(server.serverPort()); m_qmlServer.setPort(server.serverPort());
} }

View File

@@ -55,6 +55,7 @@
#include <utils/portlist.h> #include <utils/portlist.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <utils/url.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
@@ -643,7 +644,7 @@ bool DebuggerRunTool::fixupParameters()
if (rp.isQmlDebugging) { if (rp.isQmlDebugging) {
if (device() && device()->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE) { if (device() && device()->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE) {
if (rp.qmlServer.port() <= 0) { if (rp.qmlServer.port() <= 0) {
rp.qmlServer = ProjectExplorer::urlFromLocalHostAndFreePort(); rp.qmlServer = Utils::urlFromLocalHostAndFreePort();
if (rp.qmlServer.port() <= 0) { if (rp.qmlServer.port() <= 0) {
reportFailure(DebuggerPlugin::tr("Not enough free ports for QML debugging.")); reportFailure(DebuggerPlugin::tr("Not enough free ports for QML debugging."));
return false; return false;

View File

@@ -25,10 +25,6 @@
#include "runnables.h" #include "runnables.h"
#include <QTcpServer>
#include <utils/temporaryfile.h>
namespace ProjectExplorer { namespace ProjectExplorer {
bool operator==(const StandardRunnable &r1, const StandardRunnable &r2) bool operator==(const StandardRunnable &r1, const StandardRunnable &r2)
@@ -41,37 +37,4 @@ bool operator==(const StandardRunnable &r1, const StandardRunnable &r2)
void *StandardRunnable::staticTypeId = &StandardRunnable::staticTypeId; void *StandardRunnable::staticTypeId = &StandardRunnable::staticTypeId;
QUrl urlFromLocalHostAndFreePort()
{
QUrl serverUrl;
QTcpServer server;
serverUrl.setScheme(urlTcpScheme());
if (server.listen(QHostAddress::LocalHost) || server.listen(QHostAddress::LocalHostIPv6)) {
serverUrl.setHost(server.serverAddress().toString());
serverUrl.setPort(server.serverPort());
}
return serverUrl;
}
QUrl urlFromLocalSocket()
{
QUrl serverUrl;
serverUrl.setScheme(urlSocketScheme());
Utils::TemporaryFile file("qmlprofiler-freesocket");
if (file.open())
serverUrl.setPath(file.fileName());
return serverUrl;
}
QString urlSocketScheme()
{
return QString("socket");
}
QString urlTcpScheme()
{
return QString("tcp");
}
} // namespace ProjectExplorer } // namespace ProjectExplorer

View File

@@ -33,7 +33,6 @@
#include <utils/environment.h> #include <utils/environment.h>
#include <QDir> #include <QDir>
#include <QUrl>
namespace ProjectExplorer { namespace ProjectExplorer {
@@ -53,9 +52,4 @@ public:
PROJECTEXPLORER_EXPORT bool operator==(const StandardRunnable &r1, const StandardRunnable &r2); PROJECTEXPLORER_EXPORT bool operator==(const StandardRunnable &r1, const StandardRunnable &r2);
PROJECTEXPLORER_EXPORT QUrl urlFromLocalHostAndFreePort();
PROJECTEXPLORER_EXPORT QUrl urlFromLocalSocket();
PROJECTEXPLORER_EXPORT QString urlSocketScheme();
PROJECTEXPLORER_EXPORT QString urlTcpScheme();
} // namespace ProjectExplorer } // namespace ProjectExplorer

View File

@@ -31,7 +31,7 @@
#include <qmldebug/qmldebugconnection.h> #include <qmldebug/qmldebugconnection.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <projectexplorer/runnables.h> #include <utils/url.h>
namespace QmlProfiler { namespace QmlProfiler {
namespace Internal { namespace Internal {
@@ -75,9 +75,9 @@ void QmlProfilerClientManager::connectToServer(const QUrl &server)
disconnectClient(); disconnectClient();
stopConnectionTimer(); stopConnectionTimer();
} }
if (server.scheme() == ProjectExplorer::urlTcpScheme()) if (server.scheme() == Utils::urlTcpScheme())
connectToTcpServer(); connectToTcpServer();
else if (server.scheme() == ProjectExplorer::urlSocketScheme()) else if (server.scheme() == Utils::urlSocketScheme())
startLocalServer(); startLocalServer();
else else
QTC_ASSERT(false, emit connectionFailed()); QTC_ASSERT(false, emit connectionFailed());
@@ -175,9 +175,9 @@ void QmlProfilerClientManager::stopRecording()
void QmlProfilerClientManager::retryConnect() void QmlProfilerClientManager::retryConnect()
{ {
if (m_server.scheme() == ProjectExplorer::urlSocketScheme()) { if (m_server.scheme() == Utils::urlSocketScheme()) {
startLocalServer(); startLocalServer();
} else if (m_server.scheme() == ProjectExplorer::urlTcpScheme()) { } else if (m_server.scheme() == Utils::urlTcpScheme()) {
disconnectClient(); disconnectClient();
connectToTcpServer(); connectToTcpServer();
} else { } else {

View File

@@ -48,6 +48,7 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/qtcfallthrough.h> #include <utils/qtcfallthrough.h>
#include <utils/url.h>
#include <QMessageBox> #include <QMessageBox>
@@ -247,12 +248,12 @@ static QUrl localServerUrl(RunControl *runControl)
const QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(kit); const QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(kit);
if (version) { if (version) {
if (version->qtVersion() >= QtSupport::QtVersionNumber(5, 6, 0)) if (version->qtVersion() >= QtSupport::QtVersionNumber(5, 6, 0))
serverUrl = urlFromLocalSocket(); serverUrl = Utils::urlFromLocalSocket();
else else
serverUrl = urlFromLocalHostAndFreePort(); serverUrl = Utils::urlFromLocalHostAndFreePort();
} else { } else {
qWarning("Running QML profiler on Kit without Qt version?"); qWarning("Running QML profiler on Kit without Qt version?");
serverUrl = urlFromLocalHostAndFreePort(); serverUrl = Utils::urlFromLocalHostAndFreePort();
} }
return serverUrl; return serverUrl;
} }
@@ -278,9 +279,9 @@ LocalQmlProfilerSupport::LocalQmlProfilerSupport(RunControl *runControl, const Q
StandardRunnable debuggee = runnable().as<StandardRunnable>(); StandardRunnable debuggee = runnable().as<StandardRunnable>();
QString code; QString code;
if (serverUrl.scheme() == urlSocketScheme()) if (serverUrl.scheme() == Utils::urlSocketScheme())
code = QString("file:%1").arg(serverUrl.path()); code = QString("file:%1").arg(serverUrl.path());
else if (serverUrl.scheme() == urlTcpScheme()) else if (serverUrl.scheme() == Utils::urlTcpScheme())
code = QString("port:%1").arg(serverUrl.port()); code = QString("port:%1").arg(serverUrl.port());
else else
QTC_CHECK(false); QTC_CHECK(false);

View File

@@ -29,6 +29,8 @@
#include <projectexplorer/runnables.h> #include <projectexplorer/runnables.h>
#include <qmlprofiler/qmlprofilerruncontrol.h> #include <qmlprofiler/qmlprofilerruncontrol.h>
#include <utils/url.h>
#include <QtTest> #include <QtTest>
#include <QTcpServer> #include <QTcpServer>
@@ -56,7 +58,7 @@ void LocalQmlProfilerRunnerTest::testRunner()
debuggee.environment = Utils::Environment::systemEnvironment(); debuggee.environment = Utils::Environment::systemEnvironment();
// should not be used anywhere but cannot be empty // should not be used anywhere but cannot be empty
serverUrl.setScheme(ProjectExplorer::urlSocketScheme()); serverUrl.setScheme(Utils::urlSocketScheme());
serverUrl.setPath("invalid"); serverUrl.setPath("invalid");
runControl = new ProjectExplorer::RunControl(nullptr, runControl = new ProjectExplorer::RunControl(nullptr,
@@ -102,7 +104,7 @@ void LocalQmlProfilerRunnerTest::testRunner()
QTRY_VERIFY(runControl.isNull()); QTRY_VERIFY(runControl.isNull());
QVERIFY(profiler.isNull()); QVERIFY(profiler.isNull());
serverUrl = ProjectExplorer::urlFromLocalSocket(); serverUrl = Utils::urlFromLocalSocket();
debuggee.executable = qApp->applicationFilePath(); debuggee.executable = qApp->applicationFilePath();
// comma is used to specify a test function. In this case, an invalid one. // comma is used to specify a test function. In this case, an invalid one.
@@ -126,7 +128,7 @@ void LocalQmlProfilerRunnerTest::testRunner()
debuggee.commandLineArguments.clear(); debuggee.commandLineArguments.clear();
serverUrl.clear(); serverUrl.clear();
serverUrl = ProjectExplorer::urlFromLocalHostAndFreePort(); serverUrl = Utils::urlFromLocalHostAndFreePort();
runControl = new ProjectExplorer::RunControl(nullptr, runControl = new ProjectExplorer::RunControl(nullptr,
ProjectExplorer::Constants::QML_PROFILER_RUN_MODE); ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
runControl->setRunnable(debuggee); runControl->setRunnable(debuggee);
@@ -148,7 +150,7 @@ void LocalQmlProfilerRunnerTest::testRunner()
void LocalQmlProfilerRunnerTest::testFindFreePort() void LocalQmlProfilerRunnerTest::testFindFreePort()
{ {
QUrl serverUrl = ProjectExplorer::urlFromLocalHostAndFreePort(); QUrl serverUrl = Utils::urlFromLocalHostAndFreePort();
QVERIFY(serverUrl.port() != -1); QVERIFY(serverUrl.port() != -1);
QVERIFY(!serverUrl.host().isEmpty()); QVERIFY(!serverUrl.host().isEmpty());
QTcpServer server; QTcpServer server;
@@ -157,7 +159,7 @@ void LocalQmlProfilerRunnerTest::testFindFreePort()
void LocalQmlProfilerRunnerTest::testFindFreeSocket() void LocalQmlProfilerRunnerTest::testFindFreeSocket()
{ {
QUrl serverUrl = ProjectExplorer::urlFromLocalSocket(); QUrl serverUrl = Utils::urlFromLocalSocket();
QString socket = serverUrl.path(); QString socket = serverUrl.path();
QVERIFY(!socket.isEmpty()); QVERIFY(!socket.isEmpty());
QVERIFY(!QFile::exists(socket)); QVERIFY(!QFile::exists(socket));

View File

@@ -27,6 +27,7 @@
#include <qmlprofiler/qmlprofilerruncontrol.h> #include <qmlprofiler/qmlprofilerruncontrol.h>
#include <qmldebug/qpacketprotocol.h> #include <qmldebug/qpacketprotocol.h>
#include <projectexplorer/applicationlauncher.h> #include <projectexplorer/applicationlauncher.h>
#include <utils/url.h>
#include <QTcpServer> #include <QTcpServer>
#include <QTcpSocket> #include <QTcpSocket>
@@ -70,13 +71,15 @@ void QmlProfilerClientManagerTest::testConnectionFailure_data()
QTest::addColumn<QmlProfilerStateManager *>("stateManager"); QTest::addColumn<QmlProfilerStateManager *>("stateManager");
QVarLengthArray<QmlProfilerStateManager *> stateManagers({nullptr, &stateManager}); QVarLengthArray<QmlProfilerStateManager *> stateManagers({nullptr, &stateManager});
QUrl localUrl = urlFromLocalHostAndFreePort(); QUrl localUrl = Utils::urlFromLocalHostAndFreePort();
QTest::addColumn<QUrl>("serverUrl"); QTest::addColumn<QUrl>("serverUrl");
const QVarLengthArray<QString> hosts({"", "/-/|\\-\\|/-", localUrl.host()}); const QVarLengthArray<QString> hosts({"", "/-/|\\-\\|/-", localUrl.host()});
const QVarLengthArray<int> ports({-1, 5, localUrl.port()}); const QVarLengthArray<int> ports({-1, 5, localUrl.port()});
const QVarLengthArray<QString> sockets({"", "/-/|\\-\\|/-", urlFromLocalSocket().path()}); const QVarLengthArray<QString> sockets({"", "/-/|\\-\\|/-",
const QVarLengthArray<QString> schemes({"", urlSocketScheme(), urlTcpScheme()}); Utils::urlFromLocalSocket().path()});
const QVarLengthArray<QString> schemes({"", Utils::urlSocketScheme(),
Utils::urlTcpScheme()});
for (QmlProfilerModelManager *modelManager : modelManagers) { for (QmlProfilerModelManager *modelManager : modelManagers) {
for (QmlProfilerStateManager *stateManager : stateManagers) { for (QmlProfilerStateManager *stateManager : stateManagers) {
@@ -162,7 +165,7 @@ void QmlProfilerClientManagerTest::testUnresponsiveTcp()
clientManager.setProfilerStateManager(&stateManager); clientManager.setProfilerStateManager(&stateManager);
clientManager.setModelManager(&modelManager); clientManager.setModelManager(&modelManager);
QUrl serverUrl = urlFromLocalHostAndFreePort(); QUrl serverUrl = Utils::urlFromLocalHostAndFreePort();
QTcpServer server; QTcpServer server;
server.listen(QHostAddress(serverUrl.host()), serverUrl.port()); server.listen(QHostAddress(serverUrl.host()), serverUrl.port());
@@ -190,7 +193,7 @@ void QmlProfilerClientManagerTest::testUnresponsiveLocal()
clientManager.setProfilerStateManager(&stateManager); clientManager.setProfilerStateManager(&stateManager);
clientManager.setModelManager(&modelManager); clientManager.setModelManager(&modelManager);
QUrl socketUrl = urlFromLocalSocket(); QUrl socketUrl = Utils::urlFromLocalSocket();
QLocalSocket socket; QLocalSocket socket;
QSignalSpy connectionSpy(&socket, SIGNAL(connected())); QSignalSpy connectionSpy(&socket, SIGNAL(connected()));
@@ -241,7 +244,7 @@ void QmlProfilerClientManagerTest::testResponsiveTcp()
{ {
QFETCH(quint32, flushInterval); QFETCH(quint32, flushInterval);
QUrl serverUrl = urlFromLocalHostAndFreePort(); QUrl serverUrl = Utils::urlFromLocalHostAndFreePort();
QSignalSpy openedSpy(&clientManager, SIGNAL(connectionOpened())); QSignalSpy openedSpy(&clientManager, SIGNAL(connectionOpened()));
QSignalSpy closedSpy(&clientManager, SIGNAL(connectionClosed())); QSignalSpy closedSpy(&clientManager, SIGNAL(connectionClosed()));
@@ -299,7 +302,7 @@ void QmlProfilerClientManagerTest::testResponsiveLocal()
{ {
QFETCH(quint32, flushInterval); QFETCH(quint32, flushInterval);
QUrl socketUrl = urlFromLocalSocket(); QUrl socketUrl = Utils::urlFromLocalSocket();
QSignalSpy openedSpy(&clientManager, SIGNAL(connectionOpened())); QSignalSpy openedSpy(&clientManager, SIGNAL(connectionOpened()));
QSignalSpy closedSpy(&clientManager, SIGNAL(connectionClosed())); QSignalSpy closedSpy(&clientManager, SIGNAL(connectionClosed()));
@@ -363,7 +366,7 @@ void QmlProfilerClientManagerTest::testInvalidData()
clientManager.setProfilerStateManager(&stateManager); clientManager.setProfilerStateManager(&stateManager);
clientManager.setModelManager(&modelManager); clientManager.setModelManager(&modelManager);
QUrl serverUrl = urlFromLocalHostAndFreePort(); QUrl serverUrl = Utils::urlFromLocalHostAndFreePort();
bool dataSent = false; bool dataSent = false;
QTcpServer server; QTcpServer server;
@@ -393,7 +396,7 @@ void QmlProfilerClientManagerTest::testInvalidData()
void QmlProfilerClientManagerTest::testStopRecording() void QmlProfilerClientManagerTest::testStopRecording()
{ {
QUrl socketUrl = urlFromLocalSocket(); QUrl socketUrl = Utils::urlFromLocalSocket();
{ {
QmlProfilerClientManager clientManager; QmlProfilerClientManager clientManager;

View File

@@ -40,6 +40,7 @@
#include <QTcpServer> #include <QTcpServer>
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <utils/url.h>
using namespace Debugger; using namespace Debugger;
using namespace ProjectExplorer; using namespace ProjectExplorer;
@@ -66,7 +67,7 @@ WinRtDebugSupport::WinRtDebugSupport(RunControl *runControl)
} }
if (isQmlDebugging()) { if (isQmlDebugging()) {
QUrl qmlServer = ProjectExplorer::urlFromLocalHostAndFreePort(); QUrl qmlServer = Utils::urlFromLocalHostAndFreePort();
if (qmlServer.port() <= 0) { if (qmlServer.port() <= 0) {
reportFailure(tr("Not enough free ports for QML debugging.")); reportFailure(tr("Not enough free ports for QML debugging."));
return; return;