1 /*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
25 */
26
27 #include "config.h"
28 #include "RunLoop.h"
29 #include <runtime/InitializeThreading.h>
30 #include "WebProcess.h"
31 #include <wtf/Threading.h>
32
33 #include <QApplication>
34 #include <QList>
35 #include <QNetworkProxyFactory>
36 #include <QString>
37 #include <QStringList>
38 #include <QUrl>
39 #include <QtGlobal>
40
41 #if USE(MEEGOTOUCH)
42 #include <MComponentData>
43 #endif
44
45 #ifndef NDEBUG
46 #if !OS(WINDOWS)
47 #include <unistd.h>
48 #endif
49 #endif
50
51 #ifndef NDEBUG
52 #include <QDebug>
53 #endif
54
55 using namespace WebCore;
56
57 namespace WebKit {
58 #ifndef NDEBUG
59 #if OS(WINDOWS)
sleep(unsigned seconds)60 static void sleep(unsigned seconds)
61 {
62 ::Sleep(seconds * 1000);
63 }
64 #endif
65 #endif
66
67 class EnvHttpProxyFactory : public QNetworkProxyFactory
68 {
69 public:
EnvHttpProxyFactory()70 EnvHttpProxyFactory() { }
71
72 bool initializeFromEnvironment();
73
74 QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery& query = QNetworkProxyQuery());
75
76 private:
77 QList<QNetworkProxy> m_httpProxy;
78 QList<QNetworkProxy> m_httpsProxy;
79 };
80
initializeFromEnvironment()81 bool EnvHttpProxyFactory::initializeFromEnvironment()
82 {
83 bool wasSetByEnvironment = false;
84
85 QUrl proxyUrl = QUrl::fromUserInput(QString::fromLocal8Bit(qgetenv("http_proxy")));
86 if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
87 int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
88 m_httpProxy << QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort);
89 wasSetByEnvironment = true;
90 } else
91 m_httpProxy << QNetworkProxy::NoProxy;
92
93 proxyUrl = QUrl::fromUserInput(QString::fromLocal8Bit(qgetenv("https_proxy")));
94 if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
95 int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
96 m_httpsProxy << QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort);
97 wasSetByEnvironment = true;
98 } else
99 m_httpsProxy << QNetworkProxy::NoProxy;
100
101 return wasSetByEnvironment;
102 }
103
queryProxy(const QNetworkProxyQuery & query)104 QList<QNetworkProxy> EnvHttpProxyFactory::queryProxy(const QNetworkProxyQuery& query)
105 {
106 QString protocol = query.protocolTag().toLower();
107 if (protocol == QLatin1String("http"))
108 return m_httpProxy;
109 else if (protocol == QLatin1String("https"))
110 return m_httpsProxy;
111
112 QList<QNetworkProxy> proxies;
113 proxies << QNetworkProxy::NoProxy;
114 return proxies;
115 }
116
initializeProxy()117 static void initializeProxy()
118 {
119 QList<QNetworkProxy> proxylist = QNetworkProxyFactory::systemProxyForQuery();
120 if (proxylist.count() == 1) {
121 QNetworkProxy proxy = proxylist.first();
122 if (proxy == QNetworkProxy::NoProxy || proxy == QNetworkProxy::DefaultProxy) {
123 EnvHttpProxyFactory* proxyFactory = new EnvHttpProxyFactory();
124 if (proxyFactory->initializeFromEnvironment()) {
125 QNetworkProxyFactory::setApplicationProxyFactory(proxyFactory);
126 return;
127 }
128 }
129 }
130 QNetworkProxyFactory::setUseSystemConfiguration(true);
131 }
132
WebProcessMainQt(int argc,char ** argv)133 Q_DECL_EXPORT int WebProcessMainQt(int argc, char** argv)
134 {
135 QApplication::setGraphicsSystem(QLatin1String("raster"));
136 QApplication* app = new QApplication(argc, argv);
137 #ifndef NDEBUG
138 if (!qgetenv("WEBKIT2_PAUSE_WEB_PROCESS_ON_LAUNCH").isEmpty()) {
139 qDebug() << "Waiting 3 seconds for debugger";
140 sleep(3);
141 }
142 #endif
143
144 #if USE(MEEGOTOUCH)
145 new MComponentData(argc, argv);
146 #endif
147
148 initializeProxy();
149
150 srandom(time(0));
151
152 JSC::initializeThreading();
153 WTF::initializeMainThread();
154 RunLoop::initializeMainRunLoop();
155
156 // Create the connection.
157 if (app->arguments().size() <= 1) {
158 qDebug() << "Error: wrong number of arguments.";
159 return 1;
160 }
161
162 bool wasNumber = false;
163 int identifier = app->arguments().at(1).toInt(&wasNumber, 10);
164 if (!wasNumber) {
165 qDebug() << "Error: connection identifier wrong.";
166 return 1;
167 }
168
169 WebKit::WebProcess::shared().initialize(identifier, RunLoop::main());
170
171 RunLoop::run();
172
173 // FIXME: Do more cleanup here.
174
175 return 0;
176 }
177
178 }
179