diff --git a/src/libs/utils/url.cpp b/src/libs/utils/url.cpp new file mode 100644 index 00000000000..56b832eb5c9 --- /dev/null +++ b/src/libs/utils/url.cpp @@ -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 +#include + +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"); +} + +} diff --git a/src/libs/utils/url.h b/src/libs/utils/url.h new file mode 100644 index 00000000000..91ced4d4446 --- /dev/null +++ b/src/libs/utils/url.h @@ -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 + +namespace Utils { + +QTCREATOR_UTILS_EXPORT QUrl urlFromLocalHostAndFreePort(); +QTCREATOR_UTILS_EXPORT QUrl urlFromLocalSocket(); +QTCREATOR_UTILS_EXPORT QString urlSocketScheme(); +QTCREATOR_UTILS_EXPORT QString urlTcpScheme(); + +} diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index 822d247e903..c97245cbf25 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -118,7 +118,8 @@ SOURCES += \ $$PWD/guard.cpp \ $$PWD/highlightingitemdelegate.cpp \ $$PWD/camelhumpmatcher.cpp \ - $$PWD/textutils.cpp + $$PWD/textutils.cpp \ + $$PWD/url.cpp win32:SOURCES += $$PWD/consoleprocess_win.cpp else:SOURCES += $$PWD/consoleprocess_unix.cpp @@ -249,7 +250,8 @@ HEADERS += \ $$PWD/qtcfallthrough.h \ $$PWD/highlightingitemdelegate.h \ $$PWD/camelhumpmatcher.h \ - $$PWD/textutils.h + $$PWD/textutils.h \ + $$PWD/url.h FORMS += $$PWD/filewizardpage.ui \ $$PWD/projectintropage.ui \ diff --git a/src/plugins/android/androidrunner.cpp b/src/plugins/android/androidrunner.cpp index 5eaaaf5241d..a16d84f9744 100644 --- a/src/plugins/android/androidrunner.cpp +++ b/src/plugins/android/androidrunner.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -293,7 +294,7 @@ AndroidRunnerWorker::AndroidRunnerWorker(RunControl *runControl, const AndroidRu QTC_ASSERT(server.listen(QHostAddress::LocalHost) || server.listen(QHostAddress::LocalHostIPv6), 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.setPort(server.serverPort()); } diff --git a/src/plugins/debugger/debuggerruncontrol.cpp b/src/plugins/debugger/debuggerruncontrol.cpp index 5d9870d8679..79cf7fc8950 100644 --- a/src/plugins/debugger/debuggerruncontrol.cpp +++ b/src/plugins/debugger/debuggerruncontrol.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include @@ -643,7 +644,7 @@ bool DebuggerRunTool::fixupParameters() if (rp.isQmlDebugging) { if (device() && device()->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE) { if (rp.qmlServer.port() <= 0) { - rp.qmlServer = ProjectExplorer::urlFromLocalHostAndFreePort(); + rp.qmlServer = Utils::urlFromLocalHostAndFreePort(); if (rp.qmlServer.port() <= 0) { reportFailure(DebuggerPlugin::tr("Not enough free ports for QML debugging.")); return false; diff --git a/src/plugins/projectexplorer/runnables.cpp b/src/plugins/projectexplorer/runnables.cpp index 33d612f0adc..a8c9be4aad2 100644 --- a/src/plugins/projectexplorer/runnables.cpp +++ b/src/plugins/projectexplorer/runnables.cpp @@ -25,10 +25,6 @@ #include "runnables.h" -#include - -#include - namespace ProjectExplorer { 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; - -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 diff --git a/src/plugins/projectexplorer/runnables.h b/src/plugins/projectexplorer/runnables.h index a893545d23a..9a9568c686b 100644 --- a/src/plugins/projectexplorer/runnables.h +++ b/src/plugins/projectexplorer/runnables.h @@ -33,7 +33,6 @@ #include #include -#include namespace ProjectExplorer { @@ -53,9 +52,4 @@ public: 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 diff --git a/src/plugins/qmlprofiler/qmlprofilerclientmanager.cpp b/src/plugins/qmlprofiler/qmlprofilerclientmanager.cpp index 10498184612..d6f3ccbe7cd 100644 --- a/src/plugins/qmlprofiler/qmlprofilerclientmanager.cpp +++ b/src/plugins/qmlprofiler/qmlprofilerclientmanager.cpp @@ -31,7 +31,7 @@ #include #include -#include +#include namespace QmlProfiler { namespace Internal { @@ -75,9 +75,9 @@ void QmlProfilerClientManager::connectToServer(const QUrl &server) disconnectClient(); stopConnectionTimer(); } - if (server.scheme() == ProjectExplorer::urlTcpScheme()) + if (server.scheme() == Utils::urlTcpScheme()) connectToTcpServer(); - else if (server.scheme() == ProjectExplorer::urlSocketScheme()) + else if (server.scheme() == Utils::urlSocketScheme()) startLocalServer(); else QTC_ASSERT(false, emit connectionFailed()); @@ -175,9 +175,9 @@ void QmlProfilerClientManager::stopRecording() void QmlProfilerClientManager::retryConnect() { - if (m_server.scheme() == ProjectExplorer::urlSocketScheme()) { + if (m_server.scheme() == Utils::urlSocketScheme()) { startLocalServer(); - } else if (m_server.scheme() == ProjectExplorer::urlTcpScheme()) { + } else if (m_server.scheme() == Utils::urlTcpScheme()) { disconnectClient(); connectToTcpServer(); } else { diff --git a/src/plugins/qmlprofiler/qmlprofilerruncontrol.cpp b/src/plugins/qmlprofiler/qmlprofilerruncontrol.cpp index 7a24026e93e..c309dc6f127 100644 --- a/src/plugins/qmlprofiler/qmlprofilerruncontrol.cpp +++ b/src/plugins/qmlprofiler/qmlprofilerruncontrol.cpp @@ -48,6 +48,7 @@ #include #include +#include #include @@ -247,12 +248,12 @@ static QUrl localServerUrl(RunControl *runControl) const QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(kit); if (version) { if (version->qtVersion() >= QtSupport::QtVersionNumber(5, 6, 0)) - serverUrl = urlFromLocalSocket(); + serverUrl = Utils::urlFromLocalSocket(); else - serverUrl = urlFromLocalHostAndFreePort(); + serverUrl = Utils::urlFromLocalHostAndFreePort(); } else { qWarning("Running QML profiler on Kit without Qt version?"); - serverUrl = urlFromLocalHostAndFreePort(); + serverUrl = Utils::urlFromLocalHostAndFreePort(); } return serverUrl; } @@ -278,9 +279,9 @@ LocalQmlProfilerSupport::LocalQmlProfilerSupport(RunControl *runControl, const Q StandardRunnable debuggee = runnable().as(); QString code; - if (serverUrl.scheme() == urlSocketScheme()) + if (serverUrl.scheme() == Utils::urlSocketScheme()) 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()); else QTC_CHECK(false); diff --git a/src/plugins/qmlprofiler/tests/localqmlprofilerrunner_test.cpp b/src/plugins/qmlprofiler/tests/localqmlprofilerrunner_test.cpp index 3bf680be5ba..f212234932e 100644 --- a/src/plugins/qmlprofiler/tests/localqmlprofilerrunner_test.cpp +++ b/src/plugins/qmlprofiler/tests/localqmlprofilerrunner_test.cpp @@ -29,6 +29,8 @@ #include #include +#include + #include #include @@ -56,7 +58,7 @@ void LocalQmlProfilerRunnerTest::testRunner() debuggee.environment = Utils::Environment::systemEnvironment(); // should not be used anywhere but cannot be empty - serverUrl.setScheme(ProjectExplorer::urlSocketScheme()); + serverUrl.setScheme(Utils::urlSocketScheme()); serverUrl.setPath("invalid"); runControl = new ProjectExplorer::RunControl(nullptr, @@ -102,7 +104,7 @@ void LocalQmlProfilerRunnerTest::testRunner() QTRY_VERIFY(runControl.isNull()); QVERIFY(profiler.isNull()); - serverUrl = ProjectExplorer::urlFromLocalSocket(); + serverUrl = Utils::urlFromLocalSocket(); debuggee.executable = qApp->applicationFilePath(); // comma is used to specify a test function. In this case, an invalid one. @@ -126,7 +128,7 @@ void LocalQmlProfilerRunnerTest::testRunner() debuggee.commandLineArguments.clear(); serverUrl.clear(); - serverUrl = ProjectExplorer::urlFromLocalHostAndFreePort(); + serverUrl = Utils::urlFromLocalHostAndFreePort(); runControl = new ProjectExplorer::RunControl(nullptr, ProjectExplorer::Constants::QML_PROFILER_RUN_MODE); runControl->setRunnable(debuggee); @@ -148,7 +150,7 @@ void LocalQmlProfilerRunnerTest::testRunner() void LocalQmlProfilerRunnerTest::testFindFreePort() { - QUrl serverUrl = ProjectExplorer::urlFromLocalHostAndFreePort(); + QUrl serverUrl = Utils::urlFromLocalHostAndFreePort(); QVERIFY(serverUrl.port() != -1); QVERIFY(!serverUrl.host().isEmpty()); QTcpServer server; @@ -157,7 +159,7 @@ void LocalQmlProfilerRunnerTest::testFindFreePort() void LocalQmlProfilerRunnerTest::testFindFreeSocket() { - QUrl serverUrl = ProjectExplorer::urlFromLocalSocket(); + QUrl serverUrl = Utils::urlFromLocalSocket(); QString socket = serverUrl.path(); QVERIFY(!socket.isEmpty()); QVERIFY(!QFile::exists(socket)); diff --git a/src/plugins/qmlprofiler/tests/qmlprofilerclientmanager_test.cpp b/src/plugins/qmlprofiler/tests/qmlprofilerclientmanager_test.cpp index 0318a6b6185..576039f1c48 100644 --- a/src/plugins/qmlprofiler/tests/qmlprofilerclientmanager_test.cpp +++ b/src/plugins/qmlprofiler/tests/qmlprofilerclientmanager_test.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -70,13 +71,15 @@ void QmlProfilerClientManagerTest::testConnectionFailure_data() QTest::addColumn("stateManager"); QVarLengthArray stateManagers({nullptr, &stateManager}); - QUrl localUrl = urlFromLocalHostAndFreePort(); + QUrl localUrl = Utils::urlFromLocalHostAndFreePort(); QTest::addColumn("serverUrl"); const QVarLengthArray hosts({"", "/-/|\\-\\|/-", localUrl.host()}); const QVarLengthArray ports({-1, 5, localUrl.port()}); - const QVarLengthArray sockets({"", "/-/|\\-\\|/-", urlFromLocalSocket().path()}); - const QVarLengthArray schemes({"", urlSocketScheme(), urlTcpScheme()}); + const QVarLengthArray sockets({"", "/-/|\\-\\|/-", + Utils::urlFromLocalSocket().path()}); + const QVarLengthArray schemes({"", Utils::urlSocketScheme(), + Utils::urlTcpScheme()}); for (QmlProfilerModelManager *modelManager : modelManagers) { for (QmlProfilerStateManager *stateManager : stateManagers) { @@ -162,7 +165,7 @@ void QmlProfilerClientManagerTest::testUnresponsiveTcp() clientManager.setProfilerStateManager(&stateManager); clientManager.setModelManager(&modelManager); - QUrl serverUrl = urlFromLocalHostAndFreePort(); + QUrl serverUrl = Utils::urlFromLocalHostAndFreePort(); QTcpServer server; server.listen(QHostAddress(serverUrl.host()), serverUrl.port()); @@ -190,7 +193,7 @@ void QmlProfilerClientManagerTest::testUnresponsiveLocal() clientManager.setProfilerStateManager(&stateManager); clientManager.setModelManager(&modelManager); - QUrl socketUrl = urlFromLocalSocket(); + QUrl socketUrl = Utils::urlFromLocalSocket(); QLocalSocket socket; QSignalSpy connectionSpy(&socket, SIGNAL(connected())); @@ -241,7 +244,7 @@ void QmlProfilerClientManagerTest::testResponsiveTcp() { QFETCH(quint32, flushInterval); - QUrl serverUrl = urlFromLocalHostAndFreePort(); + QUrl serverUrl = Utils::urlFromLocalHostAndFreePort(); QSignalSpy openedSpy(&clientManager, SIGNAL(connectionOpened())); QSignalSpy closedSpy(&clientManager, SIGNAL(connectionClosed())); @@ -299,7 +302,7 @@ void QmlProfilerClientManagerTest::testResponsiveLocal() { QFETCH(quint32, flushInterval); - QUrl socketUrl = urlFromLocalSocket(); + QUrl socketUrl = Utils::urlFromLocalSocket(); QSignalSpy openedSpy(&clientManager, SIGNAL(connectionOpened())); QSignalSpy closedSpy(&clientManager, SIGNAL(connectionClosed())); @@ -363,7 +366,7 @@ void QmlProfilerClientManagerTest::testInvalidData() clientManager.setProfilerStateManager(&stateManager); clientManager.setModelManager(&modelManager); - QUrl serverUrl = urlFromLocalHostAndFreePort(); + QUrl serverUrl = Utils::urlFromLocalHostAndFreePort(); bool dataSent = false; QTcpServer server; @@ -393,7 +396,7 @@ void QmlProfilerClientManagerTest::testInvalidData() void QmlProfilerClientManagerTest::testStopRecording() { - QUrl socketUrl = urlFromLocalSocket(); + QUrl socketUrl = Utils::urlFromLocalSocket(); { QmlProfilerClientManager clientManager; diff --git a/src/plugins/winrt/winrtdebugsupport.cpp b/src/plugins/winrt/winrtdebugsupport.cpp index 03945602a08..6721bf9f304 100644 --- a/src/plugins/winrt/winrtdebugsupport.cpp +++ b/src/plugins/winrt/winrtdebugsupport.cpp @@ -40,6 +40,7 @@ #include #include +#include using namespace Debugger; using namespace ProjectExplorer; @@ -66,7 +67,7 @@ WinRtDebugSupport::WinRtDebugSupport(RunControl *runControl) } if (isQmlDebugging()) { - QUrl qmlServer = ProjectExplorer::urlFromLocalHostAndFreePort(); + QUrl qmlServer = Utils::urlFromLocalHostAndFreePort(); if (qmlServer.port() <= 0) { reportFailure(tr("Not enough free ports for QML debugging.")); return;