summaryrefslogtreecommitdiffstats
path: root/src/shared/envjsnatives.cpp
blob: 6508f203b2223981034367a46db1f885c2c4ad08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include <QtCore>
#include <QtScript>
#include <QtNetwork>

// Exported API.
void initializeEnvjsNatives(QScriptEngine *);

#define ENVJS_FUNCTIONS(V) \
    V(lineSource) \
    V(eval) \
    V(loadInlineScript) \
    V(loadLocalScript) \
    V(sync) \
    V(spawn) \
    V(sleep) \
    V(loadFrame) \
    V(unloadFrame) \
    V(proxy) \
    V(getcwd) \
    V(runAsync) \
    V(writeToFile) \
    V(writeToTempFile) \
    V(readFromFile) \
    V(deleteFile) \
    V(connection)

static QScriptValue Envjs_lineSource(QScriptContext *, QScriptEngine *)
{
    // Supposed to return the source text of the line causing the error.
    //QScriptValue e = ctx->argument(0);
    return "(line ?)";
}

static QScriptValue Envjs_eval(QScriptContext *ctx, QScriptEngine *eng)
{
    // Don't know what this is. this-object? Variable scope?
    // QScriptValue scope = ctx->argument(0);

    QScriptValue source = ctx->argument(1);
    QScriptValue sourceName = ctx->argument(0);
    return eng->evaluate(source.toString(), sourceName.toString());
}

static QScriptValue Envjs_loadInlineScript(QScriptContext *ctx, QScriptEngine *)
{
    return ctx->throwError("loadInlineScript() not implemented");
}

static QScriptValue Envjs_loadLocalScript(QScriptContext *ctx, QScriptEngine *)
{
    return ctx->throwError("loadLocalScript() not implemented");
}

static QScriptValue Envjs_sync(QScriptContext *ctx, QScriptEngine *)
{
    return ctx->argument(0);
}

static QScriptValue Envjs_spawn(QScriptContext *ctx, QScriptEngine *)
{
    QScriptValue fun = ctx->argument(0);
    return fun.call();
}

class SleepyThread : public QThread
{
public:
    static void msleep(unsigned long msecs)
    { QThread::msleep(msecs); }
};

static QScriptValue Envjs_sleep(QScriptContext *ctx, QScriptEngine *eng)
{
    uint msecs = ctx->argument(0).toUInt32();
    static_cast<SleepyThread*>(QThread::currentThread())->msleep(msecs);
    return eng->undefinedValue();
}

static QScriptValue Envjs_loadFrame(QScriptContext *ctx, QScriptEngine *)
{
    return ctx->throwError("loadFrame() not implemented");
}

static QScriptValue Envjs_unloadFrame(QScriptContext *ctx, QScriptEngine *)
{
    return ctx->throwError("unloadFrame() not implemented");
}

static QScriptValue Envjs_proxy(QScriptContext *ctx, QScriptEngine *)
{
    return ctx->throwError("proxy() not implemented");
}

static QScriptValue Envjs_getcwd(QScriptContext *, QScriptEngine *)
{
    return QDir::currentPath();
}

class QtScriptFunctionInvoker : public QObject
{
    Q_OBJECT
public:
    QtScriptFunctionInvoker(const QScriptValue &fn, QObject *parent = 0)
        : QObject(parent), fun(fn)
    {}

    void invokeLater()
    {
        QMetaObject::invokeMethod(this, "invoke", Qt::QueuedConnection);
    }

private slots:
    void invoke()
    {
        fun.call();
        deleteLater();
    }

private:
    QScriptValue fun;
};

static QScriptValue Envjs_runAsync(QScriptContext *ctx, QScriptEngine *eng)
{
    QScriptValue fun = ctx->argument(0);
    QtScriptFunctionInvoker *invoker = new QtScriptFunctionInvoker(fun, eng);
    invoker->invokeLater();
    return eng->undefinedValue();
}

static QScriptValue Envjs_writeToFile(QScriptContext *ctx, QScriptEngine *eng)
{
    QScriptValue text = ctx->argument(0);
    QScriptValue url = ctx->argument(1);
    QString path = QUrl(url.toString()).toLocalFile();
    QFile file(path);
    file.open(QIODevice::WriteOnly);
    QTextStream stream(&file);
    stream.setCodec(QTextCodec::codecForName("UTF-8"));
    stream << text.toString();
    return eng->undefinedValue();
}

