qmlprofilerruncontrolfactory.cpp 6.27 KB
Newer Older
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** 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 39
#include <analyzerbase/ianalyzertool.h>
#include <analyzerbase/analyzermanager.h>
#include <analyzerbase/analyzerstartparameters.h>
#include <analyzerbase/analyzerruncontrol.h>
#include <analyzerbase/analyzersettings.h>

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

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

50 51
#include <qmlprojectmanager/qmlprojectrunconfiguration.h>

52
#include <utils/qtcassert.h>
53

54 55
#include <QTcpServer>

56
using namespace Analyzer;
57 58
using namespace ProjectExplorer;

59
namespace QmlProfiler {
60 61
namespace Internal {

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

67
bool QmlProfilerRunControlFactory::canRun(RunConfiguration *runConfiguration, RunMode mode) const
68
{
69 70
    if (mode != QmlProfilerRunMode)
        return false;
71
    IAnalyzerTool *tool = AnalyzerManager::toolFromRunMode(mode);
72 73 74 75 76
    if (tool)
        return tool->canRun(runConfiguration, mode);
    return false;
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
static AnalyzerStartParameters createQmlProfilerStartParameters(RunConfiguration *runConfiguration)
{
    AnalyzerStartParameters sp;
    EnvironmentAspect *environment = runConfiguration->extraAspect<EnvironmentAspect>();
    Debugger::DebuggerRunConfigurationAspect *debugger
            = runConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
    QTC_ASSERT(debugger, return sp);

    // 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();
        sp.debuggee = rc1->observerPath();
        sp.debuggeeArgs = rc1->viewerArguments();
        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();
    }
    sp.startMode = StartQml;
    sp.toolId = "QmlProfiler";
    return sp;
}

122
RunControl *QmlProfilerRunControlFactory::create(RunConfiguration *runConfiguration, RunMode mode, QString *errorMessage)
123
{
124
    IAnalyzerTool *tool = AnalyzerManager::toolFromRunMode(mode);
125 126 127
    if (!tool) {
        if (errorMessage)
            *errorMessage = tr("No analyzer tool selected"); // never happens
128
        return 0;
129
    }
130 131 132

    QTC_ASSERT(canRun(runConfiguration, mode), return 0);

133
    AnalyzerStartParameters sp = createQmlProfilerStartParameters(runConfiguration);
134

135
    // only desktop device is supported
136
    const IDevice::ConstPtr device = DeviceKitInformation::device(runConfiguration->target()->kit());
137 138
    QTC_ASSERT(device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE, return 0);

139
    AnalyzerRunControl *rc = new AnalyzerRunControl(tool, sp, runConfiguration);
140 141 142 143 144 145 146 147 148 149 150 151 152 153
    QmlProfilerEngine *engine = qobject_cast<QmlProfilerEngine *>(rc->engine());
    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)));
    connect(engine, SIGNAL(starting(const Analyzer::IAnalyzerEngine*)), runner,
            SLOT(start()));
    connect(rc, SIGNAL(finished()), runner, SLOT(stop()));
154 155 156 157
    return rc;
}

} // namespace Internal
158
} // namespace QmlProfiler