2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ProcessLauncher.h"
29 #include "Connection.h"
31 #include "WebProcess.h"
32 #include <WebCore/PlatformString.h>
33 #include <runtime/InitializeThreading.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/Threading.h>
38 #include <QApplication>
40 #include <QLocalServer>
43 #include <QtCore/qglobal.h>
45 #include <sys/resource.h>
48 using namespace WebCore;
52 class ProcessLauncherHelper : public QObject {
55 void launch(WebKit::ProcessLauncher*);
56 QLocalSocket* takePendingConnection();
57 static ProcessLauncherHelper* instance();
59 ProcessLauncherHelper();
60 QLocalServer m_server;
61 QList<WorkItem*> m_items;
63 Q_SLOT void newConnection();
66 void ProcessLauncherHelper::launch(WebKit::ProcessLauncher* launcher)
68 QString program("QtWebProcess " + m_server.serverName());
70 QProcess* webProcess = new QProcess();
71 webProcess->start(program);
73 if (!webProcess->waitForStarted()) {
74 qDebug() << "Failed to start" << program;
80 setpriority(PRIO_PROCESS, webProcess->pid(), 10);
82 m_items.append(WorkItem::create(launcher, &WebKit::ProcessLauncher::didFinishLaunchingProcess, webProcess, m_server.serverName()).leakPtr());
85 QLocalSocket* ProcessLauncherHelper::takePendingConnection()
87 return m_server.nextPendingConnection();
90 ProcessLauncherHelper::ProcessLauncherHelper()
93 if (!m_server.listen("QtWebKit" + QString::number(random()))) {
94 qDebug() << "Failed to create server socket.";
97 connect(&m_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
100 ProcessLauncherHelper* ProcessLauncherHelper::instance()
102 static ProcessLauncherHelper* result = new ProcessLauncherHelper();
106 void ProcessLauncherHelper::newConnection()
108 ASSERT(!m_items.isEmpty());
110 m_items[0]->execute();
115 void ProcessLauncher::launchProcess()
117 ProcessLauncherHelper::instance()->launch(this);
120 void ProcessLauncher::terminateProcess()
122 if (!m_processIdentifier)
125 QObject::connect(m_processIdentifier, SIGNAL(finished(int)), m_processIdentifier, SLOT(deleteLater()), Qt::QueuedConnection);
126 m_processIdentifier->kill();
129 QLocalSocket* ProcessLauncher::takePendingConnection()
131 return ProcessLauncherHelper::instance()->takePendingConnection();
134 static void* webThreadBody(void* /* context */)
137 JSC::initializeThreading();
138 WTF::initializeMainThread();
140 // FIXME: We do not support threaded mode for now.
142 WebProcess::shared().initialize("foo", RunLoop::current());
148 CoreIPC::Connection::Identifier ProcessLauncher::createWebThread()
151 int connectionIdentifier = random();
153 if (!createThread(webThreadBody, reinterpret_cast<void*>(connectionIdentifier), "WebKit2: WebThread")) {
154 qWarning() << "failed starting thread";
158 QString serverIdentifier = QString::number(connectionIdentifier);
159 return serverIdentifier;
162 } // namespace WebKit
164 #include "ProcessLauncherQt.moc"