static QScriptValue Envjs_writeToTempFile(QScriptContext *ctx, QScriptEngine *)
{
//    QScriptValue text = ctx->argument(0);
//    QScriptValue suffix = ctx->argument(1);
//    QString fileName = QString::fromLatin1("envjs-tmp%0").arg(suffix.toString()));
//    ### Create file and write text. File should be deleted when engine is.
    return ctx->throwError("writeToTempFile() not implemented");
}

static QScriptValue Envjs_readFromFile(QScriptContext *ctx, QScriptEngine *)
{
    QScriptValue url = ctx->argument(0);
    QString path = QUrl(url.toString()).toLocalFile();
    QFile file(path);
    file.open(QIODevice::ReadOnly);
    QTextStream stream(&file);
    stream.setCodec(QTextCodec::codecForName("UTF-8"));
    return stream.readAll();
}

static QScriptValue Envjs_deleteFile(QScriptContext *ctx, QScriptEngine *eng)
{
    QScriptValue url = ctx->argument(0);
    QString path = QUrl(url.toString()).toLocalFile();
    QFile file(path);
    file.remove();
    return eng->undefinedValue();
}

static QScriptValue Envjs_connection(QScriptContext *ctx, QScriptEngine *eng)
{
    // TODO: if xhr.async is true, all this stuff should be done in a
    // delayed call or different thread, then the response handler can
    // be called (in this thread).

    QScriptValue xhr = ctx->argument(0);
    QScriptValue responseHandler = ctx->argument(1);
    QScriptValue data = ctx->argument(2);
    QUrl url = QUrl(xhr.property("url").toString());
    QNetworkAccessManager nam; // TODO: get a manager from somewhere...
    QNetworkRequest req(url);

    QScriptValueIterator it(xhr.property("headers"));
    while (it.hasNext()) {
        it.next();
        req.setRawHeader(it.name().toUtf8(), it.value().toString().toUtf8());
    }

    QString method = xhr.property("method").toString();
    QNetworkReply *rep = 0;
    if (method == QLatin1String("GET"))
        rep = nam.get(req);
    else if (method == QLatin1String("PUT"))
        rep = nam.put(req, data.toString().toUtf8());
    else if (method == QLatin1String("POST"))
        rep = nam.put(req, data.toString().toUtf8());
    else if (method == QLatin1String("HEAD"))
        rep = nam.head(req);
    else if (method == QLatin1String("DELETE"))
        rep = nam.deleteResource(req);
    else
        return ctx->throwError(QString::fromLatin1("request method %0 not implemented").arg(method));
    // TODO: connect to reply's download/uploadProgress(),
    // implement HEADERS_RECEIVED and LOADING states

    QEventLoop loop;
    QObject::connect(rep, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();

    QScriptValue responseHeaders = xhr.property("responseHeaders");
    QList<QNetworkReply::RawHeaderPair> rawHeaderPairs = rep->rawHeaderPairs();
    for (int i = 0; i < rawHeaderPairs.size(); ++i) {
        const QNetworkReply::RawHeaderPair &rhp = rawHeaderPairs.at(i);
        responseHeaders.setProperty(QString::fromUtf8(rhp.first), QString::fromUtf8(rhp.second));
    }

    xhr.setProperty("readyState", 4);
    QVariant statusCode = rep->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if (statusCode.isValid())
        xhr.setProperty("status", statusCode.toInt());
    QVariant reasonPhrase = rep->attribute(QNetworkRequest::HttpReasonPhraseAttribute);
    if (reasonPhrase.isValid())
        xhr.setProperty("statusText", reasonPhrase.toString());

    QByteArray responseData = rep->readAll();
    xhr.setProperty("responseText", QString::fromUtf8(responseData));

    if (responseHandler.isFunction())
        responseHandler.call();

    return eng->undefinedValue();
}

void initializeEnvjsNatives(QScriptEngine *eng)
{
    QScriptValue envjs = eng->globalObject().property("Envjs");
    if (!envjs.isObject()) {
        eng->currentContext()->throwError("env.js must be evaluated before natives can be initialized");
        return;
    }

    envjs.setProperty("platform", "QtScript");

    envjs.setProperty("log", eng->globalObject().property("print"));

#define SET_ENVJS_FUNCTION(f) envjs.setProperty(#f, eng->newFunction(Envjs_##f));
    ENVJS_FUNCTIONS(SET_ENVJS_FUNCTION)
#undef SET_ENVJS_FUNCTION
}

#include "envjsnatives.moc"