qmlprofilerruncontrolfactory.cpp 5.88 KB
Newer Older
1
/****************************************************************************
2
**
Aurindam Jana's avatar
Aurindam Jana committed
3
** Copyright (C) 2013 Kläralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
hjk's avatar
hjk committed
16 17 18 19 20 21 22 23 24 25
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30 31 32
#include "qmlprofilerruncontrolfactory.h"
#include "localqmlprofilerrunner.h"
#include "qmlprofilerengine.h"
33

34 35 36 37 38
#include <analyzerbase/ianalyzertool.h>
#include <analyzerbase/analyzermanager.h>
#include <analyzerbase/analyzerstartparameters.h>
#include <analyzerbase/analyzerruncontrol.h>

39 40 41
#include <debugger/debuggerrunconfigurationaspect.h>

#include <projectexplorer/environmentaspect.h>
42
#include <projectexplorer/kitinformation.h>
43 44 45 46
#include <projectexplorer/localapplicationrunconfiguration.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
47
#include <projectexplorer/target.h>
48

49 50
#include <qmlprojectmanager/qmlprojectrunconfiguration.h>

51
#include <utils/qtcassert.h>
52

53 54
#include <QTcpServer>

55
using namespace Analyzer;
56 57
using namespace ProjectExplorer;

58
namespace QmlProfiler {
59 60
namespace Internal {

61
QmlProfilerRunControlFactory::QmlProfilerRunControlFactory(QObject *parent) :
62 63 64 65
    IRunControlFactory(parent)
{
}

66
bool QmlProfilerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
67
{
hjk's avatar
hjk committed
68 69 70
    return mode == QmlProfilerRunMode
            && (qobject_cast<QmlProjectManager::QmlProjectRunConfiguration *>(runConfiguration)
                || qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration));
71 72
}

73 74 75 76 77 78 79 80 81 82 83 84
static AnalyzerStartParameters createQmlProfilerStartParameters(RunConfiguration *runConfiguration)
{
    AnalyzerStartParameters sp;
    EnvironmentAspect *environment = runConfiguration->extraAspect<EnvironmentAspect>();

    // FIXME: This is only used to communicate the connParams settings.
    if (QmlProjectManager::QmlProjectRunConfiguration *rc1 =
            qobject_cast<QmlProjectManager::QmlProjectRunConfiguration *>(runConfiguration)) {
        // This is a "plain" .qmlproject.
        if (environment)
            sp.environment = environment->environment();
        sp.workingDirectory = rc1->workingDirectory();
85 86
        sp.debuggee = rc1->executable();
        sp.debuggeeArgs = rc1->commandLineArguments();
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        sp.displayName = rc1->displayName();
    } else if (LocalApplicationRunConfiguration *rc2 =
            qobject_cast<LocalApplicationRunConfiguration *>(runConfiguration)) {
        if (environment)
            sp.environment = environment->environment();
        sp.workingDirectory = rc2->workingDirectory();
        sp.debuggee = rc2->executable();
        sp.debuggeeArgs = rc2->commandLineArguments();
        sp.displayName = rc2->displayName();
    } else {
        // What could that be?
        QTC_ASSERT(false, return sp);
    }
    const IDevice::ConstPtr device = DeviceKitInformation::device(runConfiguration->target()->kit());
    if (device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE) {
        QTcpServer server;
        if (!server.listen(QHostAddress::LocalHost) && !server.listen(QHostAddress::LocalHostIPv6)) {
            qWarning() << "Cannot open port on host for QML profiling.";
            return sp;
        }
        sp.analyzerHost = server.serverAddress().toString();
        sp.analyzerPort = server.serverPort();
    }
110
    sp.startMode = StartLocal;
111 112 113
    return sp;
}

114
RunControl *QmlProfilerRunControlFactory::create(RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
115 116 117
{
    QTC_ASSERT(canRun(runConfiguration, mode), return 0);

118
    AnalyzerStartParameters sp = createQmlProfilerStartParameters(runConfiguration);
119
    sp.runMode = mode;
120

121
    // only desktop device is supported
122
    const IDevice::ConstPtr device = DeviceKitInformation::device(runConfiguration->target()->kit());
123 124
    QTC_ASSERT(device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE, return 0);

125
    AnalyzerRunControl *rc = AnalyzerManager::createRunControl(sp, runConfiguration);
126
    QmlProfilerRunControl *engine = qobject_cast<QmlProfilerRunControl *>(rc);
127 128 129 130 131 132 133 134 135 136
    if (!engine) {
        delete rc;
        return 0;
    }
    LocalQmlProfilerRunner *runner = LocalQmlProfilerRunner::createLocalRunner(runConfiguration, sp, errorMessage, engine);
    if (!runner)
        return 0;
    connect(runner, SIGNAL(stopped()), engine, SLOT(notifyRemoteFinished()));
    connect(runner, SIGNAL(appendMessage(QString,Utils::OutputFormat)),
            engine, SLOT(logApplicationMessage(QString,Utils::OutputFormat)));
137
    connect(engine, SIGNAL(starting(const Analyzer::AnalyzerRunControl*)), runner,
138 139
            SLOT(start()));
    connect(rc, SIGNAL(finished()), runner, SLOT(stop()));
140 141 142 143
    return rc;
}

} // namespace Internal
144
} // namespace QmlProfiler