2010-07-23 Balazs Kelemen <kb@inf.u-szeged.hu>
[WebKit-https.git] / WebKit2 / UIProcess / Launcher / qt / ProcessLauncherQt.cpp
index 23af1f0..5be4419 100644 (file)
 #include <wtf/PassRefPtr.h>
 #include <wtf/Threading.h>
 
-#if USE(MEEGOTOUCH)
-#include <meegotouch/MComponentData>
-#endif
-
-#ifdef Q_OS_UNIX
-#include <signal.h>
-#endif
-
 #include <QApplication>
 #include <QDebug>
+#include <QLocalServer>
 #include <QProcess>
 
 #include <QtCore/qglobal.h>
 #include <sys/resource.h>
 #include <unistd.h>
 
-#if !defined(QWEBKIT_EXPORT)
-#  if defined(QT_SHARED)
-#    define QWEBKIT_EXPORT Q_DECL_EXPORT
-#  else
-#    define QWEBKIT_EXPORT
-#  endif
-#endif
-
 using namespace WebCore;
 
 namespace WebKit {
 
-void ProcessLauncher::launchProcess()
+class ProcessLauncherHelper : public QObject {
+    Q_OBJECT
+public:
+    void launch(WebKit::ProcessLauncher*);
+    QLocalSocket* takePendingConnection();
+    static ProcessLauncherHelper* instance();
+private:
+    ProcessLauncherHelper();
+    QLocalServer m_server;
+    QList<WorkItem*> m_items;
+
+    Q_SLOT void newConnection();
+};
+
+void ProcessLauncherHelper::launch(WebKit::ProcessLauncher* launcher)
 {
-    srandom(time(0));
-    QString connectionIdentifier = QString::number(random());
-
-    QString program("QtWebProcess " + connectionIdentifier);
+    QString program("QtWebProcess " + m_server.serverName());
 
-    QProcess* webProcess = new QProcess;
+    QProcess* webProcess = new QProcess();
     webProcess->start(program);
 
     if (!webProcess->waitForStarted()) {
         qDebug() << "Failed to start" << program;
         ASSERT_NOT_REACHED();
+        delete webProcess;
+        return;
+    }
+
+    setpriority(PRIO_PROCESS, webProcess->pid(), 10);
+
+    m_items.append(WorkItem::create(launcher, &WebKit::ProcessLauncher::didFinishLaunchingProcess, webProcess, m_server.serverName()).leakPtr());
+}
+
+QLocalSocket* ProcessLauncherHelper::takePendingConnection()
+{
+    return m_server.nextPendingConnection();
+}
+
+ProcessLauncherHelper::ProcessLauncherHelper()
+{
+    srandom(time(0));
+    if (!m_server.listen("QtWebKit" + QString::number(random()))) {
+        qDebug() << "Failed to create server socket.";
+        ASSERT_NOT_REACHED();
     }
+    connect(&m_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
+}
 
-    PlatformProcessIdentifier processIdentifier = webProcess->pid();
-    setpriority(PRIO_PROCESS, processIdentifier, 10);
+ProcessLauncherHelper* ProcessLauncherHelper::instance()
+{
+    static ProcessLauncherHelper* result = new ProcessLauncherHelper();
+    return result;
+}
 
-    qDebug() << program << "nice" << getpriority(PRIO_PROCESS, processIdentifier);
+void ProcessLauncherHelper::newConnection()
+{
+    ASSERT(!m_items.isEmpty());
 
-    // We've finished launching the process, message back to the run loop.
-    RunLoop::main()->scheduleWork(WorkItem::create(this, &ProcessLauncher::didFinishLaunchingProcess, processIdentifier, connectionIdentifier));
+    m_items[0]->execute();
+    delete m_items[0];
+    m_items.pop_front();
+}
+
+void ProcessLauncher::launchProcess()
+{
+    ProcessLauncherHelper::instance()->launch(this);
 }
 
 void ProcessLauncher::terminateProcess()
@@ -93,9 +122,13 @@ void ProcessLauncher::terminateProcess()
     if (!m_processIdentifier)
         return;
 
-#ifdef Q_OS_UNIX
-    kill(m_processIdentifier, SIGKILL);
-#endif
+    QObject::connect(m_processIdentifier, SIGNAL(finished(int)), m_processIdentifier, SLOT(deleteLater()), Qt::QueuedConnection);
+    m_processIdentifier->kill();
+}
+
+QLocalSocket* ProcessLauncher::takePendingConnection()
+{
+    return ProcessLauncherHelper::instance()->takePendingConnection();
 }
 
 static void* webThreadBody(void* /* context */)
@@ -128,27 +161,4 @@ CoreIPC::Connection::Identifier ProcessLauncher::createWebThread()
 
 } // namespace WebKit
 
-QWEBKIT_EXPORT int webProcessMain(int argc, char** argv)
-{
-    QApplication* app = new QApplication(argc, argv);
-
-#if USE(MEEGOTOUCH)
-    new MComponentData(argc, argv);
-#endif
-
-    srandom(time(0));
-
-    JSC::initializeThreading();
-    WTF::initializeMainThread();
-    RunLoop::initializeMainRunLoop();
-
-    // Create the connection.
-    QString identifier(app->arguments().size() > 1 ? app->arguments().at(1) : "");
-    WebKit::WebProcess::shared().initialize(identifier, RunLoop::main());
-
-    RunLoop::run();
-
-    // FIXME: Do more cleanup here.
-
-    return 0;
-}
+#include "ProcessLauncherQt.moc